How it works...

TensorFlow programs generate a computation graph in which the nodes of the graph are called ops. These ops take tensors as input and perform computations and produce tensors (tensors are n-dimensional arrays or lists). TensorFlow programs are structured in two phases: the construction phase and the execution phase. In the construction phase, we assemble the graph, while in the execution phase, we execute the graphs in the context of the session. Calling the tensorflow package in R creates an entry point (the tf object) to the TensorFlow API, through which we can access the main TensorFlow module. The tf$Variable() function creates a variable that holds and updates trainable parameters. TensorFlow variables are in-memory buffers containing tensors.

In step 1, we created some dummy data. In the next step, we created two tf variables for the weights and bias with initial values. In step 3, we defined the model. In step 4, we defined the loss function, as per the following equation:

The reduce_mean() function computes the mean of the elements across the dimensions of a tensor. In our code, it calculates the average loss over the training set. In this step, we also defined the optimization algorithm we need to use to train our network. Here, we used the gradient descent optimizer with a learning rate of 0.5. Then, we defined the objective of each training step; that is, we minimize loss.

In step 5, we assembled the computation graph and provided TensorFlow with a description of the computations that we wanted to execute. In our implementation, we wanted TensorFlow to minimize loss; that is, minimize the mean squared error using the gradient descent algorithm. TensorFlow does not run any computations until the session is created and the run() function is called. We launched the session and added an ops (node) in order to run some tf variable initializers. sessrun(tfrun(tfglobal_variables_initializer()) initializes all the variables simultaneously. We should only run this ops after we have fully constructed the graph and launched it in a session. Finally, in the last step, we executed the training steps in a loop and printed the tf variables (the weight and bias) at each iteration.

It is suggested that you use one of the higher-level APIs (Keras or Estimator) rather than the lower-level core TensorFlow API.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset