Keras ValueError:尺寸必须相等

时间:2017-02-27 23:52:33

标签: python machine-learning deep-learning keras

该模型的目标是通过与它们联系起来的单词对视频输入进行分类分类。每个输入具有45帧,1个灰色通道,100个像素行和150个像素列(45,1,100,150)的维度,而每个对应的输出是3个可能的单词之一的一个热编码表示(例如“是“=> [0,0,1])。

在编译模型期间,会发生以下错误:

ValueError: Dimensions must be equal, but are 1 and 3 for 'Conv2D_94' (op: 'Conv2D') with 
input shapes: [?,100,150,1], [3,3,3,32].

以下是用于训练模型的脚本:

video = Input(shape=(self.frames_per_sequence,
                     1,
                     self.rows,
                     self.columns))
cnn = InceptionV3(weights="imagenet",
                  include_top=False)
cnn.trainable = False
encoded_frames = TimeDistributed(cnn)(video)
encoded_vid = LSTM(256)(encoded_frames)
hidden_layer = Dense(output_dim=1024, activation="relu")(encoded_vid)
outputs = Dense(output_dim=class_count, activation="softmax")(hidden_layer)
osr = Model([video], outputs)
optimizer = Nadam(lr=0.002,
                  beta_1=0.9,
                  beta_2=0.999,
                  epsilon=1e-08,
                  schedule_decay=0.004)
osr.compile(loss="categorical_crossentropy",
            optimizer=optimizer,
            metrics=["categorical_accuracy"]) 

1 个答案:

答案 0 :(得分:2)

根据Keras的Convolution2D,以下内容应该是输入和过滤器的形状。

shape of input = [batch, in_height, in_width, in_channels]
shape of filter = [filter_height, filter_width, in_channels, out_channels]

那么,你得到的错误的含义是什么 -

ValueError: Dimensions must be equal, but are 1 and 3 for 'Conv2D_94' (op: 'Conv2D') with 
input shapes: [?,100,150,1], [3,3,3,32].

[?,100,150,1]表示in_channels值为1,而[3,3,3,32]表示in_channels值为3.这就是您收到错误的原因 - Dimensions must be equal, but are 1 and 3

因此,您可以将过滤器的形状更改为[3, 3, 1, 32]