繁体   English   中英

调用层“dense_6”(密集类型)时遇到异常。 尺寸必须相等

[英]Exception encountered when calling layer "dense_6" (type Dense). Dimensions must be equal

我不明白这个错误,我想让转移学习。 它是分类方,我希望没有线性分类方,在我的其他程序上没有这个错误。第一次,程序似乎工作。 你可以帮帮我吗?

那是我的脚本:

global_average_layer = tf.keras.layers.GlobalAveragePooling2D()

feature_batch_average = global_average_layer(feature_batch)

print(feature_batch_average.shape)

prediction_layer = tf.keras.layers.Dense(3, activation = 'relu')

prediction_layer2 = tf.keras.layers.Dense(256, activation='relu')

prediction_layer3 = tf.keras.layers.Dense(128, activation='relu')

prediction_batch = prediction_layer(feature_batch_average)

print(prediction_batch.shape)

inputs = tf.keras.Input(shape=(160, 160, 3))

x = data_augmentation(inputs)

x = preprocess_input(x)

x = base_model(x, training=False)

x = global_average_layer(x)

x = tf.keras.layers.Dropout(0.4)(x)

x = prediction_layer2(x)

x = prediction_layer3(x)

outputs = prediction_layer (x)

model = tf.keras.Model(inputs, outputs)

那是错误:

ValueError                                Traceback (most recent call last)

<ipython-input-36-2e1e1a4cbc58> in <module>()
      7 x = prediction_layer2(x)
      8 x = prediction_layer3(x)
----> 9 outputs = prediction_layer (x)
     10 model = tf.keras.Model(inputs, outputs)

1 frames

/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/ops.py in _create_c_op(graph, node_def, inputs, control_inputs, op_def)
   2011   except errors.InvalidArgumentError as e:
   2012     # Convert to ValueError for backwards compatibility.
-> 2013     raise ValueError(e.message)
   2014 
   2015   return c_op

ValueError: Exception encountered when calling layer "dense_6" (type Dense).

Dimensions must be equal, but are 128 and 1280 for '{{node dense_6/MatMul}} = MatMul[T=DT_FLOAT, transpose_a=false, transpose_b=false](Placeholder, dense_6/MatMul/ReadVariableOp)' with input shapes: [?,128], [1280,3].

Call arguments received:
  • inputs=tf.Tensor(shape=(None, 128), dtype=float32)

尺寸不匹配是由于 Dropout 层之后的附加层造成的。 删除这些图层会有所帮助。 我可以用Alzeihmer 的数据集复制这个问题。 请在下面找到代码。

inputs = tf.keras.Input(shape=(160, 160, 3))
x = data_augmentation(inputs)
x = preprocess_input(inputs)
x = base_model(x, training=False)
x = global_average_layer(x)
x = tf.keras.layers.Dropout(0.4)(x)
outputs = prediction_layer (x)
model = tf.keras.Model(inputs, outputs)

暂无
暂无

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

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