简体   繁体   English

Keras自动编码器简单的例子有一个奇怪的输出

[英]Keras autoencoder simple example has a strange output

I am trying to run a simple autoencoder, all the training input is the same. 我正在尝试运行一个简单的自动编码器,所有的训练输入都是一样的。 The training data features are equal to 3, and the hidden layer has 3 nodes in it. 训练数据特征等于3,隐藏层中有3个节点。 I train the autoencoder with that input, then I try to predict it (encode/decode) again (so if the autoencoder passes everything as is without any changes it should work) 我用该输入训练自动编码器,然后我再次尝试预测它(编码/解码)(所以如果自动编码器按原样传递一切而没有任何改变它应该工作)

Anyway, that's not the case, and I am a sturggling a bit to understand why. 无论如何,情况并非如此,我有点难以理解为什么。 I am not sure if it's something wrong in my code, or in my understanding of the autoencdoer implementation. 我不确定我的代码或者我对autoencdoer实现的理解是否有问题。 Here is the code for reference. 这是代码供参考。

PS I played around with the number of epoches, number of examples in the training set, the batch size, made the training data values between 0-1, and kept track of the loss value, but that didn't help either. PS我玩了多个epoches,训练集中的例子数量,批量大小,使训练数据值在0-1之间,并且跟踪损失值,但这也没有帮助。

` `

from keras.layers import Input, Dense
from keras.models import Model
import numpy as np 
# this is the size of our encoded representations
encoding_dim = 3

x_train=np.array([[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3]])
in= Input(shape=(3,))
encoded = Dense(encoding_dim, activation='relu')(in)
decoded = Dense(3, activation='sigmoid')(encoded)

# this model maps an input to its reconstruction
autoencoder = Model(in, decoded)
autoencoder.compile(optimizer='adadelta', loss='mse')

autoencoder.fit(x_train, x_train,
                epochs=100,
                batch_size=4)
autoencoder.predict(x_train)

` `

The output I get should be the same as the input (or at least close) but I get this instead) 我得到的输出应该与输入相同(或至少接近),但我得到了这个)

`Out[180]: 
array([[ 0.80265796,  0.89038897,  0.9100889 ],
       [ 0.80265796,  0.89038897,  0.9100889 ],
       [ 0.80265796,  0.89038897,  0.9100889 ],
       ..., 
       [ 0.80265796,  0.89038897,  0.9100889 ],
       [ 0.80265796,  0.89038897,  0.9100889 ],
       [ 0.80265796,  0.89038897,  0.9100889 ]], dtype=float32)`

Any help would be appreciated, most likely I understood something wrong so hopefully this question is not that hard to answer. 任何帮助将不胜感激,我很可能理解错误,所以希望这个问题不难回答。

The error is here decoded = Dense(3, activation='sigmoid')(encoded) . 错误在这里被decoded = Dense(3, activation='sigmoid')(encoded)

You shouldn't use sigmoid activation, because it will limit the output in range (0, 1), replace the sigmoid with linear or just remove it, and you can add more epochs, eg train 1000 epochs. 你不应该使用sigmoid激活,因为它会限制范围(0,1)中的输出,用linear替换sigmoid或只删除它,你可以添加更多的纪元,例如列车1000个纪元。 In this setting, I get what you need 在这种情况下,我得到你需要的东西

[[ 0.98220336  1.98066235  2.98398876]
 [ 0.98220336  1.98066235  2.98398876]
 [ 0.98220336  1.98066235  2.98398876]
 [ 0.98220336  1.98066235  2.98398876]
 [ 0.98220336  1.98066235  2.98398876]
 [ 0.98220336  1.98066235  2.98398876]
 [ 0.98220336  1.98066235  2.98398876]
 [ 0.98220336  1.98066235  2.98398876]
 [ 0.98220336  1.98066235  2.98398876]]

In addition, you should replace the input in with another name, as it is a keyword in Python :-). 此外,应更换输入in有另一个名字,因为它是一个keyword在Python :-)。

After apply @danche suggestion following is the updated code and results, I got the results after increasing the epocs = 10000 应用@danche建议后,更新的代码和结果,我在增加epocs = 10000后得到了结果

from keras.layers import Input, Dense
from keras.models import Model
import numpy as np
# this is the size of our encoded representations
encoding_dim = 3

x_train=np.array([[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3]])
input = Input(shape=(3,))
encoded = Dense(encoding_dim, activation='relu')(input)
decoded = Dense(3, activation='linear')(encoded)

# this model maps an input to its reconstruction
autoencoder = Model(input, decoded)
autoencoder.compile(optimizer='adadelta', loss='mse')

autoencoder.fit(x_train, x_train,epochs=10000,batch_size=4)
print(autoencoder.predict(x_train))



Epoch 10000/10000
8/8 [==============================] - 0s - loss: 2.4463e-04     
[[ 0.99124289  1.98534203  2.97887278]
 [ 0.99124289  1.98534203  2.97887278]
 [ 0.99124289  1.98534203  2.97887278]
 [ 0.99124289  1.98534203  2.97887278]
 [ 0.99124289  1.98534203  2.97887278]
 [ 0.99124289  1.98534203  2.97887278]
 [ 0.99124289  1.98534203  2.97887278]
 [ 0.99124289  1.98534203  2.97887278]]

Your input data is not normalized. 您的输入数据未规范化。 After normalization as below, you can get the correct output. 在如下标准化之后,您可以获得正确的输出。

x_train=np.array([[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3]])
x_train=keras.utils.normalize(x_train)  #newly added line
 ....
 ....

You can certainly build an autoencoder in Keras using the Sequential model. 您当然可以使用Sequential模型在Keras中构建自动编码器。 So I am no sure that the example you are referring to is exactly the "simplest possible autoencoder" you can create, as the article's author claims. 因此,我不确定您所指的示例是否是您可以创建的“最简单的自动编码器”,正如文章作者所声称的那样。 Here's how I would do it: 我是这样做的:

from keras.models                   import Sequential
from keras.layers                   import Dense 

import numpy as np 

# this is the size of our encoded representations
encoding_dim = 3

np.random.seed(1)  # to ensure the same results

x_train=np.array([[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3],[1,2,3]])

autoencoder = Sequential([ 
              Dense(encoding_dim,input_shape=(3,)), 
              Dense(encoding_dim)
])

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

autoencoder.fit(x_train, x_train,
            epochs=127,
            batch_size=4, 
            verbose=2)

out=autoencoder.predict(x_train)
print(out)

When running this example you get 在运行这个例子时,你得到了

 ....
 Epoch 127/127
 - 0s - loss: 1.8948e-14
[[ 1.  2.  3.]
 [ 1.  2.  3.]
 [ 1.  2.  3.]
 [ 1.  2.  3.]
 [ 1.  2.  3.]
 [ 1.  2.  3.]
 [ 1.  2.  3.]
 [ 1.  2.  3.]
 [ 1.  2.  3.]]

which is kind of nice... 这有点好......

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

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