Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @tf.function
- def train_step(inp, targ, enc_hidden, s2i=s2i_train):
- loss = 0
- with tf.GradientTape() as tape:
- enc_output, enc_hidden = encoder(inp, enc_hidden)
- dec_hidden = enc_hidden
- dec_input = tf.expand_dims([s2i['BOS']] * BATCH_SIZE, 1)
- # Teacher forcing - feeding the target as the next input
- for t in range(1, targ.shape[1]):
- # passing enc_output to the decoder
- predictions, dec_hidden, _ = decoder(dec_input, dec_hidden, enc_output)
- loss += loss_function(targ[:, t], predictions)
- # using teacher forcing
- dec_input = tf.expand_dims(targ[:, t], 1)
- batch_loss = (loss / int(targ.shape[1]))
- variables = encoder.trainable_variables + decoder.trainable_variables
- gradients = tape.gradient(loss, variables)
- optimizer.apply_gradients(zip(gradients, variables))
- return batch_loss
- EPOCHS = 20
- epoch_loss = []
- for epoch in range(EPOCHS):
- start = time.time()
- enc_hidden = encoder.initialize_hidden_state()
- total_loss = 0
- for (batch, (inp, targ)) in enumerate(dataset.take(N_BATCH)):
- batch_loss = train_step(inp, targ, enc_hidden)
- total_loss += batch_loss
- if batch % 50 == 0:
- print('Epoch {} Batch {} Loss {:.4f}'.format(epoch + 1,
- batch,
- batch_loss.numpy()))
- # saving (checkpoint) the model every 2 epochs
- if (epoch + 1) % 2 == 0:
- checkpoint.save(file_prefix = checkpoint_prefix)
- epoch_loss.append(total_loss / N_BATCH)
- print('Epoch {} Loss {:.4f}'.format(epoch + 1,
- total_loss / N_BATCH))
- print('Time taken for 1 epoch {} sec\n'.format(time.time() - start))
- plt.plot(range(EPOCHS), epoch_loss)
- plt.title('intent slot filling - training loss - seq2seq with attention')
- plt.xlabel('epoch')
- plt.ylabel('loss');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement