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()
def test_create_plot(metrics, kwargs: dict): with close_plots(): plotter, fig, ax = create_plot(metrics=metrics, **kwargs) assert isinstance(plotter, LivePlot) assert isinstance(fig, Figure) assert isinstance(ax, (Axes, ndarray)) last_n_batches = kwargs.get("last_n_batches") max_fraction_spent_plotting = kwargs.get("max_fraction_spent_plotting") assert plotter._pltkwargs["nrows"] * plotter._pltkwargs["ncols"] >= len( plotter.metrics) if last_n_batches is not None: assert plotter.last_n_batches == last_n_batches if max_fraction_spent_plotting is not None: assert plotter.max_fraction_spent_plotting == max_fraction_spent_plotting
from Model import Model from Model import train from Model import test from load import load_file from Model import accuracy from extract_triplets import all_triplets from noggin import create_plot #only for jupyter notebook plotter, fig, ax = create_plot(metrics=["loss", "accuracy"]) #getting model, model params, data learning_rate=0.1 model = Model(512, 50) num_epochs = 1 batch_size = 32 margin = 0.1 path = r'data\resnet18_features.pkl' triplets = load_file(r'data\triplets') #training the model!! train(model, num_epochs, margin, triplets, learning_rate=learning_rate, batch_size=batch_size)
def parameters(self): """ Returns the parameters of the encoding layer Returns ------- The parameters of the encoding layer """ return self.layer.parameters model = Img2Caption(512, 50) lr = 1e-3 momentum = 0.9 optim = SGD(model.parameters, learning_rate=lr, momentum=momentum) plotter, figs, axes = create_plot(metrics=["loss"]) epochs = 5 batch_size = 32 train, validation = rohan_func() # List(Tuple[]), Shape (N,) for ep in range(epochs): for batch in range(len() // batch_size): d_img = train[::, 0] w_good = train[::1] d_bad = train[::, 2] w_bad = model(d_bad) # Shape (32, 50) w_img = model(d_img) # Shape (32, 50) dot_good = mg.sum(w_img * w_good, axis=1) # Shape (32,)
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
model = m.TorchModel(f1=20, f2=10, d1=20, input_dim=1, num_classes=2).to(device) x_train_mask, x_test_mask = m.convert_data(with_mask) x_train_without, x_test_without = m.convert_data(without_mask) y_train_mask = np.ones(x_train_mask.shape[0], dtype=np.int) y_test_mask = np.ones(x_test_mask.shape[0], dtype=np.int) y_train_without = np.zeros(x_train_without.shape[0], dtype=np.int) y_test_without = np.zeros(x_test_without.shape[0], dtype=np.int) x_train = np.append(x_train_mask, x_train_without, axis=0) x_test = np.append(x_test_mask, x_test_without, axis=0) y_train = np.append(y_train_mask, y_train_without, axis=0) y_test = np.append(y_test_mask, y_test_without, axis=0) optim = torch.optim.SGD(model.parameters, lr=0.01, momentum=0.9, weight_decay=5E-4) plotter, fig, ax = create_plot(metrics=["loss", "accuracy"]) ### TODO uncomment when working in jupyter notebook loss_fn = torch.nn.CrossEntropyLoss() batch_size = 30 for epoch_cnt in range(10): idxs = np.arange(len(x_train)) np.random.shuffle(idxs) for batch_cnt in range(0, len(x_train) // batch_size): optim.zero_grad() start_ind = batch_cnt*batch_size batch_indices = idxs[start_ind : start_ind+batch_size] batch = x_train[batch_indices] # random batch of our training data pred = model(torch.Tensor(batch).to(device)) pred_true = y_train[batch_indices]
for i in range(len(x_train)): x_train[i] = np.array(to_glove(x_train[i])) x_train = np.array(x_train) print(x_train[0].shape, x_train[1].shape) print("SHAPEEE: ", x_train.shape) dim_input = 50 dim_recurrent = 16 dim_output = 2 rnn = RNN(dim_input, dim_recurrent, dim_output) optimizer = Adam(rnn.parameters) plotter, fig, ax = create_plot(metrics=["loss"]) batch_size = 20 # Trains the model over 10 epochs. for epoch_cnt in range(50): idxs = np.arange(len(x_train)) np.random.shuffle(idxs) print("training epoch number ", epoch_cnt) 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)