Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Keras:无监督的预培训扼杀绩效_Python_Machine Learning_Keras_Neural Network_Autoencoder - Fatal编程技术网

Python Keras:无监督的预培训扼杀绩效

Python Keras:无监督的预培训扼杀绩效,python,machine-learning,keras,neural-network,autoencoder,Python,Machine Learning,Keras,Neural Network,Autoencoder,我试图在Keras中训练一个深层分类器,通过堆叠的自动编码器对隐藏层进行预训练和不进行预训练。我的问题是,预训练似乎会大幅降低性能(即,如果在代码下方的代码中将pretrain设置为False,则最终分类层的训练错误收敛得更快)。这对我来说似乎太过分了,因为预训练应该只初始化隐藏层的权重,我不认为这会完全破坏模型的性能,即使初始化效果不太好。我不能包括我使用的特定数据集,但任何适当的数据集(例如minist)都会产生影响。这里发生了什么,我该如何解决 编辑:代码现在可复制,使用MNIST数据,最

我试图在Keras中训练一个深层分类器,通过堆叠的自动编码器对隐藏层进行预训练和不进行预训练。我的问题是,预训练似乎会大幅降低性能(即,如果在代码下方的代码中将
pretrain
设置为
False
,则最终分类层的训练错误收敛得更快)。这对我来说似乎太过分了,因为预训练应该只初始化隐藏层的权重,我不认为这会完全破坏模型的性能,即使初始化效果不太好。我不能包括我使用的特定数据集,但任何适当的数据集(例如minist)都会产生影响。这里发生了什么,我该如何解决

编辑:代码现在可复制,使用MNIST数据,最后一行打印损失函数的变化,这在预训练中显著降低。 我还稍微修改了代码,并在下面添加了示例学习曲线:


如果您确实认为“任何适当的数据集(如mnist)都会产生影响”,那么请使用mnist数据发布一篇文章……我尝试使用scikit learn、tensorflow等中可用的一些方法自动下载数据集。所有这些方法都因其他原因而失败,或者在新版本中不可用。如果我只是在上面的代码中加载一些文本文件,这对任何人都没有帮助。我会想办法解决这个问题。好的,现在就开始吧。干得好-向上投票和取消近距离投票。。。唯一的问题是,你能和MNIST分享你得到的实际结果吗?
from functools import partial

import matplotlib.pyplot as plt
from keras.datasets import mnist
from keras.layers import Dense
from keras.models import Sequential
from keras.optimizers import SGD
from keras.regularizers import l2
from keras.utils import to_categorical

(inputs_train, targets_train), _ = mnist.load_data()

inputs_train = inputs_train[:1000].reshape(1000, 784)
targets_train = to_categorical(targets_train[:1000])

hidden_nodes = [256] * 4
learning_rate = 0.01
regularization = 1e-6
epochs = 30

def train_model(pretrain):
    model = Sequential()

    layer = partial(Dense,
                    activation='sigmoid',
                    kernel_initializer='random_normal',
                    kernel_regularizer=l2(regularization))

    for i, hn in enumerate(hidden_nodes):
        kwargs = dict(units=hn, name='hidden_{}'.format(i + 1))

        if i == 0:
            kwargs['input_dim'] = inputs_train.shape[1]

        model.add(layer(**kwargs))

    if pretrain:
        # train autoencoders
        inputs_train_ = inputs_train.copy()

        for i, hn in enumerate(hidden_nodes):
            autoencoder = Sequential()

            autoencoder.add(layer(units=hn,
                                  input_dim=inputs_train_.shape[1],
                                  name='hidden'))

            autoencoder.add(layer(units=inputs_train_.shape[1],
                                  name='decode'))

            autoencoder.compile(optimizer=SGD(lr=learning_rate, momentum=0.9),
                                loss='binary_crossentropy')

            autoencoder.fit(
                inputs_train_,
                inputs_train_,
                batch_size=32,
                epochs=epochs,
                verbose=0)

            autoencoder.pop()

            model.layers[i].set_weights(autoencoder.layers[0].get_weights())

            inputs_train_ = autoencoder.predict(inputs_train_)

    num_classes = targets_train.shape[1]

    model.add(Dense(units=num_classes,
                    activation='softmax',
                    name='classify'))

    model.compile(optimizer=SGD(lr=learning_rate, momentum=0.9),
                  loss='categorical_crossentropy')

    h = model.fit(
        inputs_train,
        targets_train,
        batch_size=32,
        epochs=epochs,
        verbose=0)

    return h.history['loss']

plt.plot(train_model(pretrain=False), label="Without Pre-Training")
plt.plot(train_model(pretrain=True), label="With Pre-Training")

plt.xlabel("Epoch")
plt.ylabel("Cross-Entropy")

plt.legend()

plt.show()