Tensorflow 无效形状 (InvalidArgumentError)

Tensorflow invalid shape (InvalidArgumentError)

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 声明

 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)。当我们想从数组的形状中删除 single-dimensional 个条目时,使用 np.squeeze() 函数。

在密集层之前使用 Flatten()。