Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Tensorflow 高级API中的Tesorflow自定义层:抛出对象没有属性'_需要"掩码"参数';错误_Tensorflow_Layer - Fatal编程技术网

Tensorflow 高级API中的Tesorflow自定义层:抛出对象没有属性'_需要"掩码"参数';错误

Tensorflow 高级API中的Tesorflow自定义层:抛出对象没有属性'_需要"掩码"参数';错误,tensorflow,layer,Tensorflow,Layer,我正在尝试根据前几层的三个输入重建图像:正常(无,128128,3)、反照率(无,128128,3)和照明(27)。但是这里的代码仍然表示对象没有属性“\u expects\u mask\u arg”错误。我在这里介绍了我的代码,其中我使用Tensorflow v2 beta和高级API实现了一个自定义层 输入数学 类重建层(tf.keras.layers.Layer): def\uuuuu init\uuuuuuuuuuuuuuuuuuuu(自,输入形状): 超级(重建层,自我)。\uuuuu

我正在尝试根据前几层的三个输入重建图像:正常(无,128128,3)、反照率(无,128128,3)和照明(27)。但是这里的代码仍然表示对象没有属性“\u expects\u mask\u arg”错误。我在这里介绍了我的代码,其中我使用Tensorflow v2 beta和高级API实现了一个自定义层

输入数学 类重建层(tf.keras.layers.Layer):

def\uuuuu init\uuuuuuuuuuuuuuuuuuuu(自,输入形状):
超级(重建层,自我)。\uuuuu初始化
#self.num\u输出=num\u输出
#self.pixel=np.zeros((9),dtype=int)
self.sphar=np.zeros((9),dtype=float)
self.y=np.zero((9),dtype=float)
自重构\u img=np.zero((128128,3),dtype=float)
#self.y=tf.zero([128128,9])
self.normal_light=np.zero((128128,9),dtype=float)
self.y_temp=np.zero((9),dtype=float)
w_init=tf.random_normal_初始值设定项()
self.r\u img=tf.Variable(初始值=w\u init(shape=input\u shape),dtype='float32',trainable=True)
def构建(自我,输入_形状):
超级(MyLayer,self).构建(输入形状)
def调用(自,输入层):
self.normal,self.albedo,self.light=输入层
对于范围内的i(128):
对于范围(128)内的j:
#self.y=球面_谐波_计算(自法线(i,j))
self.pixel=self.normal[i,j,:]
#自正常光(i,j)=自正常光
self.sphar[0]=(1/((4*math.pi)**0.5))
self.sphar[1]=((3/(4*math.pi))**0.5)*self.pixel[2]
self.sphar[3]=((3/(4*math.pi))**0.5)*self.pixel[1])
self.sphar[4]=((1/2)*((5/(4*math.pi))**0.5)*(3*(self.pixel[2]**2)-1))
self.sphar[5]=(3*((5/(12*math.pi))**0.5)*self.pixel[2]*self.pixel[0])
self.sphar[6]=(3*((5/(12*math.pi))**0.5)*self.pixel[2]*self.pixel[1])
self.sphar[7]=((3/2)*((5/(12*math.pi))**0.5)*((self.pixel[0]**2)-(self.pixel[1]**2)))
self.sphar[8]=(3*((5/(12*math.pi))**0.5)*self.pixel[0]*self.pixel[1])
self.normal_light[i,j:]=self.sphar
对于范围(128)内的j:
对于范围(128)内的k:
对于范围(3)中的i:
自重构光[j,k,i]=自反照率[j,k,i]*tf.tensordot(自正常光[j,k],自光[i*9:(i+1)*9],轴=1)
自重构\u img=tf.convert\u to\u张量(自重构\u img)
self.r\u img=self.r\u img
返回self.r\u img
"""
---------------------------------------------------------------------------
AttributeError回溯(最近一次呼叫上次)
在里面
1作为np导入numpy
---->2 x=重建_层((128128,3))(d)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base\u layer.py in\uuuuu调用(self,input,*args,**kwargs)
明确优先。
581输入掩码=自身。收集输入掩码(输入、args、kwargs)
-->582如果(self._需要_mask_arg,而输入_mask不是None和
583非自身。_call_arg_通过('mask',args,kwargs)):
584 kwargs['mask']=输入屏蔽
AttributeError:“重建\u层”对象没有属性“\u需要\u掩码\u参数”
"""

我刚刚遇到了同样的错误,这是因为我忘记在
super()
之后调用
\uuu init\uuu()
。你做到了,但这让我觉得这个错误是由于你所派生的基础层初始化错误造成的。
我注意到,在doc示例中,不需要在基础层上调用
build()
,如果删除该函数,它对我有效(因为它与您的层无关).

即使我删除了构建函数的定义,错误仍然存在。但当我尝试在colab中运行代码时,出现了另一个错误AttributeError:“Reconstruction\u Layer”对象没有属性“\u trainable”。我在这里附上了colab链接。任何有链接的人都可以编辑。请随意编辑代码,尝试从开始实现一个最小的工作示例。所有这些代码都是误导性的。我尝试在init中重新启用您禁用的super(),然后调用函数出错,这意味着至少应该通过构建阶段。所以,正如我前面所说的,问题应该在层uuu init_uuu()中。
def __init__(self,input_shape ):
    super(Reconstruction_Layer, self).__init__()
    #self.num_outputs = num_outputs
    #self.pixel=np.zeros((9),dtype=int)
    self.sphar=np.zeros((9),dtype=float)
    self.y=np.zeros((9),dtype=float)
    self.reconstructed_img=np.zeros((128,128,3),dtype=float)
    #self.y=tf.zeros([128,128,9])
    self.normal_light=np.zeros((128,128,9),dtype=float)
    self.y_temp=np.zeros((9),dtype=float)
    w_init = tf.random_normal_initializer()
    self.r_img = tf.Variable(initial_value=w_init(shape=input_shape),dtype='float32',trainable=True)

def build(self,input_shape):
    super(MyLayer, self).build(input_shape)

def call(self,input_layer):
    self.normal,self.albedo,self.light = input_layer

    for i in range(128):
        for j in range(128):
            #self.y=spherical_harmonic_calc(self.normal(i,j))

            self.pixel=self.normal[i,j,:]


            #self.normal_light(i,j)= self.y
            self.sphar[0]=(1/((4*math.pi)**0.5))
            self.sphar[1]=((3/(4*math.pi))**0.5)*self.pixel[2]
            self.sphar[3]=(((3/(4*math.pi))**0.5)*self.pixel[1])
            self.sphar[4]=((1/2)*((5/(4*math.pi))**0.5)*(3*(self.pixel[2]**2) - 1))
            self.sphar[5]=(3*((5/(12*math.pi))**0.5)*self.pixel[2]*self.pixel[0])
            self.sphar[6]=(3*((5/(12*math.pi))**0.5)*self.pixel[2]*self.pixel[1])
            self.sphar[7]=((3/2)*((5/(12*math.pi))**0.5)*((self.pixel[0]**2)-(self.pixel[1]**2)))
            self.sphar[8]=(3*((5/(12*math.pi))**0.5)*self.pixel[0]*self.pixel[1])

            self.normal_light[i,j,:]=self.sphar

    for j in range(128):
        for k in range(128):
            for i in range(3):

                self.reconstructed_img[j,k,i]=self.albedo[j,k,i]* tf.tensordot(self.normal_light[j,k],self.light[i*9:(i+1)*9 ],axes=1)

    self.reconstructed_img=tf.convert_to_tensor(self.reconstructed_img)
    self.r_img=self.reconstructed_img


    return self.r_img


"""
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-74-06759ef5b0b5> in <module>
      1 import numpy as np
----> 2 x=Reconstruction_Layer((128,128,3))(d)

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, inputs, *args, **kwargs)
    580     # explicitly take priority.
    581     input_masks = self._collect_input_masks(inputs, args, kwargs)
--> 582     if (self._expects_mask_arg and input_masks is not None and
    583         not self._call_arg_was_passed('mask', args, kwargs)):
    584       kwargs['mask'] = input_masks

AttributeError: 'Reconstruction_Layer' object has no attribute '_expects_mask_arg'
"""