AttributeError:'变量'对象没有属性' _keras_history'在编写自定义图层时

时间:2018-05-30 07:34:09

标签: python keras attributeerror tensor

我在构建模型时遇到如何解决此错误的问题。我尝试用lambda包装所有后端函数,但错误仍然存​​在。代码如下:

arr = np.loadtxt("file.txt")
arr = arr.astype('float32')
for ii in range(len(arr)):
    a = K.tf.convert_to_tensor(arr[ii], dtype=K.tf.float32)
    #a = Lambda( lambda x: K.tf.convert_to_tensor(x, dtype=K.tf.float32) ) ## THis line gave me an error : Layer lambda_21 was called with an input that isn't a symbolic tensor.
    basis_tensor1 = Lambda( lambda x: K.reshape(x,(8,8)) )(a)
    basis_tensor.append(basis_tensor1)

basis_tensor = Lambda( lambda x: K.tf.convert_to_tensor( x, dtype=K.tf.float32) )(basis_tensor) 


def get_2d_tensor(inputs):

    coeff = inputs[0]
    basis_tensor = inputs[1]
    muls = []
    m=0
    for r in np.arange(8) :
        for c in np.arange(8) :
            f = Lambda( lambda x: K.tf.multiply(x[0],x[1]) )([coeff[:,r,c],basis_tensor[m]])

            l = Lambda( lambda x: K.tf.reshape(x,(8,8)) )(f)
            muls.append(l)
            m = m + 1
    return muls

以下是我如何构建我的功能API:

input = Input(shape=(8,8,64,))
ConvLayer = Conv2D(512, (1,1), activation='relu')(input)
Sum1 = Lambda( lambda x: K.tf.reduce_sum(x,axis=-1) )(ConvLayer)
I = Lambda( lambda x: get_2d_tensor(x) )([Sum1,basis_tensor])
Sum = Lambda( lambda x: K.tf.reduce_sum(x,axis=0) )(I)
model = Model(inputs=[input], outputs=[Sum]) ##---> Here I get the Attribute Error!

请帮我弄清楚如何解决这个错误?

1 个答案:

答案 0 :(得分:0)

请参阅official documentation了解如何编写自定义图层