gpt4 book ai didi

python - Keras model.fit ValueError : Input arrays should have the same number of samples as target arrays

转载 作者:行者123 更新时间:2023-11-30 09:18:02 25 4
gpt4 key购买 nike

我正在尝试将从运行 resnet50 中获得的bottleneck_features 加载到顶层模型中。我在resnet上运行predict_generator并将生成的bottleneck_features保存到npy文件中。由于以下错误,我无法拟合我创建的模型:

    Traceback (most recent call last):
File "Labeled_Image_Recognition.py", line 119, in <module>
callbacks=[checkpointer])
File "/home/dillon/anaconda3/envs/tensorflow/lib/python3.6/site-packages/keras/models.py", line 963, in fit
validation_steps=validation_steps)
File "/home/dillon/anaconda3/envs/tensorflow/lib/python3.6/site-packages/keras/engine/training.py", line 1630, in fit
batch_size=batch_size)
File "/home/dillon/anaconda3/envs/tensorflow/lib/python3.6/site-packages/keras/engine/training.py", line 1490, in _standardize_user_data
_check_array_lengths(x, y, sample_weights)
File "/home/dillon/anaconda3/envs/tensorflow/lib/python3.6/site-packages/keras/engine/training.py", line 220, in _check_array_lengths
'and ' + str(list(set_y)[0]) + ' target samples.')
ValueError: Input arrays should have the same number of samples as target arrays. Found 940286 input samples and 14951 target samples.

我不太确定这意味着什么。我的火车目录中有 940286 个图像,这些图像分为 14951 个子目录。我的两个假设是:

  1. 我可能没有正确设置 train_data 和 train_labels 的格式。
  2. 我错误地设置了模型

任何正确方向的指导将不胜感激!

代码如下:

# Constants
num_train_dirs = 14951 #This is the total amount of classes I have
num_valid_dirs = 13168

def load_labels(path):
targets = os.listdir(path)
labels = np_utils.to_categorical(targets, len(targets))
return labels

def create_model(train_data):
model = Sequential()
model.add(Flatten(input_shape=train_data.shape[1:]))
model.add(Dense(num_train_dirs, activation='relu'))
model.add(Dropout(0.2))
model.add(Dense(num_train_dirs, activation='softmax'))
model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
return model

train_data = np.load(open('bottleneck_features/bottleneck_features_train.npy', 'rb'))
train_labels = load_labels(raid_train_dir)

valid_data = np.load(open('bottleneck_features/bottleneck_features_valid.npy', 'rb'))
valid_labels = train_labels

model = create_model(train_data)
model.summary()

checkpointer = ModelCheckpoint(filepath='weights/first_try.hdf5', verbose=1, save_best_only=True)

print("Fitting model...")

model.fit(train_data, train_labels,
epochs=50,
batch_size=100,
verbose=1,
validation_data=(valid_data, valid_labels),
callbacks=[checkpointer])

最佳答案

在监督学习的情况下,输入样本的数量 (X) 必须与输出(标签)样本的数量 (Y) 匹配。

例如:如果我们想要拟合(学习)神经网络来识别手写数字,并且我们向模型提供 10.000 个图像 (X),那么我们还应该传递 10.000 个标签 ()。

在您的情况下,这些数字不匹配。

关于python - Keras model.fit ValueError : Input arrays should have the same number of samples as target arrays,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50081415/

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