Skip to content
Snippets Groups Projects
Commit 93de82b5 authored by Máté Zoltán Farkas's avatar Máté Zoltán Farkas
Browse files

Created pytorch tutorial + minor changes in tf-tutorial

parent 686ac858
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id:3881ec5d tags:
# PyTorch tutorial
---------------
Welcome to the pytorch tutorial!
%% 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
import tensorflow as tf
```
%% 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
t5 = tf.convert_to_tensor(np.linspace(1, 7, 4))
# Observe that all of these objects are tensors:
print("tf.ones([3, 2]) = %s" % t1)
print("tf.zeros([5]) = %s" % t2)
print("tf.random_uniform([1, 3]) = %s" % t3)
print("tf.linspace(1.0, 7.0, 4) = %s" % t4)
print("tf.convert_to_tensor( np.linspace(1, 7, 4) ) = %s" % t5)
```
%% Output
tf.ones([3, 2]) = tf.Tensor([0. 0. 0. 0. 0.], shape=(5,), dtype=float32)
tf.zeros([5]) = tf.Tensor(
[[1. 1.]
[1. 1.]
[1. 1.]], shape=(3, 2), dtype=float32)
tf.random_uniform([1, 3]) = tf.Tensor([[0.84930134 0.71385014 0.2513721 ]], shape=(1, 3), dtype=float32)
tf.linspace(1.0, 7.0, 4) = tf.Tensor([1. 3. 5. 7.], shape=(4,), dtype=float64)
tf.convert_to_tensor( np.linspace(1, 7, 4) ) = tf.Tensor([1. 3. 5. 7.], shape=(4,), dtype=float64)
%% Cell type:markdown id:e1fa23d6 tags:
%% Cell type:markdown id:f904ef1e tags:
## Variables
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)
(x+1)**2 - 2) = tf.Tensor([-1. 2. 7. 14. 23.], shape=(5,), dtype=float32)
sin(x) tf.Tensor([ 0. 0.84147096 0.9092974 0.14112 -0.7568025 ], shape=(5,), dtype=float32)
sum(x) tf.Tensor(10.0, shape=(), dtype=float32)
mean(x) tf.Tensor(2.0, shape=(), dtype=float32)
<tf.Tensor: shape=(2, 3), dtype=float32, numpy=
array([[0., 0., 0.],
[0., 0., 0.]], dtype=float32)>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment