【问题标题】:Error when checking input: expected dense_28_input to have 2 dimensions, but got array with shape (3084, 32, 32)检查输入时出错:预期 dense_28_input 有 2 维,但得到的数组形状为 (3084, 32, 32)
【发布时间】:2020-05-10 07:43:18
【问题描述】:

我正在尝试使用我的模型进行预测,其中数组的形状为 (3084, 32, 32)。 获取值错误 here is error image

这是我的模型

model.add(Dense(1028, input_shape = (3084,), activation = "sigmoid"))
model.add(Dense(514, activation="sigmoid"))
model.add(Dense(len(lb.classes_), activation="softmax"))

总结

Model: "sequential_21"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_57 (Dense)             (None, 1028)              3171380   
_________________________________________________________________
dense_58 (Dense)             (None, 514)               528906    
_________________________________________________________________
dense_59 (Dense)             (None, 4)                 2060      
=================================================================
Total params: 3,702,346
Trainable params: 3,702,346
Non-trainable params: 0
_________________________________________________________________

尝试使用

opt = SGD(lr = 0.01)
model.compile(loss = "categorical_crossentropy", optimizer=opt, metrics=["accuracy"])
H = model.fit(train_X, train_Y, validation_data = (test_X, test_Y), epochs = 75, batch_size = 32)

【问题讨论】:

  • 为什么你的输入形状是 3084 却传递了 32,32 个数据?
  • 据我了解,我正在传递 3084,这是要训练的图像数量。 32,32 是图像大小。
  • 这是我的完整代码:colab.research.google.com/drive/…

标签: python-3.x tensorflow keras artificial-intelligence


【解决方案1】:

您需要正确指定输入形状,下面的模型应该可以工作。

from tensorflow.keras.layers import *
from tensorflow.keras.models import *

model = Sequential()
model.add(Dense(1028, input_shape = (32,32), activation = "sigmoid"))
model.add(Flatten())
model.add(Dense(514, activation="sigmoid"))
model.add(Dense(4, activation="softmax"))

model.summary()

【讨论】:

  • @HRShuvo,当答案有帮助时,请不要犹豫接受/投票:)
猜你喜欢
  • 2019-12-01
  • 1970-01-01
  • 2017-06-05
  • 1970-01-01
  • 2017-05-24
  • 2020-01-22
  • 1970-01-01
  • 2020-06-10
  • 2022-01-02
相关资源
最近更新 更多