-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjax_transformer_translation.py
193 lines (146 loc) · 8.63 KB
/
jax_transformer_translation.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# jax_transformer_translation.py
import jax
import jax.numpy as jnp
import numpy as np
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.preprocessing.sequence import pad_sequences
# Define the transformer model
def jax_transformer(inputs, targets, num_layers, d_model, num_heads, dff, input_vocab_size, target_vocab_size, dropout_rate):
def positional_encoding(pos, d_model):
angles = jnp.arange(d_model)[:, np.newaxis] / jnp.power(10000, (2 * (jnp.arange(d_model) // 2)) / d_model)
angles = pos * angles
angles[:, 0::2] = jnp.sin(angles[:, 0::2])
angles[:, 1::2] = jnp.cos(angles[:, 1::2])
return angles
def multi_head_attention(q, k, v, mask):
attention_scores = jnp.matmul(q, jnp.transpose(k, (0, 2, 1))) / jnp.sqrt(d_model)
if mask is not None:
attention_scores += mask * -1e9
attention_weights = jax.nn.softmax(attention_scores)
output = jnp.matmul(attention_weights, v)
return output
def encoder_layer(inputs, mask):
attention_output = multi_head_attention(inputs, inputs, inputs, mask)
attention_output = jax.nn.dropout(attention_output, rate=dropout_rate)
attention_output = jax.nn.layer_norm(inputs + attention_output)
ffn_output = jax.nn.dense(attention_output, dff, activation=jax.nn.relu)
ffn_output = jax.nn.dense(ffn_output, d_model)
ffn_output = jax.nn.dropout(ffn_output, rate=dropout_rate)
ffn_output = jax.nn.layer_norm(attention_output + ffn_output)
return ffn_output
def decoder_layer(inputs, enc_outputs, mask):
attention_output = multi_head_attention(inputs, inputs, inputs, mask)
attention_output = jax.nn.dropout(attention_output, rate=dropout_rate)
attention_output = jax.nn.layer_norm(inputs + attention_output)
enc_attention_output = multi_head_attention(attention_output, enc_outputs, enc_outputs, None)
enc_attention_output = jax.nn.dropout(enc_attention_output, rate=dropout_rate)
enc_attention_output = jax.nn.layer_norm(attention_output + enc_attention_output)
ffn_output = jax.nn.dense(enc_attention_output, dff, activation=jax.nn.relu)
ffn_output = jax.nn.dense(ffn_output, d_model)
ffn_output = jax.nn.dropout(ffn_output, rate=dropout_rate)
ffn_output = jax.nn.layer_norm(enc_attention_output + ffn_output)
return ffn_output
def encoder(inputs, mask):
inputs = jax.nn.embedding(inputs, d_model, input_vocab_size)
inputs *= jnp.sqrt(d_model)
inputs += positional_encoding(jnp.arange(inputs.shape[1]), d_model)
inputs = jax.nn.dropout(inputs, rate=dropout_rate)
for _ in range(num_layers):
inputs = encoder_layer(inputs, mask)
return inputs
def decoder(inputs, enc_outputs, mask):
inputs = jax.nn.embedding(inputs, d_model, target_vocab_size)
inputs *= jnp.sqrt(d_model)
inputs += positional_encoding(jnp.arange(inputs.shape[1]), d_model)
inputs = jax.nn.dropout(inputs, rate=dropout_rate)
for _ in range(num_layers):
inputs = decoder_layer(inputs, enc_outputs, mask)
outputs = jax.nn.dense(inputs, target_vocab_size)
return outputs
enc_inputs = inputs
dec_inputs = targets[:, :-1]
dec_outputs_real = targets[:, 1:]
enc_outputs = encoder(enc_inputs, None)
dec_outputs = decoder(dec_inputs, enc_outputs, None)
return dec_outputs, dec_outputs_real
# Define the loss function
def jax_loss_fn(params, inputs, targets, num_layers, d_model, num_heads, dff, input_vocab_size, target_vocab_size, dropout_rate):
dec_outputs, dec_outputs_real = jax_transformer(inputs, targets, num_layers, d_model, num_heads, dff, input_vocab_size, target_vocab_size, dropout_rate)
loss = jnp.mean(jax.nn.sparse_softmax_cross_entropy_with_logits(logits=dec_outputs, labels=dec_outputs_real))
return loss
# Tokenize and preprocess the data
def preprocess_data(english_sentences, french_sentences, max_length):
english_tokenizer = Tokenizer(filters='')
english_tokenizer.fit_on_texts(english_sentences)
english_sequences = english_tokenizer.texts_to_sequences(english_sentences)
english_sequences = pad_sequences(english_sequences, maxlen=max_length, padding='post')
french_tokenizer = Tokenizer(filters='')
french_tokenizer.fit_on_texts(french_sentences)
french_sequences = french_tokenizer.texts_to_sequences(french_sentences)
french_sequences = pad_sequences(french_sequences, maxlen=max_length+1, padding='post')
return english_sequences, french_sequences, english_tokenizer, french_tokenizer
# Train the transformer model
def jax_train(params, optimizer, train_data, num_epochs, batch_size, num_layers, d_model, num_heads, dff, dropout_rate):
for epoch in range(num_epochs):
epoch_loss = 0
for i in range(0, len(train_data), batch_size):
batch_data = train_data[i:i+batch_size]
english_sequences = np.array([data[0] for data in batch_data])
french_sequences = np.array([data[1] for data in batch_data])
loss_value, grads = jax.value_and_grad(jax_loss_fn)(params, english_sequences, french_sequences, num_layers, d_model, num_heads, dff, len(english_tokenizer.word_index)+1, len(french_tokenizer.word_index)+1, dropout_rate)
params = optimizer.update(grads, params)
epoch_loss += loss_value
epoch_loss /= (len(train_data) // batch_size)
print(f"Epoch {epoch+1}, Loss: {epoch_loss}")
return params
# Translate a sentence using the trained model
def jax_translate(params, sentence, english_tokenizer, french_tokenizer, max_length, num_layers, d_model, num_heads, dff, dropout_rate):
sentence = sentence.lower().strip()
sentence = english_tokenizer.texts_to_sequences([sentence])
sentence = pad_sequences(sentence, maxlen=max_length, padding='post')
enc_inputs = sentence
dec_inputs = np.zeros((1, max_length+1))
dec_inputs[0, 0] = french_tokenizer.word_index['<start>']
for i in range(1, max_length+1):
dec_outputs, _ = jax_transformer(enc_inputs, dec_inputs, num_layers, d_model, num_heads, dff, len(english_tokenizer.word_index)+1, len(french_tokenizer.word_index)+1, dropout_rate)
dec_outputs = dec_outputs[:, i-1, :]
pred_idx = jnp.argmax(dec_outputs)
if pred_idx == french_tokenizer.word_index['<end>']:
break
dec_inputs[0, i] = pred_idx
decoded_sentence = [french_tokenizer.index_word[idx] for idx in dec_inputs[0] if idx > 0]
return ' '.join(decoded_sentence)
# Example usage
english_sentences = [
'I love to learn machine learning.',
'JAX is a great library for numerical computing.',
'Transformers have revolutionized natural language processing.'
]
french_sentences = [
"J'adore apprendre le machine learning.",
"JAX est une excellente bibliothèque pour le calcul numérique.",
"Les transformateurs ont révolutionné le traitement du langage naturel."
]
max_length = 20
english_sequences, french_sequences, english_tokenizer, french_tokenizer = preprocess_data(english_sentences, french_sentences, max_length)
train_data = list(zip(english_sequences, french_sequences))
# Initialize transformer parameters
rng = jax.random.PRNGKey(0)
params = jax.random.normal(rng, (max_length, d_model))
# Initialize optimizer
optimizer = jax.optim.Adam(learning_rate=0.001)
# Train the transformer model
params = jax_train(params, optimizer, train_data, num_epochs=10, batch_size=2, num_layers=2, d_model=128, num_heads=8, dff=512, dropout_rate=0.1)
# Translate a sentence
sentence = "I enjoy coding with JAX."
translated_sentence = jax_translate(params, sentence, english_tokenizer, french_tokenizer, max_length, num_layers=2, d_model=128, num_heads=8, dff=512, dropout_rate=0.1)
print("Translated sentence:", translated_sentence)
# Possible Errors and Solutions:
# ValueError: operands could not be broadcast together with shapes (x, y) (a, b)
# Solution: Ensure that the shapes of the predictions and targets match exactly when calculating the loss.
# ImportError: No module named 'tensorflow.keras.preprocessing.text'
# Solution: Ensure TensorFlow is installed using `pip install tensorflow`.
# IndexError: list index out of range
# Solution: Verify that the indices used in operations are within the valid range of the data structures being accessed.
# RuntimeError: Invalid argument: Non-scalable parameters
# Solution: Ensure all operations in the model are scalable and support JAX's JIT compilation.