自定义层的自定义初始化

时间:2020-11-04 12:21:46

标签: python tensorflow keras

我想为我的自定义层创建一个自定义初始化程序,只需进行矩阵乘法即可。 它给了我关于初始化程序初始化的各种错误消息。

class CLayerDOT(Layer):

    def __init__(self, output_dim, **kwargs):
        self.output_dim = output_dim
        super(CLayerDOT, self).__init__(**kwargs)

    def build(self, input_shape):
        w_init = DCTInitializer()
        self.w = tf.Variable(
            initial_value=lambda:w_init(shape=(input_shape[1], input_shape[-1]), dtype="float32"),
            trainable=False
        )
        super(CLayerDOT, self).build(input_shape)

    def call(self, inputs):
        Weight= tf.unstack(np.asarray(self.w))
        for n in range(Weight.shape[1]):
            for k in range(Weight.shape[0]):
                Weight[k, n] = np.cos(np.pi/Weight.shape[1]*(n+0.5)*k)
        self.w = tf.stack(Weight)
        x = tf.matmul(self.w, inputs)
        y = tf.dtypes.cast(x, tf.float32)
        return y

    def compute_output_shape(self, input_shape):
        return (input_shape[0], self.output_dim, input_shape[-1])

    def get_config(self):                               #important for a bigger network to see config
        config = super(CLayerDOT, self).get_config()
        config.update({"output_dim": self.output_dim})
        return config

这是初始化器类:

class DCTInitializer(tf.keras.initializers.Initializer):

    # def __init__(self):

    def __call__(self, shape, dtype=None):
        # return tf.random.normal(shape, mean=self.mean, stddev=self.stddev, dtype=dtype)
        h_list = []
        b = tf.constant(1, shape= shape)
        h = np.array(b)
        for n in range(h.shape[1]):
            for k in range(h.shape[0]):
                h_list[k, n] = np.cos(np.pi/h.shape[1]*(n+0.5)*k)
        output = tf.stack(h_list)
        return output

我认为问题在于我想使用for循环初始化初始化程序的变量。

跟踪:

TypeError: Expected binary or unicode string, got <tf.Variable 'c_layer_dot_1/Custom_weight:0' shape=(80, 24) dtype=float32>

0 个答案:

没有答案