简体   繁体   English

TensorFlow CNN 不兼容形状:4D 输入形状

[英]TensorFlow CNN Incompatible Shapes: 4D input shape

I have sample data in the form: Data[n][31][31][5][2] with:我有以下形式的样本数据: Data[n][31][31][5][2] 与:

  • "[n]" being the sample “[n]”是样本
  • "[31][31]" being the array of data points “[31][31]”是数据点数组
  • "[5]" being the number of bits within that data point “[5]”是该数据点内的位数
  • and "[2]" being one-hot encoding of the bits (eg a bit of 1 would be [1, 0] and a zero [0, 1])并且“[2]”是位的单热编码(例如,位 1 将是 [1, 0] 和零 [0, 1])

The output is intended to either be a [5][2] or a [10] array of values which is validated against another [5][2] or [10] array. output 旨在成为 [5][2] 或 [10] 值数组,可针对另一个 [5][2] 或 [10] 数组进行验证。 When trying to build the model, I get the following error:尝试构建 model 时,出现以下错误:

 "ValueError: Shapes (None, 5, 2) and (None, 10) are incompatible"

The model code looks like this: (with train_m[n][31][31][5][2], tr_m[5][2], check_m[n][31][31][5][2], cr_m[5][2] being training data and expected output followed by validation data and expected output.) model 代码如下所示: (with train_m[n][31][31][5][2], tr_m[5][2], check_m[n][31][31][5][2] , cr_m[5][2] 是训练数据和预期 output 后跟验证数据和预期 output。)

model = Sequential([
    Conv2D(num_filters, filter_size, input_shape=(31, 31, 5, 2)),
    Flatten(),
    Dense(10, activation='relu'),
])


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

model.summary()
model.fit(
    train_m,
    tr_m,
    epochs=(100),
    validation_data=(check_m, cr_m),
    verbose=0
)

As the [5][2] outputs are one-hotted, I'm uncertain if they can be made to a [10] matrix while still being interpreted correctly.由于 [5][2] 输出是单一的,我不确定它们是否可以在被正确解释的同时被制成 [10] 矩阵。 Further, would there be any way to make the dense layer to a [5][2]?此外,是否有任何方法可以使密集层成为 [5][2]?

The full error can be seen here.完整的错误可以在这里看到。 I felt it would be awfully long to include in rawtext here.我觉得在这里包含在 rawtext 中会非常长。

If there's anything more that's needed, please let me know - I'm still very new to working with TensorFlow.如果还有什么需要,请告诉我——我对使用 TensorFlow 还是很陌生。

Your label shapes are (5,2) but network output is (10,) so this is confusing.您的 label 形状是 (5,2) 但网络 output 是 (10,) 所以这很混乱。 Both output shape and label shape should be the same. output 形状和 label 形状应该相同。 use:利用:

tf.keras.layers.Reshape((5,2))

after the Dense layer.在密集层之后。 you'll be fine你会没事的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM