09.05.2023 Views

pdfcoffee

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Neural Network Foundations with TensorFlow 2.0

For now, let's start with a simple code comparison just to give you some initial

intuition. If you have never installed TensorFlow before, then let's install it using pip:

You can find more options for installing TensorFlow at https://

www.tensorflow.org/install.

Only CPU support:

pip install tensorflow

With GPU support:

pip install tensorflow-gpu

In order to understand what's new in TensorFlow 2.0, it might be useful to have a

look at the traditional way of coding neural networks in TensorFlow 1.0. If this is the

first time you have seen a neural network, please do not pay attention to the details

but simply count the number of lines:

import tensorflow.compat.v1 as tf

in_a = tf.placeholder(dtype=tf.float32, shape=(2))

def model(x):

with tf.variable_scope("matmul"):

W = tf.get_variable("W", initializer=tf.ones(shape=(2,2)))

b = tf.get_variable("b", initializer=tf.zeros(shape=(2)))

return x * W + b

out_a = model(in_a)

with tf.Session() as sess:

sess.run(tf.global_variables_initializer())

outs = sess.run([out_a],

feed_dict={in_a: [1, 0]})

In total, we have 11 lines here. Now let's install TensorFlow 2.0:

Only CPU support:

pip install tensorflow==2.0.0-alpha0

With GPU support:

pip install tensorflow-gpu==2.0.0-alpha0

[ 4 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!