简体   繁体   English

Z23EEEB4347BDD26BFC6B7EE9A3B755DD 中 keras 和 tensorflow.keras 之间的意外性能差异

[英]Unexpected performance differences between keras and tensorflow.keras in python

I am building a model in python based on a simple autoencoder example that I found online.我正在基于我在网上找到的一个简单的自动编码器示例在 python 中构建一个 model。 The example was written for keras.该示例是为 keras 编写的。 With the recommended transition to tensorflow.keras I modified the program's imports expecting no other changes would be needed.随着推荐的过渡到 tensorflow.keras 我修改了程序的导入,预计不需要其他更改。

With the keras imports用keras进口

from keras.layers import Input, Dense
from keras.models import Model
from keras.datasets import mnist

the autoencoder works fine, you can see it converge in the std output, and the recovered images make sense.自动编码器工作正常,您可以看到它在标准 output 中收敛,并且恢复的图像是有意义的。 When I use the tensorflow inputs当我使用 tensorflow 输入时

from tensorflow.python.keras.layers import Input, Dense
from tensorflow.python.keras.models import Model
from tensorflow.python.keras.datasets import mnist

the results no longer converge and the recovered images just look like noise.结果不再收敛,恢复的图像看起来像噪声。

Below is the minimum working example of my problem.以下是我的问题的最小工作示例。 Just change between the two imports above to reproduce the difference in behavior.只需在上面的两个导入之间进行更改即可重现行为差异。

import numpy as np
import matplotlib.pyplot as plt


def prepModel(inputShape, outputShape, numNeurons):
    input_image = Input(shape=(inputShape,))

    #encoded representation of input
    encoded = Dense(numNeurons, activation='relu')(input_image)
    #decoded lossy reconstruction
    decoded = Dense(outputShape, activation='sigmoid')(encoded)

    #model to encoded data
    autoencoder = Model(input_image, decoded)

    encoder = Model(input_image, encoded)
    encoded_input = Input(shape=(numNeurons,)) #placeholder
    decoder_layer = autoencoder.layers[-1] #last layer of model
    decoder = Model(encoded_input, decoder_layer(encoded_input)) #decoder model

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

    return autoencoder, encoder, decoder


def prepData():
    #import / set data
    (x_train, _), (x_test, _) = mnist.load_data()
    x_train = x_train.astype('float32')/255
    x_test = x_test.astype('float32')/255

    x_train = x_train.reshape((len(x_train), np.prod(x_train.shape[1:])))
    x_test = x_test.reshape((len(x_test), np.prod(x_test.shape[1:])))

    return x_train, x_test


def runModel(autoencoder, encoder, decoder, x_train, x_test):
    #train encoder
    autoencoder.fit(x_train, x_train,
                    epochs=50,
                    batch_size=256,
                    shuffle=True,
                    validation_data=(x_test, x_test))

    encoded_images = encoder.predict(x_test)
    decoded_images = decoder.predict(encoded_images)

    return encoded_images, decoded_images


def plotComparison(x_test, decoded_images):
    #Plot original image
    n = 10
    plt.figure(figsize=(20,4))
    for i in range(n):
        ax = plt.subplot(2,n,i+1)
        plt.imshow(x_test[i].reshape(28,28))
        plt.gray()
        ax.get_xaxis().set_visible(False)
        ax.get_yaxis().set_visible(False)
     #plot decompressed image
        ax = plt.subplot(2, n, i+1+n)
        plt.imshow(decoded_images[i].reshape(28,28))
        plt.gray()
        ax.get_xaxis().set_visible(False)
        ax.get_yaxis().set_visible(False)
    plt.show()


x_train, x_test = prepData()
autoencoder, encoder, decoder = prepModel(784, 784, 16)
encoded_images, decoded_images = runModel(autoencoder, encoder, decoder, x_train, x_test)
plotComparison(x_test, decoded_images)

I'm running python 3.8.3, keras version 2.3.1, and tensorflow version 2.2.0.我正在运行 python 3.8.3、keras 版本 2.3.1 和 tensorflow 版本 2.2.0。 I've fooled around with rescaling the input data and other naive tricks to no avail.我已经愚弄了重新调整输入数据和其他天真的技巧,但无济于事。 And I've verified the behavior on two other computers.我已经验证了另外两台计算机上的行为。 What could explain why the performance between the two sets of imports is so different?什么可以解释为什么两组进口产品的表现如此不同?

It seems that it is because of optimizer='adadelta' .似乎是因为optimizer='adadelta' As described here :如此所述:

the default learning rate for Adadelta optimizer in keras version is 1.0 and in tensorflow.keras is 0.001. keras 版本中 Adadelta 优化器的默认学习率为 1.0,在 tensorflow.keras 中为 0.001。

So to fix the problem try to use optimizer = tensorflow.keras.optimizers.Adadelta(lr = 1.0) instead of optimizer='adadelta' .因此,要解决此问题,请尝试使用optimizer = tensorflow.keras.optimizers.Adadelta(lr = 1.0)而不是optimizer='adadelta' Or alternatively you can use another optimizer, like 'adam'.或者,您也可以使用另一个优化器,例如“adam”。

Additinal note: Also as noted here try to use tensorflow.keras.* instead of tensorflow.python.keras.* . Additinal note: Also as noted here try to use tensorflow.keras.* instead of tensorflow.python.keras.* .

Importing from tensorflow.python or any other modules (including import tensorflow_core) is not supported, and can break unannounced.不支持从 tensorflow.python 或任何其他模块(包括导入 tensorflow_core)导入,并且可能会突然中断。

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

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