Tensor Operation Properties

This section provides a tutorial example on how to view tensor operation properties by calling type(x), str(x), x.__dict__ and x.get_shape().

Once a tensor operation, x, is created as object instance, we can explore its properties in different ways:

1. type(x) - Global static method returning the exact class name that the instance was created from:

>>> import tensorflow as tf
>>> a = tf.constant([10])
>>> b = tf.Variable([20])
>>> c = tf.placeholder(tf.int32,1)
>>> o = tf.add(a,b)

>>> type(a)
<class 'tensorflow.python.framework.ops.Tensor'>

>>> type(b)
<class 'tensorflow.python.ops.variables.RefVariable'>

>>> type(c)
<class 'tensorflow.python.framework.ops.Tensor'>

>>> type(o)
<class 'tensorflow.python.framework.ops.Tensor'>

As you can see, the "Variable" operation, b, was instantiated from a different flavor of "ops" class. Its behavior will probably be different too. We will see it later.

2. str(x) - Global static method returning the string that summaries the instance:

>>> str(a)
'Tensor("Const:0", shape=(1,), dtype=int32)'

>>> str(b)
"<tf.Variable 'Variable:0' shape=(1,) dtype=int32_ref>"

>>> str(c)
'Tensor("Placeholder:0", shape=(1,), dtype=int32)'

>>> str(x)
'Tensor("Add:0", shape=(1,), dtype=int32)'

Note that the string summary format of the "Variable" operation, b, is slightly different. But it has the same information of instance name, output tensor shape, and tensor element type.

3. x.__dict__ - Instance dictionary property returning other instance properties;

>>> a.__dict__
{'_op': <tf.Operation 'Const' type=Const>,
 '_value_index': 0,
 '_dtype': tf.int32,
 '_tf_output': <tensorflow.python.pywrap_tensorflow_internal...,
 '_shape_val': TensorShape([Dimension(1)]),
 '_consumers': [],
 '_id': 9,
 '_name': 'Const:0'
}

>>> b.__dict__
{'_in_graph_mode': True,
 '_graph_key': 'grap-key-0/',
 '_synchronization': <VariableSynchronization.AUTO: 0>,
 '_aggregation': <VariableAggregation.NONE: 0>,
 '_trainable': True,
 '_initial_value': <tf.Tensor 'Variable/initial_value:0' ...,
 '_variable': <tf.Tensor 'Variable:0' shape=(1,) dtype=int32_ref>,
 '_initializer_op': <tf.Operation 'Variable/Assign' type=Assign>,
 '_snapshot': <tf.Tensor 'Variable/read:0' shape=(1,) dtype=int32>,
 '_caching_device': None,
 '_save_slice_info': None,
 '_constraint': None
}

>>> c.__dict__
{'_op': <tf.Operation 'Placeholder' type=Placeholder>,
 '_value_index': 0,
 '_dtype': tf.int32,
 '_tf_output': <tensorflow.python.pywrap_tensorflow_internal...,
 '_shape_val': TensorShape([Dimension(1)]),
 '_consumers': [],
 '_id': 17,
 '_name': 'Placeholder_2:0'
}

>>> o.__dict__
{'_op': <tf.Operation 'Add' type=Add>,
 '_value_index': 0,
 '_dtype': tf.int32,
 '_tf_output': None,
 '_shape_val': None,
 '_consumers': [],
 '_id': 18,
 '_name': None
}

4. x.get_shap() - Instance method returning the output tensor shape of the instance. They are all 1 dimension of size 1.

>>> a.get_shape()
TensorShape([Dimension(1)])

>>> b.get_shape()
TensorShape([Dimension(1)])

>>> c.get_shape()
TensorShape([Dimension(1)])

>>> o.get_shape()
TensorShape([Dimension(1)])

That's enough about tensor operation properties for now.

Table of Contents

 About This Book

 Deep Playground for Classical Neural Networks

 Building Neural Networks with Python

 Simple Example of Neural Networks

TensorFlow - Machine Learning Platform

 What Is TensorFlow

 "tensorflow" - TensorFlow Python Library

 "tensorflow" Interactive Test Web Page

 Tensor and Tensor Flow Graph

Tensor Operation Properties

 TensorFlow Session Class and run() Function

 TensorFlow Variable Class and load() Function

 Linear Regression with TensorFlow

 tensorflow.examples.tutorials.mnist Module

 Simple TensorFlow Model on MNIST Database

 Commonly Used TensorFlow Funcitons

 PyTorch - Machine Learning Platform

 CNN (Convolutional Neural Network)

 RNN (Recurrent Neural Network)

 GNN (Graph Neural Network)

 References

 Full Version in PDF/EPUB