【BUG记录_挖了个坑】Function _make_dataset_ee0af26d is not defined. OneShotIterator_1 = OneShotIterator

在学习Tensorflow 实战Goole深度学习框架第二版,第8章,使用rnn预测正弦函数实例的时候出现的bug.

pycharm:报错提示:NotFoundError (see above for traceback): Function _make_dataset_ee0af26d is not defined.
     [[Node: OneShotIterator_1 = OneShotIterator[container="", dataset_factory=_make_dataset_ee0af26d[], output_shapes=[[-1,1,10], [-1,1]], output_types=[DT_FLOAT, DT_FLOAT], shared_name="", _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

使用环境是tensorflow1.2,这个报错在tensorflow1.4(也是书上的推荐环境,不知道自己脑子咋想的,非要在tf1.2跑跑试试)就不会出现,应该是tf源码改进了,原谅我不会找.

原因:使用了两个tf.contrib.data.Dataset.make_one_shot_iterator()迭代器时候,声明第二个迭代器的在sess.run(tf.global_variables_initializer())之后了,但是实测在tf1.4之上就没有这种限制,随意玩。

解决办法:所有make_one_shot_iterator迭代器声明之后再sess.run(tf.global_variables_initializer())初始化变量就好了

下面就是记录一下解决这个bug查的资料,方便水平足够了,研究研究源码。

一:查看文档

tensorflow1.2中关于make_one_shot_iterator的说明https://github.com/tensorflow/docs/blob/r1.2/site/en/api_docs/python/tf/contrib/data/Dataset.md

位置是

 

tensorflow1.4的文档,但是要注意contrib.data这个包已经合并到tf的核心函数了,网址:https://github.com/tensorflow/docs/blob/r1.4/site/en/api_docs/api_docs/python/tf/data/Dataset.md
 
光看tf1.2和1.4文档没看出来啥重大改变。只能研究源码了,但是我看不懂啊QAQ

二:文档没找到啥有用的,就还是直接程序调试看看

就把书上示例提取了关键的报错代码

import tensorflow as tf
import numpy as np


HIDDEN_SIZE = 30                            # LSTM中隐藏节点的个数。
NUM_LAYERS = 2                              # LSTM的层数。

TIMESTEPS = 10                              # 循环神经网络的训练序列长度。
TRAINING_STEPS = 10000                      # 训练轮数。
BATCH_SIZE = 32                             # batch大小。

TRAINING_EXAMPLES = 10000                   # 训练数据个数。
TESTING_EXAMPLES = 1000                     # 测试数据个数。
SAMPLE_GAP = 0.01                           # 采样间隔。
def generate_data(seq):
    X = []
    y = []
    # 序列的第i项和后面的TIMESTEPS-1项合在一起作为输入;第i + TIMESTEPS项作为输
    # 出。即用sin函数前面的TIMESTEPS个点的信息,预测第i + TIMESTEPS个点的函数值。
    for i in range(len(seq) - TIMESTEPS):
        X.append([seq[i: i + TIMESTEPS]])
        y.append([seq[i + TIMESTEPS]])
    return np.array(X, dtype=np.float32), np.array(y, dtype=np.float32)

# 用正弦函数生成训练和测试数据集合。
test_start = (TRAINING_EXAMPLES + TIMESTEPS) * SAMPLE_GAP
test_end = test_start + (TESTING_EXAMPLES + TIMESTEPS) * SAMPLE_GAP
train_X, train_y = generate_data(np.sin(np.linspace(
    0, test_start, TRAINING_EXAMPLES + TIMESTEPS, dtype=np.float32)))
test_X, test_y = generate_data(np.sin(np.linspace(
    test_start, test_end, TESTING_EXAMPLES + TIMESTEPS, dtype=np.float32)))

with tf.Session(config=tf.ConfigProto(device_count={'GPU':0})) as sess:
    # 将训练数据以数据集的方式提供给计算图。
    ds = tf.contrib.data.Dataset.from_tensor_slices((train_X, train_y))
    ds = ds.batch(1)
    X, y = ds.make_one_shot_iterator().get_next()#声明一个迭代器

    sess.run(tf.global_variables_initializer())#初始化参数

    ds2 = tf.contrib.data.Dataset.from_tensor_slices((test_X, test_y))
    ds2 = ds2.batch(1)
    ds2=ds2.make_one_shot_iterator()#再声明一个迭代器
    X2, y2 = ds2.get_next()
    print(sess.run(X2))

所有报错信息

Traceback (most recent call last):
  File "F:\python\Anaconda3.4.1\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py", line 1139, in _do_call
    return fn(*args)
  File "F:\python\Anaconda3.4.1\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py", line 1121, in _run_fn
    status, run_metadata)
  File "F:\python\Anaconda3.4.1\envs\tensorflow\lib\contextlib.py", line 88, in __exit__
    next(self.gen)
  File "F:\python\Anaconda3.4.1\envs\tensorflow\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 466, in raise_exception_on_not_ok_status
    pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors_impl.NotFoundError: Function _make_dataset_ee0af26d is not defined.
     [[Node: OneShotIterator_1 = OneShotIterator[container="", dataset_factory=_make_dataset_ee0af26d[], output_shapes=[[-1,1,10], [-1,1]], output_types=[DT_FLOAT, DT_FLOAT], shared_name="", _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:/Users/Hao/Desktop/检测错误/报错问题2.py", line 45, in <module>
    print(sess.run(X2))
  File "F:\python\Anaconda3.4.1\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py", line 789, in run
    run_metadata_ptr)
  File "F:\python\Anaconda3.4.1\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py", line 997, in _run
    feed_dict_string, options, run_metadata)
  File "F:\python\Anaconda3.4.1\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py", line 1132, in _do_run
    target_list, options, run_metadata)
  File "F:\python\Anaconda3.4.1\envs\tensorflow\lib\site-packages\tensorflow\python\client\session.py", line 1152, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.NotFoundError: Function _make_dataset_ee0af26d is not defined.
     [[Node: OneShotIterator_1 = OneShotIterator[container="", dataset_factory=_make_dataset_ee0af26d[], output_shapes=[[-1,1,10], [-1,1]], output_types=[DT_FLOAT, DT_FLOAT], shared_name="", _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

Caused by op 'OneShotIterator_1', defined at:
  File "C:/Users/Hao/Desktop/检测错误/报错问题2.py", line 43, in <module>
    ds2=ds2.make_one_shot_iterator()
  File "F:\python\Anaconda3.4.1\envs\tensorflow\lib\site-packages\tensorflow\contrib\data\python\ops\dataset_ops.py", line 419, in make_one_shot_iterator
    output_shapes=nest.flatten(self.output_shapes)), None,
  File "F:\python\Anaconda3.4.1\envs\tensorflow\lib\site-packages\tensorflow\python\ops\gen_dataset_ops.py", line 344, in one_shot_iterator
    name=name)
  File "F:\python\Anaconda3.4.1\envs\tensorflow\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 767, in apply_op
    op_def=op_def)
  File "F:\python\Anaconda3.4.1\envs\tensorflow\lib\site-packages\tensorflow\python\framework\ops.py", line 2506, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "F:\python\Anaconda3.4.1\envs\tensorflow\lib\site-packages\tensorflow\python\framework\ops.py", line 1269, in __init__
    self._traceback = _extract_stack()

NotFoundError (see above for traceback): Function _make_dataset_ee0af26d is not defined.
     [[Node: OneShotIterator_1 = OneShotIterator[container="", dataset_factory=_make_dataset_ee0af26d[], output_shapes=[[-1,1,10], [-1,1]], output_types=[DT_FLOAT, DT_FLOAT], shared_name="", _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

看了看报错,感觉是gen_dataset_ops.py 里面的one_shot_iterator函数,但还是没搞明白,什么时候调用的这个函数,这个函数本身也没看懂,哎。

def one_shot_iterator(dataset_factory, output_types, output_shapes,
                      container=None, shared_name=None, name=None):
  r"""Makes a "one-shot" iterator that can be iterated only once.

  A one-shot iterator bundles the logic for defining the dataset and
  the state of the iterator in a single op, which allows simple input
  pipelines to be defined without an additional initialization
  ("MakeIterator") step.

  One-shot iterators have the following limitations:

  * They do not support parameterization: all logic for creating the underlying
    dataset must be bundled in the `dataset_factory` function.
  * They are not resettable. Once a one-shot iterator reaches the end of its
    underlying dataset, subsequent "IteratorGetNext" operations on that
    iterator will always produce an `OutOfRange` error.

  For greater flexibility, use "Iterator" and "MakeIterator" to define
  an iterator using an arbitrary subgraph, which may capture tensors
  (including fed values) as parameters, and which may be reset multiple
  times by rerunning "MakeIterator".

  Args:
    dataset_factory: A function decorated with @Defun.
      A function of type `() -> DT_RESOURCE`, where the returned
      DT_RESOURCE is a handle to a dataset.
    output_types: A list of `tf.DTypes` that has length `>= 1`.
    output_shapes: A list of shapes (each a `tf.TensorShape` or list of `ints`) that has length `>= 1`.
    container: An optional `string`. Defaults to `""`.
    shared_name: An optional `string`. Defaults to `""`.
    name: A name for the operation (optional).

  Returns:
    A `Tensor` of type `resource`.
    A handle to the iterator that can be passed to an "IteratorGetNext"
    op.
  """
  result = _op_def_lib.apply_op("OneShotIterator",
                                dataset_factory=dataset_factory,
                                output_types=output_types,
                                output_shapes=output_shapes,
                                container=container, shared_name=shared_name,
                                name=name)

  return result
下面是tf1.4的one_shot_iterator函数定义,好吧,我死了,看不懂。
def one_shot_iterator(dataset_factory, output_types, output_shapes, container="", shared_name="", name=None):
  r"""Makes a "one-shot" iterator that can be iterated only once.

  A one-shot iterator bundles the logic for defining the dataset and
  the state of the iterator in a single op, which allows simple input
  pipelines to be defined without an additional initialization
  ("MakeIterator") step.

  One-shot iterators have the following limitations:

  * They do not support parameterization: all logic for creating the underlying
    dataset must be bundled in the `dataset_factory` function.
  * They are not resettable. Once a one-shot iterator reaches the end of its
    underlying dataset, subsequent "IteratorGetNext" operations on that
    iterator will always produce an `OutOfRange` error.

  For greater flexibility, use "Iterator" and "MakeIterator" to define
  an iterator using an arbitrary subgraph, which may capture tensors
  (including fed values) as parameters, and which may be reset multiple
  times by rerunning "MakeIterator".

  Args:
    dataset_factory: A function decorated with @Defun.
      A function of type `() -> DT_VARIANT`, where the returned
      DT_VARIANT is a dataset.
    output_types: A list of `tf.DTypes` that has length `>= 1`.
    output_shapes: A list of shapes (each a `tf.TensorShape` or list of `ints`) that has length `>= 1`.
    container: An optional `string`. Defaults to `""`.
    shared_name: An optional `string`. Defaults to `""`.
    name: A name for the operation (optional).

  Returns:
    A `Tensor` of type `resource`.
    A handle to the iterator that can be passed to an "IteratorGetNext"
    op.
  """
  if not isinstance(output_types, (list, tuple)):
    raise TypeError(
        "Expected list for 'output_types' argument to "
        "'one_shot_iterator' Op, not %r." % output_types)
  output_types = [_execute.make_type(_t, "output_types") for _t in output_types]
  if not isinstance(output_shapes, (list, tuple)):
    raise TypeError(
        "Expected list for 'output_shapes' argument to "
        "'one_shot_iterator' Op, not %r." % output_shapes)
  output_shapes = [_execute.make_shape(_s, "output_shapes") for _s in output_shapes]
  if container is None:
    container = ""
  container = _execute.make_str(container, "container")
  if shared_name is None:
    shared_name = ""
  shared_name = _execute.make_str(shared_name, "shared_name")
  _ctx = _context.context()
  if _ctx.in_graph_mode():
    _, _, _op = _op_def_lib._apply_op_helper(
        "OneShotIterator", dataset_factory=dataset_factory,
        output_types=output_types, output_shapes=output_shapes,
        container=container, shared_name=shared_name, name=name)
    _result = _op.outputs[:]
    _inputs_flat = _op.inputs
    _attrs = ("dataset_factory", _op.get_attr("dataset_factory"),
              "output_types", _op.get_attr("output_types"), "output_shapes",
              _op.get_attr("output_shapes"), "container",
              _op.get_attr("container"), "shared_name",
              _op.get_attr("shared_name"))
  else:
    _inputs_flat = []
    _attrs = ("dataset_factory", dataset_factory, "output_types",
              output_types, "output_shapes", output_shapes, "container",
              container, "shared_name", shared_name)
    _result = _execute.execute(b"OneShotIterator", 1, inputs=_inputs_flat,
                               attrs=_attrs, ctx=_ctx, name=name)
  _execute.record_gradient(
      "OneShotIterator", _inputs_flat, _attrs, _result, name)
  _result, = _result

  return _result

又是一个坑,之后再研究吧。

三 总结就是:所有make_one_shot_iterator迭代器声明之后再sess.run(tf.global_variables_initializer())初始化变量。要不然就是别全局初始化,直接run迭代器.text()也是可以的,代码如下图也是可以的。

# 将训练数据以数据集的方式提供给计算图。
ds = tf.contrib.data.Dataset.from_tensor_slices((train_X, train_y))
ds = ds.batch(1)
X, y = ds.make_one_shot_iterator().get_next()

#sess.run(tf.global_variables_initializer())

ds2 = tf.contrib.data.Dataset.from_tensor_slices((test_X, test_y))
ds2 = ds2.batch(1)
ds2=ds2.make_one_shot_iterator()
X2, y2 = ds2.get_next()
print(sess.run(X2))
print(sess.run(X))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值