【问题标题】:y_train in fit function has not the right dimension拟合函数中的 y_train 维度不正确
【发布时间】:2019-01-14 11:02:35
【问题描述】:

我以某种模式创建了一个人工点集合来运行 2D 分类器。因此,我插入点 e。 G。 (x1,x2) 并命名它们的正确类别(标签 1 或标签 2)。点 x_train 和 y_train 都放入 Keras 层模型中,最后,我运行 Model.fit 方法。

# Assign returned data
x_train, y_train = separate_dots_from_dict(dots)
y_train = to_categorical(y_train, NUM_CLASSES)
print("Shapes (x, y):", x_train.shape, ",", y_train.shape)

# Classification
model = Sequential()

model.add(Dense(NUM_CLASSES * 8, input_shape = (2, 1, 1), activation = 'relu'))
model.add(Dense(NUM_CLASSES * 4, activation = 'relu'))
model.add(Dense(NUM_CLASSES, activation = 'softmax'))

model.compile(loss = 'categorical_crossentropy',
              optimizer = 'sgd',
              metrics = ['accuracy'])

model.fit(x_train, y_train, epochs = 4, batch_size = 2)

之前,我已经打印了我的点转换结果,该结果由我的 separate_dots_from_dict() 函数成功输出,并使用 Keras 包中的 to_categorical() 方法进行了转换。我的函数以

结尾
return np.array(x_train).reshape(len(x_train), 2, 1, 1), np.array(y_train).reshape(len(y_train))

在下文中,我向您展示了在分类开始之前最终生成的 5 个虚构点:

X

[[[[ 0.5]]

  [[ 0.8]]]


 [[[ 0.3]]

  [[ 0.6]]]


 [[[ 0.1]]

  [[-0.3]]]

[[[ 1.1]]

  [[-1.1]]]


 [[[-1.4]]

  [[-1.5]]]]

是的

[[1. 0.]
 [1. 0.]
 [1. 0.]
 [0. 1.]
 [0. 1.]]

Y 是 y_train,所以它是训练目标 e。 G。标签。 x_train (X) 的格式可能看起来很尴尬,但考虑到我刚刚在此处类似实现的 MNIST 图像的重塑,这正是著名的格式。 不幸的是,我收到以下错误:

Using TensorFlow backend.
Shapes (x, y): (34, 2, 1, 1) , (34, 2)
Traceback (most recent call last):
  File "main.py", line 88, in <module>
    model.fit(x_train, y_train, epochs = 4, batch_size = 2)
  File "/home/scud3r1a/Conda/envs/numtenpy/lib/python3.6/site-packages/keras/engine/training.py", line 950, in fit
    batch_size=batch_size)
  File "/home/scud3r1a/Conda/envs/numtenpy/lib/python3.6/site-packages/keras/engine/training.py", line 787, in _standardize_user_data
    exception_prefix='target')
  File "/home/scud3r1a/Conda/envs/numtenpy/lib/python3.6/site-packages/keras/engine/training_utils.py", line 127, in standardize_input_data
    'with shape ' + str(data_shape))
ValueError: Error when checking target: expected dense_3 to have 4 dimensions, but got array with shape (34, 2)

我能够找到的所有解决方案都具有仅更改最后一个 Dense 层中的单位的解决方案。但首先,这不会影响任何事情,其次,我认为这是真的。

尺寸误差与 x_train 形状成比例。 在这里做什么?

【问题讨论】:

  • 你能试试y_train = y_train.reshape(x_train.shape)吗?

标签: python keras


【解决方案1】:

Dense 层需要输入 (input_dims, None) 的 dims,您发送的 3 的 dims 应该只是 1 的预期(格式正确)。 None代表batch_size,不需要定义。

在您的模型中尝试此更改:

x_train = x_train.reshape(2,-1)
model = Sequential()
model.add(Dense(NUM_CLASSES * 8, input_dim=(2,), activation = 'relu')

它会解决你的问题。

【讨论】:

  • 谢谢,在你的重塑方法后转置矢量后它工作了。所以一个额外的 .T 是必要的,但现在,它完美地工作了。
【解决方案2】:

我认为您的输入形状不正确。你很可能试图重建一个 Conv 模型,对吧? 您遇到的问题是您对密集层的输入具有 3 个维度。但是你想要的只是一个维度,比如 32 个神经元。为了实现这一点,您要么必须重塑 x 输入,要么在输出层之前的某个点插入一个展平层。 在模型开发过程中,打印模型可能会有所帮助(model.summary() 会这样做)。在那里你可以得到一个概览,也许对它的外观有更好的了解。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-31
    • 1970-01-01
    • 2020-05-07
    • 2014-10-17
    • 1970-01-01
    相关资源
    最近更新 更多