如何使用 TF-1.4 对带有字符串值的 tf 张量进行字符串式拆分
How to do string-style split on tf tensor with string values using TF-1.4
我有一个从外部数据源读取数据的张量(例如标签)。张量的值是一个字符串,其格式为“label1,label2”(例如“0,1”)。现在我想使用分隔符 ',' 将字符串值拆分为一个列表,因此结果将类似于 ['0', '1'].
我试过流动:
# option-1 with error: tensor type has no attribute split.
label_list = input_label_tensor.split(',')
# option-2 with error: module has no method split.
label_list = tf.strings.split(input_label_tensor, ',')
# option-3 with error:
label_list = tf.string_split(input_label_tensor, ',')
选项 3 的错误是:
File "/usr/lib/python2.7/site-packages/tensorflow/python/ops/string_ops.py", line 113, in string_split
source = ops.convert_to_tensor(source, dtype=dtypes.string)
File "/usr/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 836, in convert_to_tensor
as_ref=False)
File "/usr/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 926, in internal_convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "/usr/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 774, in _TensorTensorConversionFunction
(dtype.name, t.dtype.name, str(t)))
ValueError: Tensor conversion requested dtype string for Tensor with dtype float32: 'Tensor("StagingArea_get:467", shape=(?,), dtype=float32, device=/job:worker/task:0/device:CPU:0)'
拆分操作的正确方法是什么?我正在使用 TF-1.4
我最好的选择是 input_label_tensor
实际上不是 string
张量。例如,这有效:
%tensorflow_version 1.x
import tensorflow as tf
input_label_tensor = tf.constant(['0', '1'], dtype=tf.string)
label_list = tf.string_split(input_label_tensor, ',')
但是如果我使用 float
张量,我可以重现你的错误:
input_label_tensor = tf.constant([0, 1], dtype=tf.float32)
label_list = tf.string_split(input_label_tensor, ',')
要访问 SparseTensor
的值,请尝试这样的操作:
%tensorflow_version 1.x
import tensorflow as tf
input_label_tensor = tf.constant(['0', '1'], dtype=tf.string)
label_list = tf.string_split(input_label_tensor, ',').values
x, y = label_list[0], label_list[1]
更新 1:
%tensorflow_version 1.x
import tensorflow as tf
input_label_tensor = tf.constant(['0,1', '1,0', '1,1', '0,0'], dtype=tf.string)
label_list = tf.string_split(input_label_tensor, ',')
c = tf.sparse.to_dense(label_list)
c = tf.string_to_number(c, tf.float32)
with tf.Session() as sess:
result = c.eval()
print(result)
[[0. 1.]
[1. 0.]
[1. 1.]
[0. 0.]]
我有一个从外部数据源读取数据的张量(例如标签)。张量的值是一个字符串,其格式为“label1,label2”(例如“0,1”)。现在我想使用分隔符 ',' 将字符串值拆分为一个列表,因此结果将类似于 ['0', '1'].
我试过流动:
# option-1 with error: tensor type has no attribute split.
label_list = input_label_tensor.split(',')
# option-2 with error: module has no method split.
label_list = tf.strings.split(input_label_tensor, ',')
# option-3 with error:
label_list = tf.string_split(input_label_tensor, ',')
选项 3 的错误是:
File "/usr/lib/python2.7/site-packages/tensorflow/python/ops/string_ops.py", line 113, in string_split
source = ops.convert_to_tensor(source, dtype=dtypes.string)
File "/usr/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 836, in convert_to_tensor
as_ref=False)
File "/usr/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 926, in internal_convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "/usr/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 774, in _TensorTensorConversionFunction
(dtype.name, t.dtype.name, str(t)))
ValueError: Tensor conversion requested dtype string for Tensor with dtype float32: 'Tensor("StagingArea_get:467", shape=(?,), dtype=float32, device=/job:worker/task:0/device:CPU:0)'
拆分操作的正确方法是什么?我正在使用 TF-1.4
我最好的选择是 input_label_tensor
实际上不是 string
张量。例如,这有效:
%tensorflow_version 1.x
import tensorflow as tf
input_label_tensor = tf.constant(['0', '1'], dtype=tf.string)
label_list = tf.string_split(input_label_tensor, ',')
但是如果我使用 float
张量,我可以重现你的错误:
input_label_tensor = tf.constant([0, 1], dtype=tf.float32)
label_list = tf.string_split(input_label_tensor, ',')
要访问 SparseTensor
的值,请尝试这样的操作:
%tensorflow_version 1.x
import tensorflow as tf
input_label_tensor = tf.constant(['0', '1'], dtype=tf.string)
label_list = tf.string_split(input_label_tensor, ',').values
x, y = label_list[0], label_list[1]
更新 1:
%tensorflow_version 1.x
import tensorflow as tf
input_label_tensor = tf.constant(['0,1', '1,0', '1,1', '0,0'], dtype=tf.string)
label_list = tf.string_split(input_label_tensor, ',')
c = tf.sparse.to_dense(label_list)
c = tf.string_to_number(c, tf.float32)
with tf.Session() as sess:
result = c.eval()
print(result)
[[0. 1.]
[1. 0.]
[1. 1.]
[0. 0.]]