最近,我尝试使用自动编码器来查找异常,但一些输入特征是计数数据(例如点击数或显示数)。训练前我需要标准化或缩放吗?
我在本教程中阅读了 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 可以处理可变长度的时间序列数据,我想知道是否可以以某种方式修改上面的代码以接受任何长度的数据?
谢谢!
假设我想使用 Keras 的Convolutional2D函数构建 CNN,输入图像可以是 size[224, 320, 3]而不是类似 的大小吗[224, 224, 3]?
我应该将图像保留为矩形格式还是将其缩放为正方形?我尝试将它们制作成正方形,但质量大大下降+边缘周围有重要数据。
如果我用矩形输入图像构建它,它最终会破坏线路吗?
我还想在 CNN 的末端附加一个解码器,以输出相同形状的图像(本质上是带有矩形图像而不是正方形的 VAE)。
convolution neural-network autoencoder conv-neural-network keras
我已经为编码器和解码器实现了带有 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) 根据我之前的问题,我编写了这段代码来训练自动编码器,然后提取特征。(变量名可能有一些变化)
# 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) 我正在联合训练具有 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')
如何随机将输入路径设置为零?我如何编写一个回调来执行此操作?
我目前正在使用在卷积网络上运行良好的生成器。但是,当我使用相同的生成器来拟合自动编码器时,会出现以下错误。
**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) 众所周知,DNN的主要问题是学习时间长.
但是有一些方法可以加速学习:
=(x-AVG)/Variance:https://arxiv.org/abs/1502.03167批量标准化实现了相同的准确度,培训步骤减少了14倍
=max(x, 0) - 整流线性单元(ReLU,LReLU,PReLU,RReLU):https://arxiv.org/abs/1505.00853使用非饱和激活函数的优点在于两个方面:第一个是解决所谓的"爆炸/消失梯度".第二是加快收敛速度.
或者任何一个:( maxout,ReLU-family,tanh)
我们的初始化与标准计算机视觉任务(例如图像分类和物体检测)上的当前最先进的无监督或自我监督的预训练方法相匹配,同时大约快三个数量级.
或LSUV初始化(层序单位方差):https://arxiv.org/abs/1511.06422
但是如果我们使用所有步骤:(1)批量标准化,(2)ReLU,(3)快速权重初始化或LSUV - 那么在训练深度神经网络的任何步骤中使用自动编码器/自动关联器是否有任何意义?
machine-learning neural-network autoencoder deep-learning conv-neural-network
我正在尝试使用简单的自动编码器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) 我想将自动编码器的学习和应用分为以下两个部分:https://blog.keras.io/building-autoencoders-in-keras.html,并使用fashion-mnist数据进行测试:
对于第一步,我有一个非常简单的网络,如下所示:
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) autoencoder ×10
keras ×6
python ×6
keras-layer ×2
pytorch ×2
tensorflow ×2
convolution ×1
debugging ×1
gpu ×1
lstm ×1