TensorFlow:运行DNN Iris示例

时间:2016-08-18 15:41:03

标签: python-3.x neural-network tensorflow

我正在尝试运行在此处找到的官方TensorFlow网站上提供的示例:https://www.tensorflow.org/versions/r0.10/tutorials/tflearn/index.html

为了完整起见,我正在运行的相关代码如下:

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import tensorflow as tf
import numpy as np

# Data sets
IRIS_TRAINING = "iris_training.csv"
IRIS_TEST = "iris_test.csv"

# Load datasets.
training_set = tf.contrib.learn.datasets.base.load_csv(filename=IRIS_TRAINING,
                                                       target_dtype=np.int)
test_set = tf.contrib.learn.datasets.base.load_csv(filename=IRIS_TEST,
                                                   target_dtype=np.int)

# Build 3 layer DNN with 10, 20, 10 units respectively.
classifier = tf.contrib.learn.DNNClassifier(hidden_units=[10, 20, 10],
                                            n_classes=3,
                                            model_dir="/tmp/iris_model")

# Fit model.
classifier.fit(x=training_set.data, 
               y=training_set.target, 
               steps=2000)

# Evaluate accuracy.
accuracy_score = classifier.evaluate(x=test_set.data,
                                     y=test_set.target)["accuracy"]
print('Accuracy: {0:f}'.format(accuracy_score))

# Classify two new flower samples.
new_samples = np.array(
    [[6.4, 3.2, 4.5, 1.5], [5.8, 3.1, 5.0, 1.7]], dtype=float)
y = classifier.predict(new_samples)
print('Predictions: {}'.format(str(y)))

现在,在我运行此示例时,我遇到以下错误:

WARNING:tensorflow:Change warning: `feature_columns` will be required after 2016-08-01.
Instructions for updating:
Pass `tf.contrib.learn.infer_real_valued_columns_from_input(x)` or `tf.contrib.learn.infer_real_valued_columns_from_input_fn(input_fn)` as `feature_columns`, where `x` or `input_fn` is your argument to `fit`, `evaluate`, or `predict`.
WARNING:tensorflow:Setting feature info to TensorSignature(dtype=tf.float32, shape=TensorShape([Dimension(None), Dimension(4)]), is_sparse=False)
WARNING:tensorflow:Setting targets info to TensorSignature(dtype=tf.int64, shape=TensorShape([Dimension(None)]), is_sparse=False)
E tensorflow/core/client/tensor_c_api.cc:485] Tensor name "hiddenlayer_2/weights/Adagrad" not found in checkpoint files /tmp/iris_model/model.ckpt-16000-?????-of-00001
         [[Node: save/restore_slice_18 = RestoreSlice[dt=DT_FLOAT, preferred_shard=0, _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_save/Const_0, save/restore_slice_18/tensor_name, save/restore_slice_18/shape_and_slice)]]
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 730, in _do_call
    return fn(*args)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 712, in _run_fn
    status, run_metadata)
  File "/usr/lib/python3.5/contextlib.py", line 66, in __exit__
    next(self.gen)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/errors.py", line 450, in raise_exception_on_not_ok_status
    pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors.NotFoundError: Tensor name "hiddenlayer_2/weights/Adagrad" not found in checkpoint files /tmp/iris_model/model.ckpt-16000-?????-of-00001
         [[Node: save/restore_slice_18 = RestoreSlice[dt=DT_FLOAT, preferred_shard=0, _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_save/Const_0, save/restore_slice_18/tensor_name, save/restore_slice_18/shape_and_slice)]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 26, in <module>
    steps=2000)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 240, in fit
    max_steps=max_steps)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 578, in _train_model
    max_steps=max_steps)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/graph_actions.py", line 276, in _supervised_train
    scaffold=scaffold) as super_sess:
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/supervised_session.py", line 212, in __init__
    self._sess = recoverable_session.RecoverableSession(self._create_session)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/recoverable_session.py", line 46, in __init__
    WrappedSession.__init__(self, sess_factory())
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/supervised_session.py", line 232, in _create_session
    init_fn=self._scaffold.init_fn)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/training/session_manager.py", line 164, in prepare_session
    max_wait_secs=max_wait_secs, config=config)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/training/session_manager.py", line 224, in recover_session
    saver.restore(sess, ckpt.model_checkpoint_path)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/training/saver.py", line 1129, in restore
    {self.saver_def.filename_tensor_name: save_path})
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 382, in run
    run_metadata_ptr)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 655, in _run
    feed_dict_string, options, run_metadata)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 723, in _do_run
    target_list, options, run_metadata)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py", line 743, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors.NotFoundError: Tensor name "hiddenlayer_2/weights/Adagrad" not found in checkpoint files /tmp/iris_model/model.ckpt-16000-?????-of-00001
         [[Node: save/restore_slice_18 = RestoreSlice[dt=DT_FLOAT, preferred_shard=0, _device="/job:localhost/replica:0/task:0/cpu:0"](_recv_save/Const_0, save/restore_slice_18/tensor_name, save/restore_slice_18/shape_and_slice)]]
Caused by op 'save/restore_slice_18', defined at:
  File "test.py", line 26, in <module>
    steps=2000)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 240, in fit
    max_steps=max_steps)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/estimators/estimator.py", line 578, in _train_model
    max_steps=max_steps)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/graph_actions.py", line 252, in _supervised_train
    keep_checkpoint_max=keep_checkpoint_max)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/supervised_session.py", line 152, in __init__
    lambda: training_saver.Saver(sharded=True,
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/supervised_session.py", line 164, in _get_or_default
    op = default_constructor()
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/contrib/learn/python/learn/supervised_session.py", line 153, in <lambda>
    max_to_keep=keep_checkpoint_max))
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/training/saver.py", line 861, in __init__
    restore_sequentially=restore_sequentially)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/training/saver.py", line 515, in build
    filename_tensor, per_device, restore_sequentially, reshape)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/training/saver.py", line 312, in _AddShardedRestoreOps
    name="restore_shard"))
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/training/saver.py", line 272, in _AddRestoreOps
    values = self.restore_op(filename_tensor, vs, preferred_shard)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/training/saver.py", line 187, in restore_op
    preferred_shard=preferred_shard)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/io_ops.py", line 203, in _restore_slice
    preferred_shard, name=name)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/gen_io_ops.py", line 359, in _restore_slice
    preferred_shard=preferred_shard, name=name)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/op_def_library.py", line 703, in apply_op
    op_def=op_def)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 2310, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 1232, in __init__
    self._traceback = _extract_stack()

但是,如果我更改&#34; hidden_​​units&#34;变量为[10]而不是[10,20,10],代码运行(albiet,仍然带有警告,但运行时没有错误)。

是否有某些原因在此网络中添加其他图层会导致我收到的错误?任何输入都会有所帮助,谢谢!

2 个答案:

答案 0 :(得分:1)

如果更新模型参数,则需要更改model_dir。同样,删除/ tmp / iris文件夹中的内容与保持模型状态的效果相同,并且如果更改参数,则会在重新适配或失败时尝试更新它。

答案 1 :(得分:0)

错误相当神秘,但我刚刚删除了创建的目录“/ tmp / iris”,再次尝试,它似乎运行正常。