October 2018
Intermediate to advanced
472 pages
10h 57m
English
Now, let's train the model. We will need some helper functions for the padding of the sentence and to calculate the accuracy of the model:
def pad_sentence_batch(sentence_batch, pad_int): padded_seqs = [] seq_lens = [] max_sentence_len = 50 for sentence in sentence_batch: padded_seqs.append(sentence + [pad_int] * (max_sentence_len - len(sentence))) seq_lens.append(50) return padded_seqs, seq_lensdef check_accuracy(logits, Y): acc = 0 for i in range(logits.shape[0]): internal_acc = 0 for k in range(len(Y[i])): if Y[i][k] == logits[i][k]: internal_acc += 1 acc += (internal_acc / len(Y[i])) return acc / logits.shape[0]
We initialize our model and iterate the session for the defined number of epochs:
tf.reset_default_graph() ...
Read now
Unlock full access