Jam*_*ugh 5 python autoencoder keras
我试图在 Keras 中创建一个自定义的 Dense 层来绑定自动编码器中的权重。我已经尝试按照在卷积层中执行此操作的示例here,但似乎某些步骤不适用于 Dense 层(而且,代码来自两年多前)。
通过绑定权重,我希望解码层使用编码层的转置权重矩阵。本文(第 5 页)也采用了这种方法。以下是文章的相关引用:
在这里,我们选择编码和解码激活函数都为 sigmoid 函数,并且只考虑绑定权重的情况,其中 W ?= W T (其中 W T 是W的转置),就像大多数现有的深度学习方法一样。
在上面的引用中,W是编码层中的权重矩阵,W'(等于W的转置)是解码层中的权重矩阵。
我在密集层没有太大变化。我tied_to在构造函数中添加了一个参数,它允许您传递要绑定到的层。唯一的其他更改是对build函数的更改,其代码段如下:
def build(self, input_shape):
assert len(input_shape) >= 2
input_dim = input_shape[-1]
if self.tied_to is not None:
self.kernel = K.transpose(self.tied_to.kernel)
self._non_trainable_weights.append(self.kernel)
else:
self.kernel = self.add_weight(shape=(input_dim, self.units),
initializer=self.kernel_initializer,
name='kernel',
regularizer=self.kernel_regularizer,
constraint=self.kernel_constraint)
if self.use_bias:
self.bias = self.add_weight(shape=(self.units,),
initializer=self.bias_initializer,
name='bias',
regularizer=self.bias_regularizer,
constraint=self.bias_constraint)
else:
self.bias = None
self.input_spec = InputSpec(min_ndim=2, axes={-1: input_dim})
self.built = True
Run Code Online (Sandbox Code Playgroud)
下面是__init__方法,这里唯一的变化是增加了tied_to参数。
def __init__(self, units,
activation=None,
use_bias=True,
kernel_initializer='glorot_uniform',
bias_initializer='zeros',
kernel_regularizer=None,
bias_regularizer=None,
activity_regularizer=None,
kernel_constraint=None,
bias_constraint=None,
tied_to=None,
**kwargs):
if 'input_shape' not in kwargs and 'input_dim' in kwargs:
kwargs['input_shape'] = (kwargs.pop('input_dim'),)
super(Dense, self).__init__(**kwargs)
self.units = units
self.activation = activations.get(activation)
self.use_bias = use_bias
self.kernel_initializer = initializers.get(kernel_initializer)
self.bias_initializer = initializers.get(bias_initializer)
self.kernel_regularizer = regularizers.get(kernel_regularizer)
self.bias_regularizer = regularizers.get(bias_regularizer)
self.activity_regularizer = regularizers.get(activity_regularizer)
self.kernel_constraint = constraints.get(kernel_constraint)
self.bias_constraint = constraints.get(bias_constraint)
self.input_spec = InputSpec(min_ndim=2)
self.supports_masking = True
self.tied_to = tied_to
Run Code Online (Sandbox Code Playgroud)
该call函数没有被编辑,但它在下面以供参考。
def call(self, inputs):
output = K.dot(inputs, self.kernel)
if self.use_bias:
output = K.bias_add(output, self.bias, data_format='channels_last')
if self.activation is not None:
output = self.activation(output)
return output
Run Code Online (Sandbox Code Playgroud)
上面,我添加了一个条件来检查tied_to参数是否设置,如果设置,则将图层的内核设置为图层内核的转置tied_to。
下面是用于实例化模型的代码。它是使用 Keras 的顺序 API 完成的,DenseTied是我的自定义层。
# encoder
#
encoded1 = Dense(2, activation="sigmoid")
decoded1 = DenseTied(4, activation="sigmoid", tied_to=encoded1)
# autoencoder
#
autoencoder = Sequential()
autoencoder.add(encoded1)
autoencoder.add(decoded1)
Run Code Online (Sandbox Code Playgroud)
训练模型后,下面是模型摘要和权重。
autoencoder.summary()
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense_7 (Dense) (None, 2) 10
_________________________________________________________________
dense_tied_7 (DenseTied) (None, 4) 12
=================================================================
Total params: 22
Trainable params: 14
Non-trainable params: 8
________________________________________________________________
autoencoder.layers[0].get_weights()[0]
array([[-2.122982 , 0.43029135],
[-2.1772149 , 0.16689162],
[-1.0465667 , 0.9828905 ],
[-0.6830663 , 0.0512633 ]], dtype=float32)
autoencoder.layers[-1].get_weights()[1]
array([[-0.6521988 , -0.7131109 , 0.14814234, 0.26533198],
[ 0.04387903, -0.22077179, 0.517225 , -0.21583867]],
dtype=float32)
Run Code Online (Sandbox Code Playgroud)
如您所见,由 报告的权重autoencoder.get_weights()似乎没有联系。
因此,在展示了我的方法之后,我的问题是,这是在 Dense Keras 层中绑定权重的有效方法吗?我能够运行代码,目前正在培训中。损失函数似乎也在合理下降。我担心这只会在构建模型时将它们设置为相等,但实际上不会将它们联系起来。我希望后端transpose功能通过引擎盖下的引用将它们联系起来,但我确信我遗漏了一些东西。
小智 5
感谢 Mikhail Berlinkov,重要提示:此代码在 Keras 下运行,但在 TF2.0 中不在 eager 模式下运行。它会跑,但训练得很差。
关键点是对象如何存储转置权重。self.kernel = K.transpose(self.tied_to.kernel)
在非急切模式下,这会以正确的方式创建图表。在急切模式下,此操作会失败,可能是因为转置变量的值存储在构建时(==第一次调用),然后在后续调用中使用。
然而:解决方案是在构建时存储不变的变量,并将转置操作放入调用方法中。
我花了几天时间来解决这个问题,如果这对任何人有帮助,我很高兴。
因此,在展示我的方法之后,我的问题是,这是在 Dense Keras 层中绑定权重的有效方法吗?
是的,这是有效的。
我担心这只会在模型构建时将它们设置为相等,但实际上并不会将它们绑定。我希望后端转置函数通过引擎盖下的引用将它们联系起来,但我确信我遗漏了一些东西。
它实际上将它们绑定在一个计算图中,您可以在打印中检查model.summary()这些可训练权重只有一份副本。此外,在训练模型后,您可以使用 检查相应层的权重model.get_weights()。当模型构建完成时,实际上还没有权重,只有权重的占位符。
random.seed(1)
class DenseTied(Layer):
def __init__(self, units,
activation=None,
use_bias=True,
kernel_initializer='glorot_uniform',
bias_initializer='zeros',
kernel_regularizer=None,
bias_regularizer=None,
activity_regularizer=None,
kernel_constraint=None,
bias_constraint=None,
tied_to=None,
**kwargs):
self.tied_to = tied_to
if 'input_shape' not in kwargs and 'input_dim' in kwargs:
kwargs['input_shape'] = (kwargs.pop('input_dim'),)
super().__init__(**kwargs)
self.units = units
self.activation = activations.get(activation)
self.use_bias = use_bias
self.kernel_initializer = initializers.get(kernel_initializer)
self.bias_initializer = initializers.get(bias_initializer)
self.kernel_regularizer = regularizers.get(kernel_regularizer)
self.bias_regularizer = regularizers.get(bias_regularizer)
self.activity_regularizer = regularizers.get(activity_regularizer)
self.kernel_constraint = constraints.get(kernel_constraint)
self.bias_constraint = constraints.get(bias_constraint)
self.input_spec = InputSpec(min_ndim=2)
self.supports_masking = True
def build(self, input_shape):
assert len(input_shape) >= 2
input_dim = input_shape[-1]
if self.tied_to is not None:
self.kernel = K.transpose(self.tied_to.kernel)
self._non_trainable_weights.append(self.kernel)
else:
self.kernel = self.add_weight(shape=(input_dim, self.units),
initializer=self.kernel_initializer,
name='kernel',
regularizer=self.kernel_regularizer,
constraint=self.kernel_constraint)
if self.use_bias:
self.bias = self.add_weight(shape=(self.units,),
initializer=self.bias_initializer,
name='bias',
regularizer=self.bias_regularizer,
constraint=self.bias_constraint)
else:
self.bias = None
self.built = True
def compute_output_shape(self, input_shape):
assert input_shape and len(input_shape) >= 2
assert input_shape[-1] == self.units
output_shape = list(input_shape)
output_shape[-1] = self.units
return tuple(output_shape)
def call(self, inputs):
output = K.dot(inputs, self.kernel)
if self.use_bias:
output = K.bias_add(output, self.bias, data_format='channels_last')
if self.activation is not None:
output = self.activation(output)
return output
# input_ = Input(shape=(16,), dtype=np.float32)
# encoder
#
encoded1 = Dense(4, activation="sigmoid", input_shape=(4,), use_bias=True)
decoded1 = DenseTied(4, activation="sigmoid", tied_to=encoded1, use_bias=False)
# autoencoder
#
autoencoder = Sequential()
# autoencoder.add(input_)
autoencoder.add(encoded1)
autoencoder.add(decoded1)
autoencoder.compile(optimizer="adam", loss="binary_crossentropy")
print(autoencoder.summary())
autoencoder.fit(x=np.random.rand(100, 4), y=np.random.randint(0, 1, size=(100, 4)))
print(autoencoder.layers[0].get_weights()[0])
print(autoencoder.layers[1].get_weights()[0])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3036 次 |
| 最近记录: |