当定义我自己的keras层时会出现一个无张量的对象

时间:2017-08-15 17:00:45

标签: tensorflow keras

这里是我自己的图层代码,模型可以编译和预测很好,但是当我使用模型方法model.fit(x,y)时,它会出现关于无张量误差的错误,我找不到原因

class CenterPointClassifierLayer(Layer):
    def __init__(self, c, **kwargs):
        self.c = c
        super(CenterPointClassifierLayer, self).__init__(**kwargs)

    def build(self, input_shape):
        # check input_shape
        if len(input_shape) != 2:
            raise 'input should be in 1 dimension'
            self.kernel = self.add_weight(name='kernel',
                          shape=(self.c, input_shape[1]),
                          initializer='uniform',
                          trainable=True)
        self.one = K.constant(np.ones((self.c, 1)))
        super(CenterPointClassifierLayer, self).build(input_shape)

    def call(self, x):
        def elem_op(pre, x_input):
            x_shape = K.int_shape(x_input)
            e = K.reshape(x_input, (1, x_shape[0]))
            _x = K.dot(self.one, e)
            del_x = K.square(tf.subtract(self.kernel, _x))
            distance = K.sum(del_x, axis=1)
            _c = K.argmin(distance)
            _class = K.one_hot(_c, self.c)
            return _class
        y_pred = tf.scan(elem_op, x, initializer=K.one_hot(1, self.c))
        return y_pred

    def compute_output_shape(self, input_shape):
        out_shape = (input_shape[0], self.c)
        return out_shape

这是使用fit方法时得到的错误:

文件“\ tensorflow \ python \ ops \ math_ops.py”,第412行,方形     return gen_math_ops.square(x,name = name)

文件“\ tensorflow \ python \ ops \ gen_math_ops.py”,第2585行,方块     result = _op_def_lib.apply_op(“Square”,x = x,name = name)

文件“\ tensorflow \ python \ framework \ op_def_library.py”,第509行,在apply_op中     (input_name,err))

ValueError:尝试将'x'转换为张量并失败。错误:不支持任何值。

请帮助,我怎样才能在适合时修复错误! 我不知道x来自哪里,得到了无值,以及为什么在tf中有一个正方形运算

0 个答案:

没有答案
相关问题