0
votes

when I want to fill a tensor with the value of another tensor it produces this error:

Traceback (most recent call last):

File "", line 80, in wtm_Fill=wfill(temp(0,0))

TypeError: 'Tensor' object is not callable

wtm=Input((28,28,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)
#conv3 = Conv2D(8, (3, 3), activation='relu', padding='same', name='convl3e', kernel_initializer='Orthogonal',bias_initializer='glorot_uniform')(conv2)
BN=BatchNormalization()(conv3)
#DrO1=Dropout(0.25,name='Dro1')(BN)
encoded =  Conv2D(1, (5, 5), activation='relu', padding='same',name='encoded_I',dilation_rate=(2,2))(BN)

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

temp=tf.reshape(wtm,(28,28))
wfill=Kr.layers.Lambda(lambda x:tf.fill([28,28],x))
wtm_Fill=wfill(temp(0,0))
add_const = Kr.layers.Lambda(lambda x: x[0] + x[1])
encoded_merged = add_const([encoded,wtm])

I need sth like this: wtm=

0 1 1
1 1 0
0 0 1

wtm(0,0)=0 so I want to produce this new tensor with shape(28,28,1)

0 0 0 ... 0
.
. 0 0 ... 0
0 0 0 ... 0

from keras.layers import Input, Concatenate, GaussianNoise,Dropout,BatchNormalization
from keras.layers import Conv2D, AtrousConv2D
from keras.models import Model
from keras.datasets import mnist
from keras.callbacks import TensorBoard
from keras import backend as K
from keras import layers
import matplotlib.pyplot as plt
import tensorflow as tf
import keras as Kr
from keras.optimizers import SGD,RMSprop,Adam
from keras.callbacks import ReduceLROnPlateau
from keras.callbacks import EarlyStopping
from keras.callbacks import ModelCheckpoint
import numpy as np
import pylab as pl
import matplotlib.cm as cm
import keract
from matplotlib import pyplot
from keras import optimizers
from keras import regularizers

from tensorflow.python.keras.layers import Lambda;
#-----------------building w train---------------------------------------------
w_expand=np.zeros((49999,28,28),dtype='float32')
wv_expand=np.zeros((9999,28,28),dtype='float32')
wt_random=np.random.randint(2, size=(49999,4,4))
wt_random=wt_random.astype(np.float32)
wv_random=np.random.randint(2, size=(9999,4,4))
wv_random=wv_random.astype(np.float32)
w_expand[:,:4,:4]=wt_random
wv_expand[:,:4,:4]=wv_random
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)
wt_expand=np.zeros((1,28,28),dtype='float32')
wt_expand[:,0:4,0:4]=w_test
wt_expand=wt_expand.reshape((1,28,28,1))

#-----------------------encoder------------------------------------------------
#------------------------------------------------------------------------------
wtm=Input((28,28,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',dilation_rate=(2,2))(BN)


temp=tf.reshape(wtm,(28,28))
wfill=Kr.layers.Lambda(lambda x:tf.fill([28,28],x))
wtm_Fill=wfill(temp[0,0])
add_const = Kr.layers.Lambda(lambda x: x[0] + x[1])
encoded_merged = add_const([encoded,wtm_Fill])

#wfill=Kr.layers.Lambda(lambda x:tf.fill([28,28],x))
#value=wtm[0][0][0]
#x=tf.fill((28,28,1),value)
#add_const = Kr.layers.Lambda(lambda x: x[0] + x[1])
#encoded_merged = add_const([encoded,x])
#encoder=Model(inputs=[image,wtm], outputs= encoded_merged ,name='encoder')
#encoder.summary()

#-----------------------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()(deconv3)

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

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

decoded_noise = GaussianNoise(0.5)(decoded)

#----------------------w extraction------------------------------------
convw1 = Conv2D(64, (3,3), activation='relu', padding='same', name='conl1w',dilation_rate=(2,2))(decoded_noise)
convw2 = Conv2D(64, (3, 3), activation='relu', padding='same', name='convl2w',dilation_rate=(2,2))(convw1)
convw3 = Conv2D(64, (3, 3), activation='relu', padding='same', name='conl3w',dilation_rate=(2,2))(convw2)
convw4 = Conv2D(64, (3, 3), activation='relu', padding='same', name='conl4w',dilation_rate=(2,2))(convw3)
convw5 = Conv2D(64, (3, 3), activation='relu', padding='same', name='conl5w',dilation_rate=(2,2))(convw4)
convw6 = Conv2D(64, (3, 3), activation='relu', padding='same', name='conl6w',dilation_rate=(2,2))(convw5)
pred_w = Conv2D(1, (1, 1), activation='sigmoid', padding='same', name='reconstructed_W',dilation_rate=(2,2))(convw6)  
w_extraction=Model(inputs=[image,wtm],outputs=[decoded,pred_w])

w_extraction.summary()

this is my new code but after implementation it produces this error:

Traceback (most recent call last):

File "", line 106, in model=Model(inputs=[image,wtm],outputs=decoded)

File "D:\software\Anaconda3\envs\py36\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper return func(*args, **kwargs)

File "D:\software\Anaconda3\envs\py36\lib\site-packages\keras\engine\network.py", line 93, in init self._init_graph_network(*args, **kwargs)

File "D:\software\Anaconda3\envs\py36\lib\site-packages\keras\engine\network.py", line 231, in _init_graph_network self.inputs, self.outputs)

File "D:\software\Anaconda3\envs\py36\lib\site-packages\keras\engine\network.py", line 1366, in _map_graph_network tensor_index=tensor_index)

File "D:\software\Anaconda3\envs\py36\lib\site-packages\keras\engine\network.py", line 1353, in build_map node_index, tensor_index)

File "D:\software\Anaconda3\envs\py36\lib\site-packages\keras\engine\network.py", line 1353, in build_map node_index, tensor_index)

File "D:\software\Anaconda3\envs\py36\lib\site-packages\keras\engine\network.py", line 1353, in build_map node_index, tensor_index)

File "D:\software\Anaconda3\envs\py36\lib\site-packages\keras\engine\network.py", line 1353, in build_map node_index, tensor_index)

File "D:\software\Anaconda3\envs\py36\lib\site-packages\keras\engine\network.py", line 1353, in build_map node_index, tensor_index)

File "D:\software\Anaconda3\envs\py36\lib\site-packages\keras\engine\network.py", line 1353, in build_map node_index, tensor_index)

File "D:\software\Anaconda3\envs\py36\lib\site-packages\keras\engine\network.py", line 1353, in build_map node_index, tensor_index)

File "D:\software\Anaconda3\envs\py36\lib\site-packages\keras\engine\network.py", line 1325, in build_map node = layer._inbound_nodes[node_index]

AttributeError: 'NoneType' object has no attribute '_inbound_nodes'

1
I think you just need to change wtm_Fill=wfill(temp(0,0)) to wtm_Fill=wfill(temp[0,0]). Note square brackets ([]) must be used to index a tensor.jdehesa
I do this, but it produces this error, I put above.david

1 Answers

0
votes

To produce such tensor you need to: Suppose tensor shape is (28,28,1)

value = tensor[0][0][0]
wtm = tf.fill((10,10,1), value)

This will output tensor of shape (10,10,1) filled with value