1. 购物时间预测
http://www.cnblogs.com/arkenstone/p/5794063.html
https://github.com/CasiaFan/time_seires_prediction_using_lstm
2. 简单实例
#!/usr/bin/env python # coding=utf-8 import numpy as np from keras.models import Sequential from keras.layers import Dense, LSTM from keras.utils import np_utils import string chars = string.uppercase char_to_int = dict((c, i) for i, c in enumerate(chars)) int_to_char = dict((i, c) for i, c in enumerate(chars)) def load_data(look_back = 3): data, label = [], [] for i in range(0, len(chars)-look_back, 1): seq_in = chars[i:i+look_back] seq_out = chars[i+look_back] data.append([char_to_int[c] for c in seq_in]) label.append(char_to_int[seq_out]) return data, label seq_length = 3 data, label = load_data(seq_length) X = np.reshape(data, (len(data), seq_length, 1)) X = X / float(len(chars)) y = np_utils.to_categorical(label) model = Sequential() model.add(LSTM(32, input_shape=(X.shape[1], X.shape[2]))) model.add(Dense(128, activation='relu')) model.add(Dense(y.shape[1], activation='softmax')) model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) model.fit(X, y, nb_epoch=300, batch_size=32, verbose=2) scores = model.evaluate(X, y, verbose=1) print('accuracy: %.2f%%' % (scores[1]*100)) for p in data: x = np.reshape(p, (1, len(p), 1)) x = x /float(len(chars)) y_pred = model.predict(x, verbose=0) index = np.argmax(y_pred) result = int_to_char[index] seq_in = [int_to_char[v] for v in p] print seq_in, '-> ', result print scores
accuracy: 100.00% ['A', 'B', 'C'] -> D ['B', 'C', 'D'] -> E ['C', 'D', 'E'] -> F ['D', 'E', 'F'] -> G ['E', 'F', 'G'] -> H ['F', 'G', 'H'] -> I ['G', 'H', 'I'] -> J ['H', 'I', 'J'] -> K ['I', 'J', 'K'] -> L ['J', 'K', 'L'] -> M ['K', 'L', 'M'] -> N ['L', 'M', 'N'] -> O ['M', 'N', 'O'] -> P ['N', 'O', 'P'] -> Q ['O', 'P', 'Q'] -> R ['P', 'Q', 'R'] -> S ['Q', 'R', 'S'] -> T ['R', 'S', 'T'] -> U ['S', 'T', 'U'] -> V ['T', 'U', 'V'] -> W ['U', 'V', 'W'] -> X ['V', 'W', 'X'] -> Y ['W', 'X', 'Y'] -> Z [0.50304841995239258, 1.0]
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:keras LSTM学习实例 - Python技术站