【发布时间】:2018-03-05 09:49:04
【问题描述】:
我正在尝试使用一个玩具示例来学习使用 keras 的循环网络。数据集定义如下:
pos = [1,2,3,4] # positive observation.
neg = [1,2,3,0] # negative observation.
half_len = 500
data = np.reshape(np.concatenate((np.tile(pos, half_len), np.tile(neg, half_len)), axis=0), (2 * half_len, -1))
labels = np.asarray([1] * half_len + [0] * half_len)
这是我的模型:
model = Sequential()
model.add(Embedding(max_features, output_dim=16))
model.add(LSTM(32, dropout=0.2, recurrent_dropout=0.2))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy'])
正如预期的那样,这很容易达到精度 1。但是嵌入层不适合我的实际用例。当我将玩具示例重新格式化为以下内容时:
pos = [[1, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 1, 0]]
neg = [[1, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 0, 1]]
half_len = 500
time_steps = 4
feature_length = 5
data = np.reshape(np.concatenate((np.tile(pos, half_len), np.tile(neg, half_len)), axis=0), (2 * half_len, time_steps, feature_length))
labels = np.asarray([1] * half_len + [0] * half_len)
而模型:
model = Sequential()
model.add(LSTM(32, dropout=0.2, recurrent_dropout=0.2, input_shape=(time_steps, feature_length)))
model.add(Dense(1, activation='sigmoid'))
model.compile(optimizer='rmsprop', loss='binary_crossentropy', metrics=['accuracy'])
准确度在 0.6 左右变化(我让它训练的时间是第一个版本的 5 倍)。上面的2怎么不等价?使模型适应第二个输入的好方法是什么?
谢谢。
【问题讨论】: