python - NotFoundError : No registered 'PyFunc' OpKernel for 'CPU' devices compatible with node {{node PyFunc}} . 已注册:<没有注册的内核>

标签 python tensorflow2.0

尝试从 tf.data.Dataset 对象访问数据时出现错误。
数据集对象是从生成器构建的。任何帮助将不胜感激。
我正在使用 TensorFlow 2 并尝试运行来自 https://www.tensorflow.org/api_docs/python/tf/data/Dataset#from_generator 的示例

def gen(): 
  for i in itertools.count(1): 
    yield (i, [1] * i) 

dataset = tf.data.Dataset.from_generator( 
     gen, 
     (tf.int64, tf.int64), 
     (tf.TensorShape([]), tf.TensorShape([None]))) 

list(dataset.take(3).as_numpy_iterator()) 

错误是:
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/data/ops/iterator_ops.py in _next_internal(self)
    662         # Fast path for the case `self._structure` is not a nested structure.
--> 663         return self._element_spec._from_compatible_tensor_list(ret)  # pylint: disable=protected-access
    664       except AttributeError:

AttributeError: 'tuple' object has no attribute '_from_compatible_tensor_list'

During handling of the above exception, another exception occurred:

RuntimeError                              Traceback (most recent call last)
/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/eager/context.py in execution_mode(mode)
   1896     ctx.executor = executor_new
-> 1897     yield
   1898   finally:



...  
NotFoundError                             Traceback (most recent call last)
<ipython-input-25-ac0e933e02b3> in <module>
----> 1 list(dataset.take(3).as_numpy_iterator())

/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/data/ops/dataset_ops.py in __next__(self)
   3640 
   3641   def __next__(self):
-> 3642     return self.next()
   3643 
   3644 

/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/data/ops/dataset_ops.py in next(self)
   3637 
   3638   def next(self):
-> 3639     return nest.map_structure(lambda x: x.numpy(), next(self._iterator))
   3640 
   3641   def __next__(self):

/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/data/ops/iterator_ops.py in __next__(self)
    628 
    629   def __next__(self):  # For Python 3 compatibility
--> 630     return self.next()
    631 
    632   def _next_internal(self):

/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/data/ops/iterator_ops.py in next(self)
    672     """Returns a nested structure of `Tensor`s containing the next element."""
    673     try:
--> 674       return self._next_internal()
    675     except errors.OutOfRangeError:
    676       raise StopIteration

/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/data/ops/iterator_ops.py in _next_internal(self)
    663         return self._element_spec._from_compatible_tensor_list(ret)  # pylint: disable=protected-access
    664       except AttributeError:
--> 665         return structure.from_compatible_tensor_list(self._element_spec, ret)
    666 
    667   @property

/opt/conda/lib/python3.6/contextlib.py in __exit__(self, type, value, traceback)
     97                 value = type()
     98             try:
---> 99                 self.gen.throw(type, value, traceback)
    100             except StopIteration as exc:
    101                 # Suppress StopIteration *unless* it's the same exception that

/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/eager/context.py in execution_mode(mode)
   1898   finally:
   1899     ctx.executor = executor_old
-> 1900     executor_new.wait()
   1901 
   1902 

/opt/conda/lib/python3.6/site-packages/tensorflow_core/python/eager/executor.py in wait(self)
     65   def wait(self):
     66     """Waits for ops dispatched in this executor to finish."""
---> 67     pywrap_tensorflow.TFE_ExecutorWaitForAllPendingNodes(self._handle)
     68 
     69   def clear_error(self):

NotFoundError: No registered 'PyFunc' OpKernel for 'CPU' devices compatible with node {{node PyFunc}}
    .  Registered:  <no registered kernels>

最佳答案

我可以在 Google colab 的 TF 2.2.0 中执行您的代码

为了社区的利益,我在这里发布了带有输出的成功运行

import tensorflow as tf
print(tf.__version__)
import itertools

def gen(): 
  for i in itertools.count(1): 
    yield (i, [1] * i) 

dataset = tf.data.Dataset.from_generator( 
     gen, 
     (tf.int64, tf.int64), 
     (tf.TensorShape([]), tf.TensorShape([None]))) 

list(dataset.take(3).as_numpy_iterator()) 

输出:
2.2.0
[(1, array([1])), (2, array([1, 1])), (3, array([1, 1, 1]))]

关于python - NotFoundError : No registered 'PyFunc' OpKernel for 'CPU' devices compatible with node {{node PyFunc}} . 已注册:<没有注册的内核>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60801403/

相关文章:

python - TensorFlow 2.0 dataset.__iter__() 仅在启用即时执行时才受支持

python - 添加可见的GPU设备时遇到问题:0

python - django 在模板中实现过滤器

python - 使用 Pandas 在python中循环遍历多个excel文件

python - 如何从 Pandas 数据框中获取具有最大日期的行?

python - reshape .cc :55 stretch_dim ! = -1。节点号 X (RESHAPE) 准备失败

python - python的glob函数是否支持深度可变的通配符?

python - AWS Lambda (python) 中的 Cassandra 数据库 session 重用

python - 在此 numpy 分配中尾随逗号的效果是什么?

python-3.x - 属性错误: module 'tensorflow_core.keras.layers' has no attribute 'Conv1d'