What Are Tensors

Tensors are multilinear maps from vector spaces to the real numbers.

E.g.

Scalar: $\left( f : \mathbb{R}\rightarrow \mathbb{R}, f\left(e_1\right) = c \right)$

Vector: $\left( f : \mathbb{R}^n \rightarrow \mathbb{R}, f\left(e_i\right) = v_i \right)$

Matrix: $\left( f : \mathbb{R}^n \times \mathbb{R}^m \rightarrow \mathbb{R}, f\left(e_i, e_j\right) = A_{ij} \right)$

Importing Tensorflow for the demo

import tensorflow as tf 

Running an Interactive session

session = tf.InteractiveSession()

Tensors in Tensorflow

A. Scalar $\left( f : \mathbb{R}\rightarrow \mathbb{R}, f(e_1) = c \right)$

a = tf.constant(3)
b = tf.constant(4)
c = a + b
c.eval()
7

B. Vector $\left( f :: \mathbb{R}^n \rightarrow \mathbb{R}, : f(e_i) = v_i \right)$

v1 = tf.constant([[1., 2., 3.]]) 
v2 = tf.constant([[3., 4., 5.]])
v3 = tf.add(v1, v2)
v3.eval()
array([[ 4.,  6.,  8.]], dtype=float32)

C. Matrix $\left( f :: \mathbb{R}^n \times \mathbb{R}^m \rightarrow \mathbb{R}, : f(e_i, e_j) = A_{ij} \right)$

A = tf.constant([[1.,2.,3.],[4.,5.,6.],[7.,8.,9.]])
B = tf.ones([3,3])
C = tf.fill([3,3], 10.)
D = tf.add(A,B)
E = tf.add(D,C)
E.eval()
array([[ 12.,  13.,  14.],
       [ 15.,  16.,  17.],
       [ 18.,  19.,  20.]], dtype=float32)
%pwd
'/home/cobalt'
Written on February 26, 2017