函数原型:
tf.string_join(input, separator=’ ', name=True)
参数:
- input: 是一个list,list中存放的数据类型为 tf.string
- separator=’ ': 字符串的连接符, 连接符也是字符串型
- name: 设置当前operation对象的名称
举例 1: 拼接0维字符串
import tensorflow as tf
A_str_tf = tf.constant('hello', dtype=tf.string)
B_str_tf = tf.constant('tensor', dtype=tf.string)
C_str_tf = tf.constant('flow', dtype=tf.string)
ABC_str_tf = tf.string_join([A_str_tf, B_str_tf, C_str_tf], separator='_&_',)
with tf.Session() as sess:
ABC_str = sess.run(ABC_str_tf)
print(ABC_str.decode())
# 输出为:
# hello_&_tensor_&_flow
举例 2: 拼接多维字符串, 每个维度对应执行拼接
import tensorflow as tf
A_str_tf = tf.constant([['hello'], ['1']], dtype=tf.string)
B_str_tf = tf.constant([['tensor'], ['2']], dtype=tf.string)
C_str_tf = tf.constant([['flow'], ['3']], dtype=tf.string)
ABC_str_tf = tf.string_join([A_str_tf, B_str_tf, C_str_tf], separator='_&_',)
with tf.Session() as sess:
ABC_str = sess.run(ABC_str_tf)
print(ABC_str)
# 输出为:
# [[b'hello_&_tensor_&_flow']
# [b'1_&_2_&_3']]
- TensorFlow 中输出类型为tf.string的Tensor 对象通过tf.Session().run()函数取出后, 是字节数组类型,所以字符串前面的那个b,就代表字节数组类型