在Keras模型中要传递的数据类型适合?

时间:2019-04-23 13:58:49

标签: machine-learning keras deep-learning

我正在跟踪https://www.tensorflow.org/tutorials/keras/basic_classification来解决Kaggle挑战。

但是,我不知道应该将哪些数据输入到fit函数中。

我将训练数据集分为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)

我得到一个错误:

  

检查模型目标时出错:传递给模型的Numpy数组列表不是模型期望的大小。预计会看到1个数组,但获得了13125个数组的以下列表:

更新:

# 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)

我遇到一个错误:

  

检查输入时出错:预期flatten_7_input具有4维,但数组的形状为(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)

1 个答案:

答案 0 :(得分:1)

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

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