Пример #1
0
# In[33]:

model.fit(x=X_train,
          y=Y_train,
          batch_size=32,
          validation_data=(X_test, Y_test),
          epochs=3,
          verbose=1)

# In[34]:

model.save('./')

# In[77]:

model.evaluate(X_test, Y_test)

# In[124]:


# Preprocessing User Input
def word_to_idx(words_predict):
    words_idx = np.full((max_len, ), len(words) - 1)
    i = 0
    for w in words_predict:
        words_idx[i] = wordtoidx[w]
        i += 1
    return words_idx


input_sentence = "I want to fly in an Airbus. I am planning a trip to London"
              metrics=["accuracy"])

from tensorflow.keras.callbacks import ModelCheckpoint, EarlyStopping
from livelossplot.tf_keras import PlotLossesCallback

# chkpt = ModelCheckpoint("model_weights.h5", monitor='val_loss',verbose=1, save_best_only=True, save_weights_only=True, mode='min')
#
# early_stopping = EarlyStopping(monitor='val_accuracy', min_delta=0, patience=1, verbose=0, mode='max', baseline=None, restore_best_weights=False)
#
# callbacks = [PlotLossesCallback(), chkpt, early_stopping]
#
# history = model.fit(
#     x=x_train,
#     y=y_train,
#     validation_data=(x_test,y_test),
#     batch_size=32,
#     epochs=3,
#     callbacks=callbacks,
#     verbose=1
# )

model.evaluate(x_test, y_test)

i = np.random.randint(0, x_test.shape[0])  #659
p = model.predict(np.array([x_test[i]]))
p = np.argmax(p, axis=-1)
y_true = y_test[i]
print("{:15}{:5}\t {}\n".format("Word", "True", "Pred"))
print("-" * 30)
for w, true, pred in zip(x_test[i], y_true, p[0]):
    print("{:15}{}\t{}".format(words[w - 1], tags[true], tags[pred]))
# In[52]:

y = np.array(y_train)
y.shape

# In[53]:

get_ipython().run_cell_magic(
    'time', '',
    '\nchkpt = ModelCheckpoint("model_weights.h5", monitor=\'val_loss\',verbose=1, save_best_only=True, save_weights_only=True, mode=\'min\')\n\nearly_stopping = EarlyStopping(monitor=\'val_accuracy\', min_delta=0, patience=1, verbose=0, mode=\'max\', baseline=None, restore_best_weights=False)\n\ncallbacks = [PlotLossesCallback(), chkpt, early_stopping]\n\nhistory = model.fit(\n    x=x_train,\n    y=np.array(y_train),\n    validation_split=0.2,\n    batch_size=32, \n    epochs=10,\n    callbacks=callbacks,\n    verbose=1\n)'
)

# ### Evaluate Named Entity Recognition Model

# In[55]:

model.evaluate(x_test, np.array(y_test))

# In[61]:

i = np.random.randint(0, x_test.shape[0])
p = model.predict(np.array([x_test[i]]))
p = np.argmax(p, axis=-1)
y_true = np.argmax(np.array(y_test), axis=-1)[i]
print("{:15}{:5}\t {}\n".format("Word", "True", "Pred"))
print("-" * 30)
for w, true, pred in zip(x_test[i], y_true, p[0]):
    print("{:15}{}\t{}".format(words[w - 1], tags[true], tags[pred]))

# In[ ]: