如何访问张量中的特定值?

时间:2019-03-21 17:23:18

标签: python keras tensor

我有一个输入层wtm=Input(4,4,1),我想在学习期间访问该层的每个值。用于访问wtm[1,1](row = 1和column = 1中的值),我使用此代码a=Kr.layers.Lambda(lambda x:x[1,1])(wtm),但输出形状为TensorShape([Dimension(4), Dimension(1)])而不是(1,1),我认为它给出了第一栏,对吗?如果我在特定的行和列中仅需要一个值,该怎么办,如何更改它?我真的需要你的帮助。我知道这可能很容易,但是我是一个初学者,不知道如何解决此问题:( 编辑: 假设

wtm=
1 0 0 1
1 1 1 0
1 0 1 0
1 0 1 1

我们知道wtm(0,0)= 1,现在我想使用值为{1的shape (28,28,1)生成新的张量,并且我想对wtm中的所有值执行此操作。

 wtm=Input((4,4,1))
    image = Input((28, 28, 1))
    conv1 = Conv2D(64, (5, 5), activation='relu', padding='same', name='convl1e')(image)
    conv2 = Conv2D(64, (5, 5), activation='relu', padding='same', name='convl2e')(conv1)
    conv3 = Conv2D(64, (5, 5), activation='relu', padding='same', name='convl3e')(conv2)
    BN=BatchNormalization()(conv3)
    encoded =  Conv2D(1, (5, 5), activation='relu', padding='same',name='encoded_I')(BN)

   rep=Kr.layers.Lambda(lambda x:Kr.backend.repeat(x,28))
    a=rep(Kr.layers.Lambda(lambda x:x[1,1])(wtm))

    add_const = Kr.layers.Lambda(lambda x: x[0] + x[1])
    encoded_merged = add_const([encoded,a])


    #-----------------------decoder------------------------------------------------
    #------------------------------------------------------------------------------
    deconv1 = Conv2D(64, (5, 5), activation='elu', padding='same', name='convl1d')(encoded_merged)
    deconv2 = Conv2D(64, (5, 5), activation='elu', padding='same', name='convl2d')(deconv1)
    deconv3 = Conv2D(64, (5, 5), activation='elu',padding='same', name='convl3d')(deconv2)
    deconv4 = Conv2D(64, (5, 5), activation='elu',padding='same', name='convl4d')(deconv3)
    BNd=BatchNormalization()(deconv4)
    #DrO2=Dropout(0.25,name='DrO2')(BNd)

    decoded = Conv2D(1, (5, 5), activation='sigmoid', padding='same', name='decoder_output')(BNd) 
    #model=Model(inputs=image,outputs=decoded)

    model=Model(inputs=[image,wtm],outputs=decoded)

    decoded_noise = GaussianNoise(0.5)(decoded)

    #----------------------w extraction------------------------------------
    convw1 = Conv2D(64, (5,5), activation='relu', name='conl1w')(decoded_noise)#24
    convw2 = Conv2D(64, (5,5), activation='relu', name='convl2w')(convw1)#20
    #Avw1=AveragePooling2D(pool_size=(2,2))(convw2)
    convw3 = Conv2D(64, (5,5), activation='relu' ,name='conl3w')(convw2)#16
    convw4 = Conv2D(64, (5,5), activation='relu' ,name='conl4w')(convw3)#12
    #Avw2=AveragePooling2D(pool_size=(2,2))(convw4)
    convw5 = Conv2D(64, (5,5), activation='relu', name='conl5w')(convw4)#8
    convw6 = Conv2D(64, (5,5), activation='relu', name='conl6w')(convw5)#4
    convw7 = Conv2D(64, (5,5), activation='relu',padding='same', name='conl7w',dilation_rate=(2,2))(convw6)#4
    convw8 = Conv2D(64, (5,5), activation='relu', padding='same',name='conl8w',dilation_rate=(2,2))(convw7)#4
    convw9 = Conv2D(64, (5,5), activation='relu',padding='same', name='conl9w',dilation_rate=(2,2))(convw8)#4
    convw10 = Conv2D(64, (5,5), activation='relu',padding='same', name='conl10w',dilation_rate=(2,2))(convw9)#4
    BNed=BatchNormalization()(convw10)
    pred_w = Conv2D(1, (1, 1), activation='sigmoid', padding='same', name='reconstructed_W',dilation_rate=(2,2))(BNed)  

    w_extraction=Model(inputs=[image,wtm],outputs=[decoded,pred_w])

    w_extraction.summary()

1 个答案:

答案 0 :(得分:0)

我相信您没有考虑到第一个维度是批处理维度。

如果您运行

from keras.layers import Input, Lambda

def inspector(x):
    print(x.shape)
    return x

inp = Input((4, 4, 1))
lmb = Lambda(inspector)(inp)

您会看到它可以打印

(?, 4, 4, 1)
(?, 4, 4, 1)

表明x是四维的。