Tensor Transformations
Note: Functions taking Tensor arguments can also take anything accepted by [ tf.convert_to_tensor ]
Contents
Tensor Transformations
●Casting
○ tf.string_to_number(string_tensor, out_type=None, name=None)
○ tf.to_double(x, name='ToDouble')
○ tf.to_float(x, name='ToFloat')
○ tf.to_bfloat16(x, name='ToBFloat16')
○ tf.to_int32(x, name='ToInt32')
○ tf.to_int64(x, name='ToInt64')
○ tf.cast(x, dtype, name=None)
●Shapes and Shaping
○ tf.shape(input, name=None)
○ tf.size(input, name=None)
○ tf.rank(input, name=None)
○ tf.reshape(tensor, shape, name=None)
○ tf.squeeze(input, squeeze_dims=None, name=None)
○ tf.expand_dims(input, dim, name=None)
●Slicing and Joining
○ tf.slice(input_, begin, size, name=None)
○ tf.split(split_dim, num_split, value, name='split')
○ tf.tile(input, multiples, name=None)
○ tf.pad(input, paddings, name=None)
○ tf.concat(concat_dim, values, name='concat')
○ tf.pack(values, name='pack')
○ tf.unpack(value, num=None, name='unpack')
○ tf.reverse_sequence(input, seq_lengths, seq_dim, name=None)
○ tf.reverse(tensor, dims, name=None)
○ tf.transpose(a, perm=None, name='transpose')
○ tf.gather(params, indices, name=None)
○ tf.dynamic_partition(data, partitions, num_partitions, name=None)
○ tf.dynamic_stitch(indices, data, name=None)
Casting
TensorFlow provides several operations that you can use to cast tensor data types in your graph.
tf.string_to_number(string_tensor, out_type=None, name=None)
Converts each string in the input Tensor to the specified numeric type.
(Note that int32 overflow results in an error while float overflow results in a rounded value.)
Args:
string_tensor: ATensorof typestring.
out_type: An optionaltf.DTypefrom:tf.float32, tf.int32. Defaults totf.float32. The numeric type to interpret each string in string_tensor as.
name: A name for the operation (optional).
Returns:
ATensorof typeout_type. A Tensor of the same shape as the input string_tensor.
Example:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import tensorflow as tf
import numpy as np
a = tf.constant('10.1')
c = tf.string_to_number(a)
sess = tf.Session()
print (sess.run(c))
sess.close()