在神经网络中获取尺寸错误

Hi I a trying to make a Neural Network and getting an error ValueError: Error when checking input: expected conv2d_27_input to have 4 dimensions, but got array with shape (60000, 28, 28)


 import tensorflow as tf

import matplotlib.pyplot as plt

import numpy as np

from keras.datasets import mnist

from keras.models import Sequential

from keras.layers import Dense

from keras.layers import Dropout

from keras.layers import Flatten

from keras.constraints import maxnorm

from keras.optimizers import SGD

from keras.layers.convolutional import Conv2D

from keras.layers.convolutional import MaxPooling2D

from keras.utils import np_utils

# load data
(x_train, y_train), (x_test, y_test) = mnist.load_data()

# normalize inputs from 0-255 to 0.0-1.0
x_train = x_train.astype('float32')

x_test = x_test.astype('float32')

x_train = x_train / 255.0

x_test = x_test / 255.0

# Encode the outputs
y_train = np_utils.to_categorical(y_train) #Converts a class vector (integers) to binary class matrix.

y_test = np_utils.to_categorical(y_test)

num_classes = y_test.shape[1]

# Build the model
model = Sequential()

model.add(Conv2D(32, (3, 3), input_shape=(28, 28, 2), activation='relu'))

model.add(Conv2D(32, (3, 3), activation='relu'))

model.add(MaxPooling2D())

model.add(Flatten())

model.add(Dense(512, activation='relu', kernel_constraint=maxnorm(3)))

model.add(Dropout(0.2))

model.add(Dense(num_classes, activation='softmax'))

# Compile model
epochs = 5

lrate = 0.002

decay = lrate/epochs

有人可以帮助您理解吗?

评论
我是大怪兽
我是大怪兽

在规范化之前/之后重塑数据


# ...
# reshape to be [samples][width][height][channels]
X_train = X_train.reshape(X_train.shape[0], 28, 28, 1).astype('float32')
X_test = X_test.reshape(X_test.shape[0], 28, 28, 1).astype('float32')

X_train = X_train / 255.0
X_test = X_test / 255.0
点赞
评论