模型构建有形状 (None, 28) 问题
Model was constructed with shape (None, 28) problem
我收到一个错误。它说:
Model was constructed with shape (None, 28) for input KerasTensor(type_spec=TensorSpec(shape=(None, 28), dtype=tf.float32, name='dense_45_input'), name='dense_45_input', description="created by layer 'dense_45_input'"), but it was called on an input with incompatible shape (None, 28, 28).
我的代码在这里:
from keras.utils import np_utils
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, BatchNormalization, Dropout, Activation
import seaborn as sns
from keras.initializers import RandomNormal
from keras.initializers import he_normal
import matplotlib.pyplot as plt
(train_X, train_y), (test_X, test_y) = mnist.load_data()
output_dim = 10
input_dim = train_X.shape[1]
batch_size = 128
nb_epoch = 20
model_drop = Sequential()
model_drop.add(Dense(512, activation='relu', input_shape=(input_dim,),kernel_initializer=he_normal(seed=None)))
model_drop.add(BatchNormalization())
model_drop.add(Dropout(0.5))
model_drop.add(Dense(128, activation= 'relu', kernel_initializer=he_normal(seed=None)))
model_drop.add(BatchNormalization())
model_drop.add(Dropout(0.5))
model_drop.add(Dense(output_dim, activation = 'softmax'))
model_drop.summary()
model_drop.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
history = model_drop.fit(train_X, train_y, batch_size=batch_size, epochs=nb_epoch, verbose=1)
我该如何解决这个问题?另外我正在添加错误照片..
你在构建密集层时输入的尺寸是错误的,如果图像是 28x28 你需要能够接收所有像素(即你需要 28*28=784 输入连接)。要真正做到这一点,您还需要对 y 变量进行一次性编码以及重塑图像。
(train_X, train_y), (test_X, test_y) = mnist.load_data()
output_dim = 10
input_dim = train_X.shape[1]
batch_size = 128
nb_epoch = 20
model_drop = Sequential()
# see input_dim edit here
model_drop.add(Dense(512, activation='relu', input_shape=(input_dim*input_dim,),kernel_initializer=he_normal(seed=None)))
model_drop.add(BatchNormalization())
model_drop.add(Dropout(0.5))
model_drop.add(Dense(128, activation= 'relu', kernel_initializer=he_normal(seed=None)))
model_drop.add(BatchNormalization())
model_drop.add(Dropout(0.5))
model_drop.add(Dense(output_dim, activation = 'softmax'))
model_drop.summary()
model_drop.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
# encode Y_train and also shape X_train so it can feed to dense layer
Y_train = np_utils.to_categorical(train_y, num_classes=10)
X_train = train_X.reshape((-1, 28*28))
history = model_drop.fit(X_train, Y_train, batch_size=batch_size, epochs=nb_epoch, verbose=1)
我收到一个错误。它说:
Model was constructed with shape (None, 28) for input KerasTensor(type_spec=TensorSpec(shape=(None, 28), dtype=tf.float32, name='dense_45_input'), name='dense_45_input', description="created by layer 'dense_45_input'"), but it was called on an input with incompatible shape (None, 28, 28).
我的代码在这里:
from keras.utils import np_utils
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, BatchNormalization, Dropout, Activation
import seaborn as sns
from keras.initializers import RandomNormal
from keras.initializers import he_normal
import matplotlib.pyplot as plt
(train_X, train_y), (test_X, test_y) = mnist.load_data()
output_dim = 10
input_dim = train_X.shape[1]
batch_size = 128
nb_epoch = 20
model_drop = Sequential()
model_drop.add(Dense(512, activation='relu', input_shape=(input_dim,),kernel_initializer=he_normal(seed=None)))
model_drop.add(BatchNormalization())
model_drop.add(Dropout(0.5))
model_drop.add(Dense(128, activation= 'relu', kernel_initializer=he_normal(seed=None)))
model_drop.add(BatchNormalization())
model_drop.add(Dropout(0.5))
model_drop.add(Dense(output_dim, activation = 'softmax'))
model_drop.summary()
model_drop.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
history = model_drop.fit(train_X, train_y, batch_size=batch_size, epochs=nb_epoch, verbose=1)
我该如何解决这个问题?另外我正在添加错误照片..
你在构建密集层时输入的尺寸是错误的,如果图像是 28x28 你需要能够接收所有像素(即你需要 28*28=784 输入连接)。要真正做到这一点,您还需要对 y 变量进行一次性编码以及重塑图像。
(train_X, train_y), (test_X, test_y) = mnist.load_data()
output_dim = 10
input_dim = train_X.shape[1]
batch_size = 128
nb_epoch = 20
model_drop = Sequential()
# see input_dim edit here
model_drop.add(Dense(512, activation='relu', input_shape=(input_dim*input_dim,),kernel_initializer=he_normal(seed=None)))
model_drop.add(BatchNormalization())
model_drop.add(Dropout(0.5))
model_drop.add(Dense(128, activation= 'relu', kernel_initializer=he_normal(seed=None)))
model_drop.add(BatchNormalization())
model_drop.add(Dropout(0.5))
model_drop.add(Dense(output_dim, activation = 'softmax'))
model_drop.summary()
model_drop.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
# encode Y_train and also shape X_train so it can feed to dense layer
Y_train = np_utils.to_categorical(train_y, num_classes=10)
X_train = train_X.reshape((-1, 28*28))
history = model_drop.fit(X_train, Y_train, batch_size=batch_size, epochs=nb_epoch, verbose=1)