Skip to main content
 首页 » 编程设计

python tensorflow : cast placeholder to string

2024年08月16日31zhoujg

在 python 中,我试图将 tf.float32 类型的 Tensorflow 占位符转换为 tf.string。通过 tf.to_float() 可以反过来, 但是没有 to_string...

尝试:

resample_multiplier_ = tf.placeholder(tf.float32, [], name='resample_multiplier') 
name_ = tf.placeholder(tf.string) 
resample_multiplier_str_ = tf.cast(resample_multiplier_,tf.string) + name_  

给我(当我尝试运行 session 时):

UnimplementedError: Cast float to string is not supported

如何转换 tf.float32 类型的 Tensorflow 占位符?

请您参考如下方法:

使用tf.py_func :

x = tf.placeholder(tf.float32) 
to_str_op = tf.py_func(lambda val: str(val), [x], tf.string) 
print(to_str_op.dtype)  # <dtype: 'string'>