我正在尝试运行一个简单的自动编码器,所有的训练输入都是一样的。训练数据特征等于3,隐藏层中有3个节点。我用该输入训练自动编码器,然后我再次尝试预测它(编码/解码)(所以如果自动编码器按原样传递所有内容而不进行任何更改它应该工作)
无论如何,情况并非如此,而且我有点难以理解为什么。我不确定我的代码或者我对autoencdoer实现的理解是否有问题。以下是供参考的代码。
P.S。我玩了一些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)`
任何帮助都会受到赞赏,我很可能理解错误,所以希望这个问题不难回答。
答案 0 :(得分:6)
错误在这里decoded = Dense(3, activation='sigmoid')(encoded)
。
您不应该使用sigmoid
激活,因为它会限制范围(0,1)中的输出,将sigmoid
替换为linear
或者只删除它,以及你可以添加更多的纪元,例如训练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
替换为其他名称,因为它是Python中的keyword
: - 。
答案 1 :(得分:2)
在应用@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]]
答案 2 :(得分:1)
您的输入数据未规范化。在如下标准化之后,您可以获得正确的输出。
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
....
....
答案 3 :(得分:1)
您当然可以使用Sequential模型在Keras中构建自动编码器。因此,我不确定that the example you are referring to是否是您可以创建的“最简单的自动编码器”,正如文章作者所声称的那样。我就是这样做的:
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.]]
这很好......