【发布时间】:2017-08-18 07:13:42
【问题描述】:
我有一个迭代器函数,它产生一批特征并标记为 numpy 数组的元组。
def batch_iter(): 为了 ...: 产量(np_features,np_labels)
然后我尝试像输入张量估计器一样
# the cnn_model_fn will print out shapes of various tensor when
# constructing the model
classifier = learn.Estimator(
model_fn=cnn_model_fn, model_dir="/tmp/convnet_model")
for train_data, train_labels in batch_iter():
classifier.fit(
input_fn=lambda: (tf.constant(train_data), tf.constant(train_labels)),
steps=1,
monitors=[logging_hook])
(带注释的)日志看起来像
conv1 shape (100, 16, 20, 32)
pool1 shape (100, 8, 10, 32)
conv2 shape (100, 8, 10, 64)
pool2 shape (100, 4, 5, 64)
onehot label shape (100, 5)
INFO:tensorflow:Create CheckpointSaverHook.
INFO:tensorflow:Saving checkpoints for 1 into /tmp/convnet_model/model.ckpt. # checkpoint is saved in every iteration
INFO:tensorflow:step = 1, loss = 1618.76
INFO:tensorflow:Loss for final step: 1618.76.
conv1 shape (100, 16, 20, 32) # the model_fn is called in every iteration
pool1 shape (100, 8, 10, 32)
conv2 shape (100, 8, 10, 64)
pool2 shape (100, 4, 5, 64)
onehot label shape (100, 5)
INFO:tensorflow:Create CheckpointSaverHook.
INFO:tensorflow:Restoring parameters from /tmp/convnet_model/model.ckpt-1 # checkpoint is restored in every iteration
INFO:tensorflow:Saving checkpoints for 2 into /tmp/convnet_model/model.ckpt.
INFO:tensorflow:step = 2, loss = 69370.6
INFO:tensorflow:Loss for final step: 69370.6.
conv1 shape (100, 16, 20, 32)
pool1 shape (100, 8, 10, 32)
conv2 shape (100, 8, 10, 64)
pool2 shape (100, 4, 5, 64)
onehot label shape (100, 5)
INFO:tensorflow:Create CheckpointSaverHook.
INFO:tensorflow:Restoring parameters from /tmp/convnet_model/model.ckpt-2
INFO:tensorflow:Saving checkpoints for 3 into /tmp/convnet_model/model.ckpt.
INFO:tensorflow:step = 3, loss = 289303.0
INFO:tensorflow:Loss for final step: 289303.0.
...
批次被读取,并且随着循环的迭代,损失确实会下降。但是,似乎在每次迭代中都会保存和恢复检查点,并且在每次迭代中都会调用 model_fn。所以我觉得不对。
将迭代器提供给 Estimator/Evaluable 的正确方法是什么?
【问题讨论】:
标签: python numpy tensorflow