如何解决此错误:我想填充张量时无法调用'Tensor'对象?

时间:2019-03-20 16:19:31

标签: python tensorflow keras

当我想用另一个张量的值填充张量时,会产生此错误:

  

回溯(最近通话最近一次):

     

文件“”,第80行,在       wtm_Fill = wfill(temp(0,0))

     

TypeError:“张量”对象不可调用

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])

我需要这样的东西: wtm =

0 1 1
1 1 0
0 0 1

wtm(0,0)= 0,所以我想产生这个  形状为(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()

这是我的新代码,但是在实现后会产生此错误:

  

回溯(最近通话最近一次):

     

文件“”,第106行,在       型号=模型(输入= [图像,WTM],输出=解码)

     

文件   “ D:\ software \ Anaconda3 \ envs \ py36 \ lib \ site-packages \ keras \ legacy \ interfaces.py”,   第91行,在包装器中       返回func(* args,** kwargs)

     

文件   “ D:\ software \ Anaconda3 \ envs \ py36 \ lib \ site-packages \ keras \ engine \ network.py”,   第93行,初始化       self._init_graph_network(* args,** kwargs)

     

文件   “ D:\ software \ Anaconda3 \ envs \ py36 \ lib \ site-packages \ keras \ engine \ network.py”,   _init_graph_network中的第231行       self.inputs,self.outputs)

     

文件   “ D:\ software \ Anaconda3 \ envs \ py36 \ lib \ site-packages \ keras \ engine \ network.py”,   _map_graph_network中的第1366行       tensor_index = tensor_index)

     

文件   “ D:\ software \ Anaconda3 \ envs \ py36 \ lib \ site-packages \ keras \ engine \ network.py”,   第1353行,在build_map中       node_index,张量索引)

     

文件   “ D:\ software \ Anaconda3 \ envs \ py36 \ lib \ site-packages \ keras \ engine \ network.py”,   第1353行,在build_map中       node_index,张量索引)

     

文件   “ D:\ software \ Anaconda3 \ envs \ py36 \ lib \ site-packages \ keras \ engine \ network.py”,   第1353行,在build_map中       node_index,张量索引)

     

文件   “ D:\ software \ Anaconda3 \ envs \ py36 \ lib \ site-packages \ keras \ engine \ network.py”,   第1353行,在build_map中       node_index,张量索引)

     

文件   “ D:\ software \ Anaconda3 \ envs \ py36 \ lib \ site-packages \ keras \ engine \ network.py”,   第1353行,在build_map中       node_index,张量索引)

     

文件   “ D:\ software \ Anaconda3 \ envs \ py36 \ lib \ site-packages \ keras \ engine \ network.py”,   第1353行,在build_map中       node_index,张量索引)

     

文件   “ D:\ software \ Anaconda3 \ envs \ py36 \ lib \ site-packages \ keras \ engine \ network.py”,   第1353行,在build_map中       node_index,张量索引)

     

文件   “ D:\ software \ Anaconda3 \ envs \ py36 \ lib \ site-packages \ keras \ engine \ network.py”,   第1325行,在build_map中       节点= layer._inbound_nodes [node_index]

     

AttributeError:“ NoneType”对象没有属性“ _inbound_nodes”

1 个答案:

答案 0 :(得分:0)

要产生这样的张量,您需要: 假设张量形状为(28,28,1)

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

这将输出填充值

的形状为(10,10,1)的张量