繁体   English   中英

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

[英]Keras autoencoder simple example has a strange output

我正在尝试运行一个简单的自动编码器,所有的训练输入都是一样的。 训练数据特征等于3,隐藏层中有3个节点。 我用该输入训练自动编码器,然后我再次尝试预测它(编码/解码)(所以如果自动编码器按原样传递一切而没有任何改变它应该工作)

无论如何,情况并非如此,我有点难以理解为什么。 我不确定我的代码或者我对autoencdoer实现的理解是否有问题。 这是代码供参考。

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)

`

我得到的输出应该与输入相同(或至少接近),但我得到了这个)

`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)`

任何帮助将不胜感激,我很可能理解错误,所以希望这个问题不难回答。

错误在这里被decoded = Dense(3, activation='sigmoid')(encoded)

你不应该使用sigmoid激活,因为它会限制范围(0,1)中的输出,用linear替换sigmoid或只删除它,你可以添加更多的纪元,例如列车1000个纪元。 在这种情况下,我得到你需要的东西

[[ 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有另一个名字,因为它是一个keyword在Python :-)。

应用@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]]

您的输入数据未规范化。 在如下标准化之后,您可以获得正确的输出。

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
 ....
 ....

您当然可以使用Sequential模型在Keras中构建自动编码器。 因此,我不确定您所指的示例是否是您可以创建的“最简单的自动编码器”,正如文章作者所声称的那样。 我是这样做的:

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)

在运行这个例子时,你得到了

 ....
 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.]]

这有点好......

暂无
暂无

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

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