print('Target output sequences') print(data_fr_out[:2]) dataset = tf.data.Dataset.from_tensor_slices( (data_en, data_fr_in, data_fr_out)) dataset = dataset.shuffle(len(raw_data_en)).batch(BATCH_SIZE, drop_remainder=True) en_vocab_size = len(en_tokenizer.word_index) + 1 encoder = Encoder(en_vocab_size, EMBEDDING_SIZE, RNN_SIZE) fr_vocab_size = len(fr_tokenizer.word_index) + 1 decoder = Decoder(fr_vocab_size, EMBEDDING_SIZE, RNN_SIZE, ATTENTION_FUNC) initial_state = encoder.init_states(1) encoder_outputs = encoder(tf.constant([[1]]), initial_state) decoder_outputs = decoder(tf.constant([[1]]), encoder_outputs[1:], encoder_outputs[0]) def predict(test_source_text=None): if test_source_text is None: test_source_text = raw_data_en[np.random.choice(len(raw_data_en))] test_source_seq = en_tokenizer.texts_to_sequences([test_source_text]) if len(test_source_seq[0]) == 0: dists.append(test_source_text) return 0, test_source_text, test_source_text en_initial_states = encoder.init_states(1)
dataset = tf.data.Dataset.from_tensor_slices( (data_en, data_fr_in, data_fr_out)) dataset = dataset.shuffle(len(raw_data_en)).batch(BATCH_SIZE, drop_remainder=True) en_vocab_size = len(en_tokenizer.word_index) + 1 encoder = Encoder(en_vocab_size, EMBEDDING_SIZE, RNN_SIZE) fr_vocab_size = len(fr_tokenizer.word_index) + 1 decoder = Decoder(fr_vocab_size, EMBEDDING_SIZE, RNN_SIZE, ATTENTION_FUNC) # These lines can be used for debugging purpose # Or can be seen as a way to build the models initial_state = encoder.init_states(1) encoder_outputs = encoder(tf.constant([[1]]), initial_state) decoder_outputs = decoder(tf.constant([[1]]), encoder_outputs[1:], encoder_outputs[0]) optimizer = tf.keras.optimizers.Adam(clipnorm=5.0) @tf.function def train_step(source_seq, target_seq_in, target_seq_out, en_initial_states): loss = 0 with tf.GradientTape() as tape: en_outputs = encoder(source_seq, en_initial_states) en_states = en_outputs[1:] de_state_h, de_state_c = en_states