Summary: Recurrent Neural Networks, RNN, LSTM, Long Short-term Memory, seq2seq
DIGITS = 3
MAXLEN = DIGITS + DIGITS + 1
n_training_examples = 1000
print('Generating data...', end=' ')
pairs,ans = generate_data(n_training_examples)
print('done!')
print('Size of Training set: ' , len(pairs))
alphabet = list('0123456789+ ')
x,y = encode(pairs, ans, alphabet)
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.1)
print('x_train shape = ' , x_train.shape)
print('y_train shape = ', y_train.shape)
print('Build model...')
model = Sequential()
model.add(LSTM(128, input_shape=(MAXLEN, len(alphabet))))
model.add(RepeatVector(DIGITS + 1))
model.add(LSTM(128, return_sequences=True))
model.add(TimeDistributed(Dense(len(alphabet), activation='softmax')))
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.summary()
EPOCHS = 2
BATCH_SIZE = 32
class colors:
ok = '\033[92m'
fail = '\033[91m'
close = '\033[0m'
for epoch in range(1, EPOCHS + 1):
print('Iteration ', epoch)
model.fit(x_train, y_train, batch_size=BATCH_SIZE, epochs=1, validation_data=(x_test, y_test), verbose=1)
# Select 10 samples from test set and visualize errors
for i in range(10):
index = np.random.randint(0, len(x_test))
q = x_test[np.array([index])]
ans = y_test[np.array([index])]
preds = np.argmax(model.predict(q),axis=-1)
question = decode(q[0],alphabet)
actual = decode(ans[0],alphabet)
guessed = decode(preds[0], alphabet, calc_argmax=False)
print('Q:', question, end=' ')
print(' Actual:', actual, end=' ')
if actual == guessed:
print(colors.ok + ' ☑' + colors.close, end=' ')
else:
print(colors.fail + ' ☒' + colors.close, end=' ')
print('Guessed:', guessed)