0

我正在尝试构建一个类来快速初始化和训练自动编码器以进行快速原型设计。我想做的一件事是快速调整我训练的时期数。但是,似乎无论我做什么,模型都会将每一层训练 100 个 epoch!我正在使用 tensorflow 后端。

这是两种违规方法的代码。

    def pretrain(self, X_train, nb_epoch = 10):
    data = X_train
    for ae in self.pretrains:            
        ae.fit(data, data, nb_epoch = nb_epoch)
        ae.layers[0].output_reconstruction = False
        ae.compile(optimizer='sgd', loss='mse')
        data = ae.predict(data)

.........

    def fine_train(self, X_train, nb_epoch):
    weights = [ae.layers[0].get_weights() for ae in self.pretrains]

    dims = self.dims
    encoder = containers.Sequential()
    decoder = containers.Sequential()

    ## add special input encoder
    encoder.add(Dense(output_dim = dims[1], input_dim = dims[0], 
        weights = weights[0][0:2], activation = 'linear'))

    ## add the rest of the encoders
    for i in range(1, len(dims) - 1):
        encoder.add(Dense(output_dim = dims[i+1],
            weights = weights[i][0:2], activation = self.act))

    ## add the decoders from the end

    decoder.add(Dense(output_dim = dims[len(dims) - 2], input_dim = dims[len(dims) - 1],
        weights = weights[len(dims) - 2][2:4], activation = self.act))

    for i in range(len(dims) - 2, 1, -1):
        decoder.add(Dense(output_dim = dims[i - 1],
            weights = weights[i-1][2:4], activation = self.act))

    ## add the output layer decoder
    decoder.add(Dense(output_dim = dims[0], 
        weights = weights[0][2:4], activation = 'linear'))



    masterAE = AutoEncoder(encoder = encoder, decoder = decoder)
    masterModel = models.Sequential()
    masterModel.add(masterAE)
    masterModel.compile(optimizer = 'sgd', loss = 'mse')
    masterModel.fit(X_train, X_train, nb_epoch = nb_epoch)
    self.model = masterModel

任何有关如何解决问题的建议将不胜感激。我最初的怀疑是它与 tensorflow 有关,所以我尝试使用 theano 后端运行,但遇到了同样的问题。

是完整程序的链接。

4

1 回答 1

0

根据Keras 文档,该fit方法使用默认的 100 个训练 epoch ( nb_epoch=100):

fit(X, y, batch_size=128, nb_epoch=100, verbose=1, callbacks=[], validation_split=0.0, validation_data=None, shuffle=True, show_accuracy=False, class_weight=None, sample_weight=None)

我确定您是如何运行这些方法的,但是按照原始代码中的“典型用法” ,您应该能够运行类似的东西(num_epoch根据需要调整变量):

#Typical usage:
num_epoch = 10
ae = JPAutoEncoder(dims)
ae.pretrain(X_train, nb_epoch = num_epoch)
ae.train(X_train, nb_epoch = num_epoch)
ae.predict(X_val)
于 2016-04-07T12:53:04.003 回答