我使用Recurrent NN生成分类模型。以下是使用keras
的简单RNN模型(由blog提供):
tr_C, ts_C, tr_r, ts_r = train_test_split(C, r, train_size=.8)
tr_C = tr_C.reshape((tr_C.shape[0], tr_C.shape[1], 1))
ts_C = ts_C.reshape((ts_C.shape[0], ts_C.shape[1], 1))
tr_r = tr_r.reshape(tr_r.shape[0], 1)
ts_r = ts_r.reshape(ts_r.shape[0], 1)
print('Build model...')
model = Sequential()
model.add(LSTM(16,
batch_input_shape=(1, 1, 1),
activation='tanh',
recurrent_activation='tanh',
kernel_initializer='he_uniform',
bias_initializer='he_uniform',
# dropout=0.1,
# recurrent_dropout=0.1,
stateful=True,
return_sequences=False,
)
)
model.add(Dense(1, activation='tanh'))
model.compile(loss='mean_absolute_error', optimizer='adam', metrics=['accuracy'])
for epoch in range(10):
print ('epoch(%s): ' % epoch)
mean_tr_acc = []
mean_tr_loss = []
for i in range(len(tr_C)):
for j in range(max_len):
tr_loss, tr_acc = model.train_on_batch(np.expand_dims(np.expand_dims(tr_C[i][j], axis=1), axis=1), tr_r[i])
mean_tr_acc.append(tr_acc)
mean_tr_loss.append(tr_loss)
model.reset_states()
print('training_acc = {}'.format(np.mean(mean_tr_acc)))
print('loss training = {}'.format(np.mean(mean_tr_loss)))
print('___________________________________')
mean_te_acc = []
mean_te_loss = []
for i in range(len(ts_C)):
for j in range(max_len):
te_loss, te_acc = model.test_on_batch(np.expand_dims(np.expand_dims(ts_C[i][j], axis=1), axis=1), ts_r[i])
mean_te_acc.append(te_acc)
mean_te_loss.append(te_loss)
model.reset_states()
for j in range(max_len):
y_pred = model.predict_on_batch(np.expand_dims(np.expand_dims(ts_C[i][j], axis=1), axis=1))
model.reset_states()
print('testing_acc = {}'.format(np.mean(mean_te_acc)))
print('loss testing = {}'.format(np.mean(mean_te_loss)))
print('___________________________________')
这是模型的摘要:
Layer (type) Output Shape Param #
=================================================================
lstm_1 (LSTM) (1, 16) 1152
_________________________________________________________________
dense_1 (Dense) (1, 1) 17
=================================================================
Total params: 1,169
Trainable params: 1,169
Non-trainable params: 0
_________________________________________________________________
我可以修改此代码以训练一批样本,而不是一次训练一个样本吗?如果是这样,怎么样?任何建议表示赞赏 谢谢。
答案 0 :(得分:1)
你在这里声明:
model.add(LSTM(16,
batch_input_shape=(1, 1, 1),
activation='tanh',
recurrent_activation='tanh',
kernel_initializer='he_uniform',
bias_initializer='he_uniform',
# dropout=0.1,
# recurrent_dropout=0.1,
stateful=True,
return_sequences=False,
)
)
批量大小应为:(1,1,1)仅表示1个示例,1个时间戳,每个时间戳为标量。
如果您想要超过1个培训示例,您应该这样做:
batch_input_shape=(None,1,1)
或,将None更改为批次中的示例数 (无意味着您可以根据需要插入多少个示例)