使用“平底"或“重塑"在keras中获得未知输入形状的一维输出 [英] Use "Flatten" or "Reshape" to get 1D output of unknown input shape in keras

查看:63
本文介绍了使用“平底"或“重塑"在keras中获得未知输入形状的一维输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to use the keras layer Flatten() or Reshape((-1,)) at the end of my model to output an 1D vector like [0,0,1,0,0, ... ,0,0,1,0].

由于我的输入形状未知,这是一个问题,它是:
input_shape=(4, None, 1))).

Sadly there is an problem because of my unknown input shape which is:
input_shape=(4, None, 1))).

因此,通常输入形状在[batch_size, 4, 64, 1][batch_size, 4, 256, 1]之间,输出应为 batch_size x未知尺寸(对于上面的第一个例子:[batch_size, 64]和第二个[batch_size, 256] ).

So typically the input shape is something between [batch_size, 4, 64, 1] and [batch_size, 4, 256, 1] the output should be batch_size x unknown dimension (for the fist example above: [batch_size, 64] and for the secound [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')

使我当前的输出形状为 [批量大小,1,未知尺寸,1] . 这不允许我使用class_weights例如"ValueError: class_weight not supported for 3+ dimensional targets.".

So that my current output shape is [batchsize, 1, unknown dimension, 1]. Which does not allow me to use class_weights for example "ValueError: class_weight not supported for 3+ dimensional targets.".

当我使用灵活的输入形状时,是否可以使用Flatten()Reshape((1,))之类的东西将我的3维输出以keras(带有张量流后端的2.0.4)弄平?

Is it possible to use something like Flatten() or Reshape((1,)) to flatt my 3 dimensional output in keras (2.0.4 with tensorflow backend) when I use a flexible input shape?

非常感谢!

推荐答案

您可以尝试将K.batch_flatten()包裹在Lambda层中. K.batch_flatten()的输出形状是在运行时动态确定的.

You can try K.batch_flatten() wrapped in a Lambda layer. The output shape of K.batch_flatten() is dynamically determined at runtime.

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)

这篇关于使用“平底"或“重塑"在keras中获得未知输入形状的一维输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆