def __init__(self): # load the trained model self.model = load_model.get_model('') self.model.compile(loss='categorical_crossentropy', optimizer='adadelta', metrics=['accuracy'])
def main(argv): emot_dic = {"anger":0, "joy":1, "sadness":2, "fear":3} x = tokenize(argv[0]) emot = np.array([0, 0, 0, 0]) emot[emot_dic[argv[1]]] = 1 model = get_model("model.h5") out = model.predict([x.reshape((1, 50, 1)), emot.reshape((1, 4))])[0] print(argv[1], ":", np.argmax(out))
def main(argv): emot_dic = {"anger": 0, "joy": 1, "sadness": 2, "fear": 3} model = get_model("model.h5") for s in ["anger", "joy", "sadness", "fear"]: df = pd.read_csv(argv[0], sep='\t', header=None, encoding='utf-8', quoting=3) df.columns = ['id', 'text', 'polarity', 'class'] df = df[df["polarity"] == s] test = np.array(df['text']) test_type = np.array(df["polarity"]) X = [] for x in test: X.append(tokenize(x)) X = np.array(X) emot = np.zeros((len(test), 4)) for x in range(len(test_type)): emot[x, emot_dic[test_type[x]]] = 1 out = model.predict( [X.reshape((len(test), 50, 1)), emot.reshape((len(test), 4))]) y_ = np.array(df["class"]) y = np.array([int(x[0]) for x in y_]) acc = np.count_nonzero(y == out.argmax(axis=1)) / float( out.argmax(axis=1).shape[0]) print(s, acc) df["class"] = out.argmax(axis=1) df.to_csv("EI-oc_en_" + s + "_pred.txt", sep='\t', header=None, index=None)
X_test = X_test.reshape(X_test.shape[0], 1, img_rows, img_cols) input_shape = (1, img_rows, img_cols) else: X_train = X_train.reshape(X_train.shape[0], img_rows, img_cols, 1) X_test = X_test.reshape(X_test.shape[0], img_rows, img_cols, 1) input_shape = (img_rows, img_cols, 1) X_train = X_train.astype('float32') X_test = X_test.astype('float32') X_train /= 255 X_test /= 255 print('X_train shape:', X_train.shape) print(X_train.shape[0], 'train samples') print(X_test.shape[0], 'test samples') model = load_model.get_model(input_shape) model.compile(loss='categorical_crossentropy', optimizer='adadelta', metrics=['accuracy']) filepath = load_model.get_weights_file() checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max') callbacks_list = [checkpoint] model.fit(X_train, Y_train, batch_size=batch_size, epochs=nb_epoch, verbose=1, validation_data=(X_test,Y_test), callbacks=callbacks_list) score = model.evaluate(X_test, Y_test, verbose=0) predict = model.predict(X_test,batch_size = batch_size,verbose = 0) # calculate the accuracy with the test data