尝试拟合keras模型时出现错误“两种形状中的尺寸0必须相等,但分别为2和1”

2024-09-29 22:37:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试进行二元分类,我的数据的每个条目都由一个特征列表(以前生成的bert特征在csv上保存为tensor.numpy.flatte(),并加载为pandas dataframe)和一个标签(0或1个值)组成

然后,我创建了一个简单的模型(只是为了测试),它有一个密集的16个relu层和一个密集的1个sigmoid层。我使用二进制交叉熵损失和Adam优化器编译模型

当我运行模型拟合时,我得到了错误

ValueError: Dimension 0 in both shapes must be equal, but are 2 and 1. Shapes are [2] and [1]. for '{{node AssignAddVariableOp_8}} = AssignAddVariableOp[dtype=DT_FLOAT](AssignAddVariableOp_8/resource, Sum_7)' with input shapes: [], [1]

我已经尝试过重塑y,但得到了相同的错误。如果我使用2 softmax层(带有BinaryCrossentropy或sparse\u Category\u crossentropy),则会得到另一个错误: ValueError:logits和标签必须具有相同的形状((无,2)与(无,1))

我已经在stackoverflow中查看了许多解决方案,但无法解决我的问题。谁能帮帮我吗

Tensorflow和keras版本:2.5.0

train = pd.read_csv("../../../bert_features/panglee/panglee_bert_features_train.csv", sep=",") 
valid = pd.read_csv("../../../bert_features/panglee/panglee_bert_features_valid.csv", sep=",") 
test = pd.read_csv("../../../bert_features/panglee/panglee_bert_features_test.csv", sep=",")
x_train = train['bert_feats'].apply(np.vectorize(tf.constant))
y_train = train['label'].apply(np.vectorize(tf.constant))

x_valid = valid['bert_feats'].apply(np.vectorize(tf.constant))
y_valid = valid['label'].apply(np.vectorize(tf.constant))

x_test  = test['bert_feats'].apply(np.vectorize(tf.constant))
y_test  = test['label'].apply(np.vectorize(tf.constant))

y_train = np.asarray(y_train).astype('float32').reshape((len(y_train),1))
y_valid = np.asarray(y_valid).astype('float32').reshape((len(y_valid),1))
y_test = np.asarray(y_test).astype('float32').reshape((len(y_test),1))

model = keras.Sequential()
model.add(keras.layers.Dense(16, activation='relu'))
model.add(keras.layers.Dense(1, activation='sigmoid'))
model.compile(optimizer='adam', 
              loss=keras.losses.BinaryCrossentropy(),
              metrics=['accuracy', tf.metrics.Precision(),tf.metrics.Recall(), tfa.metrics.F1Score(num_classes=2, average='macro'), tf.metrics.AUC(curve='PR'),tf.metrics.AUC(curve='ROC')])
model.fit(x_train, y_train, epochs=10, validation_data=(x_valid, y_valid))

Tags: csvtestmodeltfnptrainkerasmetrics

热门问题