Saya melampirkan kode di bawah ini untuk mereplikasi masalah. Dataset berasal dari kompetisi Kaggle berjudul 'Tabular Playground Series Mar 2021.'

import numpy as np, pandas as pd, os
import tensorflow as tf
# Step 1. loading kaggle competition data
os.chdir('/kaggle/working')
train_df = pd.read_csv('../input/tabular-playground-series-mar-2021/train.csv', index_col=0)
cats = train_df.select_dtypes(include='object').columns.tolist()
# Step 2. label encoding
for c in cats:
    train_df[c], mp = train_df[c].factorize()
x_trn = train_df.drop('target', axis=1).values 
y_trn = train_df['target'].values 
# Step 3. creating the model 
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Input
model = Sequential()
model.add(Input(shape=(30,))) # we have only 30 features
model.add(Dense(16, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.summary()
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['AUC'])
model.fit(x=x_trn, y=y_trn, epochs=15) 

Kode di atas berfungsi. Namun, ketika saya mengganti baris terakhir dengan perintah berikut, program pecah. Adakah yang bisa memberi tahu saya cara memperbaikinya?

train_ds = tf.data.Dataset.from_tensor_slices((x_trn, y_trn))
model.fit(train_ds, epochs=15)

Pesan kesalahan mengeluh tentang ketidakcocokan:

ValueError: Input 0 of layer sequential is incompatible with the layer: expected axis -1 of input shape to have value 30 but received input with shape [30, 1]

Arsitektur model tercantum di bawah ini:

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense (Dense)                (None, 16)                496       
_________________________________________________________________
dense_1 (Dense)              (None, 8)                 136       
_________________________________________________________________
dense_2 (Dense)              (None, 1)                 9         
=================================================================
Total params: 641
Trainable params: 641
Non-trainable params: 0
_________________________________________________________________

Dimensi fitur X dan Target Y adalah:

print(x_trn.shape, y_trn.shape)
>>> (300000, 30) (300000,)
0
Li-Pin Juan 4 April 2021, 23:50

1 menjawab

Jawaban Terbaik

Saat bekerja dengan dataset di model.fit, TF mengharapkan dataset pelatihan untuk dibagi dalam batch. Dengan demikian, untuk melatih model Anda tambahkan baris berikut sebelum model.fit():

   train_ds = train_ds.batch(your_batch_size).

Ini akan menghasilkan data dari bentuk yang benar. Dalam pendekatan Anda sebelumnya, untuk secara otomatis membagi data dalam batch.

0
Ivan K. 5 April 2021, 14:20