[英]TypeError: <tf.Tensor: shape ... > has type <class 'tensorflow.python.framework.ops.EagerTensor'>, but expected one of: (<class 'int'>,)
I have been trying to run the code from this tutorial on tf.data.我一直在尝试在 tf.data 上运行 本教程中的代码。 But I am getting this error when trying to execute it in vs code.
但是当我尝试在 vs 代码中执行它时出现此错误。
TypeError: <tf.Tensor: shape=(), dtype=bool, numpy=False> has type <class 'tensorflow.python.framework.ops.EagerTensor'>, but expected one of: (<class 'int'>,)
where value is of type <class 'tensorflow.python.framework.ops.EagerTensor'>
This same code is working fine in google colab.其中值的类型为
<class 'tensorflow.python.framework.ops.EagerTensor'>
同样的代码在 google colab 中运行良好。
This is the issue related to this error in GitHub, but I didn't find anything helpful there.这是与 GitHub 中的此错误相关的问题,但我在那里找不到任何帮助。
Python version: 3.8 Python 版本:3.8
Tensorflow: Tensorflow-gpu 2.2.0 Tensorflow:Tensorflow-GPU 2.2.0
Code:代码:
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)
Error:错误:
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]
Not sure much, but Im able to fix this issue with some of the type casting changes:不太确定,但我可以通过一些类型转换更改来解决此问题:
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.