gpt4 book ai didi

python - Keras:无输入的自定义图层

转载 作者:行者123 更新时间:2023-12-04 12:44:46 25 4
gpt4 key购买 nike

我想在没有任何输入的情况下实现Keras自定义层,只是可训练的权重。

这是到目前为止的代码:

class Simple(Layer):

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

def build(self):
self.kernel = self.add_weight(name='kernel', shape=self.output_dim, initializer='uniform', trainable=True)
super(Simple, self).build()

def call(self):
return self.kernel

def compute_output_shape(self):
return self.output_dim

X = Simple((1, 784))()

我收到一条错误消息:
__call__() missing 1 required positional argument: 'inputs'
有没有在Keras中构建没有输入的自定义层的解决方法?

最佳答案

您可以执行以下操作

from tensorflow.keras.layers import Layer

class Simple(Layer):

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

def build(self, input_shapes):
self.kernel = self.add_weight(name='kernel', shape=self.output_dim, initializer='uniform', trainable=True)
super(Simple, self).build(input_shapes)

def call(self, inputs):
return self.kernel

def compute_output_shape(self):
return self.output_dim

X = Simple((1, 784))([])
print(X.shape)

哪个产生
>>> (1, 784)

关于python - Keras:无输入的自定义图层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55282481/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com