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

Tutorials w/o final ex ready

parent 2c7956db
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id:897fcfcd tags:
# Python Tutorial
-----------------------
Welcome to this warm-up tutorial! Let's take a look at some highlights of Python!
Please execute the whole notebook now by clicking on Cell->Execute All in the menubar. In the following, cells starting with TODOs and ending with a ```?``` are tasks to be solved individually.
Let's start with the usual **hello world** program:
%% Cell type:code id:c8562e26 tags:
``` python
print("Hello World!")
```
%% Output
Hello World!
%% Cell type:markdown id:48a13786 tags:
You can print multiple items in a row:
%% Cell type:code id:bea4da9f tags:
``` python
print("1 + 12 =", 13)
```
%% Output
1 + 12 = 13
%% Cell type:markdown id:5a70d15c tags:
## Comments
You can also comment your code with ```#``` for a **single line** or with ```"""comment"""``` for **multiple lines**:
%% Cell type:code id:6b5d414b tags:
``` python
# This is a comment and will be ignored upon execution
```
%% Cell type:code id:d69d90e9 tags:
``` python
print("What's that thing that spells the same backwards as forwards?")
"""
I am a comment too!
See, this line is ignored as well!
"""
print("A palindrome...?")
```
%% Output
What's that thing that spells the same backwards as forwards?
A palindrome...?
%% Cell type:markdown id:c4c8535a tags:
## Variables and Data-Types
Python is a **dynamically-typed** language, i.e. you don't have to define your variable types in advance:
%% Cell type:code id:f7179786 tags:
``` python
a = 13 # This is an integer
b = "This is a string" # This is a string
c = True # A boolean
d = 14.5 # This is a double-precision floating-point variable (called a 'double' in C-speak)
e = [1, 3, 15, 34] # This is a list, an array of objects
f = ("Emmental", "Brie", "Gouda", "Danish Blue") # This is a tuple, an immutable array
```
%% Cell type:code id:d0586d1b tags:
``` python
# You can use squared brackets to access elements in an array or tuple
# TODO: What's the second element in array e?
print("The second element in a is:", None,"(should be 3)")?
```
%% Output
Input In [6]
print("The second element in a is:", None,"(should be 3)")?
^
SyntaxError: invalid syntax
%% Cell type:markdown id:0019db03 tags:
## Elementary Arithmetics
You can use the usual **operators** +, -, *, / for elementary arithmetics:
You can use the usual **operators** ```+```, ```-```, ```*```, ```/``` for elementary arithmetics:
%% Cell type:code id:86b8e68c tags:
``` python
# TODO: Use the variables a and d defined above to calculate 13 x 14.5!
print("13 x 14.5 =", "?")?
```
%% Output
Input In [12]
print("13 x 14.5 =", "?")?
^
SyntaxError: invalid syntax
%% Cell type:markdown id:32b1985e tags:
You can use the **double star** \*\*-operator to raise number to the **n-th power**:
You can use the **double star** ```**```-operator to raise number to the **n-th power**:
%% Cell type:code id:8c45add3 tags:
``` python
print(1**2, 2**2, 3**2, 4**2, 32**2)
```
%% Output
1 4 9 16 1024
%% Cell type:code id:29eac6ed tags:
``` python
# TODO: Booleans work as numbers as well. What happens if you add a and c?
print("13 + True =", "?")?
```
%% Output
Input In [14]
print("13 + True =", "?")?
^
SyntaxError: invalid syntax
%% Cell type:markdown id:f6d98d65 tags:
## Comparison Opeartors
You can use <, >, ==, <=, >= to **compare numbers** (**and objects**), the **keywords** ```and```, ```or``` to compare statements (booleans) and the keyword ```not``` to negate them.
You can use ```<```, ```>```, ```==```, ```<=```, ```>=``` to **compare numbers** (**and objects**), the **keywords** ```and```, ```or``` to compare statements (booleans) and the keyword ```not``` to negate them.
%% Cell type:code id:b0a7cbe1 tags:
``` python
# TODO: What's the output of ("a bigger than d" AND "c is True")?
print((a==14) and not (c<d))?
```
%% Output
Input In [15]
print((a==14) and not (c<d))?
^
SyntaxError: invalid syntax
%% Cell type:markdown id:cbba9080 tags:
## Control Flow Tools
For pre-test loops you can use ```while```. Beware that in Python indented blocks of code belong to the same parent line (similarly to ```{...}``` in C); mixing indentations raises an ```IndentationError```.
%% Cell type:code id:5f42b63f tags:
``` python
# TODO: fix the while loop below:
i = 0
while i<4:
print(i)
i = i + 1
```
%% Output
Input In [16]
i = i + 1
^
IndentationError: unexpected indent
%% Cell type:markdown id:118d8935 tags:
Using ```for``` and the iterable ```range(n)``` a for-like loop can be created:
%% Cell type:code id:7740d793 tags:
``` python
# TODO: Write a for loop adding up all the numbers from 0 to 100 and print the result.
sum = 0
for i in range(5):
print("")
print(sum)?
```
%% Output
Input In [17]
print(sum)?
^
SyntaxError: invalid syntax
%% Cell type:markdown id:fd2059e5 tags:
For **conditional branching**, the usual keywords ```if``` and ```else``` are defined. Loops can be stopped with ```break``` and skipped with ```continue```.
%% Cell type:code id:0c47c5d6 tags:
``` python
# TODO: With i%2, you can get the modulus of i divided by two.
# TODO: Write a small program checking whether i is an even or odd number for every i in [0, 10).
# TODO: Use 'continue' to skip 0!
n = 1
for i in range(n):
if True:
print(i, "is an odd/even number")
else:
print("else block")?
```
%% Output
Input In [18]
print("else block")?
^
SyntaxError: invalid syntax
%% Cell type:markdown id:f51fc582 tags:
## Functions
For repeating code, functions can be defined with the ```def``` keyword and their outputs can be returned using ```return```. These funtions can be also called recursively.
%% Cell type:code id:d5f33c84 tags:
``` python
def get_greeting(name):
return "Hello " + name +"!"
print(get_greeting("John Cleese"))
print(get_greeting("Graham Chapman"))
print(get_greeting("Eric Idle"))
```
%% Output
Hello John Cleese!
Hello Graham Chapman!
Hello Eric Idle!
%% Cell type:code id:04034c0e tags:
``` python
# TODO: Implement the factorial operation
def factorial(n):
print(n)
return 0
factorial(10)?
```
%% Output
Input In [20]
factorial(10)?
^
SyntaxError: invalid syntax
%% Cell type:markdown id:49898002 tags:
## Classes
Classes are high-level objects for storing and managing primitives. They are initialized with ```class class_name:``` after which the constructor ```__init__(self, args)``` is automatically called. Data in classes are stored using the ```self``` keyword and are publicly accessible by default.
**Classes** are high-level objects for **storing and managing primitives**. They are initialized with ```class class_name:``` after which the constructor ```__init__(self, args)``` is automatically called. Data in classes are stored using the ```self``` keyword and are publicly accessible by default.
%% Cell type:code id:dd4ae61f tags:
``` python
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def print_info(self):
print("name:", self.name, ";", "age:", self.age)
alice = Person("Alice", 34)
alice.print_info()
alice.age = 18
alice.print_info()
```
%% Output
name: Alice ; age: 34
name: Alice ; age: 18
%% Cell type:markdown id:0d914e99 tags:
## Class Inheritance
A class can inherit the properties of its parent class. These properties can be freely overriden:
A class can **inherit** the **properties of its parent** class. These properties can be freely overriden:
%% Cell type:code id:caf893b1 tags:
``` python
class Student(Person):
def print_info(self):
"""
Beware that the attribute name has been inherited from 'Person'
"""
print(self.name, "is a student!")
bob = Student("Bob", 20)
bob.print_info()
```
%% Output
Bob is a student!
%% Cell type:markdown id:39f231b0 tags:
## Numpy Basics
Numpy is an performant, easy-to-use scientific library for numeric calculations. You will often encouter cases where numpy can perform calculations several orders of magnitudes faster due its C-backend and ability to process data in a vectorized (parallelized) form.
%% Cell type:code id:dfaeae8a tags:
``` python
import numpy as np # numpy is usually imported this way
```
%% Cell type:markdown id:935518b0 tags:
### Numpy One Dimensional Array Operations
Numpy operates on arrays element-wise meaning elementary operations are performed in an intuitive way.
```numpy``` **operates** on arrays **element-wise** thus elementary operations are performed in an intuitive way.
%% Cell type:code id:2aff9b94 tags:
``` python
zero_array = np.zeros(10) # creates an an array of 10 zeros
one_array = np.ones(10) # creates an array of... guess what!
two_array = np.ones(10)*2 # this one is a bit more tricky
three_array = one_array + two_array # arrays are added element-wise
print(three_array)
# You can also create numpy arrays from python lists:
np_array_from_python_list = np.array([11, 22, 33, 44, 55, 66, 77])
print(np_array_from_python_list, type(np_array_from_python_list))
```
%% Output
[3. 3. 3. 3. 3. 3. 3. 3. 3. 3.]
[11 22 33 44 55 66 77] <class 'numpy.ndarray'>
%% Cell type:markdown id:e6057042 tags:
For this to work however the arrays must be of the same shape, otherwise a ```ValueError``` will be raised. In order to avoid this error, you can always access the shape of an array using the ```array.shape``` syntax:
For this to work however the arrays must be of the **same shape**, otherwise a ```ValueError``` will be raised. In order to avoid this error, you can always access the shape of an array using the ```array.shape``` syntax:
%% Cell type:code id:1141c56f tags:
``` python
array1 = np.arange(10) # [0, 1, 2, ..., 9]
print("Shape of array1", array1.shape) # shape: (10,)
array2 = np.array([10]*5) # [10, 10, 10, 10, 10 ]
print("Shape of array2", array2.shape) # shape (5,)
print(np_array1/np_array2) # Voilà ValueError due to conflicting shapes
```
%% Output
Shape of array1 (10,)
Shape of array2 (5,)
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
Input In [25], in <cell line: 5>()
3 array2 = np.array([10]*5) # [10, 10, 10, 10, 10 ]
4 print("Shape of array2", array2.shape) # shape (5,)
----> 5 print(np_array1/np_array2)
NameError: name 'np_array1' is not defined
%% Cell type:markdown id:d236a438 tags:
### Numpy Arrays of Multiple Dimensions
Numpy supports the creation of n-dimensional arrays (arrays, matrices, tensors). Let's say you wish to generate 5 normally distributed samples of size 1000 with means 0, 10, 20, 30 and 40 and standard deviation 1. For this, you can first generate 5\*1000 samples around 0, put them into a marix of shape (5, 1000) and shift each distribution to their respective means.
%% Cell type:code id:47d6ecce tags:
``` python
ndists = 5
sample_size = 1000
dataset = np.random.normal(0, 1, ndists*sample_size) # Dataset generation
print("Shape of the original dataset:", dataset.shape) # Printing the dataset shape
dataset_reshaped = dataset.reshape(ndists, sample_size) # Reshaping it to (5, 1000)
print("Shape of the reshaped dataset:", dataset_reshaped.shape) # Printing the new shape
```
%% Output
Shape of the original dataset: (5000,)
Shape of the reshaped dataset: (5, 1000)
%% Cell type:markdown id:8bffbc39 tags:
Now we can define our shift vector, extend it with a new "dimension" (called axis, i.e. bring it into the shape (5, 1)), such that all elements at the second index "see" a scalar.
%% Cell type:code id:f2ea360c tags:
``` python
shift_vector = np.arange(5)*10 # np.arange(n) creates an array of integers from 0 to n-1
shift_vector = shift_vector[:, np.newaxis] # np.newaxis takes care of the new axis creation
print("Shift vector shape:", shift_vector.shape) # observe that reshape has not been called
```
%% Output
Shift vector shape: (5, 1)
%% Cell type:markdown id:64fdffa1 tags:
Now our gaussians can be shifted to their right places!
%% Cell type:code id:f0177d95 tags:
``` python
dataset_shifted = shift_vector + dataset_reshaped # shape (5, 1) + shape (5, 1000) = shape (5, 1000)
import matplotlib.pyplot as plt # import the plotting library to showcase the results
for hist in dataset_shifted: # Iterate over every distribution
plt.hist(hist)
```
%% Output
%% Cell type:code id:439f4bf4 tags:
``` python
# ... or you check the documentation and solve the problem directly by invoking:
dataset_shifted = np.random.normal(loc=np.arange(5)*10, scale=np.ones(5), size=(1000, 5)).T
for hist in dataset_shifted:
plt.hist(hist)
# Always check the docs before inventing something "new"!
```
%% Output
%% Cell type:markdown id:43f66264 tags:
### Numpy Matrix Operations
By default, numpy performs element-wise multiplication using the star-operator ```*```. For matrix-matrix, matrix-vector multiplications the ```@``` can be used:
%% Cell type:code id:25b44ae6 tags:
``` python
# Define a matrix manually:
matrix = np.array([[1., 1.],
[0., 3.]])
# Define a vector manually:
vector = np.array([0., -1.])
# The dirty and manual way would be:
print("Manual operation M*v: ", (matrix*vector[np.newaxis,:]).sum(axis=1))
# Fortunately, the @-operator exists!
print("Matrix multiplication @:", matrix@vector)
```
%% Output
Manual operation M*v: [-1. -3.]
Matrix multiplication @: [-1. -3.]
%% Cell type:markdown id:7a826bff tags:
## Numpy Miscellaneous:
### Functions
Below you can find a non-exhaustive list of built-in numpy functions for quick data analysis:
%% Cell type:code id:973bd72b tags:
``` python
print("Basic array operations:")
print("Sum", np.sum(np.arange(101))) # Sum of all numbers from 0 to 100
print("Mean", np.mean(dataset_shifted, axis=1)) # Mean of the generated gaussians from above...
print("Std", dataset_shifted.std(axis=1)) # ...and their respective standard deviations with a different syntax
print("Flattened array:", dataset_shifted.flatten()) # .flatten() makes an array 1D
```
%% Output
Basic array operations:
Sum 5050
Mean [ 0.08110435 9.98545632 20.02933072 30.02389687 39.97474169]
Std [0.99851965 0.97608616 1.0021044 0.98741231 1.00744611]
Flattened array: [-0.44605952 -0.22217352 -0.25286257 ... 41.29848664 39.92391046
39.72392673]
%% Cell type:markdown id:fdacbe66 tags:
### Array Indexing
Numpy support a powerful way of accessing entries of an array. For this purpose, ranges using ```:```, numpy arrays of integers and numpy arrays of booleans can be used:
%% Cell type:code id:e15a8939 tags:
``` python
array_to_be_masked = np.arange(100)
# Let's select the entries between index 20 and 30:
print(array_to_be_masked[20:30])
# First 10 entries:
print(array_to_be_masked[:10])
# Print the last 5 elements:
print(array_to_be_masked[-5:])
# Print the 10th, 13th and 21st elements:
print(array_to_be_masked[np.array([10, 13, 21])])
```
%% Output
[20 21 22 23 24 25 26 27 28 29]
[0 1 2 3 4 5 6 7 8 9]
[95 96 97 98 99]
[10 13 21]
%% Cell type:code id:184db0da tags:
``` python
# Let's shuffle the elements and reshape the array to make things more complicated!
np.random.shuffle(array_to_be_masked)
array_to_be_masked = array_to_be_masked.reshape((25, -1)) # "-1" means "do whatever needed to get the right shape"
# Select the first entries in each row (slicing)
print(array_to_be_masked[0, :])
# Select all entries <=30 in the flattened array:
print(array_to_be_masked[(array_to_be_masked<=30)])
# Print the mask:
print((array_to_be_masked<=30))
```
%% Output
[20 21 22 23 24 25 26 27 28 29]
[0 1 2 3 4 5 6 7 8 9]
[95 96 97 98 99]
[10 13 21]
[18 19 17 23 13 3 6 12 7 25 0 9 14 27 1 11 2 29 26 8 28 5 30 15
10 20 24 21 22 4 16]
[[False True True False]
[False False False True]
[False False False False]
[False False True True]
[54 49 48 17]
[17 1 12 22 14 29 3 9 13 21 23 8 20 6 16 2 10 11 4 19 30 18 28 15
0 24 5 7 25 26 27]
[[False False False True]
[ True True False False]
[ True False False False]
[False False True False]
[False False False False]
[False True True True]
[ True False False True]
[False False False True]
[ True False False False]
[False False False False]
[False True False False]
[ True True False False]
[False True False False]
[False True False True]
[ True False False False]
[ True False False False]
[False True True False]
[ True False True False]
[False False False False]
[False False True False]
[False False False True]
[False False False False]
[False False False True]
[False True True False]
[ True False True False]
[False True False True]
[False False False True]
[ True False False True]
[False False True False]
[ True False True True]
[False False False False]
[False False True False]
[False True False False]
[False True False False]
[False True True False]
[False False False True]
[ True False True False]]
[False False True False]]
......
......
%% Cell type:markdown id:3881ec5d tags:
# PyTorch Tutorial
---------------
Welcome to the pytorch tutorial! Here we will go through some of the basics of pytorch.
The library can be imported the usual way:
The library can be imported the as ```torch```:
%% Cell type:code id:e812c4c3 tags:
``` python
import torch
```
%% Cell type:markdown id:232eb87a tags:
## Tensors
Even if name ```pytorch``` is not as telling as ```tensorflow```, ```pytorch``` supports the creation of tensors too:
%% Cell type:code id:da356762 tags:
``` python
tensor = torch.tensor([1., 5., 9., 15., -24., -13.])
print(tensor)
```
%% Output
tensor([ 1., 5., 9., 15., -24., -13.])
%% Cell type:markdown id:5ab59c06 tags:
These objects can both **store data and model parameters** (recall that in tensorflow ```tf.Variable``` is a child class of ```tf.Tensor``` and used for storing weights). To check whether a tensor is storing gradients, one can use the ```requires_grad``` attribute:
%% Cell type:code id:0e83bc87 tags:
``` python
print(tensor.requires_grad)
```
%% Output
False
%% Cell type:markdown id:318a84d9 tags:
To initialize a **tensor with gradients** one can use the ```requires_grad``` keyword during initialization; this creates the rough equivalent of a ```tf.Variable```. To obtain the gradients, ```.backward()``` has to be called on the output object:
%% Cell type:code id:2d6a9052 tags:
``` python
# Create a tensor with gradients and print it:
tensor_grad = torch.tensor([1., 5., 9., 15., -24., -13.], requires_grad=True)
print("Input tensor xᵢ: ", tensor_grad)
# Perform an operation on the tensor itself and sum the output making it a 1D function:
output = (tensor_grad ** 2).sum() # This defines y = Σᵢ xᵢ² for every xᵢ
# Evaluating the gradients:
output.backward()
# ...and printing 'em:
print("Gradients of Σᵢ xᵢ²: ", tensor_grad.grad)
```
%% Output
Input tensor xᵢ: tensor([ 1., 5., 9., 15., -24., -13.], requires_grad=True)
Gradients of Σᵢ xᵢ²: tensor([ 2., 10., 18., 30., -48., -26.])
%% Cell type:markdown id:546969ea tags:
**Conversion** from and to ```numpy``` is also supported in an intuitive way:
%% Cell type:code id:b15a0f08 tags:
``` python
import numpy as np
tensor_from_np = torch.tensor(np.arange(10)).float()
print("A tensor created from a numpy array: ", tensor_from_np)
tensor_torch = torch.linspace(0, 5, 6).float()
array_from_torch = tensor_torch.numpy()
print("An array created from a torch tensor:", array_from_torch)
```
%% Output
A tensor created from a numpy array: tensor([0., 1., 2., 3., 4., 5., 6., 7., 8., 9.])
An array created from a torch tensor: [0. 1. 2. 3. 4. 5.]
%% Cell type:markdown id:2da3a5ce tags:
## Fundamental Mathematical Operations
```pytorch``` supports several basic mathematical operations on tensors too. Its syntax more or less follows that of ```numpy``` for convenience.
%% Cell type:code id:b164940f tags:
``` python
# A toy tensor:
tensor = torch.arange(10).float() # Create a tensor from 0 to 9
print("Mean:", torch.mean(tensor))
print("Std :", torch.std(tensor))
# Random numbers:
# A normal sample with mean 1 and std 0.5:
normal = torch.normal(mean=1., std=0.5, size=[1, 10000])
print("\nNormal Sample Properties:")
print(" Shape:", normal.shape)
print(" Mean: ", normal.mean())
print(" Std: ", normal.std())
# Getting elements along an axis (slicing):
print("\nThe first row of the normal samples:")
print(normal[0,:])
```
%% Output
Mean: tensor(4.5000)
Std : tensor(3.0277)
Normal Sample Properties:
Shape: torch.Size([1, 10000])
Mean: tensor(1.0189)
Std: tensor(0.4937)
The first row of the normal samples:
tensor([1.1931, 0.7112, 1.0409, ..., 1.2609, 0.1830, 1.6109])
%% Cell type:markdown id:2046ab07 tags:
A key **difference in syntax** however is that ```pytorch``` knows the ```axis``` keyword as ```dim```:
%% Cell type:code id:4d244cd6 tags:
``` python
# A uniform sample from [0, 1)
uniform = torch.rand([3, 100000])
print("\nUniform Sample Properties:")
print(" Shape:", uniform.shape)
print(" Mean: ", uniform.mean(dim=1)) # Equals 1/2 ≈ 0.5, the mean of a uniform distribution between [0, 1)
print(" Std: ", uniform.std(dim=1)) # Equals 1/12**0.5 ≈ 0.2887, the std of a uniform distribution of width 1
```
%% Output
Uniform Sample Properties:
Shape: torch.Size([3, 100000])
Mean: tensor([0.4991, 0.5005, 0.4996])
Std: tensor([0.2888, 0.2885, 0.2884])
......
......
%% 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: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 [0, 1).
t3 = tf.random.uniform([2, 3]) # Random sampling from the interval [0, 1) as a shape (2, 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.6821799 0.35027337 0.35916233]], shape=(1, 3), dtype=float32)
tf.random_uniform([1, 3]) = tf.Tensor(
[[0.9811375 0.9579929 0.44337857]
[0.4048884 0.99830174 0.6368449 ]], shape=(2, 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)
2022-08-01 14:33:35.064582: I tensorflow/compiler/jit/xla_cpu_device.cc:41] Not creating XLA devices, tf_xla_enable_xla_devices not set
2022-08-01 14:33:35.067452: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: SSE4.1 SSE4.2 AVX
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2022-08-01 14:33:35.073213: I tensorflow/core/common_runtime/process_util.cc:146] Creating new thread pool with default inter op setting: 2. Tune using inter_op_parallelism_threads for best performance.
%% Cell type:markdown id:33a5ef69 tags:
You can **transform** a tensor **to a numpy array** using ```tensor.numpy()```:
%% Cell type:code id:0b576f35 tags:
``` python
print(t3.numpy()) # Conversion to numpy
print(type(t3.numpy())) # Printing the type of the converted array
```
%% Output
[[0.6821799 0.35027337 0.35916233]]
[[0.9811375 0.9579929 0.44337857]
[0.4048884 0.99830174 0.6368449 ]]
<class 'numpy.ndarray'>
%% 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)```:
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 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:11af2105 tags:
## Fundamental Mathematical Operations
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))
```
%% 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)
%% Cell type:code id:d8b9f68c tags:
``` python
# 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)>
%% Cell type:code id:78ba73da tags:
``` python
# Create a normal distribution with mean 0 and std 1 of shape (3, 10000):
y = tf.random.normal([3, 10000], mean=0, stddev=1)
# Evaluate the means and standard deviations along an axis:
print("Means:", tf.math.reduce_mean(y, axis=1))
print("Stds: ", tf.math.reduce_std(y, axis=1))
```
%% Output
Means: tf.Tensor([-0.01646124 0.01251054 0.00447996], shape=(3,), dtype=float32)
Stds: tf.Tensor([1.0073316 0.9962494 1.0039229], shape=(3,), dtype=float32)
......
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment