Creating Tensors From Numpy Arrays
In [1]:
import tensorflow as tf
import numpy as npIn [2]:
npArrOne = np.arange(1,25, dtype=np.int32)
npArrOneOut [2]:
In [3]:
tensorFromNpArr = tf.constant(npArrOne)
tensorFromNpArrOut [3]:
Converting Between the Two
In [14]:
backToNPArr = np.array(tensorFromNpArr)
backToNPArr, type(backToNPArr)Out [14]:
Changing The Shape
Tensor shapes can be changed &/or set during tesnsor creation.
In order to do this, the multiplied dimensions must equal the number of elements in the tensor:
- EXAMPLES: a tensor, above, with 24 elements, can be shaped (2,3,4) or (12,2) or (6,2,1,2)
In [7]:
changedShape = tf.constant(npArrOne, shape=(2,3,4))
changedShapeOut [7]:
In [9]:
changedShapeTwo = tf.constant(npArrOne, shape=(6,2,2))
changedShapeTwoOut [9]: