-
Notifications
You must be signed in to change notification settings - Fork 0
/
translator.py
45 lines (31 loc) · 1.4 KB
/
translator.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
import tensorflow as tf
from encoder import Encoder
from decoder import Decoder
VOCAB_SIZE = 12000
UNITS = 256
class Translator(tf.keras.Model):
def __init__(self, vocab_size, units):
"""Initializes an instance of this class
Args:
vocab_size (int): Size of the vocabulary
units (int): Number of units in the LSTM layer
"""
super().__init__()
# Define the encoder with the appropriate vocab_size and number of units
self.encoder = Encoder(vocab_size, units)
# Define the decoder with the appropriate vocab_size and number of units
self.decoder = Decoder(vocab_size, units)
def call(self, inputs):
"""Forward pass of this layer
Args:
inputs (tuple(tf.Tensor, tf.Tensor)): Tuple containing the context (sentence to translate) and the target (shifted-to-the-right translation)
Returns:
tf.Tensor: The log_softmax probabilities of predicting a particular token
"""
# In this case inputs is a tuple consisting of the context and the target, unpack it into single variables
context, target = inputs
# Pass the context through the encoder
encoded_context = self.encoder(context)
# Compute the logits by passing the encoded context and the target to the decoder
logits = self.decoder(encoded_context, target)
return logits