http://blog.csdn.net/zjm750617105/article/details/51321889
本文是初学keras这两天来,自己仿照addition_rnn.py,写的一个实例,数据处理稍微有些不同,但是准确性相比addition_rnn.py 差一点,下面直接贴代码,
解释和注释都在代码里边。
- <span style="font-family: Arial, Helvetica, sans-serif;">
- from keras.models import Sequential
- from keras.layers.recurrent import LSTM
- from utils import log
- from numpy import random
- import numpy as np
- from keras.layers.core import RepeatVector, TimeDistributedDense, Activation
-
- ‘‘
-
- log = log()
- training_size = 50000
- hidden_size = 128
- batch_size = 128
- layers = 1
-
- maxlen = 7
- single_digit = 3
-
-
- def generate_data():
- log.info("generate the questions and answers")
- questions = []
- expected = []
- seen = set()
- while len(seen) < training_size:
- num1 = random.randint(1, 999)
- num2 = random.randint(1, 999)
-
- key = tuple(sorted((num1,num2)))
- if key in seen:
- continue
- seen.add(key)
- q = ‘{}+{}‘.format(num1,num2)
- query = q + ‘ ‘ * (maxlen - len(q))
- ans = str(num1 + num2)
- ans = ans + ‘ ‘ * (single_digit + 1 - len(ans))
- questions.append(query)
- expected.append(ans)
- return questions, expected
-
- class CharacterTable():
- ‘‘
- def __init__(self, chars, maxlen):
- self.chars = sorted(set(chars))
- ‘‘
- self.char_index = dict((c, i) for i, c in enumerate(self.chars))
- self.index_char = dict((i, c) for i, c in enumerate(self.chars))
- self.maxlen = maxlen
-
- def encode(self, C, maxlen):
- X = np.zeros((maxlen, len(self.chars)))
- for i, c in enumerate(C):
- X[i, self.char_index[c]] = 1
- return X
-
- def decode(self, X, calc_argmax=True):
- if calc_argmax:
- X = X.argmax(axis=-1)
- return ‘‘.join(self.index_char[x] for x in X)
-
- chars = ‘0123456789 +‘
- character_table = CharacterTable(chars,len(chars))
-
- questions , expected = generate_data()
-
- log.info(‘Vectorization...‘)
- inputs = np.zeros((len(questions), maxlen, len(chars)))
- labels = np.zeros((len(expected), single_digit+1, len(chars)))
-
- log.info("encoding the questions and get inputs")
- for i, sentence in enumerate(questions):
- inputs[i] = character_table.encode(sentence, maxlen=len(sentence))
- log.info("encoding the expected and get labels")
- for i, sentence in enumerate(expected):
- labels[i] = character_table.encode(sentence, maxlen=len(sentence))
-
- log.info("total inputs is %s"%str(inputs.shape))
- log.info("total labels is %s"%str(labels.shape))
-
- log.info("build model")
- model = Sequential()
- ‘‘
- model.add(LSTM(hidden_size, input_shape=(maxlen, len(chars))))
- ‘‘
- model.add(RepeatVector(single_digit + 1))
- for _ in range(layers):
- model.add(LSTM(hidden_size, return_sequences=True))
- ‘‘
- model.add(TimeDistributedDense(len(chars)))
- model.add(Activation(‘softmax‘))
- ‘‘
- model.compile(loss=‘categorical_crossentropy‘,
- optimizer=‘adam‘,
- metrics=[‘accuracy‘])
-
- for iteration in range(1, 3):
- print()
- print(‘-‘ * 50)
- print(‘Iteration‘, iteration)
- model.fit(inputs, labels, batch_size=batch_size, nb_epoch=2,
- validation_split = 0.1)
-
- model.get_config()
详细解读简单的lstm的实例
原文:http://www.cnblogs.com/DjangoBlog/p/6756362.html