ValueError:层序的输入 0 与层不兼容::预期 min_ndim=4,发现 ndim=3.收到的完整形状:[8, 28, 28] [英] ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [8, 28, 28]

查看:39
本文介绍了ValueError:层序的输入 0 与层不兼容::预期 min_ndim=4,发现 ndim=3.收到的完整形状:[8, 28, 28]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不断收到与输入形状相关的错误.任何帮助将不胜感激.谢谢!

 将 tensorflow 导入为 tf(xtrain, ytrain), (xtest, ytest) = tf.keras.datasets.mnist.load_data()模型 = tf.keras.Sequential([tf.keras.layers.Conv2D(16, kernel_size=3, activation='relu'),tf.keras.layers.MaxPooling2D(pool_size=2),tf.keras.layers.Conv2D(32, kernel_size=3, activation='relu'),tf.keras.layers.MaxPooling2D(pool_size=2),tf.keras.layers.Flatten(),tf.keras.layers.Dense(64, activation='relu'),tf.keras.layers.Dense(10, activation='softmax')])model.compile(loss='categorical_crossentropy',优化器='亚当',指标='准确度')历史 = model.fit(xtrain, ytrain,验证数据=(xtest,ytest),epochs=10,batch_size=8)

<块引用>

ValueError:层序的输入 0 与层不兼容:预期 min_ndim=4,发现 ndim=3.收到的完整形状:[8, 28, 28]

解决方案

您创建的模型的输入层需要使用 4 维张量,但您传递给它的 x_train 张量只有 3 维

这意味着您必须使用 .reshape(n_images, 286, 384, 1) 来重塑您的训练集.现在,您已在不更改数据的情况下添加了额外的维度,并且您的模型已准备好运行.

在训练模型之前,您需要将 x_train 张量重塑为 4 维.例如:

x_train = x_train.reshape(-1, 28, 28, 1)

有关 keras 输入的更多信息 检查这个答案

I keep on getting this error related to input shape. Any help would be highly appreciated. Thanks!

import tensorflow as tf

(xtrain, ytrain), (xtest, ytest) = tf.keras.datasets.mnist.load_data()

model = tf.keras.Sequential([
    tf.keras.layers.Conv2D(16, kernel_size=3, activation='relu'),
    tf.keras.layers.MaxPooling2D(pool_size=2),
    tf.keras.layers.Conv2D(32, kernel_size=3, activation='relu'),
    tf.keras.layers.MaxPooling2D(pool_size=2),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(64, activation='relu'),
    tf.keras.layers.Dense(10, activation='softmax')
    ])

model.compile(loss='categorical_crossentropy', 
              optimizer='adam',
              metrics='accuracy')

history = model.fit(xtrain, ytrain,
                    validation_data=(xtest, ytest),
                    epochs=10, batch_size=8)

ValueError: Input 0 of layer sequential is incompatible with the layer: : expected min_ndim=4, found ndim=3. Full shape received: [8, 28, 28]

解决方案

The input layers of the model you created needs a 4 dimension tensor to work with but the x_train tensor you are passing to it has only 3 dimensions

This means that you have to reshape your training set with .reshape(n_images, 286, 384, 1). Now you have added an extra dimension without changing the data and your model is ready to run.

you need to reshape your x_train tensor to a 4 dimension before training your model. for example:

x_train = x_train.reshape(-1, 28, 28, 1)

for more info on keras inputs Check this answer

这篇关于ValueError:层序的输入 0 与层不兼容::预期 min_ndim=4,发现 ndim=3.收到的完整形状:[8, 28, 28]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆