TFLearn回归损失函数未初始化

时间:2018-08-30 01:15:13

标签: python tensorflow tflearn

我在尝试在Kaggle内核中复制here中列出的tflearn autencoder。调用看起来像这样:

class AutoEncoder():
    def __init__(self, layers):
        """layers should be a list of layer sizes"""
        self.layers = layers
        self.encoder = None
        self.decoder = None
        self.decoding_model = None
        self.encoding_model = None

    def fit(self, X):
        # build encoder
        self.encoder = [tflearn.input_data(shape=[None, X.shape[1]])]
        for layer in self.layers:
            self.encoder.append(tflearn.fully_connected(self.encoder[-1], layer))

        # build decoder
        self.decoder = [self.encoder[-1]]
        for layer in reversed(self.layers[:-1]):
            self.decoder.append(tflearn.fully_connected(self.decoder[-1], layer))
        self.decoder.append(tflearn.fully_connected(self.decoder[-1], X.shape[1], activation='sigmoid'))

        # regression
        net = tflearn.regression(self.decoder[-1], 
                                 optimizer='adam', 
                                 learning_rate=0.001, 
                                 # loss='mean_square',
                                 loss='weighted_crossentropy',
                                 metric=None)
        self.decoding_model = tflearn.DNN(net)

        #encoding
        self.encoding_model = tflearn.DNN(self.encoder[-1], session=self.decoding_model.session)



        self.decoding_model.fit(X, X, n_epoch=20, batch_size=256)
        return self.decoding_model

    def predict(self, X):
        return self.encoding_model.predict(X)

ae = AutoEncoder([1024, 256, 2])
ae.fit(X_train)

但是,在运行时,它失败并显示一个错误,指示我的损失函数尚未初始化:

---------------------------------------------------------------------------
FailedPreconditionError                   Traceback (most recent call last)
/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
   1291     try:
-> 1292       return fn(*args)
   1293     except errors.OpError as e:

/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in _run_fn(feed_dict, fetch_list, target_list, options, run_metadata)
   1276       return self._call_tf_sessionrun(
-> 1277           options, feed_dict, fetch_list, target_list, run_metadata)
   1278 

/opt/conda/lib/python3.6/site-packages/tensorflow/python/client/session.py in _call_tf_sessionrun(self, options, feed_dict, fetch_list, target_list, run_metadata)
   1366         self._session, options, feed_dict, fetch_list, target_list,
-> 1367         run_metadata)
   1368 

FailedPreconditionError: Attempting to use uninitialized value WeightedCrossentropy/Mean/moving_avg_1
     [[{{node WeightedCrossentropy/Mean/moving_avg_1/read}} = Identity[T=DT_FLOAT, _class=["loc:@Adam_1/moving_avg/AssignMovingAvg"], _device="/job:localhost/replica:0/task:0/device:CPU:0"](WeightedCrossentropy/Mean/moving_avg_1)]]

使用TFLearn,我不确定全局变量初始化的正确位置在哪里,或者甚至可以解决问题。有人可以告知为什么会这样吗?

1 个答案:

答案 0 :(得分:0)

您可以像这样尝试解决此特定错误。将fit移动到上一行之前。

    self.decoding_model = tflearn.DNN(net)
    self.decoding_model.fit(X, X, n_epoch=20, batch_size=256)

    #encoding
    self.encoding_model = tflearn.DNN(self.encoder[-1], session=self.decoding_model.session)