python - Keras 值错误 : Dimensions must be equal

标签 python machine-learning deep-learning keras

该模型的目标是根据与视频输入相关联的单词对视频输入进行分类。每个输入的维度为 45 帧、1 个灰色 channel 、100 像素行和 150 像素列(45、1、100、150),而每个对应的输出是 3 个可能单词之一的单热编码表示(例如"is"=> [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"]) 

最佳答案

根据 Convolution2D在 Keras 中,下面应该是输入和过滤器的形状。

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]

关于python - Keras 值错误 : Dimensions must be equal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42497925/

相关文章:

python - 在 ubuntu 14.04 中安装 Caffe

tensorflow - 没有随机输入的 GAN

deep-learning - 如何理解 "individual neurons are the basis directions of activation space"?

python - 如何有条件地将值分配给张量[损失函数的掩蔽]?

Matlab KNN分类共识

python - Keras 中的特征提取

python - 如何使用 Pandas 将字典列表分组为子列表?

c# - 将图像转换为要绘制的线条数组

Python tornado OS 错误 24 打开的文件太多

machine-learning - 什么是强化学习中的最优性?