我正在努力复制在Kaggle内核中列出的qfxswpoi的tflearn autencoder。调用如下所示:
here
但是,在运行时,它会失败并显示一条错误,指示我的丢失函数尚未初始化:
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)
使用TFLearn,我不太确定全局变量初始化的正确位置,或者甚至可以解决问题。有人可以告诉你为什么会这样吗?
您可以尝试这样来修复此特定错误。在前一行之前移动---------------------------------------------------------------------------
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)]]
。
fit