def train_model(*args): model, y_train = args[0], args[-1] #print (y_train) loss_fn = nn.CrossEntropyLoss( ) if sourceType == SourceType.age else nn.BCELoss() loss_fn = nn.BCELoss() optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate) model.train() for t in range(epochs): for i in range(0, len(y_train), batch_size): if processType == ProcessType.mlp: models, embedding_train, y_train = args y_pred = model(embedding_train[i:i + batch_size]) #auc = eval_model(model, embedding_train, y_train) #print ("epoch "+str(t)+" "+str(auc)) elif processType == ProcessType.name_c_tbn: model, embedding_train, train_names_idx, train_names_len, y_train = args y_pred = model(embedding_train[i:i + batch_size], train_names_idx[i:i + batch_size], train_names_len[i:i + batch_size]) elif processType == ProcessType.tbn_att or processType == ProcessType.tbnn_att: model, numerical_train, embedding_train, y_train = args y_pred = model(numerical_train[i:i + batch_size], embedding_train[i:i + batch_size]) #auc = eval_model(model, embedding_train, y_train) #print (y_pred.numpy().tolist()) #print (y_train.numpy().tolist()) #print ("epoch "+str(t)+str(auc)) #break elif processType == ProcessType.name: model, train_names_idx, train_names_len, y_train = args y_pred = model(None, train_names_idx[i:i + batch_size], train_names_len[i:i + batch_size]) elif processType == ProcessType.tbn_c_name_att: model, numerical_train, train_names_idx, embedding_train, train_names_len, y_train = args y_pred = model(numerical_train[i:i + batch_size], embedding_train[i:i + batch_size], [(train_names_idx[i:i + batch_size], train_names_len[i:i + batch_size])]) elif processType == ProcessType.tbnn_e_att: model, numerical_train, train_emoji_idx, embedding_train, y_train = args batch_emoji_idx = get_batch_emojis(batch_size, train_emoji_idx, i) y_pred = model(numerical_train[i:i + batch_size], embedding_train[i:i + batch_size], [(batch_emoji_idx, None)]) else: #if processType == ProcessType.tbn_real_att: model, numerical_train, embedding_train, y_train = args y_pred = model(numerical_train[i:i + batch_size], embedding_train[i:i + batch_size]) #print (1/0) y_train = y_train.reshape(-1, 1) #print (y_pred) #print ( y_train[i:i+batch_size]) loss = loss_fn(y_pred, y_train[i:i + batch_size]) loss.backward() optimizer.step() optimizer.zero_grad() if t % 100 == 99: print(t, loss.item())
def train_model(*args): model, y_train = args[0], args[-1] loss_fn = nn.CrossEntropyLoss() if sourceType == SourceType.age else nn.BCELoss() optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate) model.train() for t in range(epochs): for i in range(0, len(y_train), batch_size): if processType == ProcessType.mlp: model, embedding_train, y_train = args y_pred = model(embedding_train[i:i+batch_size]) elif processType == ProcessType.name_c_tbn: model, embedding_train, train_names_idx, train_names_len, y_train = args y_pred = model(embedding_train[i:i+batch_size], train_names_idx[i:i+batch_size], train_names_len[i:i+batch_size]) elif processType == ProcessType.tbn_att or processType == ProcessType.tbnn_att: model, numerical_train, embedding_train, y_train = args y_pred = model(None, embedding_train[i:i+batch_size]) elif processType == ProcessType.name: model, train_names_idx, train_names_len, y_train = args y_pred = model(None, train_names_idx[i:i+batch_size], train_names_len[i:i+batch_size]) elif processType == ProcessType.tbn_c_name_att: model, numerical_train, train_names_idx, embedding_train, train_names_len, y_train = args y_pred = model(None, embedding_train[i:i+batch_size], [(train_names_idx[i:i+batch_size], train_names_len[i:i+batch_size]) ]) else: model, numerical_train, train_emoji_idx, embedding_train, y_train = args batch_emoji_idx = get_batch_emojis(batch_size, train_emoji_idx, i) y_pred = model(numerical_train[i:i+batch_size], embedding_train[i:i+batch_size], [(batch_emoji_idx, None) ]) loss = loss_fn(y_pred, y_train[i:i+batch_size]) loss.backward() optimizer.step() optimizer.zero_grad() if t % 100 == 99: print(t, loss.item()) torch.save(model, "saved_model/model_text_name")
def eval_model(*args): model = args[0] model.eval() if processType == ProcessType.mlp: models, embedding_test, y_test = args y_hat_test = model(embedding_test) elif processType == ProcessType.name_c_tbn: model, embedding_test, test_names_idx, test_names_len, y_test = args y_hat_test = model(embedding_test, test_names_idx, test_names_len) elif processType == ProcessType.tbn_att or processType == ProcessType.tbnn_att: model, numerical_test, embedding_test, y_test = args y_hat_test = model(numerical_test, embedding_test) elif processType == ProcessType.name: model, test_names_idx, test_names_len, y_test = args y_hat_test = model(test_names_idx, test_names_len) elif processType == ProcessType.tbn_c_name_att: model, numerical_test, test_names_idx, embedding_test, test_names_len, y_test = args y_hat_test = model(numerical_test, embedding_test, [(test_names_idx, test_names_len)]) elif processType == ProcessType.tbnn_e_att: model, numerical_test, test_emoji_idx, embedding_test, y_test = args y_hat_test = [] for i in range(0, len(y_test), batch_size): batch_emoji_idx = get_batch_emojis(batch_size, test_emoji_idx, i) with torch.no_grad(): y_hat_test_batch = model(numerical_test[i:i + batch_size], embedding_test[i:i + batch_size], [(batch_emoji_idx, None)]) y_hat_test.append(y_hat_test_batch) y_hat_test = torch.cat(y_hat_test) elif processType == ProcessType.tbn_real_att: model, numerical_test, embedding_test, y_test = args y_hat_test = model(numerical_test, embedding_test) y_hat_test_class = np.where( y_hat_test.cpu().detach().numpy() < 0.5, 0, 1 ) #np.argmax(y_hat_test.cpu().detach().numpy(), axis=1) if sourceType == SourceType.age else np.where(y_hat_test.cpu().detach().numpy()<0.5, 0, 1) y_test = torch.squeeze(y_test).cpu().detach().numpy() #auc = roc_auc_score(y_test, y_hat_test_class, multi_class="ovr" ) print(classification_report(y_test, y_hat_test_class)) print("-----------------") f1 = f1_score(y_test, y_hat_test_class, average='macro') return f1