在keras 1.2.2中,我创建了一个具有以下维度的数据集:
这里,2000是实例数(批量数据),100是每批中的样本数,32是图像行和列,3是通道数(RGB)。
我写过这段代码,它在CNN之后应用LSTM。我使用了TimeDistributed图层,并将LSTM的输出平均值设置为:
我希望LSTM能够处理每个批次,然后我获取该批次的LSTM输出的平均值。所以,我的总输出(我的标签)是一个(2000,1)向量。
我收到此错误:
这是我的代码:
# -*- coding: utf-8 -*-
# -*- coding: utf-8 -*-
import keras
from keras.layers import Input ,Dense, Dropout, Activation, LSTM
from keras.layers import Lambda, Convolution2D, MaxPooling2D, Flatten, Reshape
from keras.models import Sequential
from keras.layers.wrappers import TimeDistributed
from keras.layers.pooling import GlobalAveragePooling1D
from keras.optimizers import SGD
from keras.utils import np_utils
from keras.models import Model
import keras.backend as K
import numpy as np
timesteps=100;
number_of_samples=2500;
nb_samples=number_of_samples;
frame_row=32;
frame_col=32;
channels=3;
nb_epoch=1;
batch_size=timesteps;
data= np.random.random((2500,timesteps,frame_row,frame_col,channels))
label=np.random.random((2500,timesteps,1))
X_train=data[0:2000,:]
y_train=label[0:2000]
X_test=data[2000:,:]
y_test=label[2000:,:]
#%%
model=Sequential();
model.add(TimeDistributed(Convolution2D(32, 3, 3, border_mode='same'), input_shape=X_train.shape[1:]))
model.add(TimeDistributed(Activation('relu')))
model.add(TimeDistributed(Convolution2D(32, 3, 3)))
model.add(TimeDistributed(Activation('relu')))
model.add(TimeDistributed(MaxPooling2D(pool_size=(2, 2))))
model.add(TimeDistributed(Dropout(0.25)))
model.add(TimeDistributed(Flatten()))
model.add(TimeDistributed(Dense(512)))
#output dimension here is (None, 100, 512)
model.add(TimeDistributed(Dense(35, name="first_dense" )))
#output dimension here is (None, 100, 35)
model.add(LSTM(output_dim=20, return_sequences=True))
#output dimension here is (None, 100, 20)
time_distributed_merge_layer = Lambda(function=lambda x: K.mean(x, axis=1, keepdims=False))
model.add(time_distributed_merge_layer)
#output dimension here is (None, 1, 20)
#model.add(Flatten())
model.add(Dense(1, activation='sigmoid', input_shape=(None,20)))
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model.fit(X_train, y_train,
batch_size=batch_size,
nb_epoch=nb_epoch,
validation_data=(X_test, y_test))
答案 0 :(得分:3)
如果我们按照模型中的形状跟踪,我们会在shape = (None, 100, 35)
后到达Dense()
。然后你将它提供给LSTM()
,它将返回长度为20的整个隐藏向量序列,这样你就得到shape = (None, 100, 20)
。然后你在轴1上取平均值,得到shape = (None, 100, 1)
。
该网络的体系结构存在问题,因为您的目标具有shape = (None, 1)
。所以要么改变
LSTM(output_dim=20, return_sequences=False)
并在合并后添加Flatten()
图层。合并后Flatten()
或Dense(1, activation='sigmoid')
使用onChange
进行预测?
这取决于你,但现在它无法运作。