簡體   English   中英

TensorFlow 'categorical_crossentropy' 中的 ValueError

[英]ValueError in TensorFlow 'categorical_crossentropy'

我正在嘗試使用 TensorFlow 在 kaggle 上提交泰坦尼克號問題

我在損失中使用了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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM