【问题标题】:How to adjust the output dimensions of a Keras model?如何调整 Keras 模型的输出尺寸?
【发布时间】:2019-10-03 22:02:18
【问题描述】:

我正在尝试使用 Keras 构建人工神经网络。模型的输入尺寸为 (5, 5, 2),而输出尺寸为 (5,5)。运行 keras.fit() 函数时,遇到以下错误:

ValueError: Error when checking target: expected dense_3 to have 4 dimensions, but got array with shape (5, 5)

这是我正在执行的代码

from keras.models import Sequential
from keras.layers import Dense, Flatten
import matplotlib.pyplot as plt
from keras.callbacks import EarlyStopping, ModelCheckpoint

model = Sequential()

model.add(Dense(1000, input_shape=(5, 5, 2), activation="relu"))
model.add(Dense(1000, activation="relu"))
model.add(Dense(2), output_shape=(5,5))

model.summary()

model.compile(optimizer="adam",loss="mse", metrics = ["mse"])

monitor_val_acc = EarlyStopping(monitor="loss", patience = 10)


history = model.fit(trainX, trainYbliss, epochs=1000, validation_data=(testX, testY), callbacks = [monitor_val_acc], verbose = 1)

clinical = model.predict(np.arange(0, len(testY)))

这是网络的架构:

Layer (type)                 Output Shape              Param #   
=================================================================
dense_1 (Dense)              (None, 5, 5, 1000)        3000      
_________________________________________________________________
dense_2 (Dense)              (None, 5, 5, 1000)        1001000   
_________________________________________________________________
dense_3 (Dense)              (None, 5, 5, 1)           1001      
=================================================================
Total params: 1,005,001
Trainable params: 1,005,001
Non-trainable params: 0
_________________________________________________________________

模型应该基于 (5,5,2) 数组输出 (5,5) 数组,但在最低隐藏层失败。我该如何解决这个问题?

【问题讨论】:

  • 您能否确认并粘贴输入和输出形状的输出(trainXtrainYbliss)。
  • @MartinDinov trainX.shape() = (1, 5, 5, 2), trainYbliss.shape() = (1, 5, 5, 1)

标签: python python-3.x tensorflow keras keras-layer


【解决方案1】:

使用以下代码作为参考,根据您的输入值更改值:

train_data = train_data.reshape(train_data.shape[0], 10, 30, 30, 1)

对于您的输入训练数据,

【讨论】:

    【解决方案2】:

    您的网络将输出形状为 (batch_size, 5, 5, 1) 的张量。您的输出是 4 维张量吗? 如果它是(5,5) 的单个值,您需要将其重塑为(1,5,5,1) 我认为

    【讨论】:

    • 没错,但问题是我不知道该怎么做
    猜你喜欢
    • 1970-01-01
    • 2018-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-04
    • 2019-08-30
    • 2021-09-01
    • 2018-02-10
    相关资源
    最近更新 更多