Keras:错误的训练时期数量

时间:2016-04-06 03:58:18

标签: python tensorflow deep-learning keras autoencoder

我正在尝试构建一个类来快速初始化和训练自动编码器以进行快速原型设计。我希望能做的一件事就是快速调整我训练的时代数。然而,似乎无论我做什么,模型训练每层100个时代!我正在使用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后端运行,但遇到了同样的问题。

Here是完整程序的链接。

1 个答案:

答案 0 :(得分:0)

Keras doc之后,fit方法使用默认的100个训练时期(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)

我确定您是如何运行这些方法的,但是遵循original code中的“典型用法”,您应该能够运行(根据需要调整变量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)