gpt4 book ai didi

python - Keras错误: Error when checking model target: the list of Numpy arrays that you are passing to your model is not the size the model expected

转载 作者:行者123 更新时间:2023-11-30 09:04:36 28 4
gpt4 key购买 nike

我正在关注https://www.tensorflow.org/tutorials/keras/basic_classification解决 Kaggle 挑战。

但是,我不明白应该将什么样的数据输入到拟合函数中。

我将训练数据集拆分为 X_trainy_trainX_testy_testX_train 的形状为 (13125, 32, 32, 3)

model = keras.Sequential([
keras.layers.Flatten(input_shape=(32, 32, 3)),
keras.layers.Dense(128, activation=tf.nn.relu),
keras.layers.Dense(10, activation=tf.nn.softmax)
])

model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])

model.fit(X_train, y_train, epochs=5)

我收到错误:

Error when checking model target: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 1 array(s), but instead got the following list of 13125 arrays:

更新:

# TensorFlow and tf.keras
import tensorflow as tf
from tensorflow import keras

# Helper libraries
import numpy as np
import matplotlib.pyplot as plt

model = keras.Sequential([
keras.layers.Flatten(input_shape=(32,32,3)),
keras.layers.Dense(128, activation=tf.nn.relu),
keras.layers.Dense(10, activation=tf.nn.softmax)
])

model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])


X_train_stack = np.vstack(X_train)
model.fit(X_train_stack, y_train, epochs=5)

我收到一个错误:

Error when checking input: expected flatten_7_input to have 4 dimensions, but got array with shape (420000, 32, 3)

#read in training set
train_img = []
train_lb = []
for i in range(len(cactus_label)):
row = cactus_label.iloc[i]
fileName = row['id']
train_lb.append(row['has_cactus'])
path = "../input/train/train/{}".format(fileName)
im = mpimg.imread(path)
train_img.append(im)

X_train, X_test, y_train, y_test = train_test_split(train_img, train_lb)
X_train = np.array(X_train)
X_test = np.array(X_test)

最佳答案

您需要传递 numpy 数组,但您传递的是 numpy 数组列表。使用 np.stack() 从 numpy 数组列表创建单个 numpy 数组:

X_train = np.stack(X_train, axis=0)

关于python - Keras错误: Error when checking model target: the list of Numpy arrays that you are passing to your model is not the size the model expected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55812964/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com