标签: autoencoder

自动编码器的数据预处理技巧

最近,我尝试使用自动编码器来查找异常,但一些输入特征是计数数据(例如点击数或显示数)。训练前我需要标准化或缩放吗?

machine-learning data-processing autoencoder

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

如何将 LSTM 自动编码器应用于变长时间序列数据?

我在本教程中阅读了 LSTM-autoencoder:https://blog.keras.io/building-autoencoders-in-keras.html,并在下面粘贴相应的 keras 实现:

from keras.layers import Input, LSTM, RepeatVector
from keras.models import Model

inputs = Input(shape=(timesteps, input_dim))
encoded = LSTM(latent_dim)(inputs)

decoded = RepeatVector(timesteps)(encoded)
decoded = LSTM(input_dim, return_sequences=True)(decoded)

sequence_autoencoder = Model(inputs, decoded)
encoder = Model(inputs, encoded)
Run Code Online (Sandbox Code Playgroud)

在此实现中,他们将输入固定为形状 (timesteps, input_dim),这意味着时间序列数据的长度固定为timesteps。如果我没记错的话,RNN/LSTM 可以处理可变长度的时间序列数据,我想知道是否可以以某种方式修改上面的代码以接受任何长度的数据?

谢谢!

neural-network autoencoder deep-learning lstm keras

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

我可以在 Keras 中使用带有卷积神经网络的矩形图像吗?

假设我想使用 Keras 的Convolutional2D函数构建 CNN,输入图像可以是 size[224, 320, 3]而不是类似 的大小吗[224, 224, 3]

我应该将图像保留为矩形格式还是将其缩放为正方形?我尝试将它们制作成正方形,但质量大大下降+边缘周围有重要数据。

如果我用矩形输入图像构建它,它最终会破坏线路吗?

我还想在 CNN 的末端附加一个解码器,以输出相同形状的图像(本质上是带有矩形图像而不是正方形的 VAE)。

convolution neural-network autoencoder conv-neural-network keras

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

具有非方形图像的 CNN 自动编码器

我已经为编码器和解码器实现了带有 CNN 层的变分自动编码器。代码如下所示。我的训练数据 ( train_X) 由 40'000 张尺寸为 64 x 78 x 1 的图像组成,我的验证数据 ( valid_X) 由 4500 张尺寸为 64 x 78 x 1 的图像组成。

当我使用方形图像(例如 64 x 64)时,一切正常,但是当我使用上述图像(64 x 78)时,我收到以下错误:

File "C:\Users\user\AppData\Local\Continuum\anaconda3\lib\site-packages\keras\engine\training.py", line 1039, in fit
  validation_steps=validation_steps)
File "C:\Users\user\AppData\Local\Continuum\anaconda3\lib\site-packages\keras\engine\training_arrays.py", line 199, in fit_loop
  outs = f(ins_batch)
File "C:\Users\user\AppData\Local\Continuum\anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py", line 2715, in __call__
  return self._call(inputs)
File "C:\Users\user\AppData\Local\Continuum\anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py", line 2675, in _call
  fetched = self._callable_fn(*array_vals)
File "C:\Users\user\AppData\Local\Continuum\anaconda3\lib\site-packages\tensorflow\python\client\session.py", line 1458, in __call__
  run_metadata_ptr)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Incompatible shapes: [655360] vs. [638976]
   [[{{node …
Run Code Online (Sandbox Code Playgroud)

python autoencoder conv-neural-network keras keras-layer

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

参数 #2“mat1”的张量位于 CPU 上,但预计它位于 GPU 上

根据我之前的问题,我编写了这段代码来训练自动编码器,然后提取特征。(变量名可能有一些变化)

# Autoencoder class
#https://medium.com/pytorch/implementing-an-autoencoder-in-pytorch-19baa22647d1
class AE_class(nn.Module):
    def __init__(self, **kwargs):
        super().__init__()
        self.encoder_hidden_layer = nn.Linear(
            in_features=kwargs["input_shape"], out_features=128
        )
        self.encoder_output_layer = nn.Linear(
            in_features=128, out_features=128
        )
        self.decoder_hidden_layer = nn.Linear(
            in_features=128, out_features=128
        )
        self.decoder_output_layer = nn.Linear(
            in_features=128, out_features=kwargs["input_shape"]
        )

    def forward(self, features):
        #print("in forward")
        #print(type(features))
        activation = self.encoder_hidden_layer(features)
        activation = torch.relu(activation)
        code = self.encoder_output_layer(activation)
        code = torch.relu(code)
        activation = self.decoder_hidden_layer(code)
        activation = torch.relu(activation)
        activation = self.decoder_output_layer(activation)
        reconstructed = torch.relu(activation)
        return reconstructed
    
    def encode(self, features_h):
        activation_h = self.encoder_hidden_layer(features_h)
        activation_h = torch.relu(activation_h)
        code_h = …
Run Code Online (Sandbox Code Playgroud)

python debugging gpu autoencoder pytorch

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

如何在训练自动编码器(回调)期间将keras中的输入随机设置为零?

我正在联合训练具有 2 个独立输入路径的 2 个自动编码器,并且我想将其中一个输入路径随机设置为零。

我将 Tensorflow 与 keras 后端(功能 API)一起使用。

我正在计算反向传播的联合损失(两个损失的总和)。

A -> A' & B ->B'

损失 => l2(A,A')+l2(B,B')

采用 A 和 B 的网络在潜在空间中连接。我想将 A 或 B 随机设置为零,并仅在相应路径上计算损耗,这意味着如果输入路径 A 设置为零损耗,则仅使用路径 B 的输出来计算损耗,反之亦然;例如:

0 -> A' & B ->B'

损失:l2(B,B')

如何随机将输入路径设置为零?我如何编写一个回调来执行此操作?

python autoencoder deep-learning keras tensorflow

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

需要对自动编码器上的 fit_generator 工作进行哪些更改

我目前正在使用在卷积网络上运行良好的生成器。但是,当我使用相同的生成器来拟合自动编码器时,会出现以下错误。

**Exception: output of generator should be a tuple (x, y, sample_weight) or (x, y). Found: [[[[ 0.86666673  0.86666673  0.86666673 ...,  0.62352943  0.627451
     0.63137257]
   [ 0.86666673  0.86666673  0.86666673 ...,  0.63137257  0.627451
     0.627451  ]
   [ 0.86666673  0.86666673  0.86666673 ...,  0.63137257  0.627451
     0.62352943]
   ...,**
Run Code Online (Sandbox Code Playgroud)

我的代码如下

from keras.layers import Input, Dense, Convolution2D, MaxPooling2D,       
from keras.models import Model,Sequential
from keras.preprocessing.image import ImageDataGenerator 
import numpy as np
import os
import h5py


img_width=140 
img_height=140
train_data_dir=r'SitePhotos\train'
valid_data_dir=r'SitePhotos\validation'
input_img = Input(batch_shape=(32,3, img_width, img_width))

x = Convolution2D(16, 3, 3, …
Run Code Online (Sandbox Code Playgroud)

python autoencoder keras

4
推荐指数
1
解决办法
2809
查看次数

有批量标准化的网络使用autoencoder有什么意义吗?

众所周知,DNN的主要问题是学习时间长.

但是有一些方法可以加速学习:

  1. 批量标准化 =(x-AVG)/Variance:https://arxiv.org/abs/1502.03167

批量标准化实现了相同的准确度,培训步骤减少14倍

  1. ReLU =max(x, 0) - 整流线性单元(ReLU,LReLU,PReLU,RReLU):https://arxiv.org/abs/1505.00853

使用非饱和激活函数的优点在于两个方面:第一个是解决所谓的"爆炸/消失梯度".第二是加快收敛速度.

或者任何一个:( maxout,ReLU-family,tanh)

  1. 快速权重初始化(避免消失或爆炸渐变):https://arxiv.org/abs/1511.06856

我们的初始化与标准计算机视觉任务(例如图像分类和物体检测)上的当前最先进的无监督或自我监督的预训练方法相匹配,同时大约快三个数量级.

或LSUV初始化(层序单位方差):https://arxiv.org/abs/1511.06422

但是如果我们使用所有步骤:(1)批量标准化,(2)ReLU,(3)快速权重初始化或LSUV - 那么在训练深度神经网络的任何步骤中使用自动编码器/自动关联器是否有任何意义?

machine-learning neural-network autoencoder deep-learning conv-neural-network

4
推荐指数
1
解决办法
3838
查看次数

如何解决由于PyTorch中的大小不匹配导致的运行时错误?

我正在尝试使用简单的自动编码器PyTorch.我的数据集由256 x 256 x 3图像组成.我已经构建了一个torch.utils.data.dataloader.DataLoader将图像存储为张量的对象.当我运行autoencoder时,我收到运行时错误:

大小不匹配,m1:[76800 x 256],m2:[784 x 128] atUsers/soumith/minicondabuild3/conda-bld/pytorch_1518371252923/work/torch/lib/TH/generic/THTensorMath.c:1434

这些是我的超参数:

batch_size=100,
learning_rate = 1e-3,
num_epochs = 100
Run Code Online (Sandbox Code Playgroud)

以下是我的自动编码器的架构:

class autoencoder(nn.Module):
    def __init__(self):
        super(autoencoder, self).__init__()
        self.encoder = nn.Sequential(
            nn.Linear(3*256*256, 128),
            nn.ReLU(),
            nn.Linear(128, 64),
            nn.ReLU(True),
            nn.Linear(64, 12),
            nn.ReLU(True),
            nn.Linear(12, 3))

        self.decoder = nn.Sequential(
            nn.Linear(3, 12),
            nn.ReLU(True),
            nn.Linear(12, 64),
            nn.ReLU(True),
            nn.Linear(64, 128),
            nn.Linear(128, 3*256*256),
            nn.ReLU())

def forward(self, x):
    x = self.encoder(x)
    #x = self.decoder(x)
    return x
Run Code Online (Sandbox Code Playgroud)

这是我用来运行模型的代码:

for epoch in range(num_epochs):
for data in dataloader:
    img = …
Run Code Online (Sandbox Code Playgroud)

python autoencoder pytorch

4
推荐指数
2
解决办法
7336
查看次数

从训练有素的自动编码器中提取编码器和解码器

我想将自动编码器的学习和应用分为以下两个部分:https://blog.keras.io/building-autoencoders-in-keras.html,并使用fashion-mnist数据进行测试:

  1. 加载图像,进行可能需要数小时或数天的拟合,然后使用回调保存最佳的自动编码器模型。该过程可能在下一部分之前几周。
  2. 使用最佳模型(由文件名手动选择)并绘制原始图像,由自动编码器的编码器进行的编码表示以及使用自动编码器的解码器进行的预测。我在从训练有素的自动编码器中提取编码器和解码器层时遇到问题(请参阅第二步)。

对于第一步,我有一个非常简单的网络,如下所示:

input_img = Input(shape=(784,))
# encoded representation
encoded = Dense(encoding_dim, activation='relu')(input_img)
# lossy reconstruction
decoded = Dense(784, activation='sigmoid')(encoded)

# full AE model: map an input to its reconstruction
autoencoder = Model(input_img, decoded)

# encoder: map an input to its encoded representation
encoder = Model(input_img, encoded)
# placeholder for an encoded input
encoded_input = Input(shape=(encoding_dim,))
# last layer of the autoencoder model
decoder_layer = autoencoder.layers[-1]
# decoder
decoder = Model(encoded_input, decoder_layer(encoded_input))
Run Code Online (Sandbox Code Playgroud)

网络是:

autoencoder.summary()
_________________________________________________________________ …
Run Code Online (Sandbox Code Playgroud)

python autoencoder keras tensorflow keras-layer

4
推荐指数
1
解决办法
1797
查看次数