Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow...

174
[1] Deep Learning and TensorFlow – Episode 4 Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università degli Studi di Pavia

Transcript of Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow...

Page 1: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[1]Deep Learning and TensorFlow – Episode 4

Deep Learningand TensorFlowEpisode 4TensorFlow Basics

Part 1

Università degli Studi di Pavia

Page 2: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[2]Deep Learning and TensorFlow – Episode 4

Summary

Page 3: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[3]Deep Learning and TensorFlow – Episode 4

Main References

Page 4: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[4]Deep Learning and TensorFlow – Episode 4

What is TensorFlow?

Page 5: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[5]Deep Learning and TensorFlow – Episode 4

What is TensorFlow?

Page 6: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[6]Deep Learning and TensorFlow – Episode 4

Why TensorFlow? (alternatives?)

Page 7: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[7]Deep Learning and TensorFlow – Episode 4

Why TensorFlow?

Page 8: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[8]Deep Learning and TensorFlow – Episode 4

Example application: classify skin cancer

Page 9: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[9]Deep Learning and TensorFlow – Episode 4

Example application: Neural Style Transfer

Page 10: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[10]Deep Learning and TensorFlow – Episode 4

The structure of a TensorFlow program

𝑦 = (𝑎 ∗ 𝑏)𝑚𝑢𝑙

+ (𝑎 + 𝑏)𝑎𝑑𝑑

𝑎𝑑𝑑

Page 11: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[11]Deep Learning and TensorFlow – Episode 4

Graphs: an intuitive view

Page 12: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[12]Deep Learning and TensorFlow – Episode 4

Graphs: an intuitive view

Page 13: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[13]Deep Learning and TensorFlow – Episode 4

Graphs: an intuitive view

Page 14: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[14]Deep Learning and TensorFlow – Episode 4

Graphs: an intuitive view

Page 15: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[15]Deep Learning and TensorFlow – Episode 4

Graphs: an intuitive view

Page 16: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[16]Deep Learning and TensorFlow – Episode 4

Graphs: an intuitive view

Page 17: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[17]Deep Learning and TensorFlow – Episode 4

Graphs: an intuitive way (with matrices)

Page 18: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[18]Deep Learning and TensorFlow – Episode 4

Graphs: an intuitive way (with matrices)

Page 19: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[19]Deep Learning and TensorFlow – Episode 4

Graphs: an intuitive way (with matrices)

1 23 4

5 67 8

Page 20: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[20]Deep Learning and TensorFlow – Episode 4

Graphs: an intuitive way (with matrices)

1 23 4

5 67 8

5 67 8

1 23 4

Page 21: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[21]Deep Learning and TensorFlow – Episode 4

Graphs: an intuitive way (with matrices)

5 1221 32

6 810 12

Page 22: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[22]Deep Learning and TensorFlow – Episode 4

Graphs: an intuitive way (with matrices)

11 2031 44

Page 23: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[23]Deep Learning and TensorFlow – Episode 4

What is Tensorflow?

Page 24: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[24]Deep Learning and TensorFlow – Episode 4

Sessions: computing flow graphs

Page 25: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[25]Deep Learning and TensorFlow – Episode 4

The first TensorFlow program

import tensorflow as tf

a = 3

b = 7

y = tf.multiply(a, b) + tf.add(a, b)

Page 26: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[26]Deep Learning and TensorFlow – Episode 4

The first TensorFlow program

import tensorflow as tf

a = 3

b = 7

y = tf.multiply(a, b) + tf.add(a, b)

Page 27: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[27]Deep Learning and TensorFlow – Episode 4

The first TensorFlow program

import tensorflow as tf

a = 3

b = 7

y = tf.multiply(a, b) + tf.add(a, b)

print(y)

Page 28: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[28]Deep Learning and TensorFlow – Episode 4

The first TensorFlow program

import tensorflow as tf

a = 3

b = 7

y = tf.multiply(a, b) + tf.add(a, b)

print(y)

>> Tensor("Add:0", shape=(), dtype=int32)

Page 29: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[29]Deep Learning and TensorFlow – Episode 4

The first TensorFlow program

import tensorflow as tf

a = 3

b = 7

y = tf.multiply(a, b) + tf.add(a, b)

print(y)

>> Tensor("Add:0", shape=(), dtype=int32)

Page 30: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[30]Deep Learning and TensorFlow – Episode 4

Sessions

Page 31: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[31]Deep Learning and TensorFlow – Episode 4

Sessions

sess

y

Page 32: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[32]Deep Learning and TensorFlow – Episode 4

Sessions

sess

y

import tensorflow as tfy = tf.add(3, 7)sess = tf.Session()print(sess.run(y))sess.close()

Page 33: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[33]Deep Learning and TensorFlow – Episode 4

Sessions

sess

y

import tensorflow as tfy = tf.add(3, 7)sess = tf.Session()print(sess.run(y))sess.close()

with

Page 34: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[34]Deep Learning and TensorFlow – Episode 4

The first TensorFlow program

import tensorflow as tf

a = 3

b = 7

y = tf.multiply(a, b) + tf.add(a, b)

Page 35: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[35]Deep Learning and TensorFlow – Episode 4

The first TensorFlow program

import tensorflow as tf

a = 3

b = 7

y = tf.multiply(a, b) + tf.add(a, b)

with tf.Session() as sess:

print(sess.run(y))

Page 36: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[36]Deep Learning and TensorFlow – Episode 4

The first TensorFlow program

import tensorflow as tf

a = 3

b = 7

y = tf.multiply(a, b) + tf.add(a, b)

with tf.Session() as sess:

print(sess.run(y))

>> 31

Page 37: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[37]Deep Learning and TensorFlow – Episode 4

A graph in TensorFlow

import tensorflow as tf

a = 3

b = 7

y = tf.multiply(a, b) + tf.add(a, b)

Page 38: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[38]Deep Learning and TensorFlow – Episode 4

A graph in TensorFlow

import tensorflow as tf

a = 3

b = 7

y = tf.multiply(a, b) + tf.add(a, b)

with tf.Session() as sess:

print(sess.run(y))

Page 39: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[39]Deep Learning and TensorFlow – Episode 4

A graph in TensorFlow

import tensorflow as tf

a = 3

b = 7

y = tf.multiply(a, b) + tf.add(a, b)

with tf.Session() as sess:

print(sess.run(y))

>> 31

Page 40: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[40]Deep Learning and TensorFlow – Episode 4

A graph in TensorFlow (with matrices)

import tensorflow as tf

a = tf.constant([1,2,3,4], shape=(2,2))

b = tf.constant([5,6,7,8], shape=(2,2))

y = tf.multiply(a, b) + tf.add(a, b)

Page 41: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[41]Deep Learning and TensorFlow – Episode 4

A graph in TensorFlow (with matrices)

import tensorflow as tf

a = tf.constant([1,2,3,4], shape=(2,2))

b = tf.constant([5,6,7,8], shape=(2,2))

y = tf.multiply(a, b) + tf.add(a, b)

with tf.Session() as sess:

print(sess.run(y))

Page 42: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[42]Deep Learning and TensorFlow – Episode 4

A graph in TensorFlow (with matrices)

import tensorflow as tf

a = tf.constant([1,2,3,4], shape=(2,2))

b = tf.constant([5,6,7,8], shape=(2,2))

y = tf.multiply(a, b) + tf.add(a, b)

with tf.Session() as sess:

print(sess.run(y))

>> [[11 20]

[31 44]]

Page 43: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[43]Deep Learning and TensorFlow – Episode 4

A graph in TensorFlow (with matrices)

import tensorflow as tf

a = tf.constant([1,2,3,4], shape=(2,2))

b = tf.constant([5,6,7,8], shape=(2,2))

y = tf.multiply(a, b) + tf.add(a, b)

with tf.Session() as sess:

print(sess.run(y))

>> [[11 20]

[31 44]]

tf.constant

shape

Page 44: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[44]Deep Learning and TensorFlow – Episode 4

Why graphs?

Page 45: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[45]Deep Learning and TensorFlow – Episode 4

Why graphs?

Page 46: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[46]Deep Learning and TensorFlow – Episode 4

Why graphs?

Page 47: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[47]Deep Learning and TensorFlow – Episode 4

Why graphs?

Page 48: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[48]Deep Learning and TensorFlow – Episode 4

Why graphs?

Page 49: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[49]Deep Learning and TensorFlow – Episode 4

Why graphs?

Page 50: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[50]Deep Learning and TensorFlow – Episode 4

Visualization with TensorBoard

import tensorflow as tf

a = tf.constant(3, name='a') # equivalent to a = 3

b = tf.constant(7, name='b')

y = tf.multiply(a, b, name='mul_op') + tf.add(a, b, name='add_op')

with tf.Session() as sess:

print(sess.run(y))

Page 51: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[51]Deep Learning and TensorFlow – Episode 4

Visualization with TensorBoard

import tensorflow as tf

a = tf.constant(3, name='a') # equivalent to a = 3

b = tf.constant(7, name='b')

y = tf.multiply(a, b, name='mul_op') + tf.add(a, b, name='add_op')

with tf.Session() as sess:

print(sess.run(y))

Page 52: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[52]Deep Learning and TensorFlow – Episode 4

Visualization with TensorBoard

import tensorflow as tf

a = tf.constant(3, name='a') # equivalent to a = 3

b = tf.constant(7, name='b')

y = tf.multiply(a, b, name='mul_op') + tf.add(a, b, name='add_op')

with tf.Session() as sess:

print(sess.run(y))

tf.add

Page 53: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[53]Deep Learning and TensorFlow – Episode 4

Visualization with TensorBoard

import tensorflow as tf

a = tf.constant(3, name='a') # equivalent to a = 3

b = tf.constant(7, name='b')

with tf.Session() as sess:

print(sess.run(y))

tf.add

y = tf.add(tf.multiply(a, b, name='mul_op'), tf.add(a, b, name='add_op'))

Page 54: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[54]Deep Learning and TensorFlow – Episode 4

Visualization with TensorBoard

import tensorflow as tf

a = tf.constant(3, name='a') # equivalent to a = 3

b = tf.constant(7, name='b')

writer = tf.summary.FileWriter('./graphs', tf.get_default_graph())

with tf.Session() as sess:

print(sess.run(y))

writer.close() # close the writer when you’re done using it

y = tf.add(tf.multiply(a, b, name='mul_op'), tf.add(a, b, name='add_op'))

Page 55: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[55]Deep Learning and TensorFlow – Episode 4

Visualization with TensorBoard

import tensorflow as tf

a = tf.constant(3, name='a') # equivalent to a = 3

b = tf.constant(7, name='b')

writer = tf.summary.FileWriter('./graphs', tf.get_default_graph())

with tf.Session() as sess:

print(sess.run(y))

writer.close() # close the writer when you’re done using it

y = tf.add(tf.multiply(a, b, name='mul_op'), tf.add(a, b, name='add_op'))

Page 56: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[56]Deep Learning and TensorFlow – Episode 4

Visualization with TensorBoard

import tensorflow as tf

a = tf.constant(3, name='a') # equivalent to a = 3

b = tf.constant(7, name='b')

with tf.Session() as sess:writer = tf.summary.FileWriter('./graphs', sess.graph)print(sess.run(y))

y = tf.add(tf.multiply(a, b, name='mul_op'), tf.add(a, b, name='add_op'))

Page 57: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[57]Deep Learning and TensorFlow – Episode 4

Run it!

$ python3 [yourprogram].py$ tensorboard --logdir="./graphs" --port 6006

Page 58: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[58]Deep Learning and TensorFlow – Episode 4

Run it!

$ python3 [yourprogram].py$ tensorboard --logdir="./graphs" --port 6006

http://localhost:6006/

Page 59: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[59]Deep Learning and TensorFlow – Episode 4

The same graph in Tensorboard

a = tf.constant(3, name='a')b = tf.constant(7, name='b')y = tf.multiply(a, b, name='mul_op') +

tf.add(a, b, name='add_op')

Page 60: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[60]Deep Learning and TensorFlow – Episode 4

The same graph in Tensorboard

name

a = tf.constant(3, name='a')b = tf.constant(7, name='b')y = tf.multiply(a, b, name='mul_op') +

tf.add(a, b, name='add_op')

Page 61: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[61]Deep Learning and TensorFlow – Episode 4

x = 2y = 3

add_op = tf.add(x, y)mul_op = tf.multiply(x, y)

useless = tf.multiply(x, add_op,name='useless')

pow_op = tf.pow(add_op, mul_op)

with tf.Session() as sess:z = sess.run(pow_op)

Subgraphs

Page 62: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[62]Deep Learning and TensorFlow – Episode 4

x = 2y = 3

add_op = tf.add(x, y)mul_op = tf.multiply(x, y)

useless = tf.multiply(x, add_op,name='useless')

pow_op = tf.pow(add_op, mul_op)

with tf.Session() as sess:z = sess.run(pow_op)

Subgraphs

Page 63: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[63]Deep Learning and TensorFlow – Episode 4

x = 2y = 3

add_op = tf.add(x, y)mul_op = tf.multiply(x, y)

useless = tf.multiply(x, add_op,name='useless')

pow_op = tf.pow(add_op, mul_op)

with tf.Session() as sess:z = sess.run(pow_op)

Subgraphs

pow_oppow_op uselessuseless

Page 64: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[64]Deep Learning and TensorFlow – Episode 4

Subgraphs

x = 2y = 3

add_op = tf.add(x, y)mul_op = tf.multiply(x, y)

useless = tf.multiply(x, add_op,name='useless')

pow_op = tf.pow(add_op, mul_op)

with tf.Session() as sess:z, not_useless =

sess.run([useless, pow_op])

Page 65: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[65]Deep Learning and TensorFlow – Episode 4

Subgraphs

x = 2y = 3

add_op = tf.add(x, y)mul_op = tf.multiply(x, y)

useless = tf.multiply(x, add_op,name='useless')

pow_op = tf.pow(add_op, mul_op)

with tf.Session() as sess:z, not_useless =

sess.run([useless, pow_op])

sess.run

Page 66: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[66]Deep Learning and TensorFlow – Episode 4

TensorFlow and GPUs

Page 67: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[67]Deep Learning and TensorFlow – Episode 4

Distributed computation

Page 68: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[68]Deep Learning and TensorFlow – Episode 4

Distributed computation

# Creates a graph.

with tf.device('/gpu:2'):

a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], name='a')

b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], name='b')

y = tf.multiply(a, b)

# Creates a session with log_device_placement set to True.

sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))

# Runs the op.

print(sess.run(y))

Page 69: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[69]Deep Learning and TensorFlow – Episode 4

Protocol buffers

print(tf.get_default_graph().as_graph_def())

Page 70: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[70]Deep Learning and TensorFlow – Episode 4

Protocol buffers

print(tf.get_default_graph().as_graph_def())

node {name: "mul_op/a"op: "Const"attr {key: "dtype"value { type: DT_INT32 }

}attr {key: "value"value { tensor

{ dtype: DT_INT32tensor_shape {}int_val: 7 }

}}

}

Page 71: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[71]Deep Learning and TensorFlow – Episode 4

Protocol buffers

print(tf.get_default_graph().as_graph_def())

node {name: "mul_op"op: "Mul"input: "mul_op/a"input: "mull_op/b"attr {key: "T"value {type: DT_INT32

}}

}

Page 72: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[72]Deep Learning and TensorFlow – Episode 4

Protocol buffers

print(tf.get_default_graph().as_graph_def())

node {name: "add"op: "Add"input: "mul_op"input: "add_op"attr {key: "T"value {type: DT_INT32

}}

}

Page 73: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[73]Deep Learning and TensorFlow – Episode 4

Tensors, constants and ops

Page 74: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[74]Deep Learning and TensorFlow – Episode 4

What is a tensor?

Page 75: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[75]Deep Learning and TensorFlow – Episode 4

Tensors as objects tf.Tensor

float32 int32 string

Page 76: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[76]Deep Learning and TensorFlow – Episode 4

Tensors as objects tf.Tensor

float32 int32 string

Page 77: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[77]Deep Learning and TensorFlow – Episode 4

Tensors as objects tf.Tensor

float32 int32 string

r = tf.rank(cat_image) # r is a tensors = tf.shape(cat_image) # s is a tensor# after evaluation r will be 3# s will be (28, 28, 3)

Page 78: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[78]Deep Learning and TensorFlow – Episode 4

Tensors as objects tf.Tensor

float32 int32 string

r = tf.rank(cat_image) # r is a tensors = tf.shape(cat_image) # s is a tensor# after evaluation r will be 3# s will be (28, 28, 3)

r s

Page 79: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[79]Deep Learning and TensorFlow – Episode 4

Tensors Containers

Page 80: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[80]Deep Learning and TensorFlow – Episode 4

Tensors Containers

Page 81: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[81]Deep Learning and TensorFlow – Episode 4

Tensors Containers

Page 82: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[82]Deep Learning and TensorFlow – Episode 4

Tensorflow Operations

Page 83: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[83]Deep Learning and TensorFlow – Episode 4

Graph representations of tensors and containers

a = tf.constant(3, name='a')b = tf.constant(7, name='b')y = tf.multiply(a, b, name='mul_op') +

tf.add(a, b, name='add_op')

Page 84: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[84]Deep Learning and TensorFlow – Episode 4

Graph representations of tensors and containers

a = tf.constant(3, name='a')b = tf.constant(7, name='b')y = tf.multiply(a, b, name='mul_op') +

tf.add(a, b, name='add_op')

Page 85: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[85]Deep Learning and TensorFlow – Episode 4

Constants tf.constant(value,

dtype=None,shape=None,name='Const',verify_shape=False)

Page 86: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[86]Deep Learning and TensorFlow – Episode 4

Constants tf.constant(value,

dtype=None,shape=None,name='Const',verify_shape=False)

# constant of 1d tensor (vector)a = tf.constant([2, 2], name="vector")

# constant of 2x2 tensor (matrix)b = tf.constant([[0, 1], [2, 3]], name="matrix")

x = tf.multiply(a, b, name='mul_op')

>> x = [[0 2][4 6]]

Page 87: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[87]Deep Learning and TensorFlow – Episode 4

Constants tf.constant(value,

dtype=None,shape=None,name='Const',verify_shape=False)

# constant of 1d tensor (vector)a = tf.constant([2, 2], name="vector")

# constant of 2x2 tensor (matrix)b = tf.constant([[0, 1], [2, 3]], name="matrix")

x = tf.multiply(a, b, name='mul_op')

>> x = [[0 2][4 6]]

Page 88: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[88]Deep Learning and TensorFlow – Episode 4

Constants filled with specific valuestf.zeros(shape, dtype=tf.float32, name=None)

tf.zeros_like(input_tensor, dtype=None, name=None, optimize=True)

tf.ones(shape, dtype=tf.float32, name=None)

tf.ones_like (input_tensor, dtype=None, name=None, optimize=True)

tf.fill(shape, value, name=None)

Page 89: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[89]Deep Learning and TensorFlow – Episode 4

Constants filled with specific valuestf.zeros(shape, dtype=tf.float32, name=None)

tf.zeros_like(input_tensor, dtype=None, name=None, optimize=True)

tf.ones(shape, dtype=tf.float32, name=None)

tf.ones_like (input_tensor, dtype=None, name=None, optimize=True)

tf.fill(shape, value, name=None)

tf.zeros([2, 3], tf.int32)

tf.ones([2, 3], tf.int32)

0 0 00 0 0

1 1 11 1 1

Page 90: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[90]Deep Learning and TensorFlow – Episode 4

Constants filled with specific valuestf.zeros(shape, dtype=tf.float32, name=None)

tf.zeros_like(input_tensor, dtype=None, name=None, optimize=True)

tf.ones(shape, dtype=tf.float32, name=None)

tf.ones_like (input_tensor, dtype=None, name=None, optimize=True)

tf.fill(shape, value, name=None)

tf.zeros([2, 3], tf.int32)

tf.ones([2, 3], tf.int32)

# input_tensor is [[0, 1], [2, 3], [4, 5]]

tf.zeros_like(input_tensor)

tf.ones_like(input_tensor)

0 0 00 0 0

1 11 11 1

1 1 11 1 1

0 00 00 0

Page 91: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[91]Deep Learning and TensorFlow – Episode 4

Constants filled with specific valuestf.zeros(shape, dtype=tf.float32, name=None)

tf.zeros_like(input_tensor, dtype=None, name=None, optimize=True)

tf.ones(shape, dtype=tf.float32, name=None)

tf.ones_like (input_tensor, dtype=None, name=None, optimize=True)

tf.fill(shape, value, name=None)

tf.zeros([2, 3], tf.int32)

tf.ones([2, 3], tf.int32)

# input_tensor is [[0, 1], [2, 3], [4, 5]]

tf.zeros_like(input_tensor)

tf.ones_like(input_tensor)

tf.fill([2, 3], 8)

0 0 00 0 0

1 11 11 1

8 88 88 8

1 1 11 1 1

0 00 00 0

Page 92: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[92]Deep Learning and TensorFlow – Episode 4

Constants as sequences

tf.lin_space(start, stop, num, name=None)

tf.lin_space(10.0, 13.0, 4) ==> [10. 11. 12. 13.]

tf.range(start, limit=None, delta=1, dtype=None, name='range')

tf.range(3, 18, 3) ==> [3 6 9 12 15]

tf.range(5) ==> [0 1 2 3 4]

Page 93: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[93]Deep Learning and TensorFlow – Episode 4

Constants as sequences

tf.lin_space(start, stop, num, name=None)

tf.lin_space(10.0, 13.0, 4) ==> [10. 11. 12. 13.]

tf.range(start, limit=None, delta=1, dtype=None, name='range')

tf.range(3, 18, 3) ==> [3 6 9 12 15]

tf.range(5) ==> [0 1 2 3 4]

tf.range

for _ in tf.range(4):# TypeError

Page 94: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[94]Deep Learning and TensorFlow – Episode 4

Randomly generated Constants

tf.random_normal

tf.truncated_normal

tf.random_uniform

tf.random_shuffle

tf.random_crop

tf.multinomial

tf.random_gamma

Page 95: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[95]Deep Learning and TensorFlow – Episode 4

Randomly generated Constants

tf.random_normal

tf.truncated_normal

tf.random_uniform

tf.random_shuffle

tf.random_crop

tf.multinomial

tf.random_gamma

tf.set_random_seed(seed)

Page 96: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[96]Deep Learning and TensorFlow – Episode 4

Data types

Page 97: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[97]Deep Learning and TensorFlow – Episode 4

Data types

t_0 = 19 # scalars are treated like 0-d tensorstf.zeros_like(t_0) # ==> constant 0-d tensor with value 0tf.ones_like(t_0) # ==> constant 0-d tensor with value 1

t_1 = ["apple", "peach", "grape"] # 1-d arrays are treated like 1-d tensorstf.zeros_like(t_1) # ==> ['' '' '']tf.ones_like(t_1) # ==> TypeError

t_2 = [[True, False, False],[False, False, True],[False, True, False]] # 2-d arrays are treated like 2-d tensors

tf.zeros_like(t_2) # ==> 3x3 tensor, all elements are Falsetf.ones_like(t_2) # ==> 3x3 tensor, all elements are True

Page 98: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[98]Deep Learning and TensorFlow – Episode 4

Data types

Page 99: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[99]Deep Learning and TensorFlow – Episode 4

Variables

Page 100: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[100]Deep Learning and TensorFlow – Episode 4

Shortcomings of constants

Page 101: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[101]Deep Learning and TensorFlow – Episode 4

Shortcomings of constants

attr {key: "value"value {

tensor {dtype: DT_FLOAT

tensor_shape {dim { size: 2 }

}tensor_content:

"\000\000\200?\000\000\000@"}

}}

my_const = tf.constant([1.0, 2.0], name="my_const")with tf.Session() as sess:

print(sess.graph.as_graph_def())

Page 102: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[102]Deep Learning and TensorFlow – Episode 4

Shortcomings of constants

attr {key: "value"value {

tensor {dtype: DT_FLOAT

tensor_shape {dim { size: 2 }

}tensor_content:

"\000\000\200?\000\000\000@"}

}}

my_const = tf.constant([1.0, 2.0], name="my_const")with tf.Session() as sess:

print(sess.graph.as_graph_def())

Page 103: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[103]Deep Learning and TensorFlow – Episode 4

Variables

tf.Variable

Page 104: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[104]Deep Learning and TensorFlow – Episode 4

Variables

tf.Variable

# create variables with tf.Variable

s = tf.Variable(2, name="scalar") m = tf.Variable([[0, 1], [2, 3]], name="matrix") W = tf.Variable(tf.zeros([784,10]))

Page 105: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[105]Deep Learning and TensorFlow – Episode 4

Variables

tf.Variable

# create variables with tf.Variable

s = tf.Variable(2, name="scalar") m = tf.Variable([[0, 1], [2, 3]], name="matrix") W = tf.Variable(tf.zeros([784,10]))

# create variables with tf.get_variable

s = tf.get_variable("scalar", initializer=tf.constant(2)) m = tf.get_variable("matrix", initializer=tf.constant([[0, 1], [2, 3]]))W = tf.get_variable("big_matrix", shape=(784, 10),

initializer=tf.zeros_initializer())

Page 106: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[106]Deep Learning and TensorFlow – Episode 4

Variables

tf.Variable

# create variables with tf.Variable

s = tf.Variable(2, name="scalar") m = tf.Variable([[0, 1], [2, 3]], name="matrix") W = tf.Variable(tf.zeros([784,10]))

# create variables with tf.get_variable

s = tf.get_variable("scalar", initializer=tf.constant(2)) m = tf.get_variable("matrix", initializer=tf.constant([[0, 1], [2, 3]]))W = tf.get_variable("big_matrix", shape=(784, 10),

initializer=tf.zeros_initializer())

get_variable

Page 107: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[107]Deep Learning and TensorFlow – Episode 4

Variables operator tf.Variable

x = tf.Variable(…)

x.initializer # init opx.value # read op

# and more..

Page 108: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[108]Deep Learning and TensorFlow – Episode 4

Variables operator tf.Variable

x = tf.Variable(…)

x.initializer # init opx.value # read op

# and more..

W = tf.get_variable("big_matrix", shape=(784, 10),initializer=tf.zeros_initializer())

with tf.Session() as sess:

print(sess.run(W))

Page 109: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[109]Deep Learning and TensorFlow – Episode 4

Variables operator tf.Variable

x = tf.Variable(…)

x.initializer # init opx.value # read op

# and more..

W = tf.get_variable("big_matrix", shape=(784, 10),initializer=tf.zeros_initializer())

with tf.Session() as sess:

print(sess.run(W))

>> FailedPreconditionError: Attempting to use uninitialized value Variable

Page 110: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[110]Deep Learning and TensorFlow – Episode 4

Variables initialization

with tf.Session() as sess:sess.run(tf.global_variables_initializer())

Page 111: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[111]Deep Learning and TensorFlow – Episode 4

Variables initialization

with tf.Session() as sess:sess.run(tf.global_variables_initializer())

with tf.Session() as sess:sess.run(tf.variables_initializer([a, b]))

Page 112: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[112]Deep Learning and TensorFlow – Episode 4

Variables initialization

with tf.Session() as sess:sess.run(tf.global_variables_initializer())

with tf.Session() as sess:sess.run(tf.variables_initializer([a, b]))

W = tf.Variable(tf.zeros([784,10]))

with tf.Session() as sess:sess.run(W.initializer)

Page 113: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[113]Deep Learning and TensorFlow – Episode 4

# W is a random 700 x 100 variable object

W = tf.Variable(tf.truncated_normal([700, 10]))

with tf.Session() as sess:

sess.run(W.initializer)

Variables evaluation

print(W)

Page 114: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[114]Deep Learning and TensorFlow – Episode 4

# W is a random 700 x 100 variable object

W = tf.Variable(tf.truncated_normal([700, 10]))

with tf.Session() as sess:

sess.run(W.initializer)

>> Tensor("Variable/read:0", shape=(700, 10), dtype=float32)

Variables evaluation

print(W)

Page 115: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[115]Deep Learning and TensorFlow – Episode 4

# W is a random 700 x 100 variable object

W = tf.Variable(tf.truncated_normal([700, 10]))

with tf.Session() as sess:

sess.run(W.initializer)

Variables evaluation

print(W.eval())

Page 116: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[116]Deep Learning and TensorFlow – Episode 4

# W is a random 700 x 100 variable object

W = tf.Variable(tf.truncated_normal([700, 10]))

with tf.Session() as sess:

sess.run(W.initializer)

Variables evaluation

print(sess.run(W))print(W.eval())

Page 117: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[117]Deep Learning and TensorFlow – Episode 4

# W is a random 700 x 100 variable object

W = tf.Variable(tf.truncated_normal([700, 10]))

with tf.Session() as sess:

sess.run(W.initializer)

>> [[-0.76781619 -0.67020458 1.15333688 ..., -0.98434633 -1.25692499-0.90904623]

..., [ 0.19076447 -0.62968266 -1.97970271 ..., -1.48389161 0.681706431.46369624]]

Variables evaluation

print(sess.run(W))print(W.eval())

Page 118: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[118]Deep Learning and TensorFlow – Episode 4

Variable Assignments

W = tf.Variable(10)

W.assign(100)

with tf.Session() as sess:

sess.run(W.initializer)

print(W.eval())

Page 119: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[119]Deep Learning and TensorFlow – Episode 4

Variable Assignments

W = tf.Variable(10)

W.assign(100)

with tf.Session() as sess:

sess.run(W.initializer)

print(W.eval())

# >> 10

Page 120: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[120]Deep Learning and TensorFlow – Episode 4

Variable Assignments

W = tf.Variable(10)

W.assign(100)

with tf.Session() as sess:

sess.run(W.initializer)

print(W.eval())

# >> 10

W.assign(100)

Page 121: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[121]Deep Learning and TensorFlow – Episode 4

Variable Assignments

W = tf.Variable(10)

W.assign(100)

with tf.Session() as sess:

sess.run(W.initializer)

print(W.eval())

# >> 10

W = tf.Variable(10)

assign_op = W.assign(100)

with tf.Session() as sess:

sess.run(W.initializer)

sess.run(assign_op)

print(W.eval())

W.assign(100)

Page 122: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[122]Deep Learning and TensorFlow – Episode 4

Variable Assignments

W = tf.Variable(10)

W.assign(100)

with tf.Session() as sess:

sess.run(W.initializer)

print(W.eval())

# >> 10

W = tf.Variable(10)

assign_op = W.assign(100)

with tf.Session() as sess:

sess.run(W.initializer)

sess.run(assign_op)

print(W.eval())

# >> 100

W.assign(100)

Page 123: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[123]Deep Learning and TensorFlow – Episode 4

Variable Assignments

# create a variable whose original value is 2

my_var = tf.Variable(2, name="my_var")

# assign a*2 to a and call that op a_times_two

my_var_times_two = my_var.assign(2*my_var)

Page 124: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[124]Deep Learning and TensorFlow – Episode 4

Variable Assignments

# create a variable whose original value is 2

my_var = tf.Variable(2, name="my_var")

# assign a*2 to a and call that op a_times_two

my_var_times_two = my_var.assign(2*my_var)

2*my_var my_varmy_var_times_two

Page 125: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[125]Deep Learning and TensorFlow – Episode 4

Variable Assignments

# create a variable whose original value is 2

my_var = tf.Variable(2, name="my_var")

# assign a*2 to a and call that op a_times_two

my_var_times_two = my_var.assign(2*my_var)

with tf.Session() as sess:

sess.run(my_var.initializer)

sess.run(my_var_times_two) # >> the value of my_var now is 4

2*my_var my_varmy_var_times_two

Page 126: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[126]Deep Learning and TensorFlow – Episode 4

Variable Assignments

# create a variable whose original value is 2

my_var = tf.Variable(2, name="my_var")

# assign a*2 to a and call that op a_times_two

my_var_times_two = my_var.assign(2*my_var)

with tf.Session() as sess:

sess.run(my_var.initializer)

sess.run(my_var_times_two) # >> the value of my_var now is 4

sess.run(my_var_times_two) # >> the value of my_var now is 8

2*my_var my_varmy_var_times_two

Page 127: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[127]Deep Learning and TensorFlow – Episode 4

Variable Assignments

# create a variable whose original value is 2

my_var = tf.Variable(2, name="my_var")

# assign a*2 to a and call that op a_times_two

my_var_times_two = my_var.assign(2*my_var)

with tf.Session() as sess:

sess.run(my_var.initializer)

sess.run(my_var_times_two) # >> the value of my_var now is 4

sess.run(my_var_times_two) # >> the value of my_var now is 8

sess.run(my_var_times_two) # >> the value of my_var now is 16

2*my_var my_varmy_var_times_two

Page 128: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[128]Deep Learning and TensorFlow – Episode 4

Variables in multiple sessions

W = tf.Variable(10)

sess1 = tf.Session()

sess2 = tf.Session()

sess1.run(W.initializer)

sess2.run(W.initializer)

sess1.close()

sess2.close()

Page 129: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[129]Deep Learning and TensorFlow – Episode 4

Variables in multiple sessions

W = tf.Variable(10)

sess1 = tf.Session()

sess2 = tf.Session()

sess1.run(W.initializer)

sess2.run(W.initializer)

print(sess1.run(W.assign_add(10)))

print(sess2.run(W.assign_sub(2)))

sess1.close()

sess2.close()

Page 130: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[130]Deep Learning and TensorFlow – Episode 4

Variables in multiple sessions

W = tf.Variable(10)

sess1 = tf.Session()

sess2 = tf.Session()

sess1.run(W.initializer)

sess2.run(W.initializer)

print(sess1.run(W.assign_add(10)))

print(sess2.run(W.assign_sub(2)))

sess1.close()

sess2.close()

# >> 20# >> 8

Page 131: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[131]Deep Learning and TensorFlow – Episode 4

Variables in multiple sessions

W = tf.Variable(10)

sess1 = tf.Session()

sess2 = tf.Session()

sess1.run(W.initializer)

sess2.run(W.initializer)

print(sess1.run(W.assign_add(10)))

print(sess2.run(W.assign_sub(2)))

print(sess1.run(W.assign_add(100)))

print(sess2.run(W.assign_add(100)))

sess1.close()

sess2.close()

# >> 20# >> 8

Page 132: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[132]Deep Learning and TensorFlow – Episode 4

Variables in multiple sessions

W = tf.Variable(10)

sess1 = tf.Session()

sess2 = tf.Session()

sess1.run(W.initializer)

sess2.run(W.initializer)

print(sess1.run(W.assign_add(10)))

print(sess2.run(W.assign_sub(2)))

print(sess1.run(W.assign_add(100)))

print(sess2.run(W.assign_add(100)))

sess1.close()

sess2.close()

# >> 20# >> 8

# >> 120# >> 108

Page 133: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[134]Deep Learning and TensorFlow – Episode 4

Placeholders

Page 134: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[135]Deep Learning and TensorFlow – Episode 4

Placeholders

Page 135: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[136]Deep Learning and TensorFlow – Episode 4

Placeholders

Page 136: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[137]Deep Learning and TensorFlow – Episode 4

Placeholders

Page 137: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[138]Deep Learning and TensorFlow – Episode 4

Placeholders

Page 138: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[139]Deep Learning and TensorFlow – Episode 4

Placeholders

Page 139: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[140]Deep Learning and TensorFlow – Episode 4

Placeholder and feed dicts tf.placeholder(dtype, shape=None, name=None)

Page 140: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[141]Deep Learning and TensorFlow – Episode 4

Placeholder and feed dicts tf.placeholder(dtype, shape=None, name=None)

# create a placeholder for a vector of 3 elements, type tf.float32

a = tf.placeholder(tf.float32, shape=[3])

b = tf.constant([5, 5, 5], tf.float32)

# use the placeholder as you would a constant or a variable

c = a + b # short for tf.add(a, b)

Page 141: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[142]Deep Learning and TensorFlow – Episode 4

Placeholder and feed dicts tf.placeholder(dtype, shape=None, name=None)

# create a placeholder for a vector of 3 elements, type tf.float32

a = tf.placeholder(tf.float32, shape=[3])

b = tf.constant([5, 5, 5], tf.float32)

# use the placeholder as you would a constant or a variable

c = a + b # short for tf.add(a, b)

Page 142: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[143]Deep Learning and TensorFlow – Episode 4

Placeholder and feed dicts tf.placeholder(dtype, shape=None, name=None)

# create a placeholder for a vector of 3 elements, type tf.float32

a = tf.placeholder(tf.float32, shape=[3])

b = tf.constant([5, 5, 5], tf.float32)

# use the placeholder as you would a constant or a variable

c = a + b # short for tf.add(a, b)

with tf.Session() as sess:print(sess.run(c))

Page 143: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[144]Deep Learning and TensorFlow – Episode 4

Placeholder and feed dicts tf.placeholder(dtype, shape=None, name=None)

# create a placeholder for a vector of 3 elements, type tf.float32

a = tf.placeholder(tf.float32, shape=[3])

b = tf.constant([5, 5, 5], tf.float32)

# use the placeholder as you would a constant or a variable

c = a + b # short for tf.add(a, b)

>> InvalidArgumentError: a doesn’t have an>> actual value

with tf.Session() as sess:print(sess.run(c))

Page 144: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[145]Deep Learning and TensorFlow – Episode 4

Placeholder and feed dicts tf.placeholder(dtype, shape=None, name=None)

# create a placeholder for a vector of 3 elements, type tf.float32

a = tf.placeholder(tf.float32, shape=[3])

b = tf.constant([5, 5, 5], tf.float32)

# use the placeholder as you would a constant or a variable

c = a + b # short for tf.add(a, b)

with tf.Session() as sess:print(sess.run(c, feed_dict={a: [1, 2, 3]}))

Page 145: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[146]Deep Learning and TensorFlow – Episode 4

Placeholder and feed dicts tf.placeholder(dtype, shape=None, name=None)

# create a placeholder for a vector of 3 elements, type tf.float32

a = tf.placeholder(tf.float32, shape=[3])

b = tf.constant([5, 5, 5], tf.float32)

# use the placeholder as you would a constant or a variable

c = a + b # short for tf.add(a, b)

with tf.Session() as sess:print(sess.run(c, feed_dict={a: [1, 2, 3]}))

>> [6, 7, 8]

Page 146: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[147]Deep Learning and TensorFlow – Episode 4

Feed dicts

Page 147: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[148]Deep Learning and TensorFlow – Episode 4

Feed dicts

print(sess.run(some_op, feed_dict={a: [1, 2, 3]}))

Page 148: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[149]Deep Learning and TensorFlow – Episode 4

Feed dicts

print(sess.run(some_op, feed_dict={a: [1, 2, 3]}))

Page 149: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[150]Deep Learning and TensorFlow – Episode 4

Feed dicts

print(sess.run(some_op, feed_dict={a: [1, 2, 3]}))

None

Page 150: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[151]Deep Learning and TensorFlow – Episode 4

Feed dicts

print(sess.run(some_op, feed_dict={a: [1, 2, 3]}))

None

a = tf.placeholder(tf.float32, shape=None)

Page 151: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[152]Deep Learning and TensorFlow – Episode 4

Feed dicts

print(sess.run(some_op, feed_dict={a: [1, 2, 3]}))

None

a = tf.placeholder(tf.float32, shape=None)

Page 152: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[153]Deep Learning and TensorFlow – Episode 4

Feed dicts

print(sess.run(some_op, feed_dict={a: [1, 2, 3]}))

None

a = tf.placeholder(tf.float32, shape=None)

Page 153: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[154]Deep Learning and TensorFlow – Episode 4

Feed dicts

print(sess.run(some_op, feed_dict={a: [1, 2, 3]}))

None

a = tf.placeholder(tf.float32, shape=None)

with tf.Session() as sess:

for a_value in list_of_values_for_a:

print(sess.run(some_op, {a: a_value}))

Page 154: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[155]Deep Learning and TensorFlow – Episode 4

Feed dicts

Page 155: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[156]Deep Learning and TensorFlow – Episode 4

Feed dicts

Page 156: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[157]Deep Learning and TensorFlow – Episode 4

Feed dicts

a = tf.add(2, 5)

b = tf.multiply(a, 3)

with tf.Session() as sess:

# compute the value of b given a is 15

sess.run(b, feed_dict={a: 15})

Page 157: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[158]Deep Learning and TensorFlow – Episode 4

Feed dicts

a = tf.add(2, 5)

b = tf.multiply(a, 3)

with tf.Session() as sess:

# compute the value of b given a is 15

sess.run(b, feed_dict={a: 15})

Page 158: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[159]Deep Learning and TensorFlow – Episode 4

Feed dicts

a = tf.add(2, 5)

b = tf.multiply(a, 3)

with tf.Session() as sess:

# compute the value of b given a is 15

sess.run(b, feed_dict={a: 15})

tf.Graph.is_feedable(tensor

Page 159: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[160]Deep Learning and TensorFlow – Episode 4

A common mistake

Page 160: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[161]Deep Learning and TensorFlow – Episode 4

A common mistake

Page 161: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[162]Deep Learning and TensorFlow – Episode 4

A common mistake

Page 162: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[163]Deep Learning and TensorFlow – Episode 4

Lazy loading

x = tf.Variable(10, name='x')

y = tf.Variable(20, name='y')

z = tf.add(x, y)

writer = tf.summary.FileWriter('./graphs/normal_loading', tf.get_default_graph())

with tf.Session() as sess:

sess.run(tf.global_variables_initializer())

for _ in range(10):

sess.run(z)

writer.close()

Page 163: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[164]Deep Learning and TensorFlow – Episode 4

Lazy loading

x = tf.Variable(10, name='x')

y = tf.Variable(20, name='y')

z = tf.add(x, y)

writer = tf.summary.FileWriter('./graphs/normal_loading', tf.get_default_graph())

with tf.Session() as sess:

sess.run(tf.global_variables_initializer())

for _ in range(10):

sess.run(z)

writer.close()

Page 164: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[165]Deep Learning and TensorFlow – Episode 4

Lazy loading

x = tf.Variable(10, name='x')

y = tf.Variable(20, name='y')

writer = tf.summary.FileWriter('./graphs/normal_loading', tf.get_default_graph())

with tf.Session() as sess:

sess.run(tf.global_variables_initializer())

for _ in range(10):

sess.run(tf.add(x, y))

writer.close()

Page 165: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[166]Deep Learning and TensorFlow – Episode 4

Lazy loading

x = tf.Variable(10, name='x')

y = tf.Variable(20, name='y')

writer = tf.summary.FileWriter('./graphs/normal_loading', tf.get_default_graph())

with tf.Session() as sess:

sess.run(tf.global_variables_initializer())

for _ in range(10):

sess.run(tf.add(x, y))

writer.close()

Page 166: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[167]Deep Learning and TensorFlow – Episode 4

Lazy loading

Page 167: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[168]Deep Learning and TensorFlow – Episode 4

Lazy loading

node {

name: "Add"

op: "Add"

input: "x/read"

input: "y/read"

attr {

key: "T"

value {

type: DT_INT32

}

}

}

Page 168: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[169]Deep Learning and TensorFlow – Episode 4

Lazy loading

node {

name: "Add"

op: "Add"

input: "x/read"

input: "y/read"

attr {

key: "T"

value {

type: DT_INT32

}

}

}

node {

name: "Add_1"

op: "Add"

...

}

...

node {

name: "Add_10"

op: "Add"

...

}

Page 169: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[170]Deep Learning and TensorFlow – Episode 4

Lazy loading

node {

name: "Add"

op: "Add"

input: "x/read"

input: "y/read"

attr {

key: "T"

value {

type: DT_INT32

}

}

}

node {

name: "Add_1"

op: "Add"

...

}

...

node {

name: "Add_10"

op: "Add"

...

}

Page 170: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[171]Deep Learning and TensorFlow – Episode 4

Lazy loading

Page 171: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[172]Deep Learning and TensorFlow – Episode 4

Lazy loading

@property

Page 172: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[173]Deep Learning and TensorFlow – Episode 4

Next step…build the first machine learning model!

Page 173: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[174]Deep Learning and TensorFlow – Episode 4

The first machine learning model in TF

Page 174: Università degli Studi di Pavia Deep Learning and TensorFlow · Deep Learning and TensorFlow –Episode 4 [1] Deep Learning and TensorFlow Episode 4 TensorFlow Basics Part 1 Università

[175]Deep Learning and TensorFlow – Episode 4

Now's the time for part 2!