Keras LSTM ValueError:输入0与层LSTM_24不兼容:预期ndim=3,发现ndim=4
我正在尝试测试一个我想用于视频问题的架构。我正在模拟1个视频,24帧,240x320x3。我没有得到正确的尺寸,但我看不到它Keras LSTM ValueError:输入0与层LSTM_24不兼容:预期ndim=3,发现ndim=4,keras,lstm,Keras,Lstm,我正在尝试测试一个我想用于视频问题的架构。我正在模拟1个视频,24帧,240x320x3。我没有得到正确的尺寸,但我看不到它 vid = np.ndarray((24,240,320,3),np.uint8) vid = np.ndarray((24,240,320,3),np.uint8) vid = np.random.randint(0, 255, (24,240,320,3),np.uint8) nrows = vid.shape[1] ncols = vid.shape[2] ba
vid = np.ndarray((24,240,320,3),np.uint8)
vid = np.ndarray((24,240,320,3),np.uint8)
vid = np.random.randint(0, 255, (24,240,320,3),np.uint8)
nrows = vid.shape[1]
ncols = vid.shape[2]
batch_size = 32
num_classes = 1
epochs = 60
nframes = len(vid)
nchan = 3
model = Sequential()
model.image_dim_ordering = 'tf'
model.image_data_format = 'tf'
model = Sequential([
TimeDistributed(Dense(nrows*ncols, activation='softmax'),
input_shape=(nrows,ncols,nchan)),
LSTM((nrows,ncols), recurrent_dropout=0.2, dropout=0.2,
return_sequences=True),
Dense(1),
Activation('sigmoid')
])
获取错误信息:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-31-a0219d8eece3> in <module>()
41 LSTM((nrows,ncols), recurrent_dropout=0.2, dropout=0.2, return_sequences=True),
42 Dense(1),
---> 43 Activation('sigmoid')
44 ])
45
C:\Users\david\Anaconda3\envs\tensorflow\lib\site-packages\keras\models.py in __init__(self, layers, name)
396 if layers:
397 for layer in layers:
--> 398 self.add(layer)
399
400 def add(self, layer):
C:\Users\david\Anaconda3\envs\tensorflow\lib\site-packages\keras\models.py in add(self, layer)
464 output_shapes=[self.outputs[0]._keras_shape])
465 else:
--> 466 output_tensor = layer(self.outputs[0])
467 if isinstance(output_tensor, list):
468 raise TypeError('All layers in a Sequential model '
C:\Users\david\Anaconda3\envs\tensorflow\lib\site-packages\keras\layers\recurrent.py in __call__(self, inputs, initial_state, **kwargs)
241 # modify the input spec to include the state.
242 if initial_state is None:
--> 243 return super(Recurrent, self).__call__(inputs, **kwargs)
244
245 if not isinstance(initial_state, (list, tuple)):
C:\Users\david\Anaconda3\envs\tensorflow\lib\site-packages\keras\engine\topology.py in __call__(self, inputs, **kwargs)
539 # Raise exceptions in case the input is not compatible
540 # with the input_spec specified in the layer constructor.
--> 541 self.assert_input_compatibility(inputs)
542
543 # Collect input shapes to build layer.
C:\Users\david\Anaconda3\envs\tensorflow\lib\site-packages\keras\engine\topology.py in assert_input_compatibility(self, inputs)
438 self.name + ': expected ndim=' +
439 str(spec.ndim) + ', found ndim=' +
--> 440 str(K.ndim(x)))
441 if spec.max_ndim is not None:
442 ndim = K.ndim(x)
ValueError: Input 0 is incompatible with layer lstm_24: expected ndim=3, found ndim=4
---------------------------------------------------------------------------
ValueError回溯(最近一次调用上次)
在()
41 LSTM((nrows,ncols),经常性辍学=0.2,辍学=0.2,返回序列=真),
42(1),
--->43激活(‘乙状结肠’)
44 ])
45
C:\Users\david\Anaconda3\envs\tensorflow\lib\site packages\keras\models.py in\uuuuu init\uuuu(self,layers,name)
396如果层:
397对于分层:
-->398自我添加(图层)
399
400 def添加(自身,层):
C:\Users\david\Anaconda3\envs\tensorflow\lib\site packages\keras\models.py in add(self,layer)
464输出形状=[self.outputs[0]。\u keras\u形状])
465其他:
-->466输出张量=层(自输出[0])
467如果存在(输出张量,列表):
468 raise TypeError('序列模型中的所有层'
C:\Users\david\Anaconda3\envs\tensorflow\lib\site packages\keras\layers\recurrent.py in\uuuuuuuu call\uuuuuuuu(self、输入、初始状态,**kwargs)
241#修改输入规范以包含状态。
242如果初始_状态为无:
-->243返回超级(循环、自).\u调用(输入,**kwargs)
244
245如果不存在(初始状态,(列表,元组)):
C:\Users\david\Anaconda3\envs\tensorflow\lib\site packages\keras\engine\topology.py in\uuuuu调用(self,inputs,**kwargs)
539#在输入不兼容的情况下引发异常
540#具有图层构造函数中指定的输入规格。
-->541自断言输入兼容性(输入)
542
543#收集输入形状以构建图层。
C:\Users\david\Anaconda3\envs\tensorflow\lib\site packages\keras\engine\topology.py在assert\u input\u兼容性(self,inputs)中
438 self.name+':应为ndim=+
439 str(spec.ndim)+',发现ndim='+
-->440 str(K.ndim(x)))
441如果spec.max\u ndim不是无:
442 ndim=K.ndim(x)
ValueError:输入0与层lstm_24不兼容:预期ndim=3,发现ndim=4
我很感谢您的帮助这意味着第一层将输出一个4维张量,而LSTM层则希望输入一个三维张量 要解决这个问题,您可以简单地更改“稠密”的输入形状。我假设类似于
input\u shape=(Timesteps,DataLength)
,其中Timesteps*DataLength=nrows*ncols*nchan
就可以了
在一切之前添加重塑((Timesteps,DataLength))
层可能会让您受益匪浅,这样您就可以为下一层正确重塑数据
现在,由你来决定你认为是时间步长的(NROWS,NCOLS,NCHA)< P>这是因为第一层和第二层的形状误差。您需要将return_sequences=True添加到第一层 像
model.add(LSTM(32,input_shape=(1,1),return_sequences=True))
看起来LSTM不支持3D输入,我必须重新塑造或尝试其他方法。