ValueError: Input arrays should have the same number of samples as target arrays. Found 1280 input samples and 320 target samples
ValueError: Input arrays should have the same number of samples as target arrays. Found 1280 input samples and 320 target samples
What's wrong with this code?
faces = datasets.fetch_olivetti_faces()
X_train, X_test, y_train, y_test = train_test_split(faces.data,faces.target, test_size=0.2)
X_train = X_train.reshape(-1,32 ,32 ,1)
X_test = X_test.reshape(-1,32 , 32 ,1)
# Normalize the data
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255.0
X_test /= 255.0
# One hot
classes=40
y_train = keras.utils.to_categorical(y_train, classes)
y_test = keras.utils.to_categorical(y_test, classes)
#Build LetNet model with Keras
def LetNet(width, height, depth, classes):
# initialize the model
model = Sequential()
# first layer, convolution and pooling
model.add(Conv2D(input_shape=(width, height, depth), kernel_size=(5, 5), filters=6, strides=(1,1), activation='tanh'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
# second layer, convolution and pooling
model.add(Conv2D(input_shape=(width, height, depth), kernel_size=(5, 5), filters=16, strides=(1,1), activation='tanh'))
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
# Fully connection layer
model.add(Flatten())
model.add(Dense(120,activation = 'tanh'))
model.add(Dense(84,activation = 'tanh'))
# softmax classifier
model.add(Dense(classes))
model.add(Activation("softmax"))
return model
LetNet_model = LetNet(32,32,1,40)
LetNet_model.summary()
#Strat training
LetNet_model.compile(optimizer=Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08),loss = 'categorical_crossentropy',metrics=['accuracy'])
History = LetNet_model.fit(X_train, y_train, epochs=5, batch_size=32,validation_data=(X_test, y_test))
我的计算表明,您的输入图像的形状为 64 x 64 和 1 个通道,而不是 32 x 32。
您可以更改以下行。
X_train = X_train.reshape(-1, 64 ,64 ,1)
X_test = X_test.reshape(-1, 64 , 64 ,1)
此外,更改您的模型输入。
LetNet_model = LetNet(64,64,1,40)
What's wrong with this code? faces = datasets.fetch_olivetti_faces() X_train, X_test, y_train, y_test = train_test_split(faces.data,faces.target, test_size=0.2) X_train = X_train.reshape(-1,32 ,32 ,1) X_test = X_test.reshape(-1,32 , 32 ,1) # Normalize the data X_train = X_train.astype('float32') X_test = X_test.astype('float32') X_train /= 255.0 X_test /= 255.0 # One hot classes=40 y_train = keras.utils.to_categorical(y_train, classes) y_test = keras.utils.to_categorical(y_test, classes) #Build LetNet model with Keras def LetNet(width, height, depth, classes): # initialize the model model = Sequential() # first layer, convolution and pooling model.add(Conv2D(input_shape=(width, height, depth), kernel_size=(5, 5), filters=6, strides=(1,1), activation='tanh')) model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) # second layer, convolution and pooling model.add(Conv2D(input_shape=(width, height, depth), kernel_size=(5, 5), filters=16, strides=(1,1), activation='tanh')) model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2))) # Fully connection layer model.add(Flatten()) model.add(Dense(120,activation = 'tanh')) model.add(Dense(84,activation = 'tanh')) # softmax classifier model.add(Dense(classes)) model.add(Activation("softmax")) return model LetNet_model = LetNet(32,32,1,40) LetNet_model.summary() #Strat training LetNet_model.compile(optimizer=Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08),loss = 'categorical_crossentropy',metrics=['accuracy']) History = LetNet_model.fit(X_train, y_train, epochs=5, batch_size=32,validation_data=(X_test, y_test))
我的计算表明,您的输入图像的形状为 64 x 64 和 1 个通道,而不是 32 x 32。
您可以更改以下行。
X_train = X_train.reshape(-1, 64 ,64 ,1)
X_test = X_test.reshape(-1, 64 , 64 ,1)
此外,更改您的模型输入。
LetNet_model = LetNet(64,64,1,40)