标签: autoencoder

keras autoencoder不会聚

有人可以向我解释为什么自动编码器没有收敛?对我来说,下面两个网络的结果应该是相同的.但是,下面的自动编码器不会收敛,而下面的网络则是收敛的.

# autoencoder implementation, does not converge
autoencoder = Sequential()
encoder = containers.Sequential([Dense(32,16,activation='tanh')]) 
decoder = containers.Sequential([Dense(16,32)])
autoencoder.add(AutoEncoder(encoder=encoder, decoder=decoder, 
                        output_reconstruction=True))
rms = RMSprop()
autoencoder.compile(loss='mean_squared_error', optimizer=rms)

autoencoder.fit(trainData,trainData, nb_epoch=20, batch_size=64,
            validation_data=(testData, testData), show_accuracy=False)
Run Code Online (Sandbox Code Playgroud)
 # non-autoencoder implementation, converges

model = Sequential()
model.add(Dense(32,16,activation='tanh')) 
model.add(Dense(16,32))
model.compile(loss='mean_squared_error', optimizer=rms)

model.fit(trainData,trainData, nb_epoch=numEpochs, batch_size=batch_size,
            validation_data=(testData, testData), show_accuracy=False)
Run Code Online (Sandbox Code Playgroud)

autoencoder keras

6
推荐指数
1
解决办法
3599
查看次数

在Tensorflow中重用图层权重

我使用tf.slim来实现自动编码器.我完全卷积了以下架构:

[conv, outputs = 1] => [conv, outputs = 15] => [conv, outputs = 25] =>
=> [conv_transpose, outputs = 25] => [conv_transpose, outputs = 15] => 
[conv_transpose, outputs = 1]
Run Code Online (Sandbox Code Playgroud)

它必须完全卷积,我不能汇集(更大的问题的限制).我想使用绑定的重量,所以

encoder_W_3 = decoder_W_1_Transposed 
Run Code Online (Sandbox Code Playgroud)

(因此第一解码器层的权重是最后一个编码器层的权重,被转置).

如果我重复使用权重,那么tfslim允许你重复使用它们,即reuse = True,然后只提供你想要重用的层的范围名称,我得到大小问题:

ValueError: Trying to share variable cnn_block_3/weights, but specified shape (21, 11, 25, 25) and found shape (21, 11, 15, 25).
Run Code Online (Sandbox Code Playgroud)

如果您不转置先前模型的权重,这是有道理的.有没有人知道如何转换这些重量?

PS:我知道这是非常抽象和挥手,但我正在使用自定义api,在tfslim之上,所以我不能在这里发布代码示例.

autoencoder tensorflow tf-slim

6
推荐指数
1
解决办法
822
查看次数

变体自动编码器:在Keras中进行预热

我最近读了这篇文章,其中介绍了一种称为“热身”(WU)的过程,该过程包括将KL散度的损失乘以一个变量,该变量的值取决于历元数(它从0线性发展到1)

我想知道这是否是这样做的好方法:

beta = K.variable(value=0.0)

def vae_loss(x, x_decoded_mean):
    # cross entropy
    xent_loss = K.mean(objectives.categorical_crossentropy(x, x_decoded_mean))

    # kl divergence
    for k in range(n_sample):
        epsilon = K.random_normal(shape=(batch_size, latent_dim), mean=0.,
                              std=1.0)  # used for every z_i sampling
        # Sample several layers of latent variables
        for mean, var in zip(means, variances):
            z_ = mean + K.exp(K.log(var) / 2) * epsilon

            # build z
            try:
                z = tf.concat([z, z_], -1)
            except NameError:
                z = z_
            except TypeError:
                z = z_

            # sum loss …
Run Code Online (Sandbox Code Playgroud)

autoencoder deep-learning keras

6
推荐指数
1
解决办法
1182
查看次数

如何重塑文本数据以适合于Keras中的LSTM模型

更新1:

我指的代码就是本书中的代码,您可以在这里找到。

唯一的事情是我不想embed_size在解码器部分中拥有。这就是为什么我认为根本不需要具有嵌入层的原因,因为如果我放入嵌入层,则需要embed_size在解码器部分中放置(如果Im错误,请更正我)。

总的来说,我试图在不使用嵌入层的情况下采用相同的代码,因为我需要vocab_size在解码器部分中具有。

我认为评论中提供的建议可能是正确的(using one_hot_encoding)我如何面对此错误:

当我做的时候one_hot_encoding

tf.keras.backend.one_hot(indices=sent_wids, classes=vocab_size)
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

in check_num_samples you should specify the + steps_name + argument ValueError: If your data is in the form of symbolic tensors, you should specify the steps_per_epoch argument (instead of the batch_size argument, because symbolic tensors are expected to produce batches of input data)

我准备数据的方式是这样的:

sent_lensis的 形状,(87716, 200)我想以可以将其输入LSTM的方式重塑形状。这里200代表sequence_lenght87716是我拥有的样本数。

以下是代码LSTM …

python autoencoder lstm keras tensorflow

6
推荐指数
1
解决办法
274
查看次数

Keras中卷积自编码器的输出大小

我在做Keras库作者写的卷积自编码器教程:https ://blog.keras.io/building-autoencoders-in-keras.html

但是,当我启动完全相同的代码,并使用 summary() 分析网络架构时,似乎输出大小与输入大小不兼容(在自动编码器的情况下是必需的)。这是summary()的输出:

**____________________________________________________________________________________________________
Layer (type)                     Output Shape          Param #     Connected to                     
====================================================================================================
input_1 (InputLayer)             (None, 1, 28, 28)     0                                            
____________________________________________________________________________________________________
convolution2d_1 (Convolution2D)  (None, 16, 28, 28)    160         input_1[0][0]                    
____________________________________________________________________________________________________
maxpooling2d_1 (MaxPooling2D)    (None, 16, 14, 14)    0           convolution2d_1[0][0]            
____________________________________________________________________________________________________
convolution2d_2 (Convolution2D)  (None, 8, 14, 14)     1160        maxpooling2d_1[0][0]             
____________________________________________________________________________________________________
maxpooling2d_2 (MaxPooling2D)    (None, 8, 7, 7)       0           convolution2d_2[0][0]            
____________________________________________________________________________________________________
convolution2d_3 (Convolution2D)  (None, 8, 7, 7)       584         maxpooling2d_2[0][0]             
____________________________________________________________________________________________________
maxpooling2d_3 (MaxPooling2D)    (None, 8, 3, 3)       0           convolution2d_3[0][0]            
____________________________________________________________________________________________________
convolution2d_4 (Convolution2D) …
Run Code Online (Sandbox Code Playgroud)

python neural-network autoencoder deep-learning keras

5
推荐指数
1
解决办法
1014
查看次数

在 Keras 中打印/保存自动编码器生成的功能

我有这个自动编码器:

input_dim = Input(shape=(10,))
encoded1 = Dense(30, activation = 'relu')(input_dim)
encoded2 = Dense(20, activation = 'relu')(encoded1)
encoded3 = Dense(10, activation = 'relu')(encoded2)
encoded4 = Dense(6, activation = 'relu')(encoded3)
decoded1 = Dense(10, activation = 'relu')(encoded4)
decoded2 = Dense(20, activation = 'relu')(decoded1)
decoded3 = Dense(30, activation = 'relu')(decoded2)
decoded4 = Dense(ncol, activation = 'sigmoid')(decoded3)
autoencoder = Model(input = input_dim, output = decoded4) 
autoencoder.compile(-...)
autoencoder.fit(...)
Run Code Online (Sandbox Code Playgroud)

现在我想打印或保存在编码4中生成的功能。基本上,从一个巨大的数据集开始,我想在训练部分之后提取自动编码器生成的特征,以获得我的数据集的受限表示。

你可以帮帮我吗?

python autoencoder deep-learning keras

5
推荐指数
1
解决办法
1082
查看次数

变分自动编码器损失函数(keras)

我正在使用 Keras 构建一个变分自动编码器。我很大程度上受到@Fchollet 示例的启发:https : //github.com/fchollet/keras/blob/master/examples/variational_autoencoder.py

但我正在处理连续数据。我的输出是一个持续时间,而不是像 MNIST 那样对数字进行排名。在这方面,我将损失函数从 binary_crossentropy 更改为 mean_squared_error。我主要想知道第二项,即 KL 散度。它应该适用于连续数据吗?我无法绕过它。对我来说,它应该将相似的数据紧密地放在潜在空间中。例如,在 MNIST 数据的 cas 中,将每个“1”放在潜在空间中,将每个“2”放在一起等等......因为我正在处理连续数据,它是如何工作的?在我的情况下有更好的丢失功能吗?

这是丢失的功能:

def vae_loss(x, x_decoded_mean):
    xent_loss = original_dim *metrics.mean_squared_error(x, x_decoded_mean)
    kl_loss = - 0.5 * K.mean(1 + z_log_sigma - K.square(z_mean) - K.exp(z_log_sigma), axis=-1)
    return K.mean(xent_loss + kl_loss)
vae.compile(optimizer='Adam', loss=vae_loss)
Run Code Online (Sandbox Code Playgroud)

这是 3D 潜在空间中的表示。 在此处输入图片说明

如您所见,一些类似的数据会根据需要放在一起。当我将 kl_loss 函数的系数增加到 "-100000" 而不是 "-0.5" 时会发生以下情况: 在此处输入图片说明

我以为我会以几乎线性的方式从蓝色变为红色。相反,我以混乱的方式获得了所有数据的集群。

你们能帮帮我吗?谢谢 !

python python-3.x autoencoder keras loss-function

5
推荐指数
1
解决办法
2426
查看次数

张量流自动编码器的输出始终相同

目前,我尝试为 tensorflow 中的时间序列数据构建一个自动编码器。我有将近 500 天的数据,其中每天有 24 个数据点。因为这是我第一次尝试,所以我的架构非常简单。在我输入 size 之后24,隐藏层的 size:10; 3; 10输出为 again 24。我对数据进行了标准化(数据点在范围内[-0.5; 0.5]),使用 sigmoid 激活函数和 RMSPropOptimizer。

训练后(图中的损失函数)对于我输入网络的每个时间数据,输出都是相同的。有人知道这是什么原因吗?我的数据集是否有可能是问题(下面的代码)?

训练损失函数

class TimeDataset:
def __init__(self,data):
    self._index_in_epoch = 0
    self._epochs_completed = 0
    self._data = data
    self._num_examples = data.shape[0]
    pass


@property
def data(self):
    return self._data

def next_batch(self, batch_size, shuffle=True):
    start = self._index_in_epoch

    # first call
    if start == 0 and self._epochs_completed == 0:
        idx = np.arange(0, self._num_examples)  # get all possible indexes
        np.random.shuffle(idx)  # shuffle indexe
        self._data …
Run Code Online (Sandbox Code Playgroud)

python neural-network autoencoder tensorflow

5
推荐指数
1
解决办法
1794
查看次数

在密集的 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 …
Run Code Online (Sandbox Code Playgroud)

python autoencoder keras

5
推荐指数
2
解决办法
3036
查看次数

如何使用 PyTorch 构建 LSTM AutoEncoder?

我有我的数据DataFrame

       dOpen     dHigh      dLow    dClose   dVolume  day_of_week_0  day_of_week_1  ...  month_6  month_7  month_8  month_9  month_10  month_11  month_12
639 -0.002498 -0.000278 -0.005576 -0.002228 -0.002229              0              0  ...        0        0        1        0         0         0         0
640 -0.004174 -0.005275 -0.005607 -0.005583 -0.005584              0              0  ...        0        0        1        0         0         0         0
641 -0.002235  0.003070  0.004511  0.008984  0.008984              1              0  ...        0        0        1        0         0         0         0
642  0.006161 -0.000278 -0.000281 -0.001948 -0.001948              0              1  ...        0        0        1        0         0 …
Run Code Online (Sandbox Code Playgroud)

python autoencoder lstm pytorch

5
推荐指数
1
解决办法
2563
查看次数