def main(args): device = flow.device("cpu") if args.no_cuda else flow.device("cuda") with open(args.config_path, "r") as f: config = json.load(f) with open(args.vocab_path, "rb") as f: vocab = pickle.load(f) textcnn = textCNN( word_emb_dim=config["word_emb_dim"], vocab_size=len(vocab), dim_channel=config["dim_channel"], kernel_wins=config["kernel_wins"], dropout_rate=config["dropout_rate"], num_class=config["num_class"], max_seq_len=config["max_seq_len"], ) textcnn.load_state_dict(flow.load(args.model_path)) textcnn.eval() textcnn.to(device) text = utils.clean_str(args.text) text = [utils.tokenizer(text)] input = flow.tensor(np.array(utils.tensorize_data(text, vocab, max_len=200)), dtype=flow.long).to(device) predictions = textcnn(input).softmax() predictions = predictions.numpy() clsidx = np.argmax(predictions) print("predict prob: %f, class name: %s" % (np.max(predictions), clsidx))
def main(): #init net print('init net...') net = textCNN(textCNN_param) weightFile = 'textCNN_chinese_enhance\model_save\\19072619_model4_iter_49_loss_0.38.pkl' if os.path.exists(weightFile): print('load weight') net.load_state_dict(torch.load(weightFile)) else: print('No weight file!') exit() print(net) # net.cuda() net.eval() numAll = 0 numRight = 0 testData = get_testData( 'textCNN_chinese_enhance\model_save\\testdata_vec4.txt') for data in testData: numAll += 1 data = data.split(',') label = int(data[0]) sentence = np.array([int(x) for x in data[1:91]]) sentence = torch.from_numpy(sentence) predict = net(sentence.unsqueeze(0).type( torch.LongTensor)).detach().numpy()[0] label_pre, score = parse_net_result(predict) if label_pre == label and score > -100: numRight += 1 if numAll % 10 == 0: print('Accuracy:{}({}/{})'.format(100 * numRight / numAll, numRight, numAll))
def main(): # init net print('init net...') net = textCNN(textCNN_param) weightFile = 'weight.pkl' if os.path.exists(weightFile): print('load weight') net.load_state_dict(torch.load(weightFile)) else: print('No weight file!') exit() print(net) net.cuda() net.eval() numAll = 0 numRight = 0 testData = get_valData('valdata_vec.txt') for data in testData: numAll += 1 data = data.split(',') label = int(data[0]) sentence = np.array([int(x) for x in data[1:21]]) sentence = torch.from_numpy(sentence) predict = net(sentence.unsqueeze(0).type(torch.LongTensor).cuda()).cpu().detach().numpy()[0] label_pre, score = parse_net_result(predict) if label_pre == label and score > -100: numRight += 1 if numAll % 100 == 0: print('acc:{}({}/{})'.format(numRight / numAll, numRight, numAll))
def main(args): config_dct = { "word_emb_dim": args.word_emb_dim, "dim_channel": args.conv_channel_size, "kernel_wins": args.kernel_size, "dropout_rate": args.dropout_rate, "num_class": args.num_class, "max_seq_len": args.max_seq_len, } with open("config.json", "w") as f: json.dump(config_dct, f) device = flow.device("cpu") if args.no_cuda else flow.device("cuda") x_train, y_train = utils.load_dataset( os.path.join(args.dataset_path, "train")) x_test, y_test = utils.load_dataset(os.path.join(args.dataset_path, "test")) vocab_dct = utils.build_vocab(x_train + x_test) with open(args.save_vocab_path, "wb") as f: pickle.dump(vocab_dct, f) x_train = utils.tensorize_data(x_train, vocab_dct) x_test = utils.tensorize_data(x_test, vocab_dct) y_train, x_train = np.array(y_train), np.array(x_train) y_test, x_test = np.array(y_test), np.array(x_test) textcnn = textCNN( word_emb_dim=args.word_emb_dim, vocab_size=len(vocab_dct), dim_channel=args.conv_channel_size, kernel_wins=args.kernel_size, dropout_rate=args.dropout_rate, num_class=args.num_class, max_seq_len=args.max_seq_len, ) textcnn.to(device) optimizer = flow.optim.Adam(textcnn.parameters(), lr=args.learning_rate) loss_func = flow.nn.BCEWithLogitsLoss().to(device) if args.load_checkpoint != "": textcnn.load_state_dict(flow.load(args.load_checkpoint)) train( model=textcnn, device=device, train_data=(x_train, y_train), dev_data=(x_test, y_test), loss_func=loss_func, optimizer=optimizer, epochs=args.epochs, train_batch_size=args.train_batch_size, eval_batch_size=args.val_batch_size, save_path=args.save_checkpoint_path, )
def main(): # init net print('init net...') net = textCNN(textCNN_param) weightFile = 'weight.pkl' # file storages weight if os.path.exists(weightFile): print('load weight') net.load_state_dict(torch.load(weightFile)) else: net.init_weight() print(net) # net.cuda() # init dataset print('init dataset...') dataLoader = textCNN_dataLoader(dataLoader_param) # valdata = get_valdata() optimizer = torch.optim.Adam(net.parameters(), lr=0.01) criterion = nn.NLLLoss() log = open('log_{}.txt'.format(time.strftime('%y%m%d%H')), 'w') log.write('epoch step loss\n') log_test = open('log_test_{}.txt'.format(time.strftime('%y%m%d%H')), 'w') log_test.write('epoch step test_acc\n') print("training...") for epoch in range(100): for i, (clas, sentences) in enumerate(dataLoader): optimizer.zero_grad() sentences = sentences.type(torch.LongTensor) #.cuda() clas = clas.type(torch.LongTensor) #.cuda() out = net(sentences) loss = criterion(out, clas) loss.backward() optimizer.step() if (i + 1) % 1 == 0: print("epoch:", epoch + 1, "step:", i + 1, "loss:", loss.item()) data = str(epoch + 1) + ' ' + str(i + 1) + ' ' + str( loss.item()) + '\n' log.write(data) print("save model...") torch.save(net.state_dict(), weightFile) torch.save(net.state_dict(), "model\{}_model_iter_{}_{}_loss_{:.2f}.pkl".format( time.strftime('%y%m%d%H'), epoch, i, loss.item())) # current is model.pkl print("epoch:", epoch + 1, "step:", i + 1, "loss:", loss.item())
def main(): #init net print('init net...') net = textCNN(textCNN_param) weightFile = 'textCNN_chinese_enhance\model_save\weight4.pkl' if os.path.exists(weightFile): print('load weight') net.load_state_dict(torch.load(weightFile)) else: net.init_weight() print(net) # net.cuda() print('init dataset...') dataLoader = textCNN_data.textCNN_dataLoader(dataLoader_param) traindata = textCNN_data.get_testdata() optimizer = torch.optim.Adam(net.parameters(), lr=0.001) criterion = nn.NLLLoss() print("training...") for epoch in range(50): loss_total = 0 for i, (clas, sentences) in enumerate(dataLoader): optimizer.zero_grad() sentences = sentences.type(torch.LongTensor) clas = clas.type(torch.LongTensor) out = net(sentences) loss = criterion(out, clas) loss.backward() optimizer.step() loss_total += loss print('Epoch [{}/{}]:\tLoss:{:.4f}'.format(epoch + 1, 50, loss_total)) torch.save(net.state_dict(), weightFile) torch.save( net.state_dict(), "textCNN_chinese_enhance\model_save\{}_model4_iter_{}_loss_{:.2f}.pkl". format(time.strftime('%y%m%d%H'), epoch, loss_total.item())) # current is model.pkl
from torch.autograd import Variable import torch.nn.functional as F import config from model import textCNN from loadData import get_batch_data, get_data_nums from tensorboardX import SummaryWriter import numpy as np def get_acc(output, label): total = output.shape[0] _, pred_label = output.max(1) num_correct = (pred_label == label).sum().item() return num_correct / total net = textCNN(config.VOCAB_SIZE, config.EMBEDDING_SIZE, config.DROPOUT, config.CLASS_NUM) writer = SummaryWriter(log_dir='logs') if config.IS_CUDA: net = net.cuda() optimizer = torch.optim.Adam(net.parameters(), config.LEARNING_RATE) net.train() batch_data = get_batch_data('data/train.txt', batch_size=config.BATCH_SIZE, word_max_length=config.MAX_LENGTH) data_nums = get_data_nums('data/train.txt') val_batch_data = get_batch_data('data/val.txt', batch_size=config.BATCH_SIZE//4, word_max_length=config.MAX_LENGTH) val_data_nums = get_data_nums('data/val.txt') steps = 0
#设置设备 device = torch.device("cuda:1" if torch.cuda.is_available() else "cpu") # 定义超参数 parser = argparse.ArgumentParser() #本地 #parser.add_argument('--data_path', type=str, default="/Users/ren/Desktop/nlp相关/实验1/aclImdb/")#文件路径 parser.add_argument('--data_path', type=str, default="/data/renhongjie/zouye1_new/data/aclImdb/")#文件路径 parser.add_argument('--embed_size', type=int, default=300)#embeding层宽度 parser.add_argument('--num_hidens', type=int, default=100) parser.add_argument('--seq_len', type=int, default=300)#文件长度,需要截断和填充 parser.add_argument('--batch_size', type=int, default=64)#批次 parser.add_argument('--bidirectional', type=bool, default=True)#是否开启双向 parser.add_argument('--classification_num', type=int, default=2)#分类个数 parser.add_argument('--lr', type=float, default=1e-4)#学习率 parser.add_argument('--droput', type=float, default=0.5)#丢弃率 parser.add_argument('--num_epochs', type=int, default=100)#训练论数 parser.add_argument('--vocab_size', type=int, default=0)#vocab大小 parser.add_argument('--if_vail', type=bool, default=False) parser.add_argument('--word2vec_path', type=str, default="/data/renhongjie/zouye1_new/data/glove_to_word2vec.txt")#预训练词向量路径 parser.add_argument('--save_path', type=str, default="best.pth")#保存路径 parser.add_argument('--weight_decay', type=float, default=1e-4)#权重摔跤 args = parser.parse_args() if args.if_vail==True: train_iter,test_iter,vail_iter,weight=utils.data(args) else: train_iter, test_iter, weight = utils.data(args) net=model.textCNN(args,weight=weight) train.train(args,device,net,train_iter,test_iter)
def main(): print("Pytorch Version:", torch.__version__) parser = argparse.ArgumentParser(description='TextCNN') #Training args parser.add_argument('--data-csv', type=str, default='./IMDB_Dataset.csv', help='file path of training data in CSV format (default: ./train.csv)') parser.add_argument('--spacy-lang', type=str, default='en', help='language choice for spacy to tokenize the text') parser.add_argument('--pretrained', type=str, default='glove.6B.300d', help='choice of pretrined word embedding from torchtext') parser.add_argument('--epochs', type=int, default=10, help='number of epochs to train (default: 10)') parser.add_argument('--lr', type=float, default=0.001, help='learning rate (default: 0.01)') parser.add_argument('--momentum', type=float, default=0.9, help='SGD momentum (default: 0.9)') parser.add_argument('--batch-size', type=int, default=64, help='input batch size for training (default: 64)') parser.add_argument('--val-batch-size', type=int, default=64, help='input batch size for testing (default: 64)') parser.add_argument('--kernel-height', type=str, default='3,4,5', help='how many kernel width for convolution (default: 3, 4, 5)') parser.add_argument('--out-channel', type=int, default=100, help='output channel for convolutionaly layer (default: 100)') parser.add_argument('--dropout', type=float, default=0.5, help='dropout rate for linear layer (default: 0.5)') parser.add_argument('--num-class', type=int, default=2, help='number of category to classify (default: 2)') #if you are using jupyternotebook with argparser args = parser.parse_known_args()[0] #args = parser.parse_args() #Use GPU if it is available device = torch.device("cuda" if torch.cuda.is_available() else "cpu") #%% Split whole dataset into train and valid set dataset.split_train_valid(args.data_csv, './train.csv', './valid.csv', 0.7) trainset, validset, vocab = dataset.create_tabular_dataset('./train.csv', './valid.csv',args.spacy_lang, args.pretrained) #%%Show some example to show the dataset print("Show some examples from train/valid..") print(trainset[0].text, trainset[0].label) print(validset[0].text, validset[0].label) train_iter, valid_iter = dataset.create_data_iterator(args.batch_size, args.val_batch_size, trainset, validset,device) #%%Create kernels = [int(x) for x in args.kernel_height.split(',')] m = model.textCNN(vocab, args.out_channel, kernels, args.dropout , args.num_class).to(device) # print the model summery print(m) train_loss = [] train_acc = [] test_loss = [] test_acc = [] best_test_acc = -1 #optimizer optimizer = optim.Adam(m.parameters(), lr=args.lr) for epoch in range(1, args.epochs+1): #train loss tr_loss, tr_acc = training.train(m, device, train_iter, optimizer, epoch, args.epochs) print('Train Epoch: {} \t Loss: {} \t Accuracy: {}%'.format(epoch, tr_loss, tr_acc)) ts_loss, ts_acc = training.valid(m, device, valid_iter) print('Valid Epoch: {} \t Loss: {} \t Accuracy: {}%'.format(epoch, ts_loss, ts_acc)) if ts_acc > best_test_acc: best_test_acc = ts_acc #save paras(snapshot) print("model saves at {}% accuracy".format(best_test_acc)) torch.save(m.state_dict(), "best_validation") train_loss.append(tr_loss) train_acc.append(tr_acc) test_loss.append(ts_loss) test_acc.append(ts_acc) #plot train/validation loss versus epoch #plot train/validation loss versus epoch x = list(range(1, args.epochs+1)) plt.figure() plt.title("train/validation loss versus epoch") plt.xlabel("epoch") plt.ylabel("Average loss") plt.plot(x, train_loss,label="train loss") plt.plot(x, test_loss, color='red', label="test loss") plt.legend(loc='upper right') plt.grid(True) plt.show() #plot train/validation accuracy versus epoch x = list(range(1, args.epochs+1)) plt.figure() plt.title("train/validation accuracy versus epoch") plt.xlabel("epoch") plt.ylabel("accuracy(%)") plt.plot(x, train_acc,label="train accuracy") plt.plot(x, test_acc, color='red', label="test accuracy") plt.legend(loc='upper right') plt.grid(True) plt.show()