Reputation: 13
I'm trying to make a binary classification, each entry of my data is composed by a list of features (previously generated bert features saved as a tensor.numpy.flatten() on a csv and loaded as pandas dataframe) and a label (0 or 1 values).
Then I create a naive model (just to test) with a dense 16 relu layer and a dense 1 sigmoid layer. I compile the model with BinaryCrossentropy loss and Adam optimizer.
When I run the model fit, I get the error,
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]
I've already tried to reshape y, but got the same error. If I use a 2 softmax layer (with BinaryCrossentropy or sparse_categorical_crossentropy) get another error: ValueError: logits and labels must have the same shape ((None, 2) vs (None, 1))
I've given a look at many solutions here in stackoverflow, but couldn't solve my problem. Could anyone please help me?
Tensorflow and keras versions: 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))
Upvotes: 0
Views: 1478
Reputation:
Answering it here for the benefit of the community since the issues is solved as mentioned in the comment.
Problem solved. I used set_to_categorical() and worked fine.
Upvotes: 1