问题描述
我使用的是 TensorFlow r1.7 和 python3.6.5.我对 TensorFlow 也很陌生,所以如果可能的话,我希望有易于阅读的解释.
I'm using TensorFlow r1.7 and python3.6.5. I am also very new to TensorFlow, so I'd like easy to read explanations if possible.
我正在尝试使用此函数 tf.data.Dataset.from_tensor_slices()
将我的输入数据转换为张量数据集.我将混合数据类型的元组传递给这个函数.但是,在运行我的代码时,我收到此错误:ValueError: Can't convert Python sequence with mixed types to Tensor
.
I'm trying to convert my input data into a dataset of tensors with this function tf.data.Dataset.from_tensor_slices()
. I pass my tuple with mixed datatypes into this function. However, when running my code I get this error: ValueError: Can't convert Python sequence with mixed types to Tensor
.
我想知道为什么我会收到此错误,以及如何将我的数据转换为张量数据集,即使是混合数据类型.
I want to know why I am receiving this error, and how I can convert my data to a dataset of tensors even with mixed datatypes.
这是我的元组中前 5 个条目的打印输出.
Here's a printout of the top 5 entries in my tuple.
(13501, 2, None, 51, '2232', 'S35', '734.72', 'CLA', '240', 1035, 2060, 1252, 1182, 10, '967.28', '338.50', None, 14, 102, 3830)
(15124, 2, None, 57, '2641', 'S35', '234.80', 'DDA', '240', 743, 1597, 4706, 156, 0, None, None, None, 3, 27, 981)
(40035, 2, None, None, '21', 'K00', '60.06', 'CHK', '520', 76, 1863, 12, None, 1, '85.06', '25.00', None, 1, 5, 245)
(42331, 3, None, 62, '121', 'S50', '1859.01', 'ACT', '420', 952, 1583, 410, 255, 0, None, None, None, 6, 117, 1795)
(201721, 3, None, 42, '2472', 'S35', '1413.84', 'CLA', '350', 868, 1746, 963, 264, 0, None, None, None, 18, 65, 4510)
如您所见,我的输入数据中混合了整数、浮点数和字符串.
As you can see, I have a mix of integers, floats, and strings in my input data.
这是错误的追溯:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/miikey101/Documents/Khalen_Case_Loader/tensorflow/k_means/k_means.py", line 10, in prepare_dataset
dataset = tf.data.Dataset.from_tensor_slices(dm_data)
File "/usr/local/lib/python3.6/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 222, in from_tensor_slices
return TensorSliceDataset(tensors)
File "/usr/local/lib/python3.6/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 1017, in __init__
for i, t in enumerate(nest.flatten(tensors))
File "/usr/local/lib/python3.6/site-packages/tensorflow/python/data/ops/dataset_ops.py", line 1017, in <listcomp>
for i, t in enumerate(nest.flatten(tensors))
File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 950, in convert_to_tensor
as_ref=False)
File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/ops.py", line 1040, in internal_convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py", line 235, in _constant_tensor_conversion_function
return constant(v, dtype=dtype, name=name)
File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py", line 185, in constant
t = convert_to_eager_tensor(value, ctx, dtype)
File "/usr/local/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py", line 131, in convert_to_eager_tensor
return ops.EagerTensor(value, context=handle, device=device, dtype=dtype)
ValueError: Can't convert Python sequence with mixed types to Tensor.
推荐答案
在 tensorflow 中,张量不能包含多种数据类型.
In tensorflow you can't have a tensor with more than one data type.
引用文档:
tf.Tensor 不可能有多种数据类型.但是,可以将任意数据结构序列化为字符串并将其存储在 tf.Tensors 中.
因此,一种解决方法可能是创建一个数据类型为 tf.String
的张量,并在出现时将该字段转换为所需的数据类型
Hence a workaround could be to create a tensor with data type tf.String
and, on the occurrence, cast the field to the desired data type
这篇关于将具有多种数据类型的python序列转换为张量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!