python - 类型错误:有类型,但预期其中之一:(,)
问题描述
我一直在尝试在 tf.data 上运行本教程中的代码。但是当我尝试在 vs 代码中执行它时出现此错误。
TypeError: <tf.Tensor: shape=(), dtype=bool, numpy=False> has type <class 'tensorflow.python.framework.ops.EagerTensor'>, but expected one of: (<class 'int'>,)
其中值的类型<class 'tensorflow.python.framework.ops.EagerTensor'>
相同的代码在 google colab 中运行良好。
这是与 GitHub 中的此错误相关的问题,但我在那里没有发现任何帮助。
Python版本:3.8
张量流:张量流-GPU 2.2.0
代码:
import numpy as np
import tensorflow as tf
def _bytes_feature(value):
if isinstance(value, type(tf.constant(0))):
value = value.numpy()
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
def _float_feature(value):
return tf.train.Feature(float_list=tf.train.FloatList(value=[value]))
def _int64_feature(value):
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
n_observations = int(1e4)
feature0 = np.random.choice(a=[False, True], size=n_observations)
feature1 = np.random.randint(0, 5, n_observations)
strings = np.array([b'cat', b'dog', b'chicken', b'horse', b'goat'])
feature2 = np.random.choice(a=strings, size=n_observations)
feature3 = np.random.randn(n_observations)
def serialize_example(feature0, feature1, feature2, feature3):
feature = {
'feature0': _int64_feature(feature0),
'feature1': _int64_feature(feature1),
'feature2': _bytes_feature(feature2),
'feature3': _float_feature(feature3)
}
example_proto = \
tf.train.Example(features=tf.train.Features(feature=feature))
return example_proto.SerializeToString()
features_dataset = tf.data.Dataset.from_tensor_slices((feature0, feature1,
feature2, feature3))
def tf_serialize_example(f0, f1, f2, f3):
tf_string = tf.py_function(
serialize_example,
(f0, f1, f2, f3),
tf.string
)
return tf.reshape(tf_string, ())
for f0, f1, f2, f3 in features_dataset.take(1):
print(f0)
print(f1)
print(f2)
print(f3)
tf_serialize_example(f0, f1, f2, f3)
错误:
2020-06-10 16:55:39.480946: W tensorflow/core/framework/op_kernel.cc:1741] Invalid argument: TypeError: <tf.Tensor: shape=(), dtype=bool, numpy=True> has type <class 'tensorflow.python.framework.ops.EagerTensor'>, but expected one of: (<class 'int'>,)
Traceback (most recent call last):
File "c:\Users\hp\anaconda3\envs\QueryAnsweringSystem\lib\site-packages\tensorflow\python\ops\script_ops.py", line 241, in __call__
return func(device, token, args)
File "c:\Users\hp\anaconda3\envs\QueryAnsweringSystem\lib\site-packages\tensorflow\python\ops\script_ops.py", line 130, in __call__
ret = self._func(*args)
File "c:\Users\hp\anaconda3\envs\QueryAnsweringSystem\lib\site-packages\tensorflow\python\autograph\impl\api.py", line 309, in wrapper
return func(*args, **kwargs)
File "g:/Project/Query-Answering-System/code/lab.py", line 34, in serialize_example
'feature0': _int64_feature(feature0),
File "g:/Project/Query-Answering-System/code/lab.py", line 17, in _int64_feature
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
File "c:\Users\hp\anaconda3\envs\QueryAnsweringSystem\lib\site-packages\google\protobuf\internal\python_message.py", line 542, in init
copy.extend(field_value)
File "c:\Users\hp\anaconda3\envs\QueryAnsweringSystem\lib\site-packages\google\protobuf\internal\containers.py", line 282, in extend
new_values = [self._type_checker.CheckValue(elem) for elem in elem_seq_iter]
File "c:\Users\hp\anaconda3\envs\QueryAnsweringSystem\lib\site-packages\google\protobuf\internal\containers.py", line 282, in <listcomp>
new_values = [self._type_checker.CheckValue(elem) for elem in elem_seq_iter]
File "c:\Users\hp\anaconda3\envs\QueryAnsweringSystem\lib\site-packages\google\protobuf\internal\type_checkers.py", line 171, in CheckValue
raise TypeError(message)
TypeError: <tf.Tensor: shape=(), dtype=bool, numpy=True> has type <class 'tensorflow.python.framework.ops.EagerTensor'>, but expected one of: (<class 'int'>,)
Traceback (most recent call last):
File "g:/Project/Query-Answering-System/code/lab.py", line 66, in <module>
tf_serialize_example(f0, f1, f2, f3)
File "g:/Project/Query-Answering-System/code/lab.py", line 51, in tf_serialize_example
tf_string = tf.py_function(
File "c:\Users\hp\anaconda3\envs\QueryAnsweringSystem\lib\site-packages\tensorflow\python\ops\script_ops.py", line 454, in eager_py_func
return _internal_py_func(
File "c:\Users\hp\anaconda3\envs\QueryAnsweringSystem\lib\site-packages\tensorflow\python\ops\script_ops.py", line 336, in _internal_py_func
result = gen_script_ops.eager_py_func(
File "c:\Users\hp\anaconda3\envs\QueryAnsweringSystem\lib\site-packages\tensorflow\python\ops\gen_script_ops.py", line 56, in eager_py_func
_ops.raise_from_not_ok_status(e, name)
File "c:\Users\hp\anaconda3\envs\QueryAnsweringSystem\lib\site-packages\tensorflow\python\framework\ops.py", line 6653, 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.InvalidArgumentError: TypeError: <tf.Tensor: shape=(), dtype=bool, numpy=True> has type <class 'tensorflow.python.framework.ops.EagerTensor'>, but expected one of: (<class 'int'>,)
Traceback (most recent call last):
File "c:\Users\hp\anaconda3\envs\QueryAnsweringSystem\lib\site-packages\tensorflow\python\ops\script_ops.py", line 241, in __call__
return func(device, token, args)
File "c:\Users\hp\anaconda3\envs\QueryAnsweringSystem\lib\site-packages\tensorflow\python\ops\script_ops.py", line 130, in __call__
ret = self._func(*args)
File "c:\Users\hp\anaconda3\envs\QueryAnsweringSystem\lib\site-packages\tensorflow\python\autograph\impl\api.py", line 309, in wrapper
return func(*args, **kwargs)
File "g:/Project/Query-Answering-System/code/lab.py", line 34, in serialize_example
'feature0': _int64_feature(feature0),
File "g:/Project/Query-Answering-System/code/lab.py", line 17, in _int64_feature
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
File "c:\Users\hp\anaconda3\envs\QueryAnsweringSystem\lib\site-packages\google\protobuf\internal\python_message.py", line 542, in init
copy.extend(field_value)
File "c:\Users\hp\anaconda3\envs\QueryAnsweringSystem\lib\site-packages\google\protobuf\internal\containers.py", line 282, in extend
new_values = [self._type_checker.CheckValue(elem) for elem in elem_seq_iter]
File "c:\Users\hp\anaconda3\envs\QueryAnsweringSystem\lib\site-packages\google\protobuf\internal\containers.py", line 282, in <listcomp>
new_values = [self._type_checker.CheckValue(elem) for elem in elem_seq_iter]
File "c:\Users\hp\anaconda3\envs\QueryAnsweringSystem\lib\site-packages\google\protobuf\internal\type_checkers.py", line 171, in CheckValue
raise TypeError(message)
TypeError: <tf.Tensor: shape=(), dtype=bool, numpy=True> has type <class 'tensorflow.python.framework.ops.EagerTensor'>, but expected one of: (<class 'int'>,)
[Op:EagerPyFunc]
解决方案
不太确定,但我可以通过一些类型转换更改来解决这个问题:
def serialize_example(feature0, feature1, feature2, feature3):
feature = {
'feature0': _int64_feature(bool(feature0)),
'feature1': _int64_feature(float(feature1)),
'feature2': _bytes_feature(feature2),
'feature3': _float_feature(feature3)
}
example_proto = \
tf.train.Example(features=tf.train.Features(feature=feature))
return example_proto.SerializeToString()
推荐阅读
- pdf - 使用 Ghostscript 将扫描的 PDF 转换为纯文本
- python - 重复的查询行
- jenkins - 如何使用其 API 在 Jenkins 中获取构建历史记录?
- python - Python selenium 在模态中找不到元素
- java - 字符串返回值上的 compareTo java 方法
- azure-ad-b2c - B2C 自定义内容 - 将声明值输出为隐藏字段
- python - Tweepy twitter bot 如何防止在一段时间内重复推文
- node.js - GitHub 操作 CI 检查 npm 测试保持不间断运行
- html - 如果 Iphone 水平转动,HTML5 视频将不会显示
- python - 如何从 Gurobi 中简单地读出/保存优化的变量