from keras.utils.vis_utils import plot_model from model_def import deepmoji_architecture model = deepmoji_architecture(nb_classes=63, nb_tokens=1000, maxlen=100) plot_model(model, to_file='model_architecture.png', show_shapes=True, show_layer_names=True)
test_label = [p[1] for p in item[1]] train_X, _, _ = st.tokenize_sentences(train_text) test_X, _, _ = st.tokenize_sentences(test_text) train_y = np.array([label2index[l] for l in train_label]) test_y = np.array([label2index[l] for l in test_label]) nb_classes = len(label2index) nb_tokens = len(vocabulary) # use 20& of the training set for validation train_X, val_X, train_y, val_y = train_test_split(train_X, train_y, test_size=0.2, random_state=0) # model model = deepmoji_architecture(nb_classes=nb_classes, nb_tokens=nb_tokens, maxlen=MAX_LEN, embed_dropout_rate=0.25, final_dropout_rate=0.5, embed_l2=1E-6) model.summary() # load pretrained representation model load_specific_weights(model, model_path, nb_tokens, MAX_LEN, exclude_names=["softmax"]) # train model model, acc = finetune(model, [train_X, val_X, test_X], [train_y, val_y, test_y], nb_classes, 100, method="chain-thaw", verbose=2) pred_y_prob = model.predict(test_X) if nb_classes == 2: pred_y = [0 if p < 0.5 else 1 for p in pred_y_prob]
# load vocabulary with open(vocab_path, "r") as f_vocab: vocabulary = json.load(f_vocab) nb_tokens = len(vocabulary) test_text = load_data(test_path) # sentence tokenizer (MAXLEN means the max length of input text) st = SentenceTokenizer(vocabulary, MAX_LEN) # tokenize test text test_X, _, _ = st.tokenize_sentences(test_text) # load model model = deepmoji_architecture(nb_classes=nb_classes, nb_tokens=nb_tokens, maxlen=MAX_LEN) load_specific_weights(model, model_path, nb_tokens, MAX_LEN, nb_classes=nb_classes) pred_y_prob = model.predict(test_X) if nb_classes == 2: pred_y = [0 if p < 0.5 else 1 for p in pred_y_prob] else: pred_y = np.argmax(pred_y_prob, axis=1)