text:
# %%
import torch
from UnarySim.kernel.linear import *
import matplotlib.pyplot as plt
import time
import math
import numpy as np

# %%
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

# %%
def test(rounding = "round", abs_err = True):
    ufc_err_min_list = []
    ufc_err_max_list = []
    ufc_err_mean_list = []
    ufc_err_std_list = []

    ofc_err_min_list = []
    ofc_err_max_list = []
    ofc_err_mean_list = []
    ofc_err_std_list = []

    ifc_err_min_list = []
    ifc_err_max_list = []
    ifc_err_mean_list = []
    ifc_err_std_list = []

    x_label = []

    for bitwidth in range(6, 13):
        cycle = 2**(bitwidth-1)

        in_feature = 2
        out_feature = 2**12
        bias = False

        input = torch.cat(2*[(torch.arange(0, out_feature)/out_feature - 0.5).unsqueeze(1)], 1).to(device)
        input[:, 1] = 0.

        fc = torch.nn.Linear(in_feature, out_feature, bias=bias).to(device)
        fc.weight.data = torch.cat(2*[(torch.arange(0, out_feature)/out_feature - 0.5).unsqueeze(1)], 1).to(device)
        fc.weight.data[:, 1] = 0.
        fc_o = fc(input)

        ufc = HUBLinear(in_feature, out_feature, bias=bias, binary_weight=fc.weight.data, binary_bias=fc.bias, cycle=cycle, rounding=rounding).to(device)
        ufc_o = ufc(input)

        ofc = FxpLinear(in_feature, out_feature, bias=bias, binary_weight=fc.weight.data, binary_bias=fc.bias, bitwidth=bitwidth, keep_res="output", more_res="input", rounding=rounding).to(device)
        ofc_o = ofc(input)

        ifc = FxpLinear(in_feature, out_feature, bias=bias, binary_weight=fc.weight.data, binary_bias=fc.bias, bitwidth=bitwidth, keep_res="input",  more_res="input", rounding=rounding).to(device)
        ifc_o = ifc(input)

        if abs_err is True:
            ufc_err = (ufc_o - fc_o)
            ofc_err = (ofc_o - fc_o)
            ifc_err = (ifc_o - fc_o)
        else:
            ufc_err = (ufc_o - fc_o) / fc_o
            ofc_err = (ofc_o - fc_o) / fc_o
            ifc_err = (ifc_o - fc_o) / fc_o

        ufc_err_min_list.append(np.nanmin(ufc_err.cpu().detach().numpy()))
        ufc_err_max_list.append(np.nanmax(ufc_err.cpu().detach().numpy()))
        ufc_err_mean_list.append(np.nanmean(np.abs(ufc_err.cpu().detach().numpy())))
        ufc_err_std_list.append(np.nanstd(ufc_err.cpu().detach().numpy()))

        ofc_err_min_list.append(np.nanmin(ofc_err.cpu().detach().numpy()))
        ofc_err_max_list.append(np.nanmax(ofc_err.cpu().detach().numpy()))
        ofc_err_mean_list.append(np.nanmean(np.abs(ofc_err.cpu().detach().numpy())))
        ofc_err_std_list.append(np.nanstd(ofc_err.cpu().detach().numpy()))

        ifc_err_min_list.append(np.nanmin(ifc_err.cpu().detach().numpy()))
        ifc_err_max_list.append(np.nanmax(ifc_err.cpu().detach().numpy()))
        ifc_err_mean_list.append(np.nanmean(np.abs(ifc_err.cpu().detach().numpy())))
        ifc_err_std_list.append(np.nanstd(ifc_err.cpu().detach().numpy()))

        x_label.append(2**(bitwidth-1))
    return ufc_err_min_list, ufc_err_max_list, ufc_err_mean_list, ufc_err_std_list, ofc_err_min_list, ofc_err_max_list, ofc_err_mean_list, ofc_err_std_list, ifc_err_min_list, ifc_err_max_list, ifc_err_mean_list, ifc_err_std_list, x_label

# %%
rounding = "round"
abs_err = True
ufc_err_min_list, ufc_err_max_list, ufc_err_mean_list, ufc_err_std_list, ofc_err_min_list, ofc_err_max_list, ofc_err_mean_list, ofc_err_std_list, ifc_err_min_list, ifc_err_max_list, ifc_err_mean_list, ifc_err_std_list, x_label = test(rounding, abs_err)
print(ufc_err_mean_list)
print(ufc_err_std_list)
print()

print(ofc_err_mean_list)
print(ofc_err_std_list)
print()

print(ifc_err_mean_list)
print(ifc_err_std_list)
print()

print(x_label)

# %%
import matplotlib.pyplot as plt
import matplotlib
import numpy as np

font = {'family':'Times New Roman', 'size': 6}

matplotlib.rc('font', **font)

my_dpi = 300
fig_h = 1
fig_w = 3.3115

# construct some data like what you have:
x = np.array([i for i in range(len(ufc_err_mean_list))])
means1 = np.array(ufc_err_mean_list)
stds1 = np.array(ufc_err_std_list)
mins1 = np.array(ufc_err_min_list)
maxs1 = np.array(ufc_err_max_list)

means2 = np.array(ofc_err_mean_list)
stds2 = np.array(ofc_err_std_list)
mins2 = np.array(ofc_err_min_list)
maxs2 = np.array(ofc_err_max_list)

means3 = np.array(ifc_err_mean_list)
stds3 = np.array(ifc_err_std_list)
mins3 = np.array(ifc_err_min_list)
maxs3 = np.array(ifc_err_max_list)

x_label = ['6-32', '7-64', '8-128', '9-256', '10-512', '11-1024', '12-2048']

width = 0.20
fig, ax = plt.subplots(figsize=(fig_w, fig_h))

ax.plot(x, means1, "-o", label="uSystolic", color="#7A81FF", ms=4)
ax.fill_between(x, means1-stds1, means1+stds1, alpha=0.3, color="#7A81FF", edgecolor=None)

ax.plot(x, means2, "-s", label="FXP-o-res", color="#FF7F7F", ms=4)
ax.fill_between(x, means2-stds2, means2+stds2, alpha=0.3, color="#FF7F7F", edgecolor=None)

ax.plot(x, means3, "-^", label="FXP-i-res", color="#D783FF", ms=4)
ax.fill_between(x, means3-stds3, means3+stds3, alpha=0.3, color="#D783FF", edgecolor=None)

ax.set_xticks(x)
ax.set_xticklabels(x_label)
ax.set_yscale('linear')
ax.set_yticks([0, 0.01, 0.02])
ax.set_yticklabels(["0.00", "0.01", "0.02"])
ax.legend(loc="upper right", ncol=3, frameon=False)

fig.tight_layout()
plt.show()
fig.savefig("test_kernel_linear_fxp_hub_compare_abs_err.pdf", bbox_inches='tight', dpi=my_dpi, pad_inches=0.02)

# %%
rounding = "round"
abs_err = False
ufc_err_min_list, ufc_err_max_list, ufc_err_mean_list, ufc_err_std_list, ofc_err_min_list, ofc_err_max_list, ofc_err_mean_list, ofc_err_std_list, ifc_err_min_list, ifc_err_max_list, ifc_err_mean_list, ifc_err_std_list, x_label = test(rounding, abs_err)
print(ufc_err_mean_list)
print(ufc_err_std_list)
print()

print(ofc_err_mean_list)
print(ofc_err_std_list)
print()

print(ifc_err_mean_list)
print(ifc_err_std_list)
print()

print(x_label)

# %%
import matplotlib.pyplot as plt
import matplotlib
import numpy as np

font = {'family':'Times New Roman', 'size': 6}

matplotlib.rc('font', **font)

my_dpi = 300
fig_h = 1
fig_w = 3.3115

# construct some data like what you have:
x = np.array([i for i in range(len(ufc_err_mean_list))])
means1 = np.array(ufc_err_mean_list)
stds1 = np.array(ufc_err_std_list)
mins1 = np.array(ufc_err_min_list)
maxs1 = np.array(ufc_err_max_list)

means2 = np.array(ofc_err_mean_list)
stds2 = np.array(ofc_err_std_list)
mins2 = np.array(ofc_err_min_list)
maxs2 = np.array(ofc_err_max_list)

means3 = np.array(ifc_err_mean_list)
stds3 = np.array(ifc_err_std_list)
mins3 = np.array(ifc_err_min_list)
maxs3 = np.array(ifc_err_max_list)

x_label = ['6-32', '7-64', '8-128', '9-256', '10-512', '11-1024', '12-2048']

width = 0.20
fig, ax = plt.subplots(figsize=(fig_w, fig_h))

ax.plot(x, means1, "-o", label="uSystolic", color="#7A81FF", ms=4)
ax.fill_between(x, means1-stds1, means1+stds1, alpha=0.3, color="#7A81FF", edgecolor=None)

ax.plot(x, means2, "-s", label="FXP-o-res", color="#FF7F7F", ms=4)
ax.fill_between(x, means2-stds2, means2+stds2, alpha=0.3, color="#FF7F7F", edgecolor=None)

ax.plot(x, means3, "-^", label="FXP-i-res", color="#D783FF", ms=4)
ax.fill_between(x, means3-stds3, means3+stds3, alpha=0.3, color="#D783FF", edgecolor=None)

ax.set_xticks(x)
ax.set_xticklabels(x_label)
ax.set_yscale('linear')
ax.set_yticks([0, 0.4, 0.8])
ax.set_yticklabels(["0.00", "0.40", "0.80"])
# ax.legend(loc="upper right", ncol=3, frameon=False)

fig.tight_layout()
plt.show()
fig.savefig("test_kernel_linear_fxp_hub_compare_rel_err.pdf", bbox_inches='tight', dpi=my_dpi, pad_inches=0.02)

# %%


text:
# Author: Xinshuo Weng
# email: xinshuo.weng@gmail.com

# find peak locations and scores for convex blob in heatmap
# note that this function is very strict. No smooth for peak points have been applied
# if there are multiple local peak in a same blob, all of them will be returned.

import numpy as np
import pdb

def find_peaks(heatmap, thre):
    #filter = fspecial('gaussian', [3 3], 2)
    #map_smooth = conv2(map, filter, 'same')

    # variable initialization

    map_smooth = np.array(heatmap)
    map_smooth[map_smooth < thre] = 0.0

    map_aug = np.zeros([map_smooth.shape[0]+2, map_smooth.shape[1]+2])
    map_aug1 = np.zeros([map_smooth.shape[0]+2, map_smooth.shape[1]+2])
    map_aug2 = np.zeros([map_smooth.shape[0]+2, map_smooth.shape[1]+2])
    map_aug3 = np.zeros([map_smooth.shape[0]+2, map_smooth.shape[1]+2])
    map_aug4 = np.zeros([map_smooth.shape[0]+2, map_smooth.shape[1]+2])

    # shift in different directions to find peak, only works for convex blob
    map_aug[1:-1, 1:-1] = map_smooth
    map_aug1[1:-1, 0:-2] = map_smooth
    map_aug2[1:-1, 2:] = map_smooth
    map_aug3[0:-2, 1:-1] = map_smooth
    map_aug4[2:, 2:] = map_smooth

    peakMap = np.multiply(np.multiply(np.multiply((map_aug > map_aug1),(map_aug > map_aug2)),(map_aug > map_aug3)),(map_aug > map_aug4))

    peakMap = peakMap[1:-1, 1:-1]

    idx_tuple = np.nonzero(peakMap)     # find 1
    Y = idx_tuple[0]
    X = idx_tuple[1]

    score = np.zeros([len(Y),1])
    for i in range(len(Y)):
        score[i] = heatmap[Y[i], X[i]]

    return X, Y, score

ext: py

reason: import numpy


text:
from __future__ import print_function
import tensorflow as tf
from tensorflow.contrib import rnn
import numpy
import input
import time
from config import *
import utils as ut
'''
Filled zero for different feature groups to make all groups have same dimension
@param x: A tensor
@param featureDistribution: dimensions of different feature group
@return A new tensor
'''
def fillMatrix(x,featureDistribution):
    maxGroup=numpy.array(featureDistribution).max()
    filled=numpy.zeros(shape=(x.shape[0],maxGroup*len(featureDistribution)))
    index=0
    for i in range(len(featureDistribution)):
        for j in range(i*maxGroup,i*maxGroup+featureDistribution[i]):
            filled[:,j]=x[:,index]
            index+=1
    return filled
'''
Add histogram summary and scalar summary of the sparsity of the tensor for tensor analysis
@param x: A tensor
@return N/A
'''
def activation_summary(x):
    tensor_name = x.op.name
    tf.summary.histogram(tensor_name + '/activations', x)
    tf.summary.scalar(tensor_name + '/sparsity', tf.nn.zero_fraction(x))

'''
Define BiRNN model
@param x: input tensor of model
@param weights: weights of model in a dictionary format
@param n_hidden: number of hidden nodes in model
@param n_steps: number of steps in model
@param biases: biases of model in a dictionary format
@param keep_prob: keep probability in drop-out layer, which is defined in config.py
@return tensor of prediction result
'''
def BiRNN(x, weights, biases, n_hidden, n_steps, keep_prob):

    # Unstack to get a list of 'n_steps' tensors of shape (batch_size, n_input)
    x = tf.unstack(x, n_steps, 1)

    # Define lstm cells with tensorflow
    # Forward direction cell
    lstm_fw_cell = rnn.BasicLSTMCell(n_hidden, forget_bias=1.0)
    lstm_fw_cell = tf.contrib.rnn.DropoutWrapper(lstm_fw_cell, output_keep_prob=keep_prob)
    # Backward direction cell
    lstm_bw_cell = rnn.BasicLSTMCell(n_hidden, forget_bias=1.0)
    lstm_bw_cell = tf.contrib.rnn.DropoutWrapper(lstm_bw_cell, output_keep_prob=keep_prob)

    # Get lstm cell output
    try:
        outputs, _, _ = rnn.static_bidirectional_rnn(lstm_fw_cell, lstm_bw_cell, x,
                                              dtype=tf.float32)
    except Exception: # Old TensorFlow version only returns outputs not states
        outputs = rnn.static_bidirectional_rnn(lstm_fw_cell, lstm_bw_cell, x,
                                        dtype=tf.float32)

    # Linear activation, using rnn inner loop last output
    return tf.matmul(outputs[-1], weights['out']) + biases['out']

'''
Birnn trarining process
@param trainFile,trainLabelFile,testFile,testLabelFile: path of dataset
@param groupFile: group file for pairwise loss function
@param suspFile: file name for store results
@param featureDistribution: dimensions of feature groups
@param loss: loss function
@return None
'''
def run(trainFile, trainLabelFile, testFile, testLabelFile, groupFile, suspFile, featureDistribution, loss):
    # reset graph
    tf.reset_default_graph()
    # Network Parameters
    n_input = numpy.array(featureDistribution).max()
    n_steps = len(featureDistribution)
    n_hidden = numpy.array(featureDistribution).max()
    n_classes = 2 # number of output classes

    # tf Graph input
    x = tf.placeholder("float", [None, n_steps, n_input])
    y = tf.placeholder("float", [None, n_classes])
    g = tf.placeholder(tf.int32, [None, 1])

    # dropout
    keep_prob = tf.placeholder(tf.float32)

    # Define weights
    weights = {
        # Hidden layer weights => 2*n_hidden because of forward + backward cells
        'out': tf.Variable(tf.random_normal([2*n_hidden, n_classes]))
    }
    biases = {
        'out': tf.Variable(tf.random_normal([n_classes]))
    }

    pred = BiRNN(x, weights, biases, n_hidden, n_steps, keep_prob)

    # Evaluate model
    correct_pred = tf.equal(tf.argmax(pred,1), tf.argmax(y,1))
    accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

    # load datasets
    datasets = input.read_data_sets(trainFile,trainLabelFile, testFile,testLabelFile, groupFile)

    # load test data
    test_data=fillMatrix(datasets.test.instances,featureDistribution)
    test_data = test_data.reshape((-1, n_steps, n_input))
    test_label = datasets.test.labels

    # Define loss and optimizer
    variables  = tf.trainable_variables()
    regularizer = tf.add_n([ tf.nn.l2_loss(v) for v in variables if 'bias' not in v.name]) * L2_value  # l2 regularization
    cost = ut.loss_func(pred, y, loss, datasets, g)
    optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost+regularizer)

    init = tf.global_variables_initializer()

    # Launch the graph
    gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.1)
    with tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) as sess:
        sess.run(init)
        step = 1
        # Keep training until reach max iterations
        #while step * batch_size < training_epochs*:
        total_batch = int(datasets.train.num_instances/batch_size)
        for epoch in range(training_epochs):
            avg_cost = 0.
            # Loop over all batches
            for i in range(total_batch):
                batch_x, batch_y, batch_g = datasets.train.next_batch(batch_size)
                # Reshape data to get 28 seq of 28 elements
                batch_x = fillMatrix(batch_x,featureDistribution)
                batch_x = batch_x.reshape((batch_size, n_steps, n_input))
                # Run optimization op (backprop)
                _, c = sess.run([optimizer, cost], feed_dict={x: batch_x, y: batch_y, g: batch_g, keep_prob:dropout_rate})
                # Compute average loss
                avg_cost += c / total_batch
            if epoch % display_step == 0 and i==(total_batch-1):
                    print("Epoch " + str(epoch+1) + ", cost = " + "{:.6f}".format(avg_cost))
            if epoch % (dump_step) == (dump_step-1):
                res=sess.run(tf.nn.softmax(pred),feed_dict={x: test_data, y: test_label, keep_prob:1.0})
                with open(suspFile+'-'+str(epoch+1),'w') as f:
                    for susp in res[:,0]:
                        f.write(str(susp)+'\n')
        print("Optimization Finished!")


text:
"""TODO
1: Get Tweet Data
2: Turn into word vectors using Word Embedding(Will need to process the dataset somewhat for this)
3: Use the word vectors in CNN and RNN
4: Get Results
"""
from tensorflow.python.keras.layers import LSTM

from SVC_Classifier import get_dataset
from sklearn.model_selection import train_test_split
from gensim import models
import numpy as np
from keras_preprocessing.text import Tokenizer
from keras_preprocessing.sequence import pad_sequences
from tensorflow.keras.layers import Dense, Dropout, Reshape, Flatten, concatenate, Input, Conv1D, GlobalMaxPooling1D, \
    Embedding
from tensorflow.keras.models import Model, Sequential
import matplotlib.pyplot as plt
from keras import backend as K
from sklearn.model_selection import KFold

def plot_history(history):
    acc = history.history['acc']
    val_acc = history.history['val_acc']
    loss = history.history['loss']
    val_loss = history.history['val_loss']
    x = range(1, len(acc) + 1)
    plt.rc('font', size=18)
    plt.clf()
    plt.figure(figsize=(12, 5))
    plt.subplot(1, 2, 1)
    plt.plot(x, acc, 'b', label='Training acc')
    plt.plot(x, val_acc, 'r', label='Validation acc')
    plt.title('Training and validation accuracy')
    plt.legend()
    plt.subplot(1, 2, 2)
    plt.plot(x, loss, 'b', label='Training loss')
    plt.plot(x, val_loss, 'r', label='Validation loss')
    plt.title('Training and validation loss')
    plt.legend()
    plt.show()

def reprocess_data(dataset):
    dataset['Text_Final'] = [' '.join(x) for x in dataset['tokens']]
    pos = []
    neg = []
    neu = []
    for l in dataset.sentiment:
        if l == 'Positive':
            pos.append(1)
            neg.append(0)
            neu.append(0)
        elif l == 'Negative':
            pos.append(0)
            neg.append(1)
            neu.append(0)
        else:
            pos.append(0)
            neg.append(0)
            neu.append(1)

    dataset['Pos'] = pos
    dataset['Neg'] = neg
    dataset['Neu'] = neu

def test_train_split(dataset):
    train_data, test_data = train_test_split(dataset, test_size=0.2)
    all_training_words = [word for tokens in train_data["tokens"] for word in tokens]
    training_sentence_lengths = [len(tokens) for tokens in train_data["tokens"]]
    TRAINING_VOCAB = sorted(list(set(all_training_words)))
    print("%s words total, with a vocabulary size of %s" % (len(all_training_words), len(TRAINING_VOCAB)))
    print("Max sentence length is %s" % max(training_sentence_lengths))

    all_test_words = [word for tokens in test_data["tokens"] for word in tokens]
    test_sentence_lengths = [len(tokens) for tokens in test_data["tokens"]]
    TEST_VOCAB = sorted(list(set(all_test_words)))
    print("%s words total, with a vocabulary size of %s" % (len(all_test_words), len(TEST_VOCAB)))
    print("Max sentence length is %s" % max(test_sentence_lengths))

    return train_data, test_data, TRAINING_VOCAB, TEST_VOCAB

def get_average_word2vec(tokens_list, vector, generate_missing=False, k=300):
    if len(tokens_list) < 1:
        return np.zeros(k)
    if generate_missing:
        vectorized = [vector[word] if word in vector else np.random.rand(k) for word in tokens_list]
    else:
        vectorized = [vector[word] if word in vector else np.zeros(k) for word in tokens_list]
    length = len(vectorized)
    summed = np.sum(vectorized, axis=0)
    averaged = np.divide(summed, length)
    return averaged

def get_word2vec_embeddings(vectors, clean_comments, generate_missing=False):
    embeddings = clean_comments['tokens'].apply(lambda x: get_average_word2vec(x, vectors,
                                                                               generate_missing=generate_missing))
    return list(embeddings)

def ConvNet(embeddings, max_sequence_length, num_words, embedding_dim, labels_index):
    embedding_layer = Embedding(num_words, embedding_dim, weights=[embeddings], input_length=max_sequence_length,
                                trainable=False)

    sequence_input = Input(shape=(max_sequence_length,), dtype='int32')
    embedded_sequences = embedding_layer(sequence_input)

    convs = []
    filter_sizes = [2, 3, 4, 5, 6]
    for filter_size in filter_sizes:
        l_conv = Conv1D(filters=200, kernel_size=filter_size, activation='relu')(embedded_sequences)
        l_pool = GlobalMaxPooling1D()(l_conv)
        convs.append(l_pool)

    l_merge = concatenate(convs, axis=1)

    x = Dropout(0.1)(l_merge)
    x = Dense(128, activation='relu')(x)
    x = Dropout(0.2)(x)
    preds = Dense(labels_index, activation='sigmoid')(x)

    model = Model(sequence_input, preds)
    model.compile(loss='binary_crossentropy',
                  optimizer='adam',
                  metrics=['acc'])
    model.summary()
    return model

def RNN(embeddings, max_sequence_length, num_words, embedding_dim, labels_index):
    model = Sequential()
    model.add(Embedding(num_words, embedding_dim, weights=[embeddings], input_length=max_sequence_length,
                        trainable=False))
    model.add(LSTM(196, dropout=0.2, recurrent_dropout=0.2))
    model.add(Dense(labels_index, activation='softmax'))
    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['acc'])
    model.summary()
    return model

def recall_m(y_true, y_pred):
    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
    possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
    recall = true_positives / (possible_positives + K.epsilon())
    return recall

def precision_m(y_true, y_pred):
    true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
    predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
    precision = true_positives / (predicted_positives + K.epsilon())
    return precision

def f1_m(y_true, y_pred):
    precision = precision_m(y_true, y_pred)
    recall = recall_m(y_true, y_pred)
    return 2 * ((precision * recall) / (precision + recall + K.epsilon()))

def cross_val_NN(k, X, y, train_embedding_weights, MAX_SEQUENCE_LENGTH,
                 train_word_index, EMBEDDING_DIM, label_names, max_epoch,
                 nnmodel):
    """
    Inputs: kfold number, data to evaluate X,y and setting for the model

    Outputs: The accuracy, precision, recall and standard deviation of the model
    """

    accuracy_list = []
    recall_list = []
    precision_list = []
    kf = KFold(n_splits=k)

    for train, test in kf.split(X, y):
        if nnmodel == 'RNN':
            model = RNN(train_embedding_weights, MAX_SEQUENCE_LENGTH, len(train_word_index) + 1, EMBEDDING_DIM,
                        len(list(label_names)))
        if nnmodel == 'CNN':
            model = ConvNet(train_embedding_weights, MAX_SEQUENCE_LENGTH, len(train_word_index) + 1, EMBEDDING_DIM,
                            len(list(label_names)))
        model.compile(optimizer='adam', loss='binary_crossentropy', metrics=['acc', f1_m, precision_m, recall_m])

        model.fit(X[train], y[train], epochs=max_epoch, batch_size=34, validation_data=(X[test], y[test]))
        # predict=model.predict(X[test])
        loss, accuracy, f1_score, precision, recall = model.evaluate(X[test], y[test], verbose=0)

        accuracy_list.append(accuracy)
        recall_list.append(recall)
        precision_list.append(precision)

    accuracy_end = np.mean(accuracy_list)
    std = np.std(accuracy_list)
    recall = (np.mean(recall_list), np.std(recall_list))
    precision = (np.mean(precision_list), np.std(precision_list))

    print('Accuracy: ', accuracy_end)
    print('Standard Deviation', std)
    print('Recall', recall)
    print('Precision', precision)

    return accuracy_end, std, recall, precision

def main():
    dataset = get_dataset()
    reprocess_data(dataset)
    train_data, test_data, TRAINING_VOCAB, TEST_VOCAB = test_train_split(dataset)
    word2vec_path = 'GoogleNews-vectors-negative300.bin.gz'
    word2vec = models.KeyedVectors.load_word2vec_format(word2vec_path, binary=True)
    training_embeddings = get_word2vec_embeddings(word2vec, train_data, generate_missing=True)
    MAX_SEQUENCE_LENGTH = 50
    EMBEDDING_DIM = 300
    tokenizer = Tokenizer(num_words=len(TRAINING_VOCAB), lower=True, char_level=False)
    tokenizer.fit_on_texts(train_data["Text_Final"].tolist())
    training_sequences = tokenizer.texts_to_sequences(train_data["Text_Final"].tolist())

    train_word_index = tokenizer.word_index
    print('Found %s unique tokens.' % len(train_word_index))
    train_embedding_weights = np.zeros((len(train_word_index) + 1, EMBEDDING_DIM))
    for word, index in train_word_index.items():
        train_embedding_weights[index, :] = word2vec[word] if word in word2vec else np.random.rand(EMBEDDING_DIM)

    train_cnn_data = pad_sequences(training_sequences, maxlen=MAX_SEQUENCE_LENGTH)
    test_sequences = tokenizer.texts_to_sequences(test_data["Text_Final"].tolist())
    test_cnn_data = pad_sequences(test_sequences, maxlen=MAX_SEQUENCE_LENGTH)
    label_names = ['Pos', 'Neg', 'Neu']
    y_train = train_data[label_names].values
    y_test = test_data[label_names].values
    x_train = train_cnn_data
    x_test = test_cnn_data
    # model = ConvNet(train_embedding_weights, MAX_SEQUENCE_LENGTH, len(train_word_index) + 1, EMBEDDING_DIM,
    # len(list(label_names)))
    model = RNN(train_embedding_weights, MAX_SEQUENCE_LENGTH, len(train_word_index) + 1, EMBEDDING_DIM,
                len(list(label_names)))
    history = model.fit(x_train, y_train, epochs=10, batch_size=64, validation_data=(x_test, y_test))
    plot_history(history)
    '''
    X=np.concatenate((x_train,x_test))
    y=np.concatenate((y_train,y_test))

    cross_val_NN(5,X,y,train_embedding_weights,MAX_SEQUENCE_LENGTH,
                 train_word_index,EMBEDDING_DIM,label_names,max_epoch=3,
                 nnmodel='RNN')
                 #For CNN use nnmodel='CNN', max_epoch=2
    '''

if __name__ == '__main__':
    main()

max_stars_repo_name: foesa/MachineLearningProject

max_issues_repo_name: foesa/MachineLearningProject

max_forks_repo_name: foesa/MachineLearningProject


text:
import pandas as pd
import numpy as np
import unittest

class TestSeriesSum(unittest.TestCase):
    """
    Test the pandas.series.sum function
    sum() will be used to get the sym of the values for the requested axis
    it will return the sum of the requested series.
    parameters:
    level:int or level name or default none
    if the axis is a multiindex(hierarchical),count along a particular level collapsing into a smaller series
    returns sum of the of the values of requested axis.
    if you have an series[4,2,0,8] for names['dog','fish','cat','mouse'] it wil returns
    dog     4
    fish    2
    cat     0
    mouse   8
    sum()   14
    """
    def setUp(self):
        self.series1 = pd.Series([], dtype='int')
        self.series2 = pd.Series([1,2,3,4])
        self.series3 = pd.Series([2,4,6,8],["dog","cat","pig","cow"])
    def test_series_sum(self):
        self.assertEqual(self.series1.sum(),0)
        self.assertEqual(self.series2.sum(),10)
        self.assertEqual(self.series3.sum(),20)

if __name__ == '__main__':
    unittest.main()

max_stars_repo_name: soothingjennyg/pandasTestingProject

max_issues_repo_name: soothingjennyg/pandasTestingProject

max_forks_repo_name: soothingjennyg/pandasTestingProject


text:
# coding=utf-8
# Copyright 2018 The Google AI Language Team Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Tests for data utils."""

import itertools
from typing import Dict, List

from absl.testing import absltest
from absl.testing import parameterized
from language.mentionmemory.utils import mention_preprocess_utils
from language.mentionmemory.utils import test_utils
import numpy as np
import tensorflow.compat.v2 as tf

def _get_test_mask_mentions_and_tokens_iterable():

  mask_rates = [0.0, 0.2, 1.0]
  mention_mask_rates = [0.0, 0.2, 1.0]
  text_lens_0 = [80, 100]
  text_lens_1 = [0, 1]
  n_mentions_list_0 = [10]
  n_mentions_list_1 = [0]

  return itertools.chain(
      itertools.product(mask_rates, mention_mask_rates, text_lens_0,
                        n_mentions_list_0),
      itertools.product(mask_rates, mention_mask_rates, text_lens_1,
                        n_mentions_list_1))

def _get_test_preprocess_mention_targets_iterable():

  max_mention_targets_list = [0, 5, 10]
  text_lens_0 = [80, 100]
  text_lens_1 = [0, 1]
  n_mentions_list_0 = [5, 10]
  n_mentions_list_1 = [0]

  return itertools.chain(
      itertools.product(max_mention_targets_list, text_lens_0,
                        n_mentions_list_0),
      itertools.product(max_mention_targets_list, text_lens_1,
                        n_mentions_list_1))

def _get_test_process_batchwise_mention_targets_iterable():

  batch_size_list = [1, 2, 3]
  max_mention_targets_list = [0, 5, 10]
  text_lens_0 = [80, 100]
  text_lens_1 = [0, 1]
  n_mentions_list_0 = [5, 10]
  n_mentions_list_1 = [0]
  max_mentions_list = [1, 5, 10]

  return itertools.chain(
      itertools.product(batch_size_list, text_lens_0, n_mentions_list_0,
                        max_mentions_list, max_mention_targets_list),
      itertools.product(batch_size_list, text_lens_1, n_mentions_list_1,
                        max_mentions_list, max_mention_targets_list))

def _get_test_add_entity_tokens():
  batch_size_list = [1, 2, 10]
  new_length_list = [50, 75, 100, 125, 150, 200]
  p_mention_to_mask_list = [0, 0.5, 1.0]

  test_list = itertools.product(batch_size_list, new_length_list,
                                p_mention_to_mask_list)
  test_list = [tuple([index] + list(x)) for index, x in enumerate(test_list)]
  return test_list

class MentionPreprocessUtilsTest(tf.test.TestCase, test_utils.TestCase):
  """Tests for mention-specific preprocessing."""

  max_len = 150
  max_mlm_targets = 20
  mention_size = 5
  max_mentions = 10
  mask_token_id = 200
  vocab_size = 100

  def _gen_input(self, text_len: int, n_mentions: int):
    """Generate synthetic inputs to test masking."""
    text_ids = np.pad(
        np.random.randint(1, self.vocab_size, size=text_len),
        (0, self.max_len - text_len),
        mode='constant')
    text_ids = text_ids.astype(np.int64)
    text_mask = np.pad(
        np.ones(text_len, dtype=np.int64), (0, self.max_len - text_len),
        mode='constant')
    mention_start_positions = np.random.choice(
        max((text_len - self.mention_size) // self.mention_size, 0),
        size=n_mentions,
        replace=False) * self.mention_size
    mention_start_positions.sort()
    mention_end_positions = mention_start_positions + self.mention_size - 1
    mention_end_positions = np.minimum(mention_end_positions, self.max_len - 1)
    dense_span_starts = np.zeros_like(text_ids)
    dense_span_starts[mention_start_positions] = 1
    dense_span_ends = np.zeros_like(text_ids)
    dense_span_ends[mention_end_positions] = 1
    dense_mention_mask = np.zeros(self.max_len, dtype=np.int64)
    dense_mention_ids = np.zeros(self.max_len, dtype=np.int64)
    for idx in range(n_mentions):
      mention_slice = slice(mention_start_positions[idx],
                            mention_end_positions[idx] + 1)
      dense_mention_mask[mention_slice] = 1
      dense_mention_ids[mention_slice] = np.random.randint(1, 1000)

    # This feature is only for `test_process_batchwise_mention_targets`
    dense_is_masked = np.random.randint(2, size=self.max_len)
    return {
        'text_ids': text_ids,
        'text_mask': text_mask,
        'dense_span_starts': dense_span_starts,
        'dense_span_ends': dense_span_ends,
        'mention_start_positions': mention_start_positions,
        'mention_end_positions': mention_end_positions,
        'dense_mention_ids': dense_mention_ids,
        'dense_mention_mask': dense_mention_mask,
        'dense_is_masked': dense_is_masked,
    }

  @parameterized.parameters((1, 1), (5, 5), (10, 1), (10, 2), (20, 3), (20, 5),
                            (20, 10), (100, 10), (100, 20), (100, 40))
  def test_get_dense_is_inside_for_dense_spans(self, seq_length, num_spans):
    sparse_start_positions = np.random.randint(seq_length, size=(num_spans,))
    sparse_end_positions = np.random.randint(seq_length, size=(num_spans,))
    sparse_end_positions = np.maximum(sparse_start_positions,
                                      sparse_end_positions)

    dense_start_positions = np.zeros(seq_length, dtype=np.int32)
    dense_end_positions = np.zeros(seq_length, dtype=np.int32)
    for i in range(num_spans):
      dense_start_positions[sparse_start_positions[i]] += 1
      dense_end_positions[sparse_end_positions[i]] += 1

    start_positions_tf = tf.compat.v1.placeholder_with_default(
        dense_start_positions, shape=[None])
    end_positions_tf = tf.compat.v1.placeholder_with_default(
        dense_end_positions, shape=[None])
    actual_tf = mention_preprocess_utils.get_dense_is_inside_for_dense_spans(
        start_positions_tf, end_positions_tf)
    actual_np = self.evaluate(actual_tf)
    for i in range(seq_length):
      is_inside_span = False
      for j in range(num_spans):
        if sparse_start_positions[j] <= i and i <= sparse_end_positions[j]:
          is_inside_span = True
          break
      self.assertEqual(actual_np[i], int(is_inside_span))

  @parameterized.parameters((1, 1), (5, 5), (10, 1), (10, 2), (20, 3), (20, 5),
                            (20, 10), (100, 10), (100, 20), (100, 40))
  def test_get_dense_is_inside_for_sparse_spans(self, seq_length, num_spans):
    sparse_start_positions = np.random.randint(seq_length, size=(num_spans,))
    sparse_end_positions = np.random.randint(seq_length, size=(num_spans,))
    sparse_end_positions = np.maximum(sparse_start_positions,
                                      sparse_end_positions)

    start_positions_tf = tf.compat.v1.placeholder_with_default(
        sparse_start_positions, shape=[None])
    end_positions_tf = tf.compat.v1.placeholder_with_default(
        sparse_end_positions, shape=[None])
    actual_tf = mention_preprocess_utils.get_dense_is_inside_for_sparse_spans(
        start_positions_tf, end_positions_tf, seq_length)
    actual_np = self.evaluate(actual_tf)
    for i in range(seq_length):
      is_inside_span = False
      for j in range(num_spans):
        if sparse_start_positions[j] <= i and i <= sparse_end_positions[j]:
          is_inside_span = True
          break
      self.assertEqual(actual_np[i], int(is_inside_span))

  def test_dynamic_padding_1d(self):

    def pad(values, length):
      tensor = tf.convert_to_tensor(values, dtype=tf.int32)
      return mention_preprocess_utils.dynamic_padding_1d(
          tensor, length, padding_token_id=0)

    self.assertAllEqual([1, 2], pad([1, 2, 3], 2))
    self.assertAllEqual([1, 2, 3], pad([1, 2, 3], 3))
    self.assertAllEqual([1, 2, 3, 0, 0], pad([1, 2, 3], 5))

  def test_mask_tokens_by_spans_uniform_distribution(self):
    n_repeats = 2000
    seq_length = 50
    mask_rate = 0.4
    max_mlm_targets = 10
    text_ids = np.arange(seq_length)
    start_positions = [0, 10, 20, 30, 40]
    end_positions = [0, 11, 22, 34, 49]

    n_times_position_was_masked = np.zeros(seq_length)

    text_ids_tf = tf.compat.v1.placeholder_with_default(text_ids, shape=[None])
    start_positions_tf = tf.compat.v1.placeholder_with_default(
        start_positions, shape=[None])
    end_positions_tf = tf.compat.v1.placeholder_with_default(
        end_positions, shape=[None])

    for _ in range(n_repeats):
      positions_to_mask_tf = mention_preprocess_utils.mask_tokens_by_spans(
          text_ids_tf, start_positions_tf, end_positions_tf, mask_rate,
          max_mlm_targets)
      positions_to_mask = self.evaluate(positions_to_mask_tf)
      n_times_position_was_masked[positions_to_mask] += 1

    n_times_position_was_masked /= n_repeats

    for i in range(seq_length):
      is_inside_span = False
      for j in range(len(start_positions)):
        if start_positions[j] <= i and i <= end_positions[j]:
          is_inside_span = True
          break
      if is_inside_span:
        self.assertNear(n_times_position_was_masked[i], mask_rate, 0.1)
      else:
        self.assertEqual(n_times_position_was_masked[i], 0)

  @parameterized.parameters(_get_test_mask_mentions_and_tokens_iterable())
  def test_mask_mentions_and_tokens(
      self,
      non_mention_mask_rate: float,
      mention_mask_rate: float,
      text_len: int,
      n_mentions: int,
  ):
    """Test masking for correctness and consistency."""

    input_features = self._gen_input(
        text_len=text_len,
        n_mentions=n_mentions,
    )

    masked_dict_tf = mention_preprocess_utils.mask_mentions_and_tokens_tf(
        text_ids=tf.convert_to_tensor(input_features['text_ids']),
        text_mask=tf.convert_to_tensor(input_features['text_mask']),
        dense_span_starts=tf.convert_to_tensor(
            input_features['dense_span_starts']),
        dense_span_ends=tf.convert_to_tensor(input_features['dense_span_ends']),
        non_mention_mask_rate=non_mention_mask_rate,
        mention_mask_rate=mention_mask_rate,
        max_mlm_targets=self.max_mlm_targets,
        mask_token_id=self.mask_token_id,
        vocab_size=self.vocab_size,
        random_replacement_prob=0,
        identity_replacement_prob=0,
    )
    masked_dict = self.evaluate(masked_dict_tf)

    # Check if arrays have correct length
    self.assertLen(masked_dict['masked_text_ids'], self.max_len)
    self.assertLen(masked_dict['mlm_target_positions'], self.max_mlm_targets)
    self.assertLen(masked_dict['mlm_target_ids'], self.max_mlm_targets)
    self.assertLen(masked_dict['mlm_target_weights'], self.max_mlm_targets)
    self.assertLen(masked_dict['mlm_target_is_mention'], self.max_mlm_targets)

    # Check if number of masked tokens corresponds to nonzero weights
    self.assertEqual(
        (masked_dict['masked_text_ids'] == self.mask_token_id).sum(),
        int(masked_dict['mlm_target_weights'].sum()))

    # Check if target ids consistent with token ids at target positions
    self.assertTrue(
        np.all((masked_dict['mlm_target_ids'] == input_features['text_ids'][
            masked_dict['mlm_target_positions']]
               )[:int(masked_dict['mlm_target_weights'].sum())]))

  @parameterized.parameters(_get_test_mask_mentions_and_tokens_iterable())
  def test_mask_mentions_and_tokens_numpy(
      self,
      mask_rate: float,
      mention_mask_rate: float,
      text_len: int,
      n_mentions: int,
  ):
    """Test masking for correctness and consistency."""

    input_features = self._gen_input(
        text_len=text_len,
        n_mentions=n_mentions,
    )

    masked_dict = mention_preprocess_utils.mask_mentions_and_tokens(
        text_ids=input_features['text_ids'],
        text_mask=input_features['text_mask'],
        mention_start_positions=input_features['mention_start_positions'],
        mention_end_positions=input_features['mention_end_positions'],
        mask_rate=mask_rate,
        mention_mask_rate=mention_mask_rate,
        max_mlm_targets=self.max_mlm_targets,
        mask_token_id=self.mask_token_id,
    )

    # Check if arrays have correct length
    self.assertLen(masked_dict['masked_text_ids'], self.max_len)
    self.assertLen(masked_dict['mlm_target_positions'], self.max_mlm_targets)
    self.assertLen(masked_dict['mlm_target_ids'], self.max_mlm_targets)
    self.assertLen(masked_dict['mlm_target_weights'], self.max_mlm_targets)
    self.assertLen(masked_dict['mlm_target_is_mention'], self.max_mlm_targets)

    # Check if number of masked tokens corresponds to nonzero weights
    self.assertEqual(
        (masked_dict['masked_text_ids'] == self.mask_token_id).sum(),
        int(masked_dict['mlm_target_weights'].sum()))

    # Check if target ids consistent with token ids at target positions
    self.assertTrue(
        np.all((masked_dict['mlm_target_ids'] == input_features['text_ids'][
            masked_dict['mlm_target_positions']]
               )[:int(masked_dict['mlm_target_weights'].sum())]))

  @parameterized.parameters(
      ([1, 1, 0], [1, 0, 1], [0, 2, 0]),
      ([1, 1, 0, 1, 0, 0, 1, 0, 0], [1, 0, 1, 0, 0, 1, 0, 1, 0
                                    ], [0, 2, 0, 5, 0, 0, 7, 0, 0]))
  def test_get_dense_span_ends_from_starts(self, dense_span_starts: List[int],
                                           dense_span_ends: List[int],
                                           expected: List[int]):
    dense_span_starts_tf = tf.compat.v1.placeholder_with_default(
        np.array(dense_span_starts).astype(np.int32), shape=[None])
    dense_span_ends_tf = tf.compat.v1.placeholder_with_default(
        np.array(dense_span_ends).astype(np.int32), shape=[None])
    actual_tf = mention_preprocess_utils.get_dense_span_ends_from_starts(
        dense_span_starts_tf, dense_span_ends_tf)
    actual_np = self.evaluate(actual_tf)
    expected_np = np.array(expected, dtype=np.int32)
    self.assertAllEqual(actual_np, expected_np)

  @parameterized.parameters(
      _get_test_process_batchwise_mention_targets_iterable())
  def test_process_batchwise_mention_targets(
      self,
      batch_size: int,
      text_len: int,
      n_mentions: int,
      max_mentions: int,
      max_mention_targets: int,
  ):
    """Test batchwise mention processing for correctness and consistency."""
    max_mention_targets = min(max_mention_targets, max_mentions)
    dense_span_starts = np.zeros((batch_size, self.max_len), dtype=np.int32)
    dense_span_ends = np.zeros((batch_size, self.max_len), dtype=np.int32)
    dense_mention_ids_list = []
    dense_mention_mask_list = []
    dense_is_masked_list = []

    for index in range(batch_size):
      input_features = self._gen_input(
          text_len=text_len,
          n_mentions=n_mentions,
      )
      dense_span_starts[index, input_features['mention_start_positions']] = 1
      dense_span_ends[index, input_features['mention_end_positions']] = 1
      dense_mention_ids_list.append(input_features['dense_mention_ids'])
      dense_mention_mask_list.append(input_features['dense_mention_mask'])
      dense_is_masked_list.append(input_features['dense_is_masked'])

    def placeholder(array: np.ndarray) -> tf.Tensor:
      return tf.compat.v1.placeholder_with_default(
          array.astype(np.int32), shape=[None])

    dense_mention_ids = np.stack(dense_mention_ids_list)
    dense_mention_mask = np.stack(dense_mention_mask_list)
    dense_is_masked = np.stack(dense_is_masked_list)

    actual_dict_tf = mention_preprocess_utils.process_batchwise_mention_targets(
        dense_span_starts=placeholder(dense_span_starts),
        dense_span_ends=placeholder(dense_span_ends),
        dense_mention_ids=placeholder(dense_mention_ids),
        dense_linked_mention_mask=placeholder(dense_mention_mask),
        dense_is_masked=placeholder(dense_is_masked),
        max_mentions=max_mentions,
        max_mention_targets=max_mention_targets,
    )

    actual_dict_np = self.evaluate(actual_dict_tf)

    # Check if all arrays are the right length
    self.assertLen(actual_dict_np['mention_batch_positions'], max_mentions)
    self.assertLen(actual_dict_np['mention_start_positions'], max_mentions)
    self.assertLen(actual_dict_np['mention_end_positions'], max_mentions)
    self.assertLen(actual_dict_np['mention_mask'], max_mentions)
    self.assertLen(actual_dict_np['mention_target_weights'],
                   max_mention_targets)
    self.assertLen(actual_dict_np['mention_target_indices'],
                   max_mention_targets)
    self.assertLen(actual_dict_np['mention_target_ids'], max_mention_targets)
    self.assertLen(actual_dict_np['mention_target_is_masked'],
                   max_mention_targets)
    self.assertLen(actual_dict_np['mention_target_batch_positions'],
                   max_mention_targets)
    self.assertLen(actual_dict_np['mention_target_start_positions'],
                   max_mention_targets)
    self.assertLen(actual_dict_np['mention_target_end_positions'],
                   max_mention_targets)

    # Check every individual mention extracted
    num_sampled_mentions = 0
    for i in range(max_mentions):
      if actual_dict_np['mention_mask'][i] == 0:
        self.assertEqual(actual_dict_np['mention_batch_positions'][i], 0)
        self.assertEqual(actual_dict_np['mention_start_positions'][i], 0)
        self.assertEqual(actual_dict_np['mention_end_positions'][i], 0)
      else:
        num_sampled_mentions += 1
        batch_index = actual_dict_np['mention_batch_positions'][i]
        start_pos = actual_dict_np['mention_start_positions'][i]
        end_pos = actual_dict_np['mention_end_positions'][i]
        self.assertEqual(dense_span_starts[batch_index, start_pos], 1)
        self.assertEqual(dense_span_ends[batch_index, end_pos], 1)
        # Check start_pos and end_pos actually correspond to the same mention
        if start_pos < end_pos:
          self.assertEqual(
              dense_span_starts[batch_index, start_pos + 1:end_pos + 1].sum(),
              0)
          self.assertEqual(
              dense_span_ends[batch_index, start_pos + 1:end_pos].sum(), 0)

    total_mentions = dense_span_starts.sum()
    if max_mentions < total_mentions:
      self.assertEqual(num_sampled_mentions, max_mentions)
    else:
      self.assertEqual(num_sampled_mentions, total_mentions)

    # Check every individual linked mention extracted
    num_sampled_target_mentions = 0
    for j in range(max_mention_targets):
      if actual_dict_np['mention_target_weights'][j] == 0:
        self.assertEqual(actual_dict_np['mention_target_indices'][j], 0)
        self.assertEqual(actual_dict_np['mention_target_ids'][j], 0)
      else:
        num_sampled_target_mentions += 1
        i = actual_dict_np['mention_target_indices'][j]
        batch_index = actual_dict_np['mention_batch_positions'][i]
        start_pos = actual_dict_np['mention_start_positions'][i]
        end_pos = actual_dict_np['mention_end_positions'][i]
        self.assertEqual(dense_mention_mask[batch_index, start_pos], 1)
        self.assertEqual(dense_mention_ids[batch_index, start_pos],
                         actual_dict_np['mention_target_ids'][j])

    total_target_mentions = (dense_span_starts * dense_mention_mask).sum()
    if max_mention_targets < total_target_mentions:
      self.assertEqual(num_sampled_target_mentions, max_mention_targets)
    else:
      self.assertEqual(num_sampled_target_mentions, total_target_mentions)

    # Check mention masking computed correctly
    expected_value = dense_is_masked[
        actual_dict_np['mention_batch_positions'],
        actual_dict_np['mention_start_positions']][:max_mention_targets]
    self.assertTrue(
        np.all(actual_dict_np['mention_target_is_masked'] == expected_value))

    # Check mention_x_target_positions
    mention_target_indices = actual_dict_np['mention_target_indices']
    expected = actual_dict_np['mention_batch_positions'][mention_target_indices]
    self.assertTrue(
        np.all(actual_dict_np['mention_target_batch_positions'] == expected))
    expected = actual_dict_np['mention_start_positions'][mention_target_indices]
    self.assertTrue(
        np.all(actual_dict_np['mention_target_start_positions'] == expected))
    expected = actual_dict_np['mention_end_positions'][mention_target_indices]
    self.assertTrue(
        np.all(actual_dict_np['mention_target_end_positions'] == expected))

  def _gen_batch_for_test_add_entity_tokens(
      self, batch_size: int, min_length: int, max_length: int, vocab_size: int,
      max_mentions_per_sample: int, n_mentions: int, p_mention_to_pad: float,
      min_mlm_targets: int, max_mlm_targets: int,
      masked_token_id: int) -> Dict[str, np.ndarray]:
    """Generate batch with batchwise mentions to test `add_entity_tokens`."""

    features = {}

    # Generate text and mentions per sample
    features['text_ids'] = np.zeros((batch_size, max_length), dtype=np.int64)
    features['text_mask'] = np.zeros((batch_size, max_length), dtype=np.int64)
    features['mlm_target_positions'] = np.zeros((batch_size, max_mlm_targets),
                                                dtype=np.int64)
    features['mlm_target_weights'] = np.zeros((batch_size, max_mlm_targets),
                                              dtype=np.int64)
    all_mentions_in_batch = []

    for batch_index in range(batch_size):
      text_length = np.random.randint(low=min_length, high=max_length)
      text_ids = np.random.randint(
          low=1, high=vocab_size, size=text_length, dtype=np.int64)
      features['text_ids'][batch_index, :text_length] = text_ids
      features['text_mask'][batch_index, :text_length] = np.ones_like(text_ids)

      n_mlm_targets = np.random.randint(
          low=min_mlm_targets, high=max_mlm_targets)
      n_mlm_targets = min(n_mlm_targets, text_length)

      mlm_target_positions = np.random.choice(
          text_length, size=(n_mlm_targets), replace=False)
      features['mlm_target_positions'][
          batch_index, :n_mlm_targets] = mlm_target_positions
      features['mlm_target_weights'][batch_index, :n_mlm_targets] = 1

      features['text_ids'][batch_index, mlm_target_positions] = masked_token_id

      mention_positions = np.random.choice(
          text_length, size=(2 * max_mentions_per_sample), replace=False)
      mention_positions.sort()
      for index in range(max_mentions_per_sample):
        all_mentions_in_batch.append((batch_index, mention_positions[2 * index],
                                      mention_positions[2 * index + 1]))

    # Subsample at most `n_mentions` for the entire batch.
    np.random.shuffle(all_mentions_in_batch)

    def get_array_from_tuple(array_of_tuples, index):
      return np.array([x[index] for x in array_of_tuples], dtype=np.int64)

    all_mentions_in_batch = all_mentions_in_batch[:n_mentions]
    features['mention_batch_positions'] = get_array_from_tuple(
        all_mentions_in_batch, 0)
    self.assertLen(features['mention_batch_positions'], n_mentions)
    features['mention_start_positions'] = get_array_from_tuple(
        all_mentions_in_batch, 1)
    self.assertLen(features['mention_start_positions'], n_mentions)
    features['mention_end_positions'] = get_array_from_tuple(
        all_mentions_in_batch, 2)
    self.assertLen(features['mention_end_positions'], n_mentions)
    features['mention_mask'] = np.ones_like(features['mention_batch_positions'])

    # Mask fraction of `p_mention_to_pad` mentions.
    n_mentions_to_mask = int(p_mention_to_pad * n_mentions)
    if n_mentions_to_mask > 0:
      mentions_to_mask = np.random.choice(
          n_mentions, size=n_mentions_to_mask, replace=False)
      features['mention_mask'][mentions_to_mask] = 0
    self.assertEqual(features['mention_mask'].sum(),
                     n_mentions - n_mentions_to_mask)

    return features

  @parameterized.parameters(_get_test_add_entity_tokens())
  def test_add_entity_tokens(self, seed, batch_size, new_length,
                             p_mention_to_mask):
    np.random.seed(seed)
    min_length = 100
    max_length = 128
    vocab_size = 100
    max_mentions_per_sample = 32
    n_mentions = max_mentions_per_sample * batch_size
    p_mention_to_mask = 0
    entity_start_token_id = -1
    entity_end_token_id = -2
    masked_token_id = -3
    min_mlm_targets = 20
    max_mlm_targets = 30

    batch = self._gen_batch_for_test_add_entity_tokens(
        batch_size, min_length, max_length, vocab_size, max_mentions_per_sample,
        n_mentions, p_mention_to_mask, min_mlm_targets, max_mlm_targets,
        masked_token_id)

    def to_tensor(array):
      return tf.convert_to_tensor(array, dtype=tf.int64)

    actual_batch_tf = mention_preprocess_utils.add_entity_tokens(
        text_ids=to_tensor(batch['text_ids']),
        text_mask=to_tensor(batch['text_mask']),
        mention_mask=to_tensor(batch['mention_mask']),
        mention_batch_positions=to_tensor(batch['mention_batch_positions']),
        mention_start_positions=to_tensor(batch['mention_start_positions']),
        mention_end_positions=to_tensor(batch['mention_end_positions']),
        new_length=new_length,
        mlm_target_positions=to_tensor(batch['mlm_target_positions']),
        mlm_target_weights=to_tensor(batch['mlm_target_weights']),
        entity_start_token_id=entity_start_token_id,
        entity_end_token_id=entity_end_token_id,
    )
    actual_batch = self.evaluate(actual_batch_tf)

    expected_text_ids = np.zeros((batch_size, new_length), dtype=np.int64)
    expected_text_mask = np.zeros((batch_size, new_length), dtype=np.int64)
    expected_mlm_target_positions = np.zeros((batch_size, max_mlm_targets),
                                             dtype=np.int64)
    expected_mlm_target_weights = np.zeros((batch_size, max_mlm_targets),
                                           dtype=np.int64)

    mention_start_positions = batch['mention_start_positions'].copy()
    mention_end_positions = batch['mention_end_positions'].copy()
    mention_mask = batch['mention_mask'].copy()
    for batch_index in range(batch_size):
      # Subselect mentions from the current sample and insert entity tokens
      # into the corresponding sample.
      # pylint:disable=g-complex-comprehension
      current_indices = [
          (batch['mention_start_positions'][index], index)
          for index in range(n_mentions)
          if batch['mention_batch_positions'][index] == batch_index and
          batch['mention_mask'][index] == 1
      ]
      # Insert entity tokens for mentions starting from left to right.
      current_indices.sort()
      current_indices = [x[1] for x in current_indices]

      current_text_ids = batch['text_ids'][batch_index].tolist()
      current_text_mask = batch['text_mask'][batch_index].tolist()
      for index, m_index in enumerate(current_indices):
        if mention_end_positions[m_index] + 2 >= new_length:
          # Cannot insert entity tokens anymore
          for m_index_2 in current_indices[index:]:
            mention_end_positions[m_index_2] = 0
            mention_start_positions[m_index_2] = 0
            mention_mask[m_index_2] = 0
          break
        current_text_ids.insert(mention_start_positions[m_index],
                                entity_start_token_id)
        current_text_mask.insert(mention_start_positions[m_index], 1)
        mention_end_positions[m_index] += 2
        current_text_ids.insert(mention_end_positions[m_index],
                                entity_end_token_id)
        current_text_mask.insert(mention_end_positions[m_index], 1)

        for m_index_2 in current_indices[index + 1:]:
          mention_start_positions[m_index_2] += 2
          mention_end_positions[m_index_2] += 2

      current_text_ids = np.array(current_text_ids, dtype=np.int64)
      current_text_mask = np.array(current_text_mask, dtype=np.int64)

      # pylint:disable=g-complex-comprehension
      current_mlm_positions = [(batch['mlm_target_positions'][batch_index,
                                                              index], index)
                               for index in range(max_mlm_targets)
                               if batch['mlm_target_weights'][batch_index,
                                                              index] == 1]
      current_mlm_positions.sort()
      current_mlm_positions = [x[1] for x in current_mlm_positions]
      expected_mlm_target_positions[batch_index,
                                    current_mlm_positions] = np.nonzero(
                                        current_text_ids == masked_token_id)[0]
      expected_mlm_target_weights[batch_index, current_mlm_positions] = 1

      current_text_ids = current_text_ids[:new_length]
      expected_text_ids[batch_index, :len(current_text_ids)] = current_text_ids
      current_text_mask = current_text_mask[:new_length]
      expected_text_mask[
          batch_index, :len(current_text_mask)] = current_text_mask

    mlm_positions_within_new_length = expected_mlm_target_positions < new_length
    mlm_positions_within_new_length = mlm_positions_within_new_length.astype(
        np.int64)
    expected_mlm_target_weights *= mlm_positions_within_new_length
    expected_mlm_target_positions *= expected_mlm_target_weights

    self.assertArrayEqual(expected_text_ids, actual_batch['text_ids'])
    self.assertArrayEqual(expected_text_mask, actual_batch['text_mask'])

    self.assertArrayEqual(mention_start_positions,
                          actual_batch['mention_start_positions'])
    self.assertArrayEqual(mention_end_positions,
                          actual_batch['mention_end_positions'])
    self.assertArrayEqual(mention_mask, actual_batch['mention_mask'])

    self.assertArrayEqual(expected_mlm_target_positions,
                          actual_batch['mlm_target_positions'])
    self.assertArrayEqual(expected_mlm_target_weights,
                          actual_batch['mlm_target_weights'])

class HashTest(tf.test.TestCase, absltest.TestCase):
  max_int_value = 1000
  tensor_size = 1000
  batch_size = 1000

  def test_text_hash(self):
    hashes = set()
    for _ in range(self.batch_size):
      text = np.random.randint(self.max_int_value, size=self.tensor_size)
      hashes.add(mention_preprocess_utils.text_hash(text))

    duplicate_ratio = (self.batch_size - len(hashes)) / self.batch_size
    self.assertLessEqual(duplicate_ratio, 2 / self.batch_size)

  def test_text_hash_tf(self):
    hashes = set()
    for _ in range(self.batch_size):
      text = np.random.randint(self.max_int_value, size=self.tensor_size)
      text_tf = tf.convert_to_tensor(text)
      hash_tf = mention_preprocess_utils.text_hash_tf(text_tf, self.tensor_size)
      hash_np = self.evaluate(hash_tf)
      expected_hash_np = mention_preprocess_utils.text_hash(text)
      self.assertEqual(hash_np, expected_hash_np)
      hashes.add(hash_np)

    duplicate_ratio = (self.batch_size - len(hashes)) / self.batch_size
    self.assertLessEqual(duplicate_ratio, 2 / self.batch_size)

  def test_modified_cantor_pairing(self):
    a = np.random.randint(self.max_int_value, size=self.tensor_size)
    b = np.random.randint(self.max_int_value, size=self.tensor_size)

    hash_values = mention_preprocess_utils.modified_cantor_pairing(
        tf.constant(a), tf.constant(b))
    n_unique_hashes = len(np.unique(hash_values))
    duplicate_ratio = (self.tensor_size - n_unique_hashes) / self.tensor_size
    self.assertLessEqual(duplicate_ratio, 1 / 50)

if __name__ == '__main__':
  tf.test.main()


text:
""" Tests the time_shift function.
Run with:
    nosetests test_time_shift.py
"""

# Loading modules.
from __future__ import absolute_import, division, print_function
import numpy as np
import os
import sys

from numpy.testing import assert_almost_equal

# Path to the subject 009 fMRI data used in class.
pathtoclassdata = "data/ds114/"

# Add path to functions to the system path.
sys.path.append(os.path.join(os.path.dirname(__file__), "../functions/"))

from stimuli import events2neural
from event_related_fMRI_functions import hrf_single
from time_shift import time_shift,time_shift_cond,make_shift_matrix, time_correct

def test_time_shift():
    # Intialize values for class data.
    TR = 2.5
    tr_times = np.arange(0, 30, TR)
    hrf_at_trs = np.array([hrf_single(x) for x in tr_times])

    # Load class data.
    n_vols = 173
    neural_prediction = events2neural(pathtoclassdata+'ds114_sub009_t2r1_cond.txt', TR, n_vols)

    # Get np.convolve time course.
    convolved = np.convolve(neural_prediction, hrf_at_trs)
    N = len(neural_prediction)  # N == n_vols == 173

    # Compare shifted time courses by hand and using function.
    actual_shifted = convolved[5:(5+N)]
    exp_convolved2, exp_shifted = time_shift(convolved, neural_prediction, 5)
    assert_almost_equal(actual_shifted, exp_shifted)

def times_time_shift_2():
    # time_shift_cond
    assert(np.all(np.arange(5)-1==time_shift_cond(np.arange(5),1) ))

    # make_shift_matrix
    assert(np.all(make_shift_matrix(np.arange(5),np.arange(2))==
        np.array([0,1,2,3,4,
                 -1,0,1,2,3]).reshape((2,-1)).T))

def test_time_shift_3():
    happy=time_shift_cond(np.arange(100),5)

    assert(np.all(happy== -5+np.arange(100)))

def test_time_shift_4():
    happy2=make_shift_matrix(np.arange(100),np.array([1,3,5]))

    assert(np.all(happy2[:,0]== -1+np.arange(100)))

    assert(np.all(happy2[:,2]== -5+np.arange(100)))

def test_time_shift_4():
    # this isn't a good check, but increases coverage
    cond_all=2*np.arange(1,101)

    from time_shift import make_shift_matrix,time_correct

    delta_y=2*(np.arange(34))/34

    shifted=make_shift_matrix(cond_all,delta_y)

    from event_related_fMRI_functions import hrf_single, np_convolve_30_cuts

    def make_convolve_lambda(hrf_function,TR,num_TRs):
        convolve_lambda=lambda x: np_convolve_30_cuts(x,np.ones(x.shape[0]),hrf_function,TR,np.linspace(0,(num_TRs-1)*TR,num_TRs),15)[0]
        return convolve_lambda

    convolve_lambda=make_convolve_lambda(hrf_single,2,239)

    hrf_matrix=time_correct(convolve_lambda,shifted,239)

    assert(hrf_matrix[0,0]==0)

reason: import numpy,from numpy


text:
import numpy as np
import pandas as pd

def generate():
    np.random.seed(1)
    for i in range(5):
        x_range = np.random.rand(20)
        x_range *= 10
        if i == 0:
            y_range = x_range ** 2
        elif i == 1:
            y_range = x_range ** 3
        elif i == 2:
            y_range = np.sin(x_range)
        elif i == 3:
            y_range = np.exp(x_range)
        elif i == 4:
            y_range = np.log(x_range)
        data = np.hstack((x_range.reshape(20, 1), y_range.reshape(20, 1)))
        frame = pd.DataFrame(data=data, columns=["x", 'y'])
        frame.to_csv("test"+str(i+1)+'.csv', header=False, index=False)
    return

if __name__ == "__main__":
    generate()

max_stars_repo_name: hxhcreate/numerical-analysis-work

max_issues_repo_name: hxhcreate/numerical-analysis-work

max_forks_repo_name: hxhcreate/numerical-analysis-work


text:
import os
import io
import logging
import hashlib

import numpy as np

from PIL import ImageFile, Image

from .imageselection import generate_images_and_scores
from .mementoresource import memento_resource_factory

module_logger = logging.getLogger('mementoembed.mementoimagereel')

class MementoImageReelGenerationError(Exception):
    pass

class MementoImageReelFolderNotFound(MementoImageReelGenerationError):
    pass

class MementoImageReel:

    def __init__(self, user_agent, working_directory, httpcache):

        self.user_agent = user_agent
        self.working_directory = working_directory
        self.httpcache = httpcache

        # TODO: image size (height, width) defaults

    def generate_imagereel(self, urim, duration, countlimit, requested_width, requested_height):

        if os.path.isdir(self.working_directory):

            memento = memento_resource_factory(urim, self.httpcache)

            m = hashlib.sha256()
            m.update(urim.encode('utf8'))
            reelfile = "{}/{}.gif".format(self.working_directory, m.hexdigest())

            imagelist = generate_images_and_scores(
                memento.im_urim,
                self.httpcache
            )

            # TODO: what if imagelist is empty?

            scorelist = []

            for imageuri in imagelist:

                if imagelist[imageuri] is not None:
                    if 'calculated score' in imagelist[imageuri]:
                        scorelist.append( (imagelist[imageuri]["calculated score"], imageuri) )

            if not os.path.exists(reelfile):
                module_logger.info("generating animated GIF of images, output should be in {}".format(reelfile))

            baseims = []

            imagecount = 1

            for imageuri in [ i[1] for i in sorted(scorelist, reverse=True) ]:

                try:

                    r = self.httpcache.get(imageuri)

                except Exception:

                    module_logger.exception("failed to download image at {}, skipping".format(imageuri))
                    continue

                ifp = io.BytesIO(r.content)

                baseims.append(
                    Image.open(ifp).convert("RGBA", palette=Image.ADAPTIVE)
                )

                imagecount += 1

                if imagecount > countlimit:
                    break

            imout = Image.new("RGBA", (requested_width, requested_height), "black")
            imbase = Image.new("RGBA", (requested_width, requested_height), "black")

            working_ims = []

            for im in baseims:

                im_width = im.size[0]
                im_height = im.size[1]

                module_logger.debug("image size is {}".format(im.size))

                if im_width > im_height:
                    newwidth = requested_width
                    newheight = (requested_width / im_width) * im_height

                elif im_height > im_width:
                    newheight = requested_height
                    newwidth = (requested_height / im_height) * im_width

                elif im_height == im_width:
                    newheight = (requested_width / im_width) * im_height
                    newwidth = (requested_height / im_height) * im_width

                module_logger.debug("resizing")

                module_logger.debug("newimage size is {}".format( (int(newwidth), int(newheight)) ))

                im = im.resize((int(newwidth), int(newheight)), resample=Image.BICUBIC)

                working_ims.append(im)

            outputims = []

            module_logger.debug("building animated GIF")

            # Thanks: https://stackoverflow.com/questions/2563822/how-do-you-composite-an-image-onto-another-image-with-pil-in-python
            for im in working_ims:

                # imconv = im.convert("RGBA")

                im_w, im_h = im.size

                newim = imbase.copy()
                bg_w, bg_h = newim.size

                offset = ((bg_w - im_w) // 2, (bg_h - im_h) // 2)

                newim.paste(im, offset)

                outputims.append(imbase)
                # TODO: loop this! also, maybe do increments of 0.01?
                for i in range(1, 99, 10):
                    i = i / 100
                    outputims.append( Image.blend(imbase, newim, i))

                for i in range(0, 30):
                    outputims.append(newim)

                for i in range(1, 99, 10):
                    i = i / 100
                    outputims.append( Image.blend(newim, imbase, i) )

            module_logger.debug("saving animated GIF")

            with open(reelfile, 'wb') as outputfp:
                imout.save(
                    outputfp, save_all=True, format="GIF",
                    append_images=outputims, duration=duration, loop=0
                )

            with open(reelfile, 'rb') as f:
                data = f.read()

            module_logger.info("Image reel generation successful, returning video")

            return data

        else:
            msg = "Imagereel folder {} not found".format(self.working_directory)
            module_logger.error(msg)
            raise MementoImageReelFolderNotFound(msg)

max_stars_repo_name: shawnmjones/MementoEmbed

max_issues_repo_name: shawnmjones/MementoEmbed

max_forks_repo_name: shawnmjones/MementoEmbed


text:
import tensorflow as tf
import numpy as np

class TextBiRNN(object):
    """
    A Bi-directional RNN for text classification/regression.
    Uses an embedding layer, followed by a bi-directional recurrent, fully-connected (and softmax) layer.
    """
    def __init__(
      self, model_type, sequence_length, num_classes, vocab_size,
      embedding_size, rnn_size, num_layers, l2_reg_lambda=0.5, model='lstm'):  # batch_size,

        # Placeholders for input, output and dropout
        self.input_x = tf.placeholder(tf.int32, [None, sequence_length], name="input_x")
        self.input_y = tf.placeholder(tf.float32, [None, num_classes], name="input_y")
        self.dropout_keep_prob = tf.placeholder(tf.float32, name="dropout_keep_prob")
        self.learning_rate = tf.placeholder(tf.float32, name="learning_rate")

        # Keeping track of l2 regularization loss (optional)
        l2_loss = tf.constant(0.0)

        # Embedding layer
        with tf.device('/cpu:0'), tf.name_scope("embedding"):
            # When trainable parameter equals True the embedding vector is non-static, otherwise is static
            self.W = tf.Variable(
                tf.random_uniform([vocab_size, embedding_size], -1.0, 1.0),
                name="W", trainable=True)
            self.embedded_chars = tf.nn.embedding_lookup(self.W, self.input_x)  # [None, sequence_length, embedding_size]

        # Create a bi-directional recurrent layer for each rnn layer
        with tf.name_scope('bi'+model):
            if model == 'rnn':
                cell_fun = tf.nn.rnn_cell.BasicRNNCell
            elif model == 'gru':
                cell_fun = tf.nn.rnn_cell.GRUCell
            elif model == 'lstm':
                cell_fun = tf.nn.rnn_cell.BasicLSTMCell

            def get_bi_cell():
                fw_cell = cell_fun(rnn_size, state_is_tuple=True) #forward direction cell
                bw_cell = cell_fun(rnn_size, state_is_tuple=True) #backward direction cell
                # fw_cell = tf.contrib.rnn.DropoutWrapper(fw_cell, output_keep_prob=self.dropout_keep_prob)
                # bw_cell = tf.contrib.rnn.DropoutWrapper(bw_cell, output_keep_prob=self.dropout_keep_prob)
                return fw_cell, bw_cell

            # Stacking multi-layers
            # cell = tf.nn.rnn_cell.MultiRNNCell([get_bi_cell() for _ in range(num_layers)])
            # initial_state = cell.zero_state(None, tf.float32)

            # Bi-lstm layer
            fw_cell, bw_cell = get_bi_cell()
            outputs, last_state = tf.nn.bidirectional_dynamic_rnn(fw_cell, bw_cell, self.embedded_chars, dtype=tf.float32)
            # outputs, last_state = tf.nn.dynamic_rnn(cell, self.embedded_chars, dtype=tf.float32)  # , initial_state=initial_state
            # --'outputs' is a tensor of shape [batch_size, max_time, cell_state_size], [batch_size, max_time, cell_state_size]
            # --'last_state' is a tensor of shape [batch_size, cell_state_size], [batch_size, cell_state_size]
            outputs = tf.concat(outputs, axis=2)
            # self.output = outputs[:, -1, :]
            self.output = tf.reduce_mean(outputs, axis=1)
            # self.output = tf.reshape(outputs, [batch_size, -1])

        # Add dropout
        with tf.name_scope("dropout"):
            self.rnn_drop = tf.nn.dropout(self.output, self.dropout_keep_prob)

        # Final (unnormalized) scores and predictions
        with tf.name_scope("output"):
            W = tf.get_variable(
                "W",
                shape=[rnn_size*2, num_classes],
                initializer=tf.contrib.layers.xavier_initializer())
            b = tf.Variable(tf.constant(0.1, shape=[num_classes]), name="b")
            l2_loss += tf.nn.l2_loss(W)
            l2_loss += tf.nn.l2_loss(b)
            self.scores = tf.nn.xw_plus_b(self.rnn_drop, W, b, name="scores")
            if model_type == 'clf':
                self.predictions = tf.argmax(self.scores, 1, name="predictions")
            elif model_type == 'reg':
                self.predictions = tf.reduce_max(self.scores, 1, name="predictions")
                self.predictions = tf.expand_dims(self.predictions, -1)

        # Calculate mean cross-entropy loss, or root-mean-square error loss
        with tf.name_scope("loss"):
            if model_type == 'clf':
                losses = tf.nn.softmax_cross_entropy_with_logits(logits=self.scores, labels=self.input_y)
                self.loss = tf.reduce_mean(losses) + l2_reg_lambda * l2_loss
            elif model_type == 'reg':
                losses = tf.sqrt(tf.losses.mean_squared_error(predictions=self.predictions, labels=self.input_y))
                self.loss = tf.reduce_mean(losses) + l2_reg_lambda * l2_loss

        # Accuracy
        with tf.name_scope("accuracy"):
            if model_type == 'clf':
                correct_predictions = tf.equal(self.predictions, tf.argmax(self.input_y, 1))
                self.accuracy = tf.reduce_mean(tf.cast(correct_predictions, "float"), name="accuracy")
            elif model_type == 'reg':
                self.accuracy = tf.constant(0.0, name="accuracy")

max_stars_repo_name: zhengwsh/text-classification


text:
from schools3.ml.baselines.ranked_baseline import RankedBaseline
from schools3.data.features.snapshot_features import SnapshotFeatures
import numpy as np
import pandas as pd

class DisciplineBaseline(RankedBaseline):
    def __init__(self):
        features = SnapshotFeatures()
        features_col = 'discipline_incidents'
        super(DisciplineBaseline, self).__init__(
            features,
            features_col,
            is_sort_asc=False
        )


text:
import tensorflow as tf
from tensorflow.keras import Model
from tensorflow.keras.layers import Layer
from tensorflow.keras import Sequential
import tensorflow.keras.layers as nn

from einops import rearrange, repeat
from einops.layers.tensorflow import Rearrange

import numpy as np

def pair(t):
    return t if isinstance(t, tuple) else (t, t)

def shift(x):
    b, h, w, c = x.shape
    shifted_x = []

    shifts = [1, -1] # [shift, axis]

    # width
    z = tf.zeros([b, h, 1, c], dtype=tf.float32)
    for idx, shift in enumerate(shifts):
        if idx == 0:
            s = tf.roll(x, shift, axis=2)[:, :, shift:, :]
            concat = tf.concat([z, s], axis=2)

        else:
            s = tf.roll(x, shift, axis=2)[:, :, :shift, :]
            concat = tf.concat([s, z], axis=2)

        shifted_x.append(concat)

    # height
    z = tf.zeros([b, 1, w, c], dtype=tf.float32)
    for idx, shift in enumerate(shifts):
        if idx == 0:
            s = tf.roll(x, shift, axis=1)[:, shift:, :, :]
            concat = tf.concat([z, s], axis=1)
        else:
            s = tf.roll(x, shift, axis=1)[:, :shift, :, :]
            concat = tf.concat([s, z], axis=1)

        shifted_x.append(concat)

    return shifted_x

def gelu(x, approximate=False):
    if approximate:
        coeff = tf.cast(0.044715, x.dtype)
        return 0.5 * x * (1.0 + tf.tanh(0.7978845608028654 * (x + coeff * tf.pow(x, 3))))
    else:
        return 0.5 * x * (1.0 + tf.math.erf(x / tf.cast(1.4142135623730951, x.dtype)))

class GELU(Layer):
    def __init__(self, approximate=False):
        super(GELU, self).__init__()
        self.approximate = approximate

    def call(self, x, training=True):
        return gelu(x, self.approximate)

class PreNorm(Layer):
    def __init__(self, fn):
        super(PreNorm, self).__init__()

        self.norm = nn.LayerNormalization()
        self.fn = fn

    def call(self, x, **kwargs):
        return self.fn(self.norm(x), **kwargs)

class MLP(Layer):
    def __init__(self, dim, hidden_dim, dropout=0.0):
        super(MLP, self).__init__()
        self.net = Sequential([
            nn.Dense(units=hidden_dim),
            GELU(),
            nn.Dropout(rate=dropout),
            nn.Dense(units=dim),
            nn.Dropout(rate=dropout)
        ])

    def call(self, x, training=True):
        return self.net(x, training=training)

class LSA(Layer):
    def __init__(self, dim, heads=8, dim_head=64, dropout=0.0):
        super(LSA, self).__init__()

        inner_dim = dim_head * heads
        self.heads = heads
        self.temperature = tf.Variable(tf.math.log(dim_head ** -0.5))

        self.attend = nn.Softmax()
        self.to_qkv = nn.Dense(units=inner_dim * 3, use_bias=False)

        self.to_out = Sequential([
            nn.Dense(units=dim),
            nn.Dropout(rate=dropout)
        ])

    def call(self, x, training=True):
        qkv = self.to_qkv(x)
        qkv = tf.split(qkv, num_or_size_splits=3, axis=-1)
        q, k, v = map(lambda t: rearrange(t, 'b n (h d) -> b h n d', h=self.heads), qkv)

        dots = tf.matmul(q, tf.transpose(k, perm=[0, 1, 3, 2])) * tf.math.exp(self.temperature)

        mask = tf.eye(dots.shape[-1], dtype=tf.bool)
        mask_value = -np.finfo(dots.dtype.as_numpy_dtype).max
        dots = tf.where(mask, mask_value, dots)

        attn = self.attend(dots)

        out = tf.matmul(attn, v)
        out = rearrange(out, 'b h n d -> b n (h d)')
        out = self.to_out(out, training=training)

        return out

class Transformer(Layer):
    def __init__(self, dim, depth, heads, dim_head, mlp_dim, dropout=0.0):
        super(Transformer, self).__init__()

        self.layers = []

        for _ in range(depth):
            self.layers.append([
                PreNorm(LSA(dim, heads = heads, dim_head = dim_head, dropout = dropout)),
                PreNorm(MLP(dim, mlp_dim, dropout=dropout))
            ])

    def call(self, x, training=True):
        for attn, ff in self.layers:
            x = attn(x, training=training) + x
            x = ff(x, training=training) + x

        return x

class SPT(Layer):
    def __init__(self, dim, patch_size):
        super(SPT, self).__init__()

        self.to_patch_tokens = Sequential([
            Rearrange('b (h p1) (w p2) c -> b (h w) (p1 p2 c)', p1=patch_size, p2=patch_size),
            nn.LayerNormalization(),
            nn.Dense(units=dim)
        ])

    def call(self, x, training=True):
        shifted_x = shift(x)
        x_with_shifts = tf.concat([x, *shifted_x], axis=-1)
        x = self.to_patch_tokens(x_with_shifts)

        return x

class ViT(Model):
    def __init__(self,
                 image_size,
                 patch_size,
                 num_classes,
                 dim,
                 depth,
                 heads,
                 mlp_dim,
                 pool='cls',
                 dim_head=64,
                 dropout=0.0,
                 emb_dropout=0.0
                 ):
        super(ViT, self).__init__()
        image_height, image_width = pair(image_size)
        patch_height, patch_width = pair(patch_size)

        assert image_height % patch_height == 0 and image_width % patch_width == 0, 'Image dimensions must be divisible by the patch size.'

        num_patches = (image_height // patch_height) * (image_width // patch_width)
        assert pool in {'cls', 'mean'}, 'pool type must be either cls (cls token) or mean (mean pooling)'

        self.patch_embedding = SPT(dim=dim, patch_size=patch_size)

        self.pos_embedding = tf.Variable(initial_value=tf.random.normal([1, num_patches + 1, dim]))
        self.cls_token = tf.Variable(initial_value=tf.random.normal([1, 1, dim]))
        self.dropout = nn.Dropout(rate=emb_dropout)

        self.transformer = Transformer(dim, depth, heads, dim_head, mlp_dim, dropout)

        self.pool = pool

        self.mlp_head = Sequential([
            nn.LayerNormalization(),
            nn.Dense(units=num_classes)
        ])

    def call(self, img, training=True, **kwargs):
        x = self.patch_embedding(img)
        b, n, d = x.shape

        cls_tokens = repeat(self.cls_token, '() n d -> b n d', b=b)
        x = tf.concat([cls_tokens, x], axis=1)
        x += self.pos_embedding[:, :(n + 1)]
        x = self.dropout(x, training=training)

        x = self.transformer(x, training=training)

        if self.pool == 'mean':
            x = tf.reduce_mean(x, axis=1)
        else:
            x = x[:, 0]

        x = self.mlp_head(x)

        return x

""" Usage
v = ViT(
    image_size = 256,
    patch_size = 16,
    num_classes = 1000,
    dim = 1024,
    depth = 6,
    heads = 16,
    mlp_dim = 2048,
    dropout = 0.1,
    emb_dropout = 0.1
)

img = tf.random.normal(shape=[4, 256, 256, 3])
preds = v(img) # (4, 1000)

spt = SPT(
    dim = 1024,
    patch_size = 16
)

tokens = spt(img) # (4, 256, 1024)
"""

max_stars_repo_name: innat/vit-tensorflow

max_issues_repo_name: innat/vit-tensorflow

max_forks_repo_name: innat/vit-tensorflow


text:
# this file will be used to train and
# report accuracy results of the model

import os
import copy
import time
import pickle

import numpy as np #numbers
from tqdm import tqdm #progress bar
import torch #pytorch
from tensorboardX import SummaryWriter #display metrics
import syft as sy

import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
from torch import nn
class Network(nn.Module):

    def __init__(self):
        super().__init__()

        # Inputs to hidden layer linear transformation
        self.hidden = nn.Linear(3, 5)
        # Output layer, 10 units - one for each digit
        self.output = nn.Linear(5, 2)
        # Define sigmoid activation and softmax output
        self.ReLU = nn.ReLU()
        self.softmax = nn.Softmax(dim=1)

    def forward(self, x):
        # Pass the input tensor through each of our operations
        x = self.hidden(x)
        x = self.ReLU(x)
        x = self.output(x)
        x = self.softmax(x)

        return x

model = Network()
hook = sy.TorchHook(torch) # add extra functionality to PyTorch
# create a machine owned by worker1
worker1 = sy.VirtualWorker(hook=hook,id='worker1')
# create a machine owned by worker2
worker2 = sy.VirtualWorker(hook=hook,id='worker2')

#### transform data from csv numpy ndarray
import csv
data = []
with open('data.csv', 'r') as csvfile:
       reader = csv.reader(csvfile)
       for row in reader:
           data.append([int(val) for val in row])
### learning task parameters
class Arguments():
    def __init__(self):
        self.batch_size = 64
        self.test_batch_size = 1000
        self.epochs = 10
        self.lr = 0.01
        self.momentum = 0.5
        self.no_cuda = False
        self.seed = 1
        self.log_interval = 10
        self.save_model = False

args = Arguments()

use_cuda = not args.no_cuda and torch.cuda.is_available()

torch.manual_seed(args.seed)

device = torch.device("cuda" if use_cuda else "cpu")

kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {}
####### data loading and sending to workers
federated_train_loader = sy.FederatedDataLoader( # <-- this is now a FederatedDataLoader
    datasets.MNIST('../data', train=True, download=True,
                   transform=transforms.Compose([
                       transforms.ToTensor(),
                       transforms.Normalize((0.1307,), (0.3081,))
                   ]))
    .federate((worker1, worker2)), # <-- NEW: we distribute the dataset across all the workers, it's now a FederatedDataset
    batch_size=args.batch_size, shuffle=True, **kwargs)

test_loader = torch.utils.data.DataLoader(
    datasets.MNIST('../data', train=False, transform=transforms.Compose([
                       transforms.ToTensor(),
                       transforms.Normalize((0.1307,), (0.3081,))
                   ])),
    batch_size=args.test_batch_size, shuffle=True, **kwargs)

#### define the train and test function
def train(args, model, device, train_loader, optimizer, epoch):
    model.train()
    for batch_idx, (data, target) in enumerate(federated_train_loader): # <-- now it is a distributed dataset
        model.send(data.location) # <-- NEW: send the model to the right location
        data, target = data.to(device), target.to(device)
        optimizer.zero_grad()
        output = model(data)
        loss = F.nll_loss(output, target)
        loss.backward()
        optimizer.step()
        model.get() # <-- NEW: get the model back
        if batch_idx % args.log_interval == 0:
            loss = loss.get() # <-- NEW: get the loss back
            print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
                epoch, batch_idx * args.batch_size, len(train_loader) * args.batch_size, #batch_idx * len(data), len(train_loader.dataset),
                100. * batch_idx / len(train_loader), loss.item()))

#### test function to run locally
def test(args, model, device, test_loader):
    model.eval()
    test_loss = 0
    correct = 0
    with torch.no_grad():
        for data, target in test_loader:
            data, target = data.to(device), target.to(device)
            output = model(data)
            test_loss += F.nll_loss(output, target, reduction='sum').item() # sum up batch loss
            pred = output.argmax(1, keepdim=True) # get the index of the max log-probability
            correct += pred.eq(target.view_as(pred)).sum().item()

    test_loss /= len(test_loader.dataset)

    print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(
        test_loss, correct, len(test_loader.dataset),
        100. * correct / len(test_loader.dataset)))

#### launch the training
model = Network().to(device)
optimizer = optim.SGD(model.parameters(), lr=args.lr) # momentum is not supported at the moment

for epoch in range(1, args.epochs + 1):
    train(args, model, device, federated_train_loader, optimizer, epoch)
    test(args, model, device, test_loader)

if (args.save_model):
    torch.save(model.state_dict(), "mnist_cnn.pt")

max_stars_repo_name: Cescollino/FederatedPi

max_issues_repo_name: Cescollino/FederatedPi

max_forks_repo_name: Cescollino/FederatedPi


text:
import numpy as np

from rl.core import Agent
from keras.layers import Concatenate
from os.path import splitext

class InterleavedAgent(Agent):
    def __init__(self, agents):
        self.agents = agents
        self.cur_agent = -1
        self.n = len(agents)

        self.compiled = False
        self.m_names = []
        self._training = False
        self._step = 0

        super(InterleavedAgent, self).__init__()

    @property
    def training(self):
        return self._training

    @training.setter
    def training(self,t):
        self._training = t
        for agent in self.agents:
            agent.training = t

    @property
    def step(self):
        return self._step

    @step.setter
    def step(self,s):
        #print "setting step %i" % s
        self._step = s
        for agent in self.agents:
            agent.step = s

    def reset_states(self):
        self.cur_agent = 1
        for agent in self.agents:
            agent.reset_states()

    def forward(self, observation):
        """Takes the an observation from the environment and returns the action to be taken next.
        If the policy is implemented by a neural network, this corresponds to a forward (inference) pass.
        # Argument
            observation (object): The current observation from the environment.
        # Returns
            The next action to be executed in the environment.
        """

        # Determine current player
        self.cur_agent = (self.cur_agent+1)%self.n

        return self.agents[self.cur_agent].forward(observation)

    def backward(self, reward, terminal):
        """Updates the agent after having executed the action returned by `forward`.
        If the policy is implemented by a neural network, this corresponds to a weight update using back-prop.
        # Argument
            reward (float): The observed reward after executing the action returned by `forward`.
            terminal (boolean): `True` if the new state of the environment is terminal.
        # Returns
            List of metrics values
        """
        return self.agents[self.cur_agent].backward(reward,terminal)[:len(self.m_names)]

    def compile(self, optimizer, metrics=[]):
        """Compiles an agent and the underlaying models to be used for training and testing.
        # Arguments
            optimizer (`keras.optimizers.Optimizer` instance): The optimizer to be used during training.
            metrics (list of functions `lambda y_true, y_pred: metric`): The metrics to run during training.
        """
        for i,agent in enumerate(self.agents):
            if not agent.compiled:
                agent.compile(optimizer[i],metrics)

        # Just truncate the list of metrics if one has more (assume prefixes match)
        if len(self.agents[0].metrics_names)<=len(self.agents[1].metrics_names):
            self.m_names = self.agents[0].metrics_names
        else:
            self.m_names = self.agents[1].metrics_names

        self.compiled = True

    def load_weights(self, filepath):
        """Loads the weights of an agent from an HDF5 file.
        # Arguments
            filepath (str): The path to the HDF5 file.
        """
        fbase, fext = splitext(filepath)
        for i, agent in enumerate(self.agents):
            agent.load_weights('%s%i%s' % (fbase,i,fext))

    def save_weights(self, filepath, overwrite=False):
        """Saves the weights of an agent as an HDF5 file.
        # Arguments
            filepath (str): The path to where the weights should be saved.
            overwrite (boolean): If `False` and `filepath` already exists, raises an error.
        """
        fbase, fext = splitext(filepath)
        for i, agent in enumerate(self.agents):
            agent.save_weights('%s%i%s' % (fbase,i,fext), overwrite)

    @property
    def layers(self):
        """Returns all layers of the underlying model(s).
        If the concrete implementation uses multiple internal models,
        this method returns them in a concatenated list.
        # Returns
            A list of the model's layers
        """
        return [ layer for agent in self.agents
                    for layer in agent.layers() ]

    @property
    def metrics_names(self):
        """The human-readable names of the agent's metrics. Must return as many names as there
        are metrics (see also `compile`).
        # Returns
            A list of metric's names (string)
        """
        # Assumes all agents share metric names
        return self.m_names

    def _on_train_begin(self):
        """Callback that is called before training begins."
        """
        for agent in self.agents:
            agent._on_train_begin()

    def _on_train_end(self):
        """Callback that is called after training ends."
        """
        for agent in self.agents:
            agent._on_train_end()

    def _on_test_begin(self):
        """Callback that is called before testing begins."
        """
        for agent in self.agents:
            agent._on_test_begin()

    def _on_test_end(self):
        """Callback that is called after testing ends."
        """
        for agent in self.agents:
            agent._on_test_end()

max_stars_repo_name: velochy/rl-bargaining

max_issues_repo_name: velochy/rl-bargaining

max_forks_repo_name: velochy/rl-bargaining


text:
import numpy as np

def cosine_apod(t, t_0, Dt):
    """If Dt>0, one for t<t_0, cosine from t_0 to t_0+Dt, zero for t>t_0+Dt.
    If Dt<0, one for t>t_0, cosine from t_0 to t_0+Dt, zero for t<t0+Dt."""
    return np.cos(np.maximum(np.minimum((t - t_0)/Dt, 1), 0)*np.pi/2)**2

def cosine_window(t, t1, t2, Dt1, Dt2=None):
    """0 in t<(t1-Dt) and t>(t2+Dt), 1 in t1<t<t2, cosine transitions."""
    t = np.asarray(t)
    if Dt2 is None:
        Dt2 = Dt1
    # return np.cos(np.minimum(np.maximum(np.maximum(t-t2,t1-t)/Dt,0),1)*math.pi/2)**2
    return np.cos(np.minimum(np.maximum(np.maximum((t1 - t)/Dt1, (t - t2)/Dt2), 0), 1)*np.pi/2)**2

def xcorr_fft_2d(a, b, a_ft=False, b_ft=False, shift=True):
    """Compute cross-correlation between two 2D arrays of equal shape by using fourier transform
    (uses convolution theorem with an extra conjugate).
    Can supply pre-calculated fts of a and/or b. If so, set a_ft and/or b_ft to True"""
    if a.shape != b.shape:
        raise ValueError('Both input arrays must have the same shape - got {0} and {1}.'.format(a.shape, b.shape))
    afft = (a if a_ft else np.fft.fft2(a))
    bfft = (b if b_ft else np.fft.fft2(b))
    xc = np.fft.ifft2(afft*np.conj(bfft))
    if shift:
        xc = np.fft.fftshift(xc)
    return xc

def xcorr_fft_1d(a, b, a_ft=False, b_ft=False, shift=True):
    """Compute cross-correlation between two 1D arrays of equal shape by using fourier transform
    (uses convolution theorem with an extra conjugate).
    Can supply pre-calculated fts of a and/or b. If so, set a_ft and/or b_ft to True"""
    if a.shape != b.shape:
        raise ValueError('Both input arrays must have the same shape - got {0} and {1}.'.format(a.shape, b.shape))
    afft = (a if a_ft else np.fft.fft(a))
    bfft = (b if b_ft else np.fft.fft(b))
    xc = np.fft.ifft(afft*np.conj(bfft))
    if shift:
        xc = np.fft.fftshift(xc)
    return xc

def thresholded(y,fraction_max=None,level=None):
    if level is not None:
        assert fraction_max is None
    else:
        level=y.max()*fraction_max
    y=y.copy()
    y[y<level]=0
    return y

max_stars_repo_name: draustin/matseq

max_issues_repo_name: draustin/matseq

max_forks_repo_name: draustin/matseq


text:
import numpy as np
import math
import pdb
from scipy.optimize import fmin_cobyla

from ilqr.obstacles import Obstacle

class Constraints:
	def __init__(self, args, obstacle_bb):
		self.args = args
		self.control_cost = np.array([[self.args.w_acc,                   0],
									  [              0, self.args.w_yawrate]])

		self.state_cost = np.array([[self.args.w_pos, 0, 0, 0],
									[0, self.args.w_pos, 0, 0],
									[0, 0, self.args.w_vel, 0],
									[0, 0, 0,               0]])
		self.coeffs = None

		self.number_of_npc = 1 # hardcode

		self.obs_constraints = {}
		for i in range(self.number_of_npc):
			self.obs_constraints[i] = Obstacle(args, i, obstacle_bb)

	def get_state_cost_derivatives(self, state, poly_coeffs, x_local_plan, npc_traj):
		"""
		Returns the first order and second order derivative of the value function wrt state
		"""
		l_x = np.zeros((self.args.num_states, self.args.horizon))
		l_xx = np.zeros((self.args.num_states, self.args.num_states, self.args.horizon))
		c_state = 0
		for i in range(self.args.horizon):
			# Offset in path derivative
			x_r, y_r = self.find_closest_point(state[:, i], poly_coeffs, x_local_plan)
			traj_cost = 2*self.state_cost@(np.array([state[0, i]-x_r, state[1, i]-y_r, state[2, i]-self.args.desired_speed, 0]))

			# Compute first order derivative
			l_x_i = traj_cost

			# Compute second order derivative
			l_xx_i = 2*self.state_cost

			# Obstacle derivative
			for j in range(self.number_of_npc):
				# pdb.set_trace()
				b_dot_obs, b_ddot_obs = self.obs_constraints[j].get_obstacle_cost_derivatives(npc_traj, i, state[:, i])
				# b_dot_obs = np.array([0, 0, 0, 0])
				# b_ddot_obs = np.zeros((4,4))

				l_x_i += b_dot_obs.squeeze()
				l_xx_i += b_ddot_obs
				# print(b_dot_obs)

			l_xx[:, :, i] = l_xx_i
			l_x[:, i] = l_x_i
			# Calulate total state cost
			# ref_state = np.array([x_r, y_r, self.args.desired_speed, 0]) # Theta does not matter
			# state_diff = state[:,i]-ref_state
			# c_state = c_state + state_diff.T @ self.state_cost @ state_diff
		# pdb.set_trace()
		return l_x, l_xx

	def get_control_cost_derivatives(self, state, control):
		"""
		Returns the control quadratic (R matrix) and linear cost term (r vector) for the trajectory
		"""
		P1 = np.array([[1],[0]])
		P2 = np.array([[0],[1]])

		l_u = np.zeros((self.args.num_ctrls, self.args.horizon))
		l_uu = np.zeros((self.args.num_ctrls, self.args.num_ctrls, self.args.horizon))
		# c_ctrl = 0
		for i in range(self.args.horizon):
			# Acceleration Barrier Max
			c = (np.matmul(control[:, i].T, P1) - self.args.acc_limits[1])
			b_1, b_dot_1, b_ddot_1 = self.barrier_function(self.args.q1_acc, self.args.q2_acc, c, P1)

			# Acceleration Barrier Min
			c = (self.args.acc_limits[0] - np.matmul(control[:, i].T, P1))
			b_2, b_dot_2, b_ddot_2 = self.barrier_function(self.args.q1_acc, self.args.q2_acc, c, -P1)

			velocity = state[2, i]

			# Yawrate Barrier Max
			c = (np.matmul(control[:, i].T, P2) - velocity*math.tan(self.args.steer_angle_limits[1])/self.args.wheelbase)
			b_3, b_dot_3, b_ddot_3 = self.barrier_function(self.args.q1_yawrate, self.args.q2_yawrate, c, P2)

			# Yawrate Barrier Min
			c = (velocity*math.tan(self.args.steer_angle_limits[0])/self.args.wheelbase - np.matmul(control[:, i].T, P2))
			b_4, b_dot_4, b_ddot_4 = self.barrier_function(self.args.q1_yawrate, self.args.q2_yawrate, c, -P2)

			l_u_i = b_dot_1 + b_dot_2 + b_dot_3 + b_dot_4 + (2*control[:, i].T @ self.control_cost).reshape(-1, 1)
			l_uu_i = b_ddot_1 + b_ddot_2 + b_ddot_3 + b_ddot_4 + 2*self.control_cost

			l_u[:, i] = l_u_i.squeeze()
			l_uu[:, :, i] = l_uu_i.squeeze()

			# Calulate total control cost
			# c_ctrl = c_ctrl + control[:,i].T @ self.control_cost @ control[:,i]

		return l_u, l_uu

	def barrier_function(self, q1, q2, c, c_dot):
		b = q1*np.exp(q2*c)
		b_dot = q1*q2*np.exp(q2*c)*c_dot
		b_ddot = q1*(q2**2)*np.exp(q2*c)*np.matmul(c_dot, c_dot.T)

		return b, b_dot, b_ddot

	def get_cost_derivatives(self, state, control, poly_coeffs, x_local_plan, npc_traj):
		"""
		Returns the different cost terms for the trajectory
		This is the main function which calls all the other functions
		"""
		self.state = state
		# pdb.set_trace()
		l_u, l_uu = self.get_control_cost_derivatives(state, control)
		l_x, l_xx = self.get_state_cost_derivatives(state, poly_coeffs, x_local_plan, npc_traj)
		l_ux = np.zeros((self.args.num_ctrls, self.args.num_states, self.args.horizon))
		# l = c_state + c_ctrl

		return l_x, l_xx, l_u, l_uu, l_ux

	def get_total_cost(self, state, control_seq, poly_coeffs, x_local_plan, npc_traj):
		"""
		Returns cost of a sequence
		"""
		J = 0
		for i in range(self.args.horizon):
			x_r, y_r = self.find_closest_point(state[:, i], poly_coeffs, x_local_plan)
			ref_state = np.array([x_r, y_r, self.args.desired_speed, 0]) # Theta does not matter
			state_diff = state[:,i]-ref_state

			c_state = state_diff.T @ self.state_cost @ state_diff
			c_ctrl = control_seq[:,i].T @ self.control_cost @ control_seq[:,i]

			J = J + c_state + c_ctrl
		return J

	def find_closest_point(self, state, coeffs, x_local_plan):
		new_x = np.linspace(x_local_plan[0], x_local_plan[-1], num=10*self.args.number_of_local_wpts)
		new_y = np.polyval(np.poly1d(coeffs), new_x)
		local_plan = np.vstack((new_x, new_y)).T

		closest_ind = np.sum((local_plan - [state[0], state[1]])**2, axis=1)
		min_i = np.argmin(closest_ind)

		return local_plan[min_i, :]

	# def get_acceleration_cost(self):
	# 	return np.matmul(np.matmul(self.args.w_acc*self.control.T*np.array([[1,0],[0,0]]))*self.control)

	# def get_yawrate_cost(self):
	# 	return np.mamtul(np.matmul(self.args.w_acc*self.control.T*np.array([[0,0],[0,1]]))*self.control)

	def desired_pose_function(self, x):
		return np.polyval(self.coeffs,x)

	def offset_obj(self, X):
		x,y = X
		return np.sqrt((x - self.state[0])**2 + (y - self.state[1])**2)

	def c1(self, X):
		x,y = X
		return self.desired_pose_function(x) - y

	def get_offset_cost(self):
		# Get closest point from the curve
		X = fmin_cobyla(offset_obj, x0=[self.state[0], self.state[1]], cons=[c1])
		x_r, y_r = X
		state_diff = np.array([state[0]-x_r, state[1]-y_r])
		Qk = np.array([[1,0,0],[0,1,0],[0,0,self.args.w_vel]])

		return np.matmul(np.matmul(state_diff.T*Q),state_diff)

	def get_velocity_cost(self, current_speed):
		return self.args.w_vel*(abs(current_speed - self.desired_speed))

reason: import numpy,from scipy