为 keras w tensorflow 后端重塑张量的正确方法
Correct way to reshape tensor for keras w tensorflow backend
我已经有一段时间没有做任何神经网络了。我正在编写一个简单的示例作为热身,我很困惑为什么我的张量形状不正确。我认为模型符合预期(样本、高度、宽度、通道)。
当我打电话给
X_train.shape
我得到 (784, 100, 100, 3)
但是我得到一个错误:ValueError:检查目标时出错:预期 dense_10 具有形状 (2,) 但得到形状为 (1,)
的数组
这是下面的简单模型。我哪里错了?我需要转换成灰度吗?
#THE MODEL#
batch_size = 32
nb_classes = 2
nb_epoch = 2
img_rows =100
img_cols=100
img_channels = 3
model_input=Input(shape=(img_rows, img_cols,img_channels))
x = Convolution2D(32, 3, 3, border_mode='same')(model_input)
x = Activation('relu')(x)
x = Convolution2D(32, 3, 3)(x)
x = Activation('relu')(x)
x = MaxPooling2D(pool_size=(2, 2))(x)
x = Dropout(0.25)(x)
conv_out = Flatten()(x)
x1 = Dense(nb_classes, activation='softmax')(conv_out)
lst = [x1]
#model = Model(input=model_input, output=lst)
model = Model(input=model_input, output=lst) #I learned you can't use a sequential model for this type of prediction
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, callbacks=[history],verbose=1)
在 运行 模型之前,我这样做了:
print('X_train shape:', X_train.shape)
print('y_train shape:', X_train.shape)
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')
给出了这个输出:
X_train shape: (784, 1, 100, 100, 3)
y_train shape: (784, 1, 100, 100, 3)
784 train samples
196 test samples
然后我这样重塑:
X_test = X_test.reshape(X_test.shape[0], 100,100, 3)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255 #normalize our data values to the range [0, 1], 255 is the max value of X_train.max()
X_test /= 255
然后我得到这个形状 X_train
(784, 100, 100, 3)
数据是来自 kaggle 的水果彩色图片:https://www.kaggle.com/moltean/fruits
我猜是批量大小冲突。就您共享的代码片段而言,它看起来应该可以正常工作。
但是 X_train.shape
给我们的输出元组是 (784, 100, 100, 3)。在这种情况下,我会将输入初始化为如下所示:
model_input = Input(shape=(X_train.shape[0], img_rows, img_cols,img_channels))
注意:如果你想做可变批量大小初始化输入 None 并在训练时动态设置值。干杯!
我是个白痴。错误是因为我没有对 y_train 进行编码。我以为我已经有但我没有。感谢所有提供帮助的人!
y_train=np_utils.to_categorical(y_train, n_class)
y_test=np_utils.to_categorical(y_test, n_class)
我已经有一段时间没有做任何神经网络了。我正在编写一个简单的示例作为热身,我很困惑为什么我的张量形状不正确。我认为模型符合预期(样本、高度、宽度、通道)。
当我打电话给
X_train.shape
我得到 (784, 100, 100, 3)
但是我得到一个错误:ValueError:检查目标时出错:预期 dense_10 具有形状 (2,) 但得到形状为 (1,)
的数组这是下面的简单模型。我哪里错了?我需要转换成灰度吗?
#THE MODEL#
batch_size = 32
nb_classes = 2
nb_epoch = 2
img_rows =100
img_cols=100
img_channels = 3
model_input=Input(shape=(img_rows, img_cols,img_channels))
x = Convolution2D(32, 3, 3, border_mode='same')(model_input)
x = Activation('relu')(x)
x = Convolution2D(32, 3, 3)(x)
x = Activation('relu')(x)
x = MaxPooling2D(pool_size=(2, 2))(x)
x = Dropout(0.25)(x)
conv_out = Flatten()(x)
x1 = Dense(nb_classes, activation='softmax')(conv_out)
lst = [x1]
#model = Model(input=model_input, output=lst)
model = Model(input=model_input, output=lst) #I learned you can't use a sequential model for this type of prediction
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model.fit(X_train, y_train, batch_size=batch_size, nb_epoch=nb_epoch, callbacks=[history],verbose=1)
在 运行 模型之前,我这样做了:
print('X_train shape:', X_train.shape)
print('y_train shape:', X_train.shape)
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')
给出了这个输出:
X_train shape: (784, 1, 100, 100, 3)
y_train shape: (784, 1, 100, 100, 3)
784 train samples
196 test samples
然后我这样重塑:
X_test = X_test.reshape(X_test.shape[0], 100,100, 3)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255 #normalize our data values to the range [0, 1], 255 is the max value of X_train.max()
X_test /= 255
然后我得到这个形状 X_train
(784, 100, 100, 3)
数据是来自 kaggle 的水果彩色图片:https://www.kaggle.com/moltean/fruits
我猜是批量大小冲突。就您共享的代码片段而言,它看起来应该可以正常工作。
但是 X_train.shape
给我们的输出元组是 (784, 100, 100, 3)。在这种情况下,我会将输入初始化为如下所示:
model_input = Input(shape=(X_train.shape[0], img_rows, img_cols,img_channels))
注意:如果你想做可变批量大小初始化输入 None 并在训练时动态设置值。干杯!
我是个白痴。错误是因为我没有对 y_train 进行编码。我以为我已经有但我没有。感谢所有提供帮助的人!
y_train=np_utils.to_categorical(y_train, n_class)
y_test=np_utils.to_categorical(y_test, n_class)