在 Keras 中重塑图层
Reshaping layer in Keras
我正在尝试构建一个带有输出矩阵的卷积神经网络。输入形状为 (100,100,4),输出形状为 (2,125)。
这是我当前模型的总结:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_63 (InputLayer) (None, 100, 100, 4) 0
_________________________________________________________________
conv2d_44 (Conv2D) (None, 100, 100, 25) 2525
_________________________________________________________________
max_pooling2d_38 (MaxPooling (None, 50, 50, 25) 0
_________________________________________________________________
flatten_38 (Flatten) (None, 62500) 0
_________________________________________________________________
dense_47 (Dense) (None, 10) 625010
_________________________________________________________________
dense_48 (Dense) (None, 250) 2750
_________________________________________________________________
reshape_63 (Reshape) (None, 2, 125) 0
=================================================================
Total params: 630,285
Trainable params: 630,285
Non-trainable params: 0
_________________________________________________________________
None
我认为应该没问题,但当我尝试拟合模型时出现此错误:
ValueError: Error when checking target: expected reshape_62 to have shape (2, 1) but got array with shape (2, 125)
这是我使用的代码
batch_size = 100
input_layer = Input(shape=(xs[1],xs[2],xs[3]))
conv1 = Conv2D(filters = 25, kernel_size = 5,padding="same",activation="relu", data_format = 'channels_last')(input_layer)
pool1 = MaxPooling2D(pool_size=(2,2),padding="same")(conv1)
flat = Flatten()(pool1)
hidden1 = Dense(10, activation='relu')(flat)
output_layer = Dense(ys[1]*ys[2], activation='softmax')(hidden1)
output_reshape = Reshape((2,125))(output_layer)
model = Model(inputs=input_layer, outputs=output_reshape)
print(model.summary())
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', sample_weight_mode='temporal')
model.fit(x_train,y_train,batch_size=batch_size,epochs=3)
我一直在寻找重塑层的工作原理,但我仍然无法弄清楚。非常感激任何的帮助。
发生这种情况是因为您正在使用 'sparse_categorical_crossentropy'
。
"Sparse" 表示系统不会期望整个数组,而只是热点的坐标。它不会期待常规的 (None, 2, 125)
张量,而只会期待 (None, 2, 1)
指示 125 类 中的哪一个是正确的。
要解决这个问题,要么开始使用稀疏 y_train
,要么用 'categorical_crossentropy'
替换损失。
我相信 y_train
可以用 sparse_y_train = numpy.argmax(y_train, axis=-1)
获得稀疏的 y_train
。如果此模型不会给您带来内存问题,则无需稀疏。
我正在尝试构建一个带有输出矩阵的卷积神经网络。输入形状为 (100,100,4),输出形状为 (2,125)。
这是我当前模型的总结:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_63 (InputLayer) (None, 100, 100, 4) 0
_________________________________________________________________
conv2d_44 (Conv2D) (None, 100, 100, 25) 2525
_________________________________________________________________
max_pooling2d_38 (MaxPooling (None, 50, 50, 25) 0
_________________________________________________________________
flatten_38 (Flatten) (None, 62500) 0
_________________________________________________________________
dense_47 (Dense) (None, 10) 625010
_________________________________________________________________
dense_48 (Dense) (None, 250) 2750
_________________________________________________________________
reshape_63 (Reshape) (None, 2, 125) 0
=================================================================
Total params: 630,285
Trainable params: 630,285
Non-trainable params: 0
_________________________________________________________________
None
我认为应该没问题,但当我尝试拟合模型时出现此错误:
ValueError: Error when checking target: expected reshape_62 to have shape (2, 1) but got array with shape (2, 125)
这是我使用的代码
batch_size = 100
input_layer = Input(shape=(xs[1],xs[2],xs[3]))
conv1 = Conv2D(filters = 25, kernel_size = 5,padding="same",activation="relu", data_format = 'channels_last')(input_layer)
pool1 = MaxPooling2D(pool_size=(2,2),padding="same")(conv1)
flat = Flatten()(pool1)
hidden1 = Dense(10, activation='relu')(flat)
output_layer = Dense(ys[1]*ys[2], activation='softmax')(hidden1)
output_reshape = Reshape((2,125))(output_layer)
model = Model(inputs=input_layer, outputs=output_reshape)
print(model.summary())
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', sample_weight_mode='temporal')
model.fit(x_train,y_train,batch_size=batch_size,epochs=3)
我一直在寻找重塑层的工作原理,但我仍然无法弄清楚。非常感激任何的帮助。
发生这种情况是因为您正在使用 'sparse_categorical_crossentropy'
。
"Sparse" 表示系统不会期望整个数组,而只是热点的坐标。它不会期待常规的 (None, 2, 125)
张量,而只会期待 (None, 2, 1)
指示 125 类 中的哪一个是正确的。
要解决这个问题,要么开始使用稀疏 y_train
,要么用 'categorical_crossentropy'
替换损失。
我相信 y_train
可以用 sparse_y_train = numpy.argmax(y_train, axis=-1)
获得稀疏的 y_train
。如果此模型不会给您带来内存问题,则无需稀疏。