Warm tip: This article is reproduced from serverfault.com, please click

merging tensors and converting byte string into float string

发布于 2020-11-30 06:08:00

I am new with Tensorflow. I have a tensor like the following example:

(<tf.Tensor: shape=(2, 193373), dtype=string, numpy=
array([[b'0', b'2711268_2711268', b'0', ..., b'1', b'2', b'2']],
  dtype=object)>, 
<tf.Tensor: shape=(2, 221741), dtype=string, numpy=
array([[b'0', b'271', b'0', ..., b'2', b'2', b'2']],
  dtype=object)>)

And I am trying to get the following output: (merging tensors and converting byte string into float string):

(<tf.Tensor: shape=(2, 415114), dtype=float, numpy=
array([[0, 271, 0, ..., 1, 2, 2]],
  dtype=object)>)

How can I do that?

Questioner
hsn15051
Viewed
0
Lescurel 2020-11-30 16:57:07

You can use tf.strings.to_number :

>>> numbers_as_string = tf.constant(list("123456789"))
>>> numbers_as_string
 <tf.Tensor: shape=(9,), dtype=string, numpy=array([b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9'], dtype=object)>
>>> tf.strings.to_number(numbers_as_string)
<tf.Tensor: shape=(9,), dtype=float32, numpy=array([1., 2., 3., 4., 5., 6., 7., 8., 9.], dtype=float32)>

For the merging, use tf.concat :

>>> a = tf.random.normal((2,100))
>>> b = tf.random.normal((2,50))
>>> a.shape
TensorShape([2, 100])
>>> b.shape
TensorShape([2, 50])
>>> tf.concat([a,b], axis=1)
<tf.Tensor 'concat:0' shape=(2, 150) dtype=float32>