Avançar para o conteúdo principal

TensorFlow Variables and Placeholders

In this second part of the introduction to TensorFlow we add two new node types: variables and placeholders.

Let's use the same example that we have used in the first part. We start by importing TensorFlow:

import tensorflow as tf

Next we start a session, but before let's make a reset to the graph internal state:

tf.reset_default_graph()
sess=tf.Session()

And now we create two variables of type 32 bit float:

x = tf.Variable(2.0,tf.float32)
y = tf.Variable(3.0,tf.float32)

Since we are using variables we must initialize them:

init = tf.global_variables_initializer()
sess.run(init)

The mathematical expression is this:

sumnodes = x + y

To evaluate the expression:

print(sess.run(sumnodes))

Because we are using variables we can change the values like so:

sess.run(x.assign(5.0))

As always we must execute the assign operation inside a TF session. To make multiple assigns we create references to the operations and execute them with one line:

NewX = x.assign(5.0)
NewY = y.assign(10.0)

sess.run([NewX,NewY])

Now the expression evaluates to a diferente result:

print(sess.run(sumnodes))

Placeholders have a different behavior as they allow to define the value only when the expression is evaluated and they can be assign to one value or a range of values.

Something like this, first define the placeholder:

a = tf.placeholder(tf.float32)

As you can see there is no value set. Next, let's change the expression:

sumnodes = x*a + y

To evaluate the expression we must use:

print(sess.run(sumnodes,{a: 10}))

The parameter defines the value of the placeholder. It's possible to use this:

print(sess.run(sumnodes,{a: range(10)}))

or this:

print(sess.run(sumnodes,{a: [2,5,8,11]}))

As we are working with placeholders and variables it's very important to close the TensorFlow session:

sess.close()

The video on youtube


The code on GitHub

Comentários

Mensagens populares deste blogue

New Unity 3D Project

Today I will present a new project that I started. From the post about the car I am building a game with cars, or transportation. The idea is very simple: the player starts with a car and a mission, when he is done with the mission he gets some cash that can be spent buying a new vehicle. Here are some pics: - the car in Unity - the car in Blender - working in the texture - looks great - a wheel - the texture in Gimp - back in Unity testing different materials  like water - and code

Single Page App with C# WPF/XAML

 In this post we are going to create a single page app. The app will have multiple pages that get rendered in the main window. We will be using Visual Studio, C#, WPF and XAML. Let's start by creating a new project in Visual Studio of this type: Next, in the MainWindow, we define the interface structure. On the left side we place a menu and on the right side a DockPanel with a Frame in it. The Frame is the element that is used to render de pages content. Now let's add the new pages. In this example I will add two pages. Click in the Solution Explorer with the mouse right button, then choose Add and Page. The project looks like this. The app content goes on the recently create pages. Because this is just an example I will just change the background color and add a small text. Page1 Page2 Finally the code. Back to the MainWindow we need to create the click events on the menu items. So, in the MenuItem line add the click event and pick New Event Handler. If that option doesn't...

Let's make a car in Unity 3D

In this post we will make a simple car in Unity 3D. The Unity 3D physics engine is used in order to give the car a real behavior. This are the steps: [1] - Create a new Project