6

我想使用 keras 层Flatten()Reshape((-1,))在模型的末尾输出一维向量,例如[0,0,1,0,0, ... ,0,0,1,0].

可悲的是,由于我未知的输入形状存在问题,即:
input_shape=(4, None, 1))).

因此,通常输入形状介于两者之间[batch_size, 4, 64, 1][batch_size, 4, 256, 1]输出应该是batch_size x 未知维度(对于上面的第一个示例:[batch_size, 64]和第二个[batch_size, 256])。

我的模型看起来像:

model = Sequential()
model.add(Convolution2D(32, (4, 32), padding='same', input_shape=(4, None, 1)))
model.add(BatchNormalization())
model.add(LeakyReLU())
model.add(Convolution2D(1, (1, 2), strides=(4, 1), padding='same'))
model.add(Activation('sigmoid'))
# model.add(Reshape((-1,))) produces the error
# int() argument must be a string, a bytes-like object or a number, not 'NoneType' 
model.compile(loss='binary_crossentropy', optimizer='adadelta')

所以我当前的输出形状是[batchsize, 1, unknown dimension, 1]。例如,这不允许我使用 class_weights "ValueError: class_weight not supported for 3+ dimensional targets."

当我使用灵活的输入形状时,是否可以在 keras(带有 tensorflow 后端的 2.0.4)中使用类似Flatten()或扁平化我的 3 维输出?Reshape((1,))

非常感谢!

4

1 回答 1

9

你可以试试K.batch_flatten()Lambda一层。的输出形状K.batch_flatten()在运行时动态确定。

model.add(Lambda(lambda x: K.batch_flatten(x)))
model.summary()

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_5 (Conv2D)            (None, 4, None, 32)       4128      
_________________________________________________________________
batch_normalization_3 (Batch (None, 4, None, 32)       128       
_________________________________________________________________
leaky_re_lu_3 (LeakyReLU)    (None, 4, None, 32)       0         
_________________________________________________________________
conv2d_6 (Conv2D)            (None, 1, None, 1)        65        
_________________________________________________________________
activation_3 (Activation)    (None, 1, None, 1)        0         
_________________________________________________________________
lambda_5 (Lambda)            (None, None)              0         
=================================================================
Total params: 4,321
Trainable params: 4,257
Non-trainable params: 64
_________________________________________________________________


X = np.random.rand(32, 4, 256, 1)
print(model.predict(X).shape)
(32, 256)

X = np.random.rand(32, 4, 64, 1)
print(model.predict(X).shape)
(32, 64)
于 2017-11-28T17:04:06.797 回答