我只是想在Keras模型中实现一些可训练的参数。在Pytorch中,我们可以使用torch.nn.Parameter()如下所示:
self.a = nn.Parameter(torch.ones(8))
self.b = nn.Parameter(torch.zeros(16,8))
我认为通过在pytorch中执行此操作,可以在模型中添加一些可训练的参数。现在我想知道如何在keras中实现类似的操作? 欢迎任何建议!
THX! :)
p.s。我只是在Keras中编写了一个自定义图层,如下所示:
class Mylayer(Layer):
def __init__(self,input_dim,output_dim,**kwargs):
self.input_dim = input_dim
self.output_dim = output_dim
super(Mylayer,self).__init__(**kwargs)
def build(self):
self.kernel = self.add_weight(name='pi',
shape=(self.input_dim,self.output_dim),
initializer='zeros',
trainable=True)
self.kernel_2 = self.add_weight(name='mean',
shape=(self.input_dim,self.output_dim),
initializer='ones',
trainable=True)
super(Mylayer,self).build()
def call(self,x):
return x,self.kernel,self.kernel_2
我想知道是否没有更改通过该层的张量,是否应该编写函数def compute_output_shape()
?
答案 0 :(得分:0)
您需要在自定义图层中创建可训练的权重:
class MyLayer(Layer):
def __init__(self, my_args, **kwargs):
#do whatever you need with my_args
super(MyLayer, self).__init__(**kwargs)
#you create the weights in build:
def build(self, input_shape):
#use the input_shape to infer the necessary shapes for weights
#use self.whatever_you_registered_in_init to help you, like units, etc.
self.kernel = self.add_weight(name='kernel',
shape=the_shape_you_calculated,
initializer='uniform',
trainable=True)
#create as many weights as necessary for this layer
#build the layer - equivalent to self.built=True
super(MyLayer, self).build(input_shape)
#create the layer operation here
def call(self, inputs):
#do whatever operations are needed
#example:
return inputs * self.kernel #make sure the shapes are compatible
#tell keras about the output shape of your layer
def compute_output_shape(self, input_shape):
#calculate the output shape based on the input shape and your layer's rules
return calculated_output_shape
现在在模型中使用图层。
如果在tensorflow上使用急切执行并创建自定义训练循环,则可以使用与PyTorch几乎相同的方式进行操作,并且可以使用tf.Variable
在图层外部创建权重,并将其作为参数传递梯度计算方法。