def train(): fact_dict = load_pickle() sentences, ratings = split(fact_dict) plotter, fig, ax = create_plot(["loss", "accuracy"]) model = fm.Model(dim_input=50, dim_recurrent=100, dim_output=1) optimizer = Adam(model.parameters) plot_every = 500 for k in range(100000): output = model(sentences) loss = softmax_crossentropy(output, ratings) acc = float(output.data.squeeze() == ratings.item()) plotter.set_train_batch({ "loss": loss.item(), "accuracy": acc }, batch_size=1, plot=False) if k % plot_every == 0 and k > 0: plotter.set_train_epoch() loss.backward() optimizer.step() loss.null_gradients()
preds = model(batch) # print(len(preds)) # compute the true (a.k.a desired) values for this batch: f(x) true = true_f(batch) # print(true) # compute the loss associated with our predictions loss = l1_loss(preds,true) # back-propagate through your computational graph through your loss # this will compute: dL/dw, dL/db, dL/dv loss.backward() # execute your gradient descent function, passing all of your model's # parameters (w, b, v), and the learning rate. This will update your # model's parameters based on the loss that was computed optim.step() weights1 = model.weights1() weights2 = model.weights2() weights3 = model.weights3() weights4 = model.weights4() # FOR THE LOVE OF ALL THAT IS GOOD: NULL YOUR GRADIENTS loss.null_gradients() # this will plot add your loss value to the liveplot. Note that # `loss.item()` returns a float - we can't pass the plotter a 0D-Tensor #Loads person's weights for person in list(final_counter.keys()): c = "x" if(CoordIndex==1): c="y" data = np.load("walker_data/"+person+"/"+person+"_"+str(BodyPartIndex)+"_"+c+".npy") #adds distance between weights and data to counter
def train(x_tr, y_tr, x_te, y_te): x_tr = np.array(x_tr) y_tr = np.array(y_tr) x_train_vec = vectorize(x_tr, 92) x_te = np.array(x_te) y_te = np.array(y_te) x_test_vec = vectorize(x_te, 58) model = Model() optim = Adam(model.parameters, learning_rate=1e-4) plotter, fig, ax = create_plot(metrics=["loss", "accuracy"]) batch_size = 50 for epoch_cnt in range(7): idxs = np.arange(len(x_tr)) np.random.shuffle(idxs) for batch_cnt in range(len(x_tr) // batch_size): # make slice object so indices can be referenced later batch_indices = idxs[slice(batch_cnt * batch_size, (batch_cnt + 1) * batch_size)] #batch = x_train[batch_indices] # random batch of our training data # retrieve glove embeddings for batch # initialize every value as small number which will be the placeholder for not found embeddings arr = x_train_vec[batch_indices] """ arr = np.ones((len(batch), 200, max(train_max, test_max))) / 1000000 for i, sent in enumerate(batch): for j , word in enumerate(sent): # retrieve glove embedding for every word in sentence try: arr[i,:,j] = glove_model.get_vector(word.lower()) # continue if glove embedding not found except Exception as e: continue """ # pass model through batch and perform gradient descent pred = model(arr) truth = y_tr[batch_indices] loss = binary_cross_entropy(pred[:, 0], truth) loss.backward() optim.step() loss.null_gradients() acc = accuracy(pred[:, 0], truth) # pass loss and accuracy to noggin for plotting plotter.set_train_batch({ "loss": loss.item(), "accuracy": acc }, batch_size=batch_size) """ return model def test(x_te, y_te, model): # compute test statistics #idxs = np.arange(len(x_test)) x_te = np.array(x_te) y_te = np.array(y_te) x_test_vec = vectorize(x_te, 58) logger = LiveLogger() idxs = np.arange(len(x_te)) logger.set_train_batch(dict(metric_a=50., metric_b=5.), batch_size=50) batch_size = 50 for epoch_cnt in range(2): idxs = np.arange(len(x_te)) np.random.shuffle(idxs) """ idxs = np.arange(len(x_te)) for batch_cnt in range(0, len(x_te) // batch_size): batch_indices = idxs[slice(batch_cnt * batch_size, (batch_cnt + 1) * batch_size)] #batch = x_test[batch_indices] # again, find embeddings for batch arr = x_test_vec[batch_indices] """ arr = np.ones((len(batch), 200, max(train_max, test_max))) / 1000000 for i, sent in enumerate(batch): for j , word in enumerate(sent): try: arr[i,:,j] = glove_model.get_vector(word.lower()) except Exception as e: continue """ # perform forward pass and find accuracy but DO NOT backprop pred = model(arr) truth = y_te[batch_indices] acc = accuracy(pred[:, 0], truth) # log the test-accuracy in noggin plotter.set_test_batch({"accuracy": acc}, batch_size=batch_size) # plot the epoch-level train/test statistics plotter.set_train_epoch() plotter.set_test_epoch() return model
def main(): path = r"glove.6B.50d.txt.w2v" glove = KeyedVectors.load_word2vec_format(path, binary=False) # loads the json file path_to_json = "captions_train2014.json" with open(path_to_json, "rb") as f: json_data = json.load(f) resnet = unpickle.unpickle() with open("idfs1.pkl", mode="rb") as idf: idfs = pickle.load(idf) with open("img_to_caption1.pkl", mode="rb") as cap: img_to_caption = pickle.load(cap) #with open("img_to_coco1.pkl", mode="rb") as coco: #img_to_coco=pickle.load(coco) model = Model() model.dense1.weight = mg.Tensor(np.load('weight.npy')) model.dense1.bias = mg.Tensor(np.load('bias.npy')) optim = Adam(model.parameters) batch_size = 100 for epoch_cnt in range(100): idxs = list(resnet.keys()) np.random.shuffle(idxs) for batch_cnt in range(0, len(idxs) // batch_size - 1): batch_indices = idxs[(batch_cnt * batch_size):((batch_cnt + 1) * batch_size)] batch_indices2 = idxs[((batch_cnt + 1) * batch_size):((batch_cnt + 2) * batch_size)] # id1 = np.random.choice(list(resnet.keys())) # print(id1) id1 = batch_indices # while id1 == id2: id2 = batch_indices2 # print(type(resnet[id1]),type(img_to_caption[id1][0]),type(resnet[id2])) good_image = resnet[id1[0]] bad_image = resnet[id2[0]] text = embed_text.se_text(img_to_caption[id1[0]][0], glove, idfs) for i in id1[1:]: good_image = np.vstack((good_image, resnet[i])) text = np.vstack( (text, embed_text.se_text(img_to_caption[i][0], glove, idfs))) for i in id2[1:]: bad_image = np.vstack((bad_image, resnet[i])) sim_to_good = cos_sim.cos_sim(model(good_image), text) sim_to_bad = cos_sim.cos_sim(model(bad_image), text) # compute the loss associated with our predictions(use softmax_cross_entropy) loss = margin_ranking_loss(sim_to_good, sim_to_bad, 1, 0.1) # back-propagate through your computational graph through your loss loss.backward() # compute the accuracy between the prediction and the truth acc = accuracy(sim_to_good.data, sim_to_bad.data) # execute gradient descent by calling step() of optim optim.step() # null your gradients loss.null_gradients() np.save('weight', model.dense1.parameters[0].data) np.save('bias', model.dense1.parameters[1].data)
batch_size = #give number here for batch_cnt in range(0, len(xtrain) // batch_size): batch_indices = idxs[batch_cnt * batch_size: (batch_cnt + 1) * batch_size] batch = xtrain[batch_indices] # random batch of our training data # compute the predictions for this batch by calling on model predictions = model(batch) pass truth = ytrain[batch_indices] pass # compute the loss loss = softmax_crossentropy(predictions, truth) pass # back-propagate through your computational graph through your loss loss.backward() pass # compute the accuracy between the prediction and the truth #acc = accuracy(predictions, truth) pass # execute gradient descent by calling step() of optimization optimization.step() pass # null your gradients loss.null_gradients()
for batch_cnt in range(0, len(x_train) // batch_size): batch_indices = idxs[batch_cnt * batch_size: (batch_cnt + 1) * batch_size] old = x_train[batch_indices] batch = np.ascontiguousarray(np.swapaxes(old, 0, 1)) prediction = rnn(batch) # print(prediction.shape) truth = y_train[batch_indices] # print("pred: ", prediction) # print("truth: ", truth) loss = softmax_crossentropy(prediction, truth) loss.backward() optimizer.step() loss.null_gradients() plotter.set_train_batch({"loss": loss.item()}, batch_size=batch_size) plotter.set_train_epoch() diff = 0 sum = 0 # Tests the model for i in range(len(y_train)): old = x_train[i] w = np.ascontiguousarray(np.swapaxes(np.array(old).reshape(1, 78, 50), 0, 1)) pred = rnn(w) true = y_train[i] diff += mg.abs(pred - true)