import numpy as np import pickle from keras.models import Sequential from keras.layers.embeddings import Embedding from keras.layers.recurrent import SimpleRNN from keras.layers.core import Dense,Dropout from keras.utils import to_categorical from keras.layers.wrappers import TimeDistributed from matplotlib import pyplot as plt
import data.load from metrics.accuracy import evaluate
idx2w = {w2idx[i]:i for i in w2idx} idx2lab = {labels2idx[i]:i for i in labels2idx}
1 2
n_classes = len(idx2lab) n_vocab = len(idx2w)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
words_train = [[idx2w[i] for i in w[:]] for w in train_x] labels_train = [[idx2lab[i] for i in w[:]] for w in train_label]
words_val = [[idx2w[i] for i in w[:]] for w in val_x] # labels_val = [[idx2lab[i] for i in w[:]] for w in val_label] labels_val =[] for w in val_label: for i in w[:]: labels_val.append(idx2lab[i])
print('Real Sentence : {}'.format(words_train[0])) print('Encoded Form : {}'.format(train_x[0])) print('='*40) print('Real Label : {}'.format(labels_train[0])) print('Encoded Form : {}'.format(train_label[0]))
deftrain_the_model(n_epochs,train_x,train_label,val_x,val_label): epoch,train_avgloss,val_avgloss,f1s = [],[],[],[] for i in range(1,n_epochs+1): epoch.append(i) ## training train_avg_loss =0 for n_batch,sent in enumerate(train_x): label = train_label[n_batch] # label to one-hot label = to_categorical(label,num_classes=n_classes)[np.newaxis,:] sent = sent[np.newaxis,:] loss = model.train_on_batch(sent,label) train_avg_loss += loss train_avg_loss = train_avg_loss/n_batch train_avgloss.append(train_avg_loss) ## evaluate&predict val_pred_label,pred_label_val,val_avg_loss = [],[],0 for n_batch,sent in enumerate(val_x): label = val_label[n_batch] label = to_categorical(label,num_classes=n_classes)[np.newaxis,:] sent = sent[np.newaxis,:] loss = model.test_on_batch(sent,label) val_avg_loss += loss pred = model.predict_on_batch(sent) pred = np.argmax(pred,-1)[0] val_pred_label.append(pred) val_avg_loss = val_avg_loss/n_batch val_avgloss.append(val_avg_loss)
for w in val_pred_label: for k in w[:]: pred_label_val.append(idx2lab[k]) prec, rec, f1 = evaluate(labels_val,pred_label_val, verbose=False) print('Training epoch {}\t train_avg_loss = {} \t val_avg_loss = {}'.format(i,train_avg_loss,val_avg_loss)) print('precision: {:.2f}% \t recall: {:.2f}% \t f1 :{:.2f}%'.format(prec,rec,f1)) print('-'*60) f1s.append(f1)