-
6 种用 LSTM 做时间序列预测的模型结构 - Keras 实现
- keras中TimeDistributed和RepeatVector的解释
-
tf.nn.dynamic_rnn的输出outputs和state含义
-
numpy之ones,array,asarray
batch_size
用minibatch方法时会定义batch_size,即把数据分几份后,每份的大小是多少。iteration
迭代,每个循环的一遍,一次参数更新。用minibatch时就意味着train完一个batchepoch
one forward pass and one backward pass of all the training examples, in the neural network terminology.即所有训练数据过一遍,如果数据有300条,你把数据分成了3个batch,batch_size是300 / 3 = 100,3个batch都跑完,即跑了三个iteration,就是一个epoch。episode
one a sequence of states, actions and rewards, which ends with terminal state. 如果玩围棋,从头下到尾的一局棋就是一个episode。
-
keras搭建神经网络
- keras中获取层输出shape的方法汇总
- 在model.compile后:
print(model.summary())
例子①
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import TimeDistributed
from keras.layers import LSTM
# prepare sequence
length = 5
seq = array([i/float(length) for i in range(length)])
# 输入为1个样本,5个步长,1个特征值
X = seq.reshape(1, 5, 1)
# 输出为1个样本,5个步长,1个特征值
y = seq.reshape(1, 5, 1)
# define LSTM configuration
n_batch = 1
# create LSTM
model = Sequential()
# 输入类型为5个步长和1个特征值,return_sequences=True返回整个序列
model.add(LSTM(5, input_shape=(5, 1), return_sequences=True))
model.add(TimeDistributed(Dense(1)))
model.compile(loss='mean_squared_error', optimizer='adam')
print(model.summary())
# train LSTM
model.fit(X, y, epochs=10, batch_size=n_batch, verbose=2)
# evaluate
result = model.predict(X, batch_size=n_batch, verbose=0)
for layer in model.layers:
print(layer.output)
for value in result[0,:,0]:
print('%.1f' % value)
实践证明,使用model.add(TimeDistributed(Dense(1)))
误差<直接用model.add((Dense(1)))
虽然两者使用参数数目相同,返回数据类型也都相同
例子②
# 简易多层感知神经网络示例
from keras.models import Sequential
from keras.layers import Dense
import numpy as np
import numpy
# 加载,预处理数据集
dataset = numpy.loadtxt("pima-indians-diabetes.csv", delimiter=",")
X = dataset[:100,0:8]
Y = dataset[:100,8]
# 1. 定义模型
model = Sequential()
model.add(Dense(12, input_dim=8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
# 2. 编译模型
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
# 3. 训练模型
history = model.fit(X, Y, nb_epoch=10, batch_size=10)
# 4. 评估模型
loss, accuracy = model.evaluate(X, Y)
print("\nLoss: %.2f, Accuracy: %.2f%%" % (loss, accuracy*100))
# 5. 数据预测
probabilities = model.predict(X)
for layer in model.layers: # 获取每一层的输出shape
print(layer.output)
predictions = [float(np.round(x)) for x in probabilities]
accuracy = numpy.mean(predictions == Y)
print("Prediction Accuracy: %.2f%%" % (accuracy*100))