In this recipe, we will learn how to use LSTM to predict a value that is of the same or a slightly different length, such as subtraction of two numbers.
Create a requirements.txt
with Keras and six.moves
dependencies. Import the relevant classes from keras
, numpy
, and six.moves
as follows:
from __future__ import print_function from keras.models import Sequential from keras import layers import numpy as np import six.moves
In the next section, we will learn how to implement an LSTM network that can handle any three-digit subtraction.
- Create a character table that can handle encoding and decoding. This class has three methods, as follows:
__init__()
encode()
decode()
- The code is as follows:
class CharTable(object): def __init__(self, char): self.char = sorted(set(char)) self.char_indices = dict((ch, i) for i, ch in enumerate(self.char)) self.indices_char = dict((i, ch) for i, ch...