首页 文章

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

提问于
浏览
3

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

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

附:我玩了一些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)`

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

4 回答

  • 1

    错误在这里 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 ,因为它是Python中的 keyword :-) .

  • 1

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

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

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

    您当然可以使用Sequential模型在Keras中构建自动编码器 . 所以我不确定that the example you are referring to究竟是你能创造的"simplest possible autoencoder" . 我是这样做的:

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

    这有点好......

相关问题