Tensorflow2.0 text classification determines article translator

project instruction

We will use three different versions of the English translation of the same work (Homer ’s Iliad), and then train a model to determine the translator through a single line of text.

The three translators are William Cowper, Edward, Earl of Derby and Samuel Butler.

Code

1. Load the dataset

Please refer to the seventh part of the summary of Tensorflow2.0's method of loading and preprocessing data : importing text (for text classification).

2. Establish the model

model = tf.keras.Sequential()
model.add(tf.keras.layers.Embedding(vocab_size, 64))
model.add(tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(64)))
# 一个或多个紧密连接的层
# 编辑 `for` 行的列表去检测层的大小
for units in [64, 64]:
  model.add(tf.keras.layers.Dense(units, activation='relu'))

# 输出层。第一个参数是标签个数。
model.add(tf.keras.layers.Dense(3, activation='softmax'))
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

3. Train the model

model.fit(train_data, epochs=3, validation_data=test_data)
Epoch 1/3
697/697 [==============================] - 26s 38ms/step - loss: 0.5212 - accuracy: 0.7495 - val_loss: 0.3996 - val_accuracy: 0.8162
Epoch 2/3
697/697 [==============================] - 22s 31ms/step - loss: 0.2982 - accuracy: 0.8700 - val_loss: 0.4038 - val_accuracy: 0.8204
Epoch 3/3
697/697 [==============================] - 23s 33ms/step - loss: 0.2288 - accuracy: 0.9010 - val_loss: 0.4032 - val_accuracy: 0.8266

<tensorflow.python.keras.callbacks.History at 0x7f77c058cb00>

4. Test model

eval_loss, eval_acc = model.evaluate(test_data)

print('\nEval loss: {}, Eval accuracy: {}'.format(eval_loss, eval_acc))
     79/Unknown - 3s 35ms/step - loss: 0.4032 - accuracy: 0.8266
Eval loss: 0.40319899082938326, Eval accuracy: 0.8266000151634216
Published 141 original articles · praised 39 · 110,000 views

Guess you like

Origin blog.csdn.net/qq_36758914/article/details/105451680