Tensorflow 使用tf.estimator.estimator时,步长始终为0
我一直在努力学习最近从contrib迁移到MainAPI的layers and estimators框架。我遇到了一个相当奇怪的问题。我为MNIST编写了一个简单的自动编码器,但不知怎的,当我训练时,它总是说我在第0步,即使损失值在减少,所以我猜模型正在训练。当然,因为它不计算步骤,所以它不保存检查点,也不保存任何摘要。我不确定我做错了什么,所有的文档都指向旧的“tf.contrib.learn”框架,那里的很多API似乎都被标记为不推荐使用。我该怎么做?这是我的密码:Tensorflow 使用tf.estimator.estimator时,步长始终为0,tensorflow,Tensorflow,我一直在努力学习最近从contrib迁移到MainAPI的layers and estimators框架。我遇到了一个相当奇怪的问题。我为MNIST编写了一个简单的自动编码器,但不知怎的,当我训练时,它总是说我在第0步,即使损失值在减少,所以我猜模型正在训练。当然,因为它不计算步骤,所以它不保存检查点,也不保存任何摘要。我不确定我做错了什么,所有的文档都指向旧的“tf.contrib.learn”框架,那里的很多API似乎都被标记为不推荐使用。我该怎么做?这是我的密码: def encoder(
def encoder(x):
l1 = tf.layers.dense(x, 256, activation=tf.nn.relu, name='encode1')
l2 = tf.layers.dense(l1, 128, activation=tf.nn.relu, name='encode2')
return l2
def decoder(x):
l1 = tf.layers.dense(x, 256, activation=tf.nn.relu, name='decode1')
l2 = tf.layers.dense(l1, 784, activation=tf.nn.relu, name='decode2')
return l2
def loss(labels, preds):
return tf.losses.huber_loss(labels, preds)
def train(loss):
optimizer = tf.train.AdamOptimizer()
return optimizer.minimize(loss)
def model_fn(features, labels, mode):
_encoder = encoder(features)
_decoder = decoder(_encoder)
_loss = loss(labels, _decoder)
_train = train(_loss)
return tf.estimator.EstimatorSpec(mode=mode,
predictions=_decoder,
loss=_loss,
train_op=_train)
data = input_data.read_data_sets(".", one_hot=True)
display.clear_output()
# remove current log dir
shutil.rmtree('logs', ignore_errors=True)
def input_fn():
if data.train.epochs_completed <= 10:
features, labels = data.train.next_batch(100)
return tf.constant(features), tf.constant(features)
raise StopIteration
estimator = tf.estimator.Estimator(model_fn, "logs")
estimator.train(input_fn=input_fn)
在
training op
中,您需要设置global\u step
参数,该参数是针对每个模型训练运行递增的步长计数器。因此,请改为:
optimizer.minimize(loss, global_step=tf.train.get_global_step())
非常感谢。这似乎是一件需要内部处理的事情,毕竟train op是定义好的,所以为什么它不能计算它运行的次数。甚至我都很惊讶为什么它没有在内部完成。但是这本指南提到了如何定义培训op:是的,我有点希望这些东西在1.3中得到澄清。1.2在核心tensorflow API和tf.contrib.learn API之间似乎有很多混淆。我已经发布了另外两个关于使用estimator框架的问题。你能看一下吗?当然,我去看看
optimizer.minimize(loss, global_step=tf.train.get_global_step())