简体   繁体   English

使用自动编码器重建潜在空间

[英]reconstruction latent space with autoencoder

import numpy as np
import numpy.matlib
import numpy as np
import pandas as pd
from keras.layers import Input, Dense
from keras.models import Model
from keras.optimizers import RMSprop
from sklearn.preprocessing import StandardScaler
from sklearn.preprocessing import MinMaxScaler

sgx=np.random.randn(25200, 100)
normx = MinMaxScaler().fit(sgx)
sgxx=normx.fit_transform(sgx)

encoding_dim = 32
input_img = Input(shape=(100,))
encoded = Dense(80, activation='relu')(input_img)
encoded = Dense(64, activation='relu')(encoded)
encoded = Dense(32, activation='relu')(encoded)
decoded = Dense(64, activation='relu')(encoded)
decoded = Dense(90, activation='relu')(decoded)
decoded = Dense(100, activation='sigmoid')(decoded)

autoencoder = Model(input_img, decoded)
autoencoder.compile(loss='mean_squared_error', optimizer = RMSprop())
# this model maps an input to its encoded representation
encoder = Model(input_img, encoded)
# create the decoder model
encoded_input = Input(shape=(encoding_dim,))
deco = autoencoder.layers[-3](encoded_input)
deco = autoencoder.layers[-2](deco)
deco = autoencoder.layers[-1](deco)
decoder = Model(encoded_input, deco)
autoencoder.fit(sgxx, sgxx,
                epochs=100,
                batch_size=560)

encoded_imgs = encoder.predict(sgx)
decoded_imgs = decoder.predict(encoded_imgs)

in the code above, I reconstructed same initial dimension, can we use AE to reconstruct a lower dimension from initial one, I mean for ex in my case I have 100 dimensions, then after reducing the dimension I would like reconstruct only 30??在上面的代码中,我重建了相同的初始维度,我们可以使用 AE 从初始维度重建一个较低的维度,我的意思是在我的情况下,我有 100 个维度,然后在减少维度后我只想重建 30 个??

From the way your question is stated no , but I think there is just a miss understanding.从你的问题陈述的方式来看,没有,但我认为只是理解失误。

A typical way of training an auto-encoder is using reconstruction error.训练自动编码器的典型方法是使用重构误差。 This is defined simply as ||x - f(g(x))||这被简单地定义为 ||x - f(g(x))|| where g is your encoder network, f is your decoder network and x is some input example.其中 g 是您的编码器网络,f 是您的解码器网络,x 是一些输入示例。 According to this particular loss f(g(x)) must have the same dimension as x (100 in your case).根据这个特定的损失 f(g(x)) 必须与 x 具有相同的尺寸(在您的情况下为 100)。

You need to think about what it means to reconstruct something with a decoder, what exactly are you wanting to produce that has dimension 30?您需要考虑使用解码器重建某些东西意味着什么,您到底想要生成什么尺寸为 30 的东西? To rephrase your question, can we create a representation of x that has 30 dimensions?重新表述您的问题,我们可以创建具有 30 个维度的 x 表示吗? Answer: Yes!回答:是的! This is what auto-encoders are often used for, the encoder can take a 100 dimensional input and reduce its dimension to 30. To get this representation, (which in the code given above will be 32 dimensional) simply run x through g (your encoder) and stop.这是自动编码器经常使用的用途,编码器可以接受 100 维输入并将其维数减少到 30。要获得这种表示,(在上面给出的代码中将是 32 维)只需运行 x 到 g(您的编码器)并停止。 If the goal is only to construct this representation, the decoder is only used for training.如果目标只是构建这种表示,则解码器仅用于训练。

I hope this clarifies things!我希望这能澄清事情!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM