当我读取文件时,tf.read_file
我得到了类型的东西tf.string
.文档只说它是"可变长度字节数组.Tensor的每个元素都是一个字节数组." (https://www.tensorflow.org/versions/r0.10/resources/dims_types.html).我不知道如何解释这个.
我对这种类型无能为力.在通常的python中你可以通过索引获取元素my_string[:4]
,但是当我运行以下代码时,我得到一个错误.
import tensorflow as tf
import numpy as np
x = tf.constant("This is string")
y = x[:4]
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
result = sess.run(y)
print result
Run Code Online (Sandbox Code Playgroud)
它说
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/tensor_shape.py", line 621, in assert_has_rank raise ValueError("Shape %s must have rank %d" % (self, rank)) ValueError: Shape () must have rank 1
我也无法将我的字符串转换为tf.float32
张量.它是.flo
文件,它有魔术标题"PIEH".这个numpy代码成功地将这样的头转换为数字(参见这里的例子/sf/answers/1961152861/)但我不能用tensorflow做到这一点.我试过tf.string_to_number(string, out_type=tf.float32)
但它说
tensorflow.python.framework.errors.InvalidArgumentError: StringToNumberOp could not correctly convert string: …