Esempio n. 1
0
out = TimeDistributed(Dense(n_tags, activation="softmax"))(
    model)  # softmax output layer

model = Model(input, out)

model.compile(optimizer="rmsprop",
              loss="categorical_crossentropy",
              metrics=["accuracy"])

history = model.fit(X_train,
                    np.array(y_train),
                    batch_size=32,
                    epochs=3,
                    validation_split=0.1,
                    verbose=1)

hist = pd.DataFrame(history.history)
plt.figure(figsize=(12, 12))
plt.plot(hist["accuracy"])
plt.plot(hist["val_accuracy"])
plt.show()

i = random.randint(0, len(X_test))
p = model.predict(np.array([X_test[i]]))
p = np.argmax(p, axis=-1)
print("{:15} ({:5}): {}".format("Word", "True", "Pred"))
for w, pred in zip(X_test[i], p[0]):
    print("{:15}: {}".format(words[w], tags[pred]))

model.save_weights("atomizer.h5")
Esempio n. 2
0
X_tr, X_te, y_tr, y_te = train_test_split(X, y, test_size=0.1)

print("Setting Up Model...")
input = Input(shape=(max_len, ))
print('1')
model = Embedding(input_dim=n_notes, output_dim=50,
                  input_length=max_len)(input)
print('2')
model = Dropout(0.1)(model)
print('3')
model = Bidirectional(
    LSTM(units=100, return_sequences=True, recurrent_dropout=0.1))(model)
print('4')
out = TimeDistributed(Dense(n_chords, activation="softmax"))(model)
print('5')
model = Model(input, out)
print('6')
model.compile(optimizer="rmsprop",
              loss="categorical_crossentropy",
              metrics=["accuracy"])
print('7')
history = model.fit(X_tr,
                    np.array(y_tr),
                    batch_size=32,
                    epochs=1,
                    validation_split=0.1,
                    verbose=1)
print('8')
model.save_weights(state_name + "model")
                  input_length=train_max_len, mask_zero=True,trainable=False)(input)  # 300-dim embedding
model = Bidirectional(LSTM(units=50, return_sequences=True,
                           recurrent_dropout=0.1))(model)  # variational biLSTM
model = Bidirectional(LSTM(units=50, return_sequences=True,
                           recurrent_dropout=0.1))(model)
model = TimeDistributed(Dense(50, activation="relu"))(model)  # a dense layer as suggested by neuralNer
crf = CRF(len(train_tag_set))  # CRF layer
out = crf(model)  # output
model = Model(input, out)
model.compile(optimizer="rmsprop", loss=crf.loss_function, metrics=[crf.accuracy])
model.summary()
history = model.fit(X_tr, np.array(y_tr), batch_size=32, epochs=20,
                    validation_split=0.1, verbose=1)
save_name = 'bilstm_crf_ner_weights.h5'
print("Saving model weights to : {}".format(save_name))
model.save_weights(save_name)
pred = model.predict(X_te)
print("Preds")
for i in range(10):
    sent_idx = X_te[i]
    word_arr = idx_to_arr(sent_idx,vocab2)
    #sent = idx_to_arr(sent_idx,vocab)
    print(arr_to_str(word_arr))
    sent_len = get_sent_length(word_arr)
    print(sent_len)
    truth = y_te[i][:sent_len]
    #print(truth[i])
    pred_arr = categorical_pred_to_tags(pred[i][:sent_len],train_tag_list)
    truth_arr = categorical_pred_to_tags(truth,train_tag_list)
    for w,p,t in zip(word_arr,pred_arr,truth_arr):
        print("{} {} {}".format(w,t,p))
    model)  # a dense layer as suggested by neuralNer
crf = CRF(n_tags)  # CRF layer
out = crf(model)  # output

model = Model(input, out)
model.compile(optimizer="rmsprop",
              loss=crf.loss_function,
              metrics=[crf.accuracy])
history = model.fit(X_tr,
                    np.array(y_tr),
                    batch_size=16,
                    epochs=40,
                    validation_split=0.1,
                    verbose=1)

model.save_weights('model/lstm_crf.h5')
from seqeval.metrics import f1_score, classification_report

test_pred = model.predict(X_te, verbose=1)
idx2tag = {i: w for w, i in tag2idx.items()}


def pred2label(pred):
    out = []
    for pred_i in pred:
        out_i = []
        for p in pred_i:
            p_i = np.argmax(p)
            out_i.append(idx2tag[p_i].replace("PAD", "O"))
        out.append(out_i)
    return out
Esempio n. 5
0
# traditional evaluation the model's performance
traditional_evaluation.evaluation(y_pred=y_pred,
                                  x_filename=x_filename,
                                  y_filename=y_filename)

# sequence evaluation of the model's performance
sequence_evaluation.evaluation(y_pred, MAX_LEN, y_test_filename)

# ======================================================================================================================
# Model Saving
# ======================================================================================================================

# Save the model (weights)
# save_all_weights | load_all_weights: saves model and optimizer weights (save_weights and save)
model.save_weights("pretrained_models\\fulltext_model_weights.h5"
                   )  # sentences_model_weights.h5
'''
# `assert_consumed` can be used as validation that all variable values have been restored from the checkpoint. 
# See `tf.train.Checkpoint.restore` for other methods in the Status object.
print(load_status.assert_consumed())

# Check that all of the pretrained weights have been loaded.
for a, b in zip(pretrained.weights, model.weights):
    np.testing.assert_allclose(a.numpy(), b.numpy())
'''

# Save the model (architecture, loss, metrics, optimizer state, weights)
model.save('pretrained_models\\fulltext_bi_lstm_crf_dense_linear.h5'
           )  # sentences_bi_lstm_crf_dense_linear.h5
'''
# Load the model