向 Keras 中的自动编码器添加乘法层

我想在 LSTM 自动编码器之上添加一个乘法层。乘法层应该将张量乘以一个常数值。我编写了以下代码,无需乘法层即可工作。有谁知道如何调整并使其工作?


import keras

from keras import backend as K

from keras.models import Sequential, Model

from keras.layers import Input, LSTM, RepeatVector, TimeDistributed

from keras.layers.core import Flatten, Dense, Dropout, Lambda

from keras.optimizers import SGD, RMSprop, Adam

from keras import objectives

from keras.engine.topology import Layer

import numpy as np


class LayerKMultiply(Layer):


    def __init__(self, output_dim, **kwargs):

        self.output_dim = output_dim

        self.k = Null

        super(LayerKMultiply, self).__init__(**kwargs)


    def build(self, input_shape):

        # Create a trainable weight variable for this layer.

        self.k = self.add_weight(

            name='k',

            shape=(),

            initializer='ones',

            dtype='float32',

            trainable=True,

        )

        super(LayerKMultiply, self).build(input_shape)  # Be sure to call this at the end


    def call(self, x):

        #return K.tf.multiply(self.k, x)

        return self.k * x


    def compute_output_shape(self, input_shape):

        return (input_shape[0], self.output_dim)


    timesteps, input_dim, latent_dim = 10, 3, 32



inputs = Input(shape=(timesteps, input_dim))

encoded = LSTM(latent_dim, return_sequences=False, activation='linear')(inputs)

decoded = RepeatVector(timesteps)(encoded)

decoded = LSTM(input_dim, return_sequences=True, activation='linear')(decoded)

decoded = TimeDistributed(Dense(input_dim, activation='linear'))(decoded)

#decoded = LayerKMultiply(k = 20)(decoded)


sequence_autoencoder = Model(inputs, decoded)

encoder = Model(inputs, encoded)


autoencoder = Model(inputs, decoded)

autoencoder.compile(optimizer='adam', loss='mse')


    X = np.array([[[1,2,3,4,5,6,7,8,9,10],[1,2,3,4,5,6,7,8,9,10],[1,2,3,4,5,6,7,8,9,10]]])

X = X.reshape(1,10,3)

p = autoencoder.predict(x=X, batch_size=1)

print(p)



杨__羊羊
浏览 205回答 2
2回答

侃侃无极

您将位置参数与关键字参数混合在一起。当你定义一个函数时,就像def __init__(self, output_dim, **kwargs) output_dim是一个位置参数。你需要:要么自己通过 20 LayerMultiply(20)(decoded)或改变 def __init__(self, k=10, **kwargs)或output_dim从定义中删除并使用self.output_dim = kwargs['k']更多信息在这里。

墨色风雨

我相信解决方案如下:import kerasfrom keras import backend as Kfrom keras.models import Sequential, Modelfrom keras.layers import Input, LSTM, RepeatVector, TimeDistributedfrom keras.layers.core import Flatten, Dense, Dropout, Lambdafrom keras.optimizers import SGD, RMSprop, Adamfrom keras import objectivesfrom keras.engine.topology import Layerimport numpy as npclass LayerKMultiply(Layer):    def __init__(self, output_dim, **kwargs):        self.output_dim = output_dim        self.k = None        super(LayerKMultiply, self).__init__(**kwargs)    def build(self, input_shape):        # Create a trainable weight variable for this layer.        self.k = self.add_weight(            name='k',            shape=(),            initializer='ones',            dtype='float32',            trainable=True,        )        super(LayerKMultiply, self).build(input_shape)  # Be sure to call this at the end    def call(self, x):        return self.k * x    def compute_output_shape(self, input_shape):        return (input_shape[0], input_shape[1], input_shape[2])
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python