首页 > 代码库 > keras rnn做加减法

keras rnn做加减法

一、背景

学习rnn怎么使用

例子: 输入两个数,做加法

二、 代码赏析

from __future__ import print_functionfrom keras.models import Sequentialfrom keras.engine.training import slice_Xfrom keras.layers import Activation, TimeDistributed, Dense, RepeatVector, recurrentimport numpy as npfrom six.moves import rangeclass CharacterTable(object):    ‘‘‘    Given a set of characters:    + Encode them to a one hot integer representation    + Decode the one hot integer representation to their character output    + Decode a vector of probabilities to their character output    ‘‘‘    def __init__(self, chars, maxlen):        self.chars = sorted(set(chars))        self.char_indices = dict((c, i) for i, c in enumerate(self.chars))        self.indices_char = dict((i, c) for i, c in enumerate(self.chars))        self.maxlen = maxlen    def encode(self, C, maxlen=None):        maxlen = maxlen if maxlen else self.maxlen        X = np.zeros((maxlen, len(self.chars)))        for i, c in enumerate(C):            X[i, self.char_indices[c]] = 1        return X    def decode(self, X, calc_argmax=True):        if calc_argmax:            X = X.argmax(axis=-1)        return ‘‘.join(self.indices_char[x] for x in X)class colors:    ok = ‘\033[92m‘    fail = ‘\033[91m‘    close = ‘\033[0m‘# Parameters for the model and datasetTRAINING_SIZE = 50000 #输入大小,即训练数据个数DIGITS = 3   #数字位数INVERT = True # Try replacing GRU, or SimpleRNNRNN = recurrent.LSTMHIDDEN_SIZE = 128 #隐含层个数BATCH_SIZE = 128 LAYERS = 1 #几层MAXLEN = DIGITS + 1 + DIGITS #位数chars = ‘0123456789+ ‘ # char 列表ctable = CharacterTable(chars, MAXLEN)questions = []expected = []seen = set()print(‘Generating data...‘)while len(questions) < TRAINING_SIZE:    f = lambda: int(‘‘.join(np.random.choice(list(‘0123456789‘)) for i in range(np.random.randint(1, DIGITS + 1))))    a, b = f(), f()    # Skip any addition questions we‘ve already seen    # Also skip any such that X+Y == Y+X (hence the sorting)    key = tuple(sorted((a, b)))    if key in seen:        continue    seen.add(key)    # Pad the data with spaces such that it is always MAXLEN    q = ‘{}+{}‘.format(a, b)    query = q + ‘ ‘ * (MAXLEN - len(q))    ans = str(a + b)    # Answers can be of maximum size DIGITS + 1    ans += ‘ ‘ * (DIGITS + 1 - len(ans)) #补位,凑够四位    if INVERT:        query = query[::-1]    questions.append(query)    expected.append(ans)print(‘Total addition questions:‘, len(questions))
#向量化print(‘Vectorization...‘)X = np.zeros((len(questions), MAXLEN, len(chars)), dtype=np.bool)y = np.zeros((len(questions), DIGITS + 1, len(chars)), dtype=np.bool)for i, sentence in enumerate(questions): X[i] = ctable.encode(sentence, maxlen=MAXLEN)for i, sentence in enumerate(expected): y[i] = ctable.encode(sentence, maxlen=DIGITS + 1)# Shuffle (X, y) in unison as the later parts of X will almost all be larger digitsindices = np.arange(len(y))np.random.shuffle(indices) #混淆X = X[indices]y = y[indices]# Explicitly set apart 10% for validation data that we never train oversplit_at = len(X) - len(X) / 10(X_train, X_val) = (slice_X(X, 0, split_at), slice_X(X, split_at))(y_train, y_val) = (y[:split_at], y[split_at:])print(X_train.shape)print(y_train.shape)print(‘Build model...‘)model = Sequential()# "Encode" the input sequence using an RNN, producing an output of HIDDEN_SIZE# note: in a situation where your input sequences have a variable length,# use input_shape=(None, nb_feature).model.add(RNN(HIDDEN_SIZE, input_shape=(MAXLEN, len(chars))))# For the decoder‘s input, we repeat the encoded input for each time stepmodel.add(RepeatVector(DIGITS + 1)) #输入 × n 的意思, 貌似就是加强效果# The decoder RNN could be multiple layers stacked or a single layerfor _ in range(LAYERS): model.add(RNN(HIDDEN_SIZE, return_sequences=True))# For each of step of the output sequence, decide which character should be chosenmodel.add(TimeDistributed(Dense(len(chars))))model.add(Activation(‘softmax‘))model.compile(loss=‘categorical_crossentropy‘, optimizer=‘adam‘, metrics=[‘accuracy‘])# Train the model each generation and show predictions against the validation datasetfor iteration in range(1, 200): print() print(‘-‘ * 50) print(‘Iteration‘, iteration) model.fit(X_train, y_train, batch_size=BATCH_SIZE, nb_epoch=1, validation_data=http://www.mamicode.com/(X_val, y_val))>

 

keras rnn做加减法