如何为自定义keras层创建可训练的权重变量
how to create trainable weight variable for custom keras layer
我正在实现自定义平均池化层,其中每个神经元计算其输入的平均值,然后将结果乘以可学习系数并添加可学习偏差项,最后应用激活函数
from tensorflow.keras.layers import Layer
from keras import backend as K
class Average_Pooling_Layer(Layer):
def __init__(self, output_dimension, **kwargs):
super(Average_Pooling_Layer, self).__init__(**kwargs)
self.output_dimension = output_dimension
def build(self, input_shape):
self.weights = self.add_weight(name='weights2',
shape=(input_shape[0],
int(self.output_dimension[0]),
int(self.output_dimension[1]),
int(self.output_dimension[2])),
initializer='uniform',
trainable=True)
super(Average_Pooling_Layer, self).build(input_shape)
def call(self, inputs):
return K.tanh((inputs * self.weights))
def compute_output_shape(self, input_shape):
return (input_shape)
代码用法
model = tf.keras.Sequential()
stride = 1
c1 = model.add(Conv2D(6, kernel_size=[5,5], strides=(stride,stride), padding="valid", input_shape=(32,32,1),
activation = 'tanh'))
s2_before_activation = model.add(AveragePooling2D(pool_size=(2, 2), strides=(2, 2)))
s2 = model.add(Average_Pooling_Layer(output_dimension = (14, 14, 6)))
我收到错误消息,因为 "Failed to convert object of type to Tensor. Contents: (Dimension(None), 14, 14, 6). Consider casting elements to a supported type." "None" 是批量大小,我是从上一层得到的。
如何解决?
您的错误是由数据类型引起的。 input_shape[0]
returns <class 'tensorflow.python.framework.tensor_shape.Dimension'>
而不是 int
.
您可以将 input_shape[0]
替换为 tf.TensorShape(input_shape).as_list()[0]
。但是你的数据维度不对,需要根据自己的需要进行调整修改。
编辑
如果您收到错误 "can't set attribute",您应该重命名您的权重变量而不是 self.weights
。例如,更改为 self.weights_new
.
我正在实现自定义平均池化层,其中每个神经元计算其输入的平均值,然后将结果乘以可学习系数并添加可学习偏差项,最后应用激活函数
from tensorflow.keras.layers import Layer
from keras import backend as K
class Average_Pooling_Layer(Layer):
def __init__(self, output_dimension, **kwargs):
super(Average_Pooling_Layer, self).__init__(**kwargs)
self.output_dimension = output_dimension
def build(self, input_shape):
self.weights = self.add_weight(name='weights2',
shape=(input_shape[0],
int(self.output_dimension[0]),
int(self.output_dimension[1]),
int(self.output_dimension[2])),
initializer='uniform',
trainable=True)
super(Average_Pooling_Layer, self).build(input_shape)
def call(self, inputs):
return K.tanh((inputs * self.weights))
def compute_output_shape(self, input_shape):
return (input_shape)
代码用法
model = tf.keras.Sequential()
stride = 1
c1 = model.add(Conv2D(6, kernel_size=[5,5], strides=(stride,stride), padding="valid", input_shape=(32,32,1),
activation = 'tanh'))
s2_before_activation = model.add(AveragePooling2D(pool_size=(2, 2), strides=(2, 2)))
s2 = model.add(Average_Pooling_Layer(output_dimension = (14, 14, 6)))
我收到错误消息,因为 "Failed to convert object of type to Tensor. Contents: (Dimension(None), 14, 14, 6). Consider casting elements to a supported type." "None" 是批量大小,我是从上一层得到的。
如何解决?
您的错误是由数据类型引起的。 input_shape[0]
returns <class 'tensorflow.python.framework.tensor_shape.Dimension'>
而不是 int
.
您可以将 input_shape[0]
替换为 tf.TensorShape(input_shape).as_list()[0]
。但是你的数据维度不对,需要根据自己的需要进行调整修改。
编辑
如果您收到错误 "can't set attribute",您应该重命名您的权重变量而不是 self.weights
。例如,更改为 self.weights_new
.