Tensorflow/Keras模型不拟合数据

ctehm74n  于 2023-04-30  发布在  其他
关注(0)|答案(1)|浏览(142)

我正在使用Tensorflow和Keras进行一个项目,但我的数据形状出现了错误。当我运行www. example www.example.com ()部分的代码我得到下面这个错误。我知道我需要重塑数据,但我不确定我需要重塑的确切位置。
下面是我的dataframe的头:

House   Q1  Q2  Q3  Q4  Q5  Q6  Q7
0   slytherin   ask for more stories    black visions goblet    cold    ghosts  worried about mental health call dr forest  left
1   slytherin   ask for more stories    black visions goblet    hunger  superstrength   nightmare silly voice   moon    black
2   slytherin   ask for more stories    fresh parchment being ignored   every area of magic volunteer to fight  moon    tails
3   slytherin   ask for more stories    golden sunspots potion  boredom merpeople   silly voice dusk    tails
4   slytherin   ask for more stories    golden sunspots potion  feared  vampires    draw wand and stand ground  dawn    white

df.shape(1209,8)

from sklearn.model_selection import train_test_split
from tensorflow import keras
from tensorflow.keras import layers

Input:
X = df[['Q1', 'Q2', 'Q3', 'Q4', 'Q5', 'Q6', 'Q7']]
y = df['House']

# splitting data 75% train
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

print('X_train shape:', X_train.shape)
print('X_test shape:', X_test.shape)
print('Y_train shape:', y_train.shape)
print('Y_test shape:', y_test.shape)

Output:
X_train shape: (967, 7)
X_test shape: (242, 7)
Y_train shape: (967,)
Y_test shape: (242,)

Input:
model = keras.Sequential([
    layers.Dense(64, activation='relu', input_shape=[7]),
    layers.Dense(32, activation='relu'),
    layers.Dense(16, activation='relu'),
    layers.Dense(4, activation='softmax')
])
model.summary()

Output:
Model: "sequential_15"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 dense_57 (Dense)            (None, 64)                512       
                                                                 
 dense_58 (Dense)            (None, 32)                2080      
                                                                 
 dense_59 (Dense)            (None, 16)                528       
                                                                 
 dense_60 (Dense)            (None, 4)                 68        
                                                                 
=================================================================
Total params: 3,188
Trainable params: 3,188
Non-trainable params: 0
_________________________________________________________________

Input:
model.compile(
    optimizer='adam', 
    loss='categorical_crossentropy', 
    metrics=['accuracy'])

model.fit(
    X_train, 
    y_train, 
    epochs=50, 
    validation_data=(X_test, y_test))

Output:
  File "/usr/local/lib/python3.9/site-packages/keras/backend.py", line 5559, in categorical_crossentropy
        target.shape.assert_is_compatible_with(output.shape)

    ValueError: Shapes (None, 1) and (None, 4) are incompatible

我返回并将输出层更改为1,并得到以下错误。

SyntaxWarning: In loss categorical_crossentropy, expected y_pred.shape to be (batch_size, num_classes) with num_classes > 1. Received: y_pred.shape=(None, 1). Consider using 'binary_crossentropy' if you only have 2 classes.
  return dispatch_target(*args, **kwargs)
2023-04-29 15:53:30.502022: W tensorflow/core/framework/op_kernel.cc:1807] OP_REQUIRES failed at cast_op.cc:121 : UNIMPLEMENTED: Cast string to float is not supported

我也有点困惑,我应该有4节课,而不是2节。如果任何人有任何方向或可以澄清我不能看到的,我将不胜感激。先谢谢你了!

velaa5lx

velaa5lx1#

首先你的Y是文本数据,所以你需要使用tokenizer将它转换成序列。texts_to_sequnces(x_train),同样对于x_test,然后填充pad_sequences(x_train,padding = 'post',maxlen = 100),同样对于x_test,然后你可以给予这个填充的x_train和X_test给你的模型,关于输出层中的类的数量,它取决于你在House列中拥有的唯一数字的数量,所以,它将是模型。add(Dense(set(df[house])).values),activation = 'softmax')。

相关问题