Creating a tensor
You should always assign the dtype
when creating a tensor.
Tensor Attributes
Tensor Shape
Tensor Data Type
Changing a Tensor
Use tf.reshape
- Will accept the tensor, and the shape, with the shape in the form of:
(# of rows, # of columns)
- If you know all but one of the dimensions, you can leave the unknown dimension as
-1
:(# of rows, -1)
Here I’m explcitly defining the reshaping as (3,4)
tf.Tensor(
[[ 0. 1. 2. 3.]
[ 4. 5. 6. 7.]
[ 8. 9. 10. 11.]], shape=(3, 4), dtype=float32)
Here I left the column number blank, and it filled in automatically:
Ones, Random, Constant Tensor
tf.Tensor(
[[1. 1. 1. 1.]
[1. 1. 1. 1.]
[1. 1. 1. 1.]], shape=(3, 4), dtype=float32)
Keep in mind you can change a lot of parameters w.r.t. $, , $ etc. since this is a random normal distribution. There are more distributions as well if need be.
tf.Tensor(
[[ 2.202327 -0.26792583 -0.74406874 -0.6372847 ]
[ 0.46985132 -0.3888988 0.47005928 0.08380948]
[-0.6244405 0.47245353 -1.8344202 -1.2178894 ]], shape=(3, 4), dtype=float32)
Tensor Operations
These operations are performed elementwise: - Addition - Subtraction - Multiplication - Division - Exponentiation
Definition of elementwise in this context:
Given vectors \(x\) and \(y\), an elementwise operation (for example addition) will be performed between each corresponding element in the given vectors:
If:
- \(\bar{x} := [x_1, \dots , x_n]\)
- \(\bar{y} := [y_1, \dots, y_n]\)
then:
\[ \begin{aligned} \bar{x} + \bar{y} &= \begin{bmatrix} x_1 + y_1 \\ x_2 + y_2 \\ \vdots \\ x_n + y_n \end{bmatrix} \end{aligned} \]
Example:
Addition:
Subtraction
Multiplication
Division
Exponentiation
Dot Product
If:
- \(\bar{x} := [x_1, \dots , x_n]\)
- \(\bar{y} := [y_1, \dots, y_n]\)
Then the dot product (\(\bullet\)) is defined:
\[ \bar{x} \bullet \bar{y} = \sum_{i=1}^{n} x_i \cdot y_i \]
Broadcasting
Look at the explanation NumPy provides
x = tf.reshape(tf.range(start=12), shape=(3, 4))
y = tf.range(start=4)
print("================= Matrix of shape 3x4 =================")
print(x.numpy(), "\n")
print("================= Vector of shape 1x3 =================")
print(y.numpy())
print("================== Sum of shape 3x4 ===================")
print(tf.math.add(x, y).numpy())
================= Matrix of shape 3x4 =================
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
================= Vector of shape 1x3 =================
[0 1 2 3]
================== Sum of shape 3x4 ===================
[[ 0 2 4 6]
[ 4 6 8 10]
[ 8 10 12 14]]