gpt4 book ai didi

python - 值错误 : Input arrays should have the same number of samples as target arrays. 找到 166 个输入样本和 4 个目标样本

转载 作者:太空宇宙 更新时间:2023-11-04 04:23:48 27 4
gpt4 key购买 nike

我正在使用 Keras DL 库对图像数据集进行分类。我在尝试训练模型时遇到错误。

我正在处理的数据集没有大量数据,因此训练集示例包含 166 张图像。我不确定这个错误,但我认为我必须以某种方式更改标签集的形状才能修复它。这是代码:

import tensorflow as tf
import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten
from tensorflow.keras.layers import Conv2D, MaxPooling2D


DIR = '/home/.../'
IMG_H = 256
IMG_W = 256
IMG_CH = 1


loadFile = DIR + 'Img.npz'
X = np.load(loadFile)
trainImgSet = X['trainImgSet']
trainLabelSet = X['trainLabelSet']
testImgSet = X['testImgSet']

print('Shape of trainImgSet: {}'.format(trainImgSet.shape))
print('Shape of trainLabelSet: {}'.format(trainLabelSet))
#print('Shape of testImgSet:{}'.format(testImgSet))


model = tf.keras.Sequential()
model.add(tf.keras.layers.Conv2D(256, (3, 3), input_shape = (IMG_H, IMG_W, IMG_CH)))
model.add(tf.keras.layers.Activation('relu'))
model.add(tf.keras.layers.MaxPooling2D(pool_size=(1, 1)))

model.add(tf.keras.layers.Conv2D(256, (3, 3)))
model.add(tf.keras.layers.Activation('relu'))
model.add(tf.keras.layers.MaxPooling2D(pool_size=(1, 1)))

model.add(tf.keras.layers.Flatten())

model.add(tf.keras.layers.Dense(64))

model.add(tf.keras.layers.Dense(1))
model.add(tf.keras.layers.Activation('sigmoid'))

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

model.summary()

#train the CNN
model.fit(trainImgSet, trainLabelSet, batch_size=10, epochs=5, validation_split=0.1)



Here is the error:
Traceback (most recent call last):
File "/home/Code/DeepCl.py", line 49, in <module>
model.fit(trainImgSet, trainLabelSet, batch_size=10, epochs=5, validation_split=0.1)
File "anaconda3/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1536, in fit
validation_split=validation_split)
File "/anaconda3/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 992, in _standardize_user_data
class_weight, batch_size)
File "/anaconda3/lib/python3.6/site-packages/tensorflow/python/keras/engine/training.py", line 1169, in _standardize_weights
training_utils.check_array_lengths(x, y, sample_weights)
File "/anaconda3/lib/python3.6/site-packages/tensorflow/python/keras/engine/training_utils.py", line 426, 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 166 input samples and 4 target samples.

最佳答案

在这里,

  • 训练样本数不等于标签数。

  • 有 144 个训练样本,但只有 4 个标签。

  • 训练数据和测试数据的形状必须具有相同数量的样本。

  • 例如。训练数据的形状为 ( 100 , 256 , 256 , 1 )。测试数据的形状应为 ( 100 , 1 )

关于python - 值错误 : Input arrays should have the same number of samples as target arrays. 找到 166 个输入样本和 4 个目标样本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53888320/

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