其他分享
首页 > 其他分享> > Input 0 of layer conv2d is incompatible with the layer: expected ndim=4, found ndim=3. Full shape re

Input 0 of layer conv2d is incompatible with the layer: expected ndim=4, found ndim=3. Full shape re

作者:互联网

Input 0 of layer conv2d is incompatible with the layer: expected ndim=4, found ndim=3. Full shape received: [None, 208, 1]

在这里插入图片描述
情景:在卷积用于结构化数据时,自定义Keras类中Conv2D卷积调用报错

报错原因:输入数据是3维的,不符合卷积要求,卷积要求是4维的

卷积计算要求输入的图片必须是4个维度的,第0个维度表示一次喂入几个batch,第1、2、3个维度分别表示输入图片的分辨率和通道数。这一部分详细且简单明了的解释可以参考下面的链接:https://baijiahao.baidu.com/s?id=1647365166670429226&wfr=spider&for=pc

代码部分修改:

只贴出关键代码

def call(self, inputs, training=None, mask=None):
    dense_inputs, sparse_inputs = inputs[:, :13], inputs[:, 13:]

    sparse_embed = [layer(sparse_inputs[:, i]) for i, layer in enumerate(self.emb_layers)]  # 26 * [None, k]
    # 卷积要求输入为4维
    sparse_embed = tf.transpose(tf.convert_to_tensor(sparse_embed), [1, 0, 2])  # [None, 26, k]
    print(sparse_embed.shape)

    fgcnn_out = self.fgcnn_layers(sparse_embed)
 def call(self, inputs, **kwargs):
    x = tf.expand_dims(inputs, axis=-1)  # [None, n, k, 1] 最后一维为通道

参考链接:
https://blog.csdn.net/tushuguan_sun/article/details/105914661

标签:ndim,inputs,layer,Full,卷积,None,sparse,embed
来源: https://blog.csdn.net/qq_42363032/article/details/122823556