登录和标签必须是可广播的:logits_size=[29040,3] labels_size=[290400,3]

Logits and labels must be broadcastable: logits_size=[29040,3] labels_size=[290400,3]

我正在使用此代码:

import tensorflow as tf
import numpy as np
from tensorflow.keras.layers import Dense, LSTM, Input, Conv2D, Lambda
from tensorflow.keras import Model

def reshape_n(x):
    x = tf.compat.v1.placeholder_with_default(  
            x,
            [None, 121, 240, 2])
    return x

input_shape = (121, 240, 1)
inputs = Input(shape=input_shape)

x = Conv2D(1, 1)(inputs)
x = LSTM(2, return_sequences=True)(x[0, :, :, :])
x = Lambda(reshape_n, (121, 240,2))(x[None, :, :, :])
x = Conv2D(1, 1)(x)
output = Dense(3, activation='softmax')(x)

model = Model(inputs, output)
model.compile(optimizer='adam',
              loss='categorical_crossentropy',
              metrics='accuracy')

print(model.summary())

train_x = np.random.randint(0, 30, size=(10, 121, 240))
train_y = np.random.randint(0, 3, size=(10, 121, 240))
train_y = tf.one_hot(tf.cast(train_y, 'int32'), depth=3)

model.fit(train_x, train_y, epochs=2)

我收到:

logits and labels must be broadcastable: logits_size=[29040,3] labels_size=[290400,3]

如果我只是省略 LSTM 层:

x = Conv2D(1, 1)(inputs)
x = Conv2D(1, 1)(x)
output = Dense(3, activation='softmax')(x)

然后代码运行没有任何问题!

使用 tensorflow-gpu==2.3.0numpy==1.19.5,当我 运行 你的代码时,我没有观察到任何错误,退出代码是 0。我的 python 版本是 Python 3.8.6,以防万一。

显示的模型摘要是

Model: "functional_1"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         [(None, 121, 240, 1)]     0         
_________________________________________________________________
conv2d (Conv2D)              (None, 121, 240, 1)       2         
_________________________________________________________________
tf_op_layer_strided_slice (T [(121, 240, 1)]           0         
_________________________________________________________________
lstm (LSTM)                  (121, 240, 2)             32        
_________________________________________________________________
tf_op_layer_strided_slice_1  [(1, 121, 240, 2)]        0         
_________________________________________________________________
lambda (Lambda)              (None, 121, 240, 2)       0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 121, 240, 1)       3         
_________________________________________________________________
dense (Dense)                (None, 121, 240, 3)       6         
=================================================================
Total params: 43
Trainable params: 43
Non-trainable params: 0
_________________________________________________________________
None

训练阶段:

Epoch 1/2
2021-07-14 13:42:20.645002: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcublas.so.10
2021-07-14 13:42:20.793137: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcudnn.so.7
1/1 [==============================] - 0s 824us/step - loss: 1.1036 - accuracy: 0.3336
Epoch 2/2
1/1 [==============================] - 0s 2ms/step - loss: 1.1033 - accuracy: 0.3336