how do I access to each elements in a tensor and producing a new tensor with that element's value?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-11 16:41:42

问题


I need to access each value in a tensor and then produce a new tensor with the extracted value in the first tensor. I received a suggestion in this post: how do I solve this error : AttributeError: 'NoneType' object has no attribute '_inbound_nodes'?1

and the proposed code is :

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

a=rep(Kr.layers.Reshape([1,1,1])(a_1))

but when I feed the network with a test wtm in test phase and then check the output of each layer, I find out it produces zero all the time while wtm contains 0 and 1. now, I need your explanation about this issue. suppose

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

due to the above code, I expect a_1 will be 1 and a be a tensor with shape (1,28,28,1) and value 1, but it is a tensor with value 0. could you please tell me what is a problem? do I perform sth wrong? this is the full code:

wt_random=np.random.randint(2, size=(49999,4,4))
w_expand=wt_random.astype(np.float32)
wv_random=np.random.randint(2, size=(9999,4,4))
wv_expand=wv_random.astype(np.float32)
x,y,z=w_expand.shape
w_expand=w_expand.reshape((x,y,z,1))
x,y,z=wv_expand.shape
wv_expand=wv_expand.reshape((x,y,z,1))

#-----------------building w test---------------------------------------------
w_test = np.random.randint(2,size=(1,4,4))
w_test=w_test.astype(np.float32)
w_test=w_test.reshape((1,4,4,1))


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

#-----------------------adding w---------------------------------------

    rep=Kr.layers.Lambda(lambda x:Kr.backend.tile(x,[1, 28, 28, 1]), name='replayer')
    a_1 = Kr.layers.Lambda(lambda x: x[:, 1, 1, :])(wtm)

    a=rep(Kr.layers.Reshape([1,1,1])(a_1))
    encoded_merged=wpad([encoded,a])

#-----------------------decoder------------------------------------------------
#------------------------------------------------------------------------------
deconv1 = Conv2D(64, (5, 5), activation='relu', padding='same', name='convl1d',dilation_rate=(2,2))(encoded_merged)
deconv2 = Conv2D(64, (5, 5), activation='relu', padding='same', name='convl2d',dilation_rate=(2,2))(deconv1)
deconv3 = Conv2D(64, (5, 5), activation='relu',padding='same', name='convl3d',dilation_rate=(2,2))(deconv2)
deconv4 = Conv2D(64, (5, 5), activation='relu',padding='same', name='convl4d',dilation_rate=(2,2))(deconv3)
BNd=BatchNormalization()(deconv4)

decoded = Conv2D(1, (5, 5), activation='sigmoid', padding='same', name='decoder_output')(BNd) 

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
convw3 = Conv2D(64, (5,5), activation='relu' ,name='conl3w')(convw2)#16
convw4 = Conv2D(64, (5,5), activation='relu' ,name='conl4w')(convw3)#12
convw5 = Conv2D(64, (5,5), activation='relu', name='conl5w')(convw4)#8
convw6 = Conv2D(64, (5,5), activation='relu', name='conl6w')(convw5)#4
BNed=BatchNormalization()(convw6)
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])

after training I save the model and then load it with this:

modl=load_model('Desktop/model.h5')
layer_name = 'replayer'
intermediate_layer_model = Model(inputs=modl.input,outputs=modl.get_layer(layer_name).output)
intermediate_output = intermediate_layer_model.predict([x_test[8000:8001],w_test])
fig = plt.figure(figsize=(20, 20))

rows = 8
columns = 8

first = intermediate_output

for i in range(1, columns*rows +1):
    img = intermediate_output[0,:,:,i-1]
    fig.add_subplot(rows, columns, i)
    plt.imshow(img, interpolation='nearest',cmap='gray')
    plt.axis('off')
plt.show()

but it shows 0 I change the index but the output is the same. could you please tell me why does it happen? the code works wrong or it is my mistake? Thank you.

来源:https://stackoverflow.com/questions/55403317/how-do-i-access-to-each-elements-in-a-tensor-and-producing-a-new-tensor-with-tha

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!