Python 自定义图层的自定义初始化
我想为我的自定义层做一个自定义初始值设定项,它只做一个矩阵乘法。 它为初始化器的初始化提供了各种错误消息Python 自定义图层的自定义初始化,python,tensorflow,keras,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):
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>
TypeError:应为二进制或unicode字符串,已获取