2016-08-23 47 views
1

我想模擬相當於theano後端(它已經存在的TensorFlow後端)的一個SeparableConvolution2D圖層。作爲第一步我需要做的是從張量傳遞一個通道到下一層。所以說我有一個名爲conv1的二維卷積圖層,帶有16個過濾器,它們生成一個形狀爲(batch_size,16,height,width)的輸出。我需要選擇形狀爲(:,0,:,:)的子擴展,並將其傳遞給下一層。夠簡單吧?將單個通道的張量傳遞給Keras中的圖層

這是我的代碼:

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後端重塑。這是將個人頻道傳遞到下一層的正確方法嗎?

回答

1

我問的keras用戶羣這個問題,我有一個答案:

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後端。

+1

您應該引用該鏈接的相關部分,以便該答案是獨立的。 –

相關問題