如何从Tensorflow中layer.layers类中定义的layer子类中获取权重



我有下面的代码,在convProjection类中,我从keras定义了一个Conv1D层。我想定义Conv1D的权重。尽管它没有初始化,但使用self.Projection.get_weights()获取权重会返回一个空数组。我试着在调用中手动初始化它,但似乎不起作用。在这种情况下,我如何获得Conv1D层的权重?在操纵它们之后,我还需要将它们放回原处。

kernelSize = 32
stride = 16
class convProjections(layers.Layer):
def __init__(self, kernelSize,stride):
super(convProjections, self).__init__()
self.Projection = layers.Conv1D(filters = projection_dim,kernel_size = kernelSize,strides = stride)
def call(self, inputData):
Projections = self.Projection(inputData)
convWeights = self.Projection.get_weights()
####I will do some something here with convWeights####
return Projections
def model
inputs = layers.Input(shape=input_shape)
patches = convProjections(kernelSize,stride)(inputs)
logits = layers.Dense(8,  activation='softmax')(patches )
model = keras.Model(inputs=inputs, outputs=logits)

最好,我想坚持Model子类化来定义我的模型。但对的解决方案持开放态度

您必须正确构建convProjections。您必须在层的build(self, inputs_shape)方法中创建层权重。然后,您必须在call方法中使用这些权重来使用tf.nn.conv1d进行卷积运算。在call结束后,你可以用self.convWeights做任何你想做的事。

class convProjections(layers.Layer):
def __init__(self, projection_dim, kernelSize, stride, use_bias=True):
super(convProjections, self).__init__()
self.projection_dim = projection_dim
self.kernelSize = kernelSize
self.stride = stride
self.use_bias = use_bias
def build(self, input_shape):
self.convWeights = self.add_weight(
shape=(self.kernelSize, input_shape[-1], self.projection_dim),
initializer="random_normal",
trainable=True,
)
if self.use_bias:
self.convBias = self.add_weight(
shape=(self.projection_dim,), initializer="random_normal", trainable=True
)
def call(self, inputData):
conv = tf.nn.conv1d(
inputData, 
filters=self.convWeights, 
stride=[1,self.stride,1], 
padding='SAME',
data_format='NWC'
)
if self.use_bias:
conv = tf.nn.bias_add(conv, self.convBias)
### DO SOMETHING WITH self.convWeights ###
print(self.convWeights.shape)
return conv

构建模型:

input_shape = (24,6)
kernelSize = 32
stride = 1
projection_dim = 64
inputs = layers.Input(shape=input_shape)
patches = convProjections(projection_dim,kernelSize,stride)(inputs)
outputs = layers.Dense(8,  activation='softmax')(patches)
model = Model(inputs=inputs, outputs=outputs)

相关内容

  • 没有找到相关文章

最新更新