[英]How to access individual feature map of tensors to keras layers
我正在實現兩個不同的keras層之間的自定義連接。 神經網絡開始如下:
from tensorflow.keras.layers import Dense, Conv2D, Flatten, AveragePooling2D
import tensorflow as tf
from keras.models import Model
from tensorflow.keras.layers import Conv2D, Input, Concatenate, Lambda, Add
inputTensor = Input(shape=( 32, 32,1))
stride = 1
c1 = Conv2D(6, kernel_size=[5,5], strides=(stride,stride), padding="valid", input_shape=(32,32,1),
activation = 'tanh')(inputTensor)
s2 = AveragePooling2D(pool_size=(2, 2), strides=(2, 2))(c1)
在這里,我想將自定義連接應用於輸出大小為10 * 10 * 16的卷積層c3(即,需要在大小為14 * 14 * 6的s2上應用16個濾鏡,並獲得輸出10 * 10 * 16)。 為此,我需要使用kernal_size = 5*5
, filers=16
, stride = 1
和padding=valid
。
對於我的自定義連接,我不想一次使用s2的全部6個特征圖,而是要單獨使用它們。 我正在使用lambda
函數,如下所示:
例如,如果我想使用s2的第零個特征圖並對其應用1個過濾器,我將執行以下操作:
group0_a = Lambda(lambda x: x[:,:,:,0], output_shape=lambda x: (x[0], x[1], x[2], 1))(s2)
conv_group0_a = Conv2D(1, kernel_size=[5,5], strides=(stride,stride), padding="valid", activation = 'tanh')(group0_a)
現在,我得到一個錯誤:
Input 0 of layer conv2d_10 is incompatible with the layer: expected ndim=4, found ndim=3. Full shape received: [None, 14, 14]
Input 0 of layer conv2d_10 is incompatible with the layer: expected ndim=4, found ndim=3. Full shape received: [None, 14, 14]
group0_a
Input 0 of layer conv2d_10 is incompatible with the layer: expected ndim=4, found ndim=3. Full shape received: [None, 14, 14]
用於group0_a
好的,因此您可以訪問s2的第零個特征圖並對其應用1個過濾器,您可以按如下所示訪問它:
group0_a = Lambda(lambda x: x[:,:,:,0:1], output_shape=lambda x: (x[0], x[1], x[2], 1))(s2)
conv_group0_a = Conv2D(1, kernel_size=[5,5], strides=(stride,stride), padding="valid", activation = 'tanh')(group0_a)
我唯一做的是代替lambda
函數的x
作為x[:,:,:,0]
,我將其作為x[:,:,:,0:1]
傳遞。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.