loss.backward() # Updates parameters and zeroes gradients optimizer.step() optimizer.zero_grad() # Returns the loss return loss.item() # Returns the function that will be called inside the train loop return train_step def hinge_loss(y, y_hat): return torch.mean(torch.clamp(1 - y_hat * y, min=0)) model = SVM() # Our model optimizer = optim.Adam(model.parameters(), lr=learning_rate) # Our optimizer model.train() # Our model, SVM is a subclass of the nn.Module, so it inherits the train method losses = [] val_losses = [] train_step = make_train_step(model, hinge_loss, optimizer) train_per_epoch = int(len(train_set) / batch_size) for epoch in range(n_epochs): sum_loss = 0 sum_val_loss = 0 kbar = pkbar.Kbar(target=train_per_epoch, width=8) for i, batch in enumerate(train_loader): #TODO: when have CUDA: #x_batch = batch['imagePower'].to(device) #y_batch = batch['label'].to(device) x_batch = batch['imagePower']
test_samples = samples[int(n_samples*_train_ratio):n_samples] print("Samples distribution:", preprocessing.samples_statistics(samples, _classes, get_question)) print("Train set distribution:", preprocessing.samples_statistics(train_samples, _classes, get_question)) print("Test set distribution:", preprocessing.samples_statistics(test_samples, _classes, get_question)) train_texts = [sample.text for sample in train_samples] test_texts = [sample.text for sample in test_samples] train_matrix, test_matrix, words = preprocessing.preprocess(train_texts, test_texts, words_src = "samples", normalize_flag = False) if _model == "SVM": train_labels = preprocessing.samples_to_label(train_samples, _classes, get_question) test_labels = preprocessing.samples_to_label(test_samples, _classes, get_question) model = SVM() model.train(train_matrix, train_labels) predict = model.predict(test_matrix) elif _model == "NN": train_dists = preprocessing.samples_to_dists(train_samples, _classes, get_question) test_dists = preprocessing.samples_to_dists(test_samples, _classes, get_question) model = Neural_Network(_n_factors = train_matrix.shape[1], _learning_rate = _learning_rate, _hidden_nodes = _hidden_nodes, _last_layer = len(_classes)) model.train(train_matrix, train_dists, test_matrix, test_dists) predict = model.predict(test_matrix) predict = preprocessing.dists_to_labels(predict, _classes) test_labels = preprocessing.samples_to_label(test_samples, _classes) else: raise Exception("Unknown model flag '%s'"%str(_model)) accuracy = np.mean(predict == test_labels)
words_src=tfidf_vectorizer, normalize_flag=False, reduction=_reduction, reduce_n_attr=_reduce_n_attr, stem_words=_stem_words) model = None print("Generating labels..") if _model == "SVM": train_labels = preprocessing.samples_to_label(train_samples, _sections, get_section) test_labels = preprocessing.samples_to_label(test_samples, _sections, get_section) model = SVM() print("Training.. ") model.train(train_matrix, train_labels) predict = model.predict(test_matrix) elif _model == "NN": train_dists = preprocessing.samples_to_dists(train_samples, _sections, get_section) test_dists = preprocessing.samples_to_dists(test_samples, _sections, get_section) model = Neural_Network(_n_factors=train_matrix.shape[1], _learning_rate=_learning_rate, _hidden_nodes=_hidden_nodes, _last_layer=len(_sections)) print("Training.. ") model.train(train_matrix, train_dists, test_matrix,
train_texts = [sample.text for sample in train_samples] test_texts = [sample.text for sample in test_samples] tfidf_vectorizer = get_tfidfVectorizer_of_essay_top_tf_words() print("Vectorizer built..") train_matrix, test_matrix, words = preprocessing.preprocess(train_texts, test_texts, savedir = _save_dir, words_src = tfidf_vectorizer, normalize_flag = False, reduction = _reduction, reduce_n_attr = _reduce_n_attr, stem_words = _stem_words) model = None print("Generating labels..") if _model == "SVM": train_labels = preprocessing.samples_to_label(train_samples, _sections, get_section) test_labels = preprocessing.samples_to_label(test_samples, _sections, get_section) model = SVM() print("Training.. ") model.train(train_matrix, train_labels) predict = model.predict(test_matrix) elif _model == "NN": train_dists = preprocessing.samples_to_dists(train_samples, _sections, get_section) test_dists = preprocessing.samples_to_dists(test_samples, _sections, get_section) model = Neural_Network(_n_factors = train_matrix.shape[1], _learning_rate = _learning_rate, _hidden_nodes = _hidden_nodes, _last_layer = len(_sections)) print("Training.. ") model.train(train_matrix, train_dists, test_matrix, test_dists, max_iter = _max_iter) predict = model.predict(test_matrix) predict = preprocessing.dists_to_labels(predict, _sections) test_labels = preprocessing.samples_to_label(test_samples, _sections, get_section) else: raise Exception("Unknown model flag '%s'"%str(_model))
print("Test set distribution:", preprocessing.samples_statistics(test_samples, _classes, get_question)) train_texts = [sample.text for sample in train_samples] test_texts = [sample.text for sample in test_samples] train_matrix, test_matrix, words = preprocessing.preprocess( train_texts, test_texts, words_src="samples", normalize_flag=False) if _model == "SVM": train_labels = preprocessing.samples_to_label(train_samples, _classes, get_question) test_labels = preprocessing.samples_to_label(test_samples, _classes, get_question) model = SVM() model.train(train_matrix, train_labels) predict = model.predict(test_matrix) elif _model == "NN": train_dists = preprocessing.samples_to_dists(train_samples, _classes, get_question) test_dists = preprocessing.samples_to_dists(test_samples, _classes, get_question) model = Neural_Network(_n_factors=train_matrix.shape[1], _learning_rate=_learning_rate, _hidden_nodes=_hidden_nodes, _last_layer=len(_classes)) model.train(train_matrix, train_dists, test_matrix, test_dists) predict = model.predict(test_matrix) predict = preprocessing.dists_to_labels(predict, _classes) test_labels = preprocessing.samples_to_label(test_samples, _classes)