-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_transformer.py
161 lines (129 loc) · 4.4 KB
/
main_transformer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# data3.txt has 57,008 words and 309,690 characters
# Importing necessary libraries
import tensorflow as tf
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.layers import (
Embedding,
Dense,
Input,
GlobalAveragePooling1D,
MultiHeadAttention,
LayerNormalization,
Dropout,
)
from tensorflow.keras.models import Model
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.optimizers import Adam
import pickle
import numpy as np
import os
# Importing files in Google Colab
from google.colab import files
uploaded = files.upload()
# Opening and reading data from a file
file = open("data3.txt", "r", encoding="utf8")
lines = []
for i in file:
lines.append(i)
# Concatenating lines to form a single string
data = ""
for i in lines:
data = " ".join(lines)
# Cleaning the data
data = (
data.replace("\n", "")
.replace("\r", "")
.replace("\ufeff", "")
.replace("“", "")
.replace("”", "")
.replace("_", "")
)
# Splitting data into words
data = data.split()
data = " ".join(data)
data[:500]
# Tokenizing the data
tokenizer = Tokenizer()
tokenizer.fit_on_texts([data])
# Saving the tokenizer for later use
pickle.dump(tokenizer, open("token.pk1", "wb"))
# Converting the data into sequences
sequence_data = tokenizer.texts_to_sequences([data])[0]
sequence_data[:15]
# Determining the vocabulary size
vocab_size = len(tokenizer.word_index) + 1
print(vocab_size)
# Creating sequences of three words and their next word
sequences = []
for i in range(3, len(sequence_data)):
words = sequence_data[i - 3 : i + 1]
sequences.append(words)
# Converting sequences to a numpy array
sequences = np.array(sequences)
# Splitting sequences into input (X) and output (Y)
X, Y = sequences[:, :-1], sequences[:, -1]
# One-hot encoding the output (Y)
Y = to_categorical(Y, num_classes=vocab_size)
# Transformer block
def transformer_encoder(inputs, head_size, num_heads, ff_dim, dropout=0):
x = MultiHeadAttention(key_dim=head_size, num_heads=num_heads, dropout=dropout)(
inputs, inputs
)
x = Dropout(dropout)(x)
x = LayerNormalization(epsilon=1e-6)(x)
res = x + inputs
x = tf.keras.layers.Conv1D(filters=ff_dim, kernel_size=1, activation="relu")(res)
x = Dropout(dropout)(x)
x = tf.keras.layers.Conv1D(filters=inputs.shape[-1], kernel_size=1)(x)
x = LayerNormalization(epsilon=1e-6)(x)
return x + res
# Building the model with Transformer
def build_model(vocab_size, max_len=3):
inputs = Input(shape=(max_len,))
embedding_layer = Embedding(vocab_size, 64)(inputs)
x = transformer_encoder(embedding_layer, head_size=64, num_heads=2, ff_dim=4)
x = GlobalAveragePooling1D()(x)
x = Dense(1000, activation="relu")(x)
outputs = Dense(vocab_size, activation="softmax")(x)
model = Model(inputs=inputs, outputs=outputs)
return model
model = build_model(vocab_size, max_len=3)
# Setting up ModelCheckpoint for saving the best model
from tensorflow.keras.callbacks import ModelCheckpoint
checkpoint = ModelCheckpoint(
"next_words.h5", monitor="loss", verbose=1, save_best_only=True
)
# Compiling and training the model
model.compile(loss="categorical_crossentropy", optimizer=Adam(learning_rate=0.001))
model.fit(X, Y, epochs=70, batch_size=64, callbacks=[checkpoint])
# Loading the trained model and tokenizer for prediction
model = load_model("next_words.h5")
tokenizer = pickle.load(open("token.pk1", "rb"))
# Function to predict the next word
def Predict_Next_Words(model, tokenizer, text, top_n=5):
sequence = tokenizer.texts_to_sequences([text])
sequence = np.array(sequence)
preds = model.predict(sequence)
top_preds_indices = np.argsort(-preds)[0, :top_n]
predicted_words = []
for i in range(top_n):
predicted_word = ""
for key, value in tokenizer.word_index.items():
if value == top_preds_indices[i]:
predicted_word = key
break
predicted_words.append(predicted_word)
print("Top " + str(top_n) + " possible words: " + ", ".join(predicted_words))
return predicted_words
# Taking user input for text prediction
while True:
text = input("Enter your line: ")
if text == "STOP":
print("Exiting.")
break
else:
try:
Predict_Next_Words(model, tokenizer, text)
except Exception as e:
print("Error:", e)
print("Please try again.")