To run and execute Deep learning models, we can use different libraries such as TensorFlow, Torch, Theano, and Keras in python and other languages
In this article, we will focus on Tensoflow 2.0 library and its implementation in python.
What is TensorFlow?
TensorFlow is an open-source library developed by the Google Brain Team for large numerical computations. It is applied primarily for deep learning and can be used for traditional machine learning too.
This framework was intended to be used internally by Google for their own products but later it became open-sourced and released under the Apache 2.0 open-source license.
TensorFlow is a scalable library that can run on a single CPU or GPU, mobile devices, and large-scale distributed systems of machines.
TensorFlow API is used mainly for python. The TensorFlow python API is the easiest and the most complete one. Other language APIs of Tensorfow are not fully stable yet like:
- Javascript
- C++
- Java
Tensorflow uses C/C++ and data flow graphs.
Big organizations using Tensorflow are Airbnb for object detection, Paypal for fraud detection…
Tensorflow manipulates data in the form of tensors.
What is a tensor?
Tensors are multidimensional arrays with a uniform type. This type of data effectively handles large amounts of data, especially in deep learning.
Difference between Tensorflow 1.0 and 2.0:
Tensorflow 2.0 came in 2019 with new features than TensorFlow 1.0.
The key differences are:
*The new version is simpler
*Many old libraries (for example tf.contrib) were removed, and some were consolidated.
*Adapted Keras for High-level APIs
*Eager execution with graphs
Core Terms of Tensorflow 2.0:
The TensorFlow core concept is data flow graph. This graph contains three main terms: nodes, edges, and operations.
The computational graph is a series of TensorFlow operations arranged into a graph of nodes.
- Nodes refer to computational operations. Each node takes zero or more tensors as inputs and produces a tensor as an output. Each of the graph’s nodes represents an operation.
- Edges refer to tensors. The edges allow data to flow from one node to another in a directed manner.
- Operation: Represents a graph node that performs computation on tensors. An operation is a node in a graph that takes zero or more Tensor objects as input and produces zero or more Tensor objects as output. Examples: number summation, string concatenation, number multiplication, number subtraction, number divination, matrix multiplication, matrix transport,
- session: Enables the execution of a graph or portion of a graph. Important note: sessions have been removed in TensorFlow 2.0, and now eager execution is enabled by default!
Code basics:
Let’s discover some basic code to implement a deep learning model using TensorFlow:
Import TensorFlow:
import tensorflow as tf
Load the dataset:
Data= pd.read_csv('URL')
X=Data['columns']
y=Data['label']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)
Or load a pre-saved dataset from Keras datasets like Mnist:
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
Build a machine learning model: Build a Sequential model by stacking layers:
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10)
])
Train your model:
model.fit(x_train, y_train, epochs=5)
Evaluate your model:
model.evaluate(x_test, y_test, verbose=2)
Conclusion:
In this article, we overview some core concepts of the Tensorflow library and how to implement a basic deep learning model using this framework.
Hey there! I am the creator of AI Decoder.
I am a data scientist by training and a Ph.D. student in AI. In this blog, I try to explain the knowledge I learn in simple words and help someone somewhere.