我正试图使用TensorFlow在kaggle上提交泰坦尼克号问题。
我在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.
我得到了它。我使用sklearn
中的StandardScaler()
作为输入数据,并解决了我的问题。
TF:loss是NaN,找不到可以处理输入的数据适配器:<类';pandas.core.frame.DataFrame'>lt;类';非eType'>