gpt4 book ai didi

python-3.x - Tensorflow 无效形状 (InvalidArgumentError)

转载 作者:行者123 更新时间:2023-12-01 21:57:13 24 4
gpt4 key购买 nike

model.fit 产生异常:

tensorflow.python.framework.errors_impl.InvalidArgumentError: Cannot update variable with shape [] using a Tensor with shape [32], shapes must be equal.
[[{{node metrics/accuracy/AssignAddVariableOp}}]]
[[loss/dense_loss/categorical_crossentropy/weighted_loss/broadcast_weights/assert_broadcastable/AssertGuard/pivot_f/_50/_63]] [Op:__inference_keras_scratch_graph_1408]

模型定义:

model = tf.keras.Sequential()

model.add(tf.keras.layers.InputLayer(
input_shape=(360, 7)
))

model.add(tf.keras.layers.Conv1D(32, 1, activation='relu', input_shape=(360, 7)))
model.add(tf.keras.layers.Conv1D(32, 1, activation='relu'))
model.add(tf.keras.layers.MaxPooling1D(3))
model.add(tf.keras.layers.Conv1D(512, 1, activation='relu'))
model.add(tf.keras.layers.Conv1D(1048, 1, activation='relu'))
model.add(tf.keras.layers.GlobalAveragePooling1D())
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.Dense(32, activation='softmax'))

输入特征形状

(105, 360, 7)

输入标签形状

(105, 32, 1)

编译语句

model.compile(optimizer='adam',
loss=tf.keras.losses.CategoricalCrossentropy(),
metrics=['accuracy'])

模型拟合语句

 model.fit(features,
labels,
epochs=50000,
validation_split=0.2,
verbose=1)

任何帮助将不胜感激

最佳答案

您可以使用 model.summary() 查看您的模型架构。

print(model.summary())

_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv1d (Conv1D) (None, 360, 32) 256
_________________________________________________________________
conv1d_1 (Conv1D) (None, 360, 32) 1056
_________________________________________________________________
max_pooling1d (MaxPooling1D) (None, 120, 32) 0
_________________________________________________________________
conv1d_2 (Conv1D) (None, 120, 512) 16896
_________________________________________________________________
conv1d_3 (Conv1D) (None, 120, 1048) 537624
_________________________________________________________________
global_average_pooling1d (Gl (None, 1048) 0
_________________________________________________________________
dropout (Dropout) (None, 1048) 0
_________________________________________________________________
dense (Dense) (None, 32) 33568
=================================================================
Total params: 589,400
Trainable params: 589,400
Non-trainable params: 0
_________________________________________________________________
None

输出层的形状必须是(None,32),但是labels的形状是(105,32,1) 。因此,您需要将形状更改为 (105,32)np.squeeze() 函数用于从数组形状中删除一维条目。

关于python-3.x - Tensorflow 无效形状 (InvalidArgumentError),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56000401/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com