"Tensorflow supports several basic maths functions out of the box. Unlike numpy, some operations run by the name ```reduce_operation(*args)``` like ```reduce_sum``` and ```reduce_mean```.\n",
"Tensorflow supports several basic maths functions out of the box. Compared to numpy, **some operations run by the name** ```reduce_operation(*args)``` like ```reduce_sum``` and ```reduce_mean```.\n",
"\n",
"You can find an incomplete list of some basic calls:"
]
...
...
%% Cell type:markdown id:269d507c tags:
# Tensorflow Tutorial
---------------------------------
Welcome to the Tesorflow tutorial! Here we are going to go through the most fundamental functions and the syntax of the library.
Usually, tensorflow is imported as ``` tf```:
%% Cell type:code id:fb7e3964 tags:
``` python
importtensorflowastf
```
%% Cell type:markdown id:3f663a34 tags:
%% Cell type:markdown id:ffc59cef tags:
## Tensors
One can create tensors in tensorflow using a **syntax similar to** that of **numpy**. These objects are considered to be constants by the library and hence **cannot be modified**; each operation on these objects returns a new tensor object.
You can find some examples of tensor creation below:
%% Cell type:code id:e776d989 tags:
``` python
t1=tf.zeros([5])# Zeros of length 5 (note the necessary squared brackets)
t2=tf.ones([3,2])# Array of ones of shape (3, 2)
t3=tf.random.uniform([1,3])# Random sampling from the interval [1, 3)-
t4=tf.linspace(1,7,4)# Create a tensor of linear spacing from 1 to 7 with 4 entries
On the other hand, ```Variables``` (based on tensors) are objects which are **updated during training** through backpropagation. Each tensorflow variable has to be initialized and their values can be changed with using ```variable.assign(new_values)```:
%% Cell type:code id:a70aa763 tags:
``` python
w=tf.Variable(tf.zeros([3,2]))# Create an empty variable w
print("w = %s"%w)# ...which has zeros only by default
w.assign(tf.ones([3,2]))# Assign new values to w
print("w = %s"%w)# ... and retrieve them
```
%% Output
w = <tf.Variable 'Variable:0' shape=(3, 2) dtype=float32, numpy=
array([[0., 0.],
[0., 0.],
[0., 0.]], dtype=float32)>
w = <tf.Variable 'Variable:0' shape=(3, 2) dtype=float32, numpy=
array([[1., 1.],
[1., 1.],
[1., 1.]], dtype=float32)>
%% Cell type:markdown id:fc389074 tags:
%% Cell type:markdown id:11af2105 tags:
## Fundamental Mathematical Operations
Tensorflow supports several basic maths functions out of the box. Unlike numpy, some operations run by the name ```reduce_operation(*args)``` like ```reduce_sum``` and ```reduce_mean```.
Tensorflow supports several basic maths functions out of the box. Compared to numpy, **some operations run by the name**```reduce_operation(*args)``` like ```reduce_sum``` and ```reduce_mean```.
You can find an incomplete list of some basic calls:
%% Cell type:code id:367e4ba0 tags:
``` python
x=tf.linspace(0.,4.,5)# Create a tensor array
print("x =",x)
print("(x+1)**2 - 2) =",(x+1.)**2-2.)
print("sin(x)",tf.sin(x))
print("sum(x)",tf.reduce_sum(x))
print("mean(x)",tf.reduce_mean(x))
# Create some other tensors to showcase arithmatic operations:
a=tf.zeros(shape=(2,3))
b=tf.ones(shape=(2,3))
c=tf.ones(shape=(3,2))
# Operators (+, -, /, *) are available
a+b# same as tf.add(a, b)
a-b# same as tf.subtract(a, b)
a*b# same as tf.mul(a, b)
a/b# same as tf.division(a, b)
```
%% Output
x = tf.Tensor([0. 1. 2. 3. 4.], shape=(5,), dtype=float32)