|
import tensorflow as tf |
|
|
|
|
|
corpus = """ |
|
The quick brown fox jumps over the lazy dog. |
|
Machine learning is the study of algorithms that can learn from data. |
|
Natural language processing is a subfield of artificial intelligence concerned with the interactions between computers and human language. |
|
Deep learning is a class of machine learning algorithms that use multiple layers of artificial neural networks to learn from data. |
|
""" |
|
|
|
|
|
tokens = corpus.split() |
|
vocab_size = len(set(tokens)) |
|
token_vectors = tf.one_hot(tf.strings.lookup(tokens, tf.constant([0] * vocab_size)), vocab_size) |
|
|
|
|
|
model = tf.keras.Sequential([ |
|
tf.keras.layers.Embedding(vocab_size, 8), |
|
tf.keras.layers.LSTM(16, return_sequences=True), |
|
tf.keras.layers.LSTM(16), |
|
tf.keras.layers.Dense(vocab_size) |
|
]) |
|
|
|
|
|
model.compile(loss="categorical_crossentropy", optimizer="adam", metrics=["accuracy"]) |
|
|
|
|
|
model.fit(token_vectors[:-1], token_vectors[1:], epochs=20) |
|
|
|
|
|
prompt_vector = tf.one_hot(tf.constant([tokens.index("The")]), vocab_size) |
|
for i in range(10): |
|
prediction = model.predict(tf.expand_dims(prompt_vector, axis=0)) |
|
predicted_index = tf.argmax(prediction, axis=1).numpy()[0] |
|
prompt_vector = tf.concat([prompt_vector, tf.one_hot([predicted_index], vocab_size)], axis=0) |
|
print(tokens[predicted_index], end=" ") |
|
|