【问题标题】:Tensorflow One Hot Encoding - Could not find valid device for nodeTensorflow One Hot Encoding - 找不到节点的有效设备
【发布时间】:2020-09-17 02:19:59
【问题描述】:

在我的功能工程期间发生以下错误。我的功能列表有 21 个子列表,每个 8537 值为 0 或 1。当尝试通过 tensorflow 运行 One Hot Encoding 时,它显示错误 Could not find valid device for node 有没有人能快速解决这个错误?

for feature in featurelist[1:]:
    df = tensorflow.convert_to_tensor(feature, dtype=tensorflow.float32)
    print(df)
    df_enc = tensorflow.one_hot(df, 2, on_value=None, off_value=None, axis=None, dtype=None, name=None)
    print(df_enc)
2020-05-29 15:08:41.969878: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x7fdefbc23d50 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2020-05-29 15:08:41.969919: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
tf.Tensor([0. 0. 0. ... 0. 0. 0.], shape=(8537,), dtype=float32)
Traceback (most recent call last):
  File "/Users/marius/Desktop/Masterarbeit/Github/virtual7/tempCodeRunnerFile.python", line 1234, in <module>
    df_enc = tensorflow.one_hot(df, 2, on_value=None, off_value=None, axis=None, dtype=None, name=None)
  File "/Users/marius/.pyenv/versions/3.7.3/lib/python3.7/site-packages/tensorflow_core/python/util/dispatch.py", line 180, in wrapper
    return target(*args, **kwargs)
  File "/Users/marius/.pyenv/versions/3.7.3/lib/python3.7/site-packages/tensorflow_core/python/ops/array_ops.py", line 3645, in one_hot
    name)
  File "/Users/marius/.pyenv/versions/3.7.3/lib/python3.7/site-packages/tensorflow_core/python/ops/gen_array_ops.py", line 5549, in one_hot
    _ops.raise_from_not_ok_status(e, name)
  File "/Users/marius/.pyenv/versions/3.7.3/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py", line 6606, in raise_from_not_ok_status
    six.raise_from(core._status_to_exception(e.code, message), None)
  File "<string>", line 3, in raise_from
tensorflow.python.framework.errors_impl.NotFoundError: Could not find valid device for node.
Node:{{node OneHot}}
All kernels registered for op OneHot :
  device='XLA_CPU_JIT'; TI in [DT_INT32, DT_UINT8, DT_INT64]; T in [DT_FLOAT, DT_DOUBLE, DT_INT32, DT_UINT8, DT_INT16, ..., DT_UINT16, DT_COMPLEX128, DT_HALF, DT_UINT32, DT_UINT64]
  device='XLA_CPU'; TI in [DT_INT32, DT_UINT8, DT_INT64]; T in [DT_FLOAT, DT_DOUBLE, DT_INT32, DT_UINT8, DT_INT16, ..., DT_UINT16, DT_COMPLEX128, DT_HALF, DT_UINT32, DT_UINT64]
  device='CPU'; TI in [DT_UINT8]; T in [DT_INT64]
  device='CPU'; TI in [DT_INT32]; T in [DT_INT64]
  device='CPU'; TI in [DT_INT64]; T in [DT_INT64]
  device='CPU'; TI in [DT_UINT8]; T in [DT_INT32]
  .....
  device='CPU'; TI in [DT_INT32]; T in [DT_INT32]
  device='CPU'; TI in [DT_INT64]; T in [DT_INT32]
  device='CPU'; TI in [DT_UINT8]; T in [DT_UINT16]


 [Op:OneHot] name: one_hot/```

【问题讨论】:

    标签: python tensorflow keras one-hot-encoding feature-engineering


    【解决方案1】:

    您将目标投射到tf.float32,这与tf.one_hot 不兼容在使用列表时。在 one-hot 编码之前,您需要将目标转换为整数 dtype。试试:

    x = tf.cast(x, tf.int32)
    

    或者,将您的张量转换为 NumPy 数组:

    tensor = np.array([0., 1., 2., 3.])
    
    tf.one_hot(tensor, depth=4)
    
    <tf.Tensor: shape=(4, 4), dtype=float32, numpy=
    array([[1., 0., 0., 0.],
           [0., 1., 0., 0.],
           [0., 0., 1., 0.],
           [0., 0., 0., 1.]], dtype=float32)>
    

    【讨论】:

    • 现在弹出错误:TypeError: cannot convert [1, 0, 0,...,1] to EagerTensor of dtype int32
    • 尝试将 feature 转换为一个 numpy 数组(它目前是一个列表,对吧?)
    • 谢谢,尼古拉斯!此错误消息确实一点也不直观。 tf.int32 是我需要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-07
    • 2020-01-21
    • 1970-01-01
    • 2021-12-25
    • 2021-04-15
    • 1970-01-01
    • 2021-11-02
    相关资源
    最近更新 更多