def main(): rnn = RNN(data.input_size, data.output_size) losses = [] for epoch in range(num_epochs): print("=" * 50 + (" EPOCH %i " % epoch) + "=" * 50) for i, batch in enumerate(data.sentences(num_batches)): input, target = batch #print(target) loss, outputs = rnn.train(input, target.copy()) losses.append(loss) if i % 100 == 0: print("Loss at step %d: %.2f" % (i, loss)) print("Truth: \"%s\"" % data.vec_to_sentence(target)) print("Guess: \"%s\"\n" % data.vec_to_sentence(outputs[:-1])) rnn.save() torch.save(rnn.state_dict(), "models/baseline.module")
delta_valid = 10.0 old_training_error = 0.0 old_valid_error = 0.0 result = [] # list for saving all predictions made by the network # file = 'training_pred.pickle.gz' for k in range(n_epoch): correct_number_train = 0.0 correct_number_valid = 0.0 class_occurrence_train = np.zeros(n_classes) class_occurrence_valid = np.zeros(n_classes) confusion_matrix_train = np.zeros((n_classes, n_classes)) confusion_matrix_valid = np.zeros((n_classes, n_classes)) n_data = 0 for i in range(n_batches): cost, output, gradient, hidden_act, t, x_1, wout = rnn.train(input[i], mask[i], label[i], lrate) rnn.save(filesave) train_cost.append(cost) prob = rnn.predict(input[i], mask[i]) for jj in range(prob.shape[1]): for kk in range(prob.shape[0]): prediction = (-prob[kk, jj, :]).argsort() label_sorted = (-label[i][kk, jj, :]).argsort() if any(label[i][kk, jj, :]): n_data += 1 if prediction[0] == label_sorted[0]: correct_number_train += 1 confusion_matrix_train[prediction[0], label_sorted[0]] += 1 class_occurrence_train[label_sorted[0]] += 1 log('> epoch ' + str(k) + ', training cost: ' + str(100 * np.mean(train_cost))) confusion_matrix_train = 100 * confusion_matrix_train / class_occurrence_train train_error_rate = 100 * (1.0 - correct_number_train / n_data)
f = open( 'synthesized-' + str( datetime.datetime.fromtimestamp( time.time()).strftime('%Y-%m-%d %H:%M:%S')), 'w+') for epoch in range(n_epoch): print("\t\t---NEW EPOCH--- number: %d" % (epoch + last_epoch)) RNN.h0 = np.zeros((m, 1)) for X_seq, Y_seq in get_batch(): step += 1 loss = RNN.train(X_seq, Y_seq) smooth_loss = 0.999 * smooth_loss + 0.001 * loss if smooth_loss != -1 else loss losses.append(smooth_loss) if step % 500 == 0: f.write( '\n\tSynthesized text at iteration: %d with smooth loss: %f\n' % (step, smooth_loss)) text = synthesize(X_seq) f.write(text.encode('ascii', 'ignore').decode('ascii')) f.write('\n') f.flush() elif step % 100 == 0: print(' ... Smooth loss: %f ...' % (smooth_loss)) if save: RNN.save(smooth_loss, step, epoch + last_epoch + 1) plt.plot(losses, 'g', label='losses') plt.ylabel("loss") plt.legend() plt.show()
delta_valid = 10.0 old_training_error = 0.0 old_valid_error = 0.0 result = [] # list for saving all predictions made by the network # file = 'training_pred.pickle.gz' for k in range(n_epoch): correct_number_train = 0.0 correct_number_valid = 0.0 class_occurrence_train = np.zeros(n_classes) class_occurrence_valid = np.zeros(n_classes) confusion_matrix_train = np.zeros((n_classes, n_classes)) confusion_matrix_valid = np.zeros((n_classes, n_classes)) n_data = 0 for i in range(n_batches): cost, output = rnn.train(input[i], mask[i], label[i], lrate) rnn.save(filesave) train_cost.append(cost) prob = rnn.predict(input[i], mask[i]) if multi_label == "false": for jj in range(prob.shape[1]): for kk in range(prob.shape[0]): prediction = (-prob[kk, jj, :]).argsort() label_sorted = (-label[i][kk, jj, :]).argsort() if any(label[i][kk, jj, :]): n_data += 1 if prediction[0] == label_sorted[0]: correct_number_train += 1 confusion_matrix_train[prediction[0], label_sorted[0]] += 1 class_occurrence_train[label_sorted[0]] += 1 if multi_label == "true":