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