Understand TensorFlow Basic with Python
https://cdn-images-1.medium.com/max/2600/0*7Dr334zRBzim6JOL
Original Source Here
Understand TensorFlow Basic with Python
Basic terminologies used in TensorFlow
Why everyone wants to learn TensorFlow in deep learning, as we deep dive into the machine learning projects, we use the “sklearn” library, and when we talk about deep neural networks, TensorFlow comes into the picture. Anaconda distribution is perfect for data science and machine learning with the pre-installed library packages, but we have to install TensorFlow explicitly because it does not come in anaconda distribution. To download the TensorFlow write the following command in the anaconda prompt and press enter.
#command to install TensorFlow
pip install tensorflow
TensorFlow is a library for deep computational processes in many applications and is widely used in deep neural networks. Before knowing what tensor is, go through with some definitions.
- Scaler: It is a single value with which we can do many computational tasks with tensor rank zero.
- Vector: These are the values comprised either in a row or in columns with a tensor rank one.
- Matrix: it is a formation of vectors in a row and column format with a tensor rank of two.
- Tensor: It is a combination of matrices or vectors.
Both sklearn and TensorFlow us used for machine learning. We know that Google has good algorithms and packages use in machines because of their brain team. They were working on machine learning projects and needed to develop more fast and better programming methods, so they develop TensorFlow for internal use. In 2015 google released TensorFlow as open source. Now this package is used for neural networks, convolutional neural networks, etc. TensorFlow is not only used CPU for computational processes but also use GPU and TPU for more fast and reliability. Sklearn library can also do all machine learning algorithms but is restricted to some limit.
TensorFlow is used for the computational process with data flow graphs. The workflow of the tensors is shown below, as I learned in practice.
Importing the libraries
import tensorflow as tf
import numpy as np
Generation of data
Normally we work on CSV, excel, and other files. But TensorFlow uses file format in tensors. For that, we save data in (.npz) file format. Likely, it is NumPy file type. In this process, we save inputs and target values in tensors format.
np.savez('file_name, arrays)
Here arrays are the input and output values. File_name is a name given to the file we saved in a tensor file format. After executing the code, the file is saved in the ‘.npz’ file format.
TensorFlow objects
We need to specify the size of input and target in the variables. Input and output are the number of columns we used in the data generation.
TensorFlow works on its objects where we use to hold the values.
- Placeholder: It is used to insert the size of the input and target.
input = tf.placeholder(tf.float32,[none, input_size])
target = tf.placeholder(tf.float32,[none, output_size])
These input and target are the values we saved as a ‘.npz’ file format. Float32 is used to flow data with 32bits.
2. Variable: It is used to insert the weights and biases values. Variables are capable of holding values while iterations and placeholders do not. Now we have to solve the equation for the layer with weights multiplication
y = wx + b
y1 = tf.matmul(input, weight) + bais
The matmul is used to multiply the two values in the brackets as dot product in tensors.
Objective function
It is used as a loss function with which we have to optimize our algorithm. For example, the loss function for regression is L-2 norm loss. The L-2 norm loss in this library is mean_squared_loss.
loss = tf.losses.mean_squared_error(labels, predictions)
Optimization
Optimization is used to get the minimum so that the error will be less. We set learning rate values in this process and generally gradient descent optimizer most of the time.
optimization = tf.train.GradientDescentOptimizer(learning rate).loss
Sessions
In TensorFlow, the execution happens in these sessions.
sessions = tf.InteractiveSession()
Initialize
It is used to initialize the session. The variables have been initialized in the session run method.
intializer = ()
sess.run(initializer)
Training Data loading
The data for training comes from the saved file, which we saved as ‘.npz’ file format as tensors.
np.load(file)
Epoch
It is a complete cycle of training of data until execution. Instead of iterations, we will use this term. For that, we can use for loop to get the loss function value from the optimize and loss. Optimize doesn’t give any value but loss gives.
Conclusion
TensorFlow is a very good package dealing with deep neural networks. The length of the code is minimized using TensorFlow.
Reach me on my LinkedIn
Recommended Articles
AI/ML
Trending AI/ML Article Identified & Digested via Granola by Ramsey Elbasheer; a Machine-Driven RSS Bot
via WordPress https://ramseyelbasheer.wordpress.com/2020/12/31/understand-tensorflow-basic-with-python/