【发布时间】:2021-09-22 05:09:32
【问题描述】:
我正在尝试使用 TensorFlow 向 kaggle 上的Titanic problem 提交内容。
我在loss中使用了categorical_crossentropy,使用fit()后出现错误。该错误表明我的目标数组应该是二进制矩阵,但我的目标数组是来自训练数据的Survived 列。该列只有 1 和 0。它有什么问题?
这是我的代码:
import pandas as pd
import numpy as np
import tensorflow as tf
train_data = pd.read_csv('train.csv')
x_data = train_data[['Pclass', 'Sex', 'Age', 'SibSp',
'Parch', 'Fare']]
x_data = pd.get_dummies(x_data)
y_data = train_data[['Survived']]
X = tf.keras.layers.Input(shape=[7])
Y = tf.keras.layers.Dense(1, activation = 'softmax')(X)
model = tf.keras.models.Model(X, Y)
model.compile(loss = 'categorical_crossentropy', metrics = ['accuracy'])
model.fit(x_data, y_data, epochs=10) # The error occurred in here.
我收到了这个错误信息:
You are passing a target array of shape (891, 1) while using as loss `categorical_crossentropy`. `categorical_crossentropy` expects targets to be binary matrices (1s and 0s) of shape (samples, classes). If your targets are integer classes, you can convert them to the expected format via:
from keras.utils import to_categorical
y_binary = to_categorical(y_int)
Alternatively, you can use the loss function `sparse_categorical_crossentropy` instead, which does expect integer targets.
【问题讨论】:
-
这个错误甚至告诉你如何修复它,你试过了吗?二进制矩阵是指对标签进行一次性编码。
-
我试过了,但是当我修复它并安装它时,我的笔记本内核死了。
标签: python pandas tensorflow keras deep-learning