我正在尝试模拟与theano后端的SeparableConvolution2D层相当的东西(它已经存在于TensorFlow后端)。作为第一步我需要做的是将一个通道从张量传递到下一层。所以说我有一个名为conv1的2D卷积层,有16个过滤器,它产生一个形状为的输出:(batch_size,16,height,width)我需要选择形状为(:,0,:,:)的subtensor并将其传递给下一层。很简单吧?
这是我的代码:
from keras import backend as K
image_input = Input(batch_shape = (batch_size, 1, height, width ), name = 'image_input' )
conv1 = Convolution2D(16, 3, 3, name='conv1', activation = 'relu')(image_input)
conv2_input = K.reshape(conv1[:,0,:,:] , (batch_size, 1, height, width))
conv2 = Convolution2D(16, 3, 3, name='conv1', activation = 'relu')(conv2_input)
这引发:
Exception: You tried to call layer "conv1". This layer has no information about its expected input shape, and thus cannot be built. You can build it manually via: layer.build(batch_input_shape)
为什么图层没有所需的形状信息?我正在使用theano后端的重塑。这是将各个频道传递到下一层的正确方法吗?
答案 0 :(得分:1)
我在keras-user组问了这个问题,我在那里得到了答案:
https://groups.google.com/forum/#!topic/keras-users/bbQ5CbVXT1E
引用它:
您需要使用lambda图层,例如:Lambda(x:x [:,0:1,:,],output_shape = lambda x:(x [0],1,x [2],x [ 3]))
请注意,这种可分离卷积的手动实现效率非常低。正确的解决方案是使用TensorFlow后端。