How can Tensorflow be used to compare the linear model and the Convolutional model using Python?

PythonServer Side ProgrammingProgramming

Tensorflow is a machine learning framework that is provided by Google. It is an open-source framework used in conjunction with Python to implement algorithms, deep learning applications, and much more. It is used in research and for production purposes.

The ‘tensorflow’ package can be installed on Windows using the below line of code −

pip install tensorflow

Tensor is a data structure used in TensorFlow. It helps connect edges in a flow diagram. This flow diagram is known as the ‘Data flow graph’. Tensors are nothing but a multidimensional array or a list.

They can be identified using three main attributes −

  • Rank − It tells about the dimensionality of the tensor. It can be understood as the order of the tensor or the number of dimensions in the tensor that has been defined.

  • Type − It tells about the data type associated with the elements of the Tensor. It can be a one dimensional, two dimensional or n-dimensional tensor.

  • Shape − It is the number of rows and columns together.

Keras was developed as a part of research for the project ONEIROS (Open ended Neuro-Electronic Intelligent Robot Operating System). Keras is a deep learning API, which is written in Python. It is a high-level API that has a productive interface that helps solve machine learning problems. It runs on top of Tensorflow framework. It was built to help experiment in a quick manner. It provides essential abstractions and building blocks that are essential in developing and encapsulating machine learning solutions.

We are using Google Colaboratory to run the below code. Google Colab or Colaboratory helps run Python code over the browser and requires zero configuration and free access to GPUs (Graphical Processing Units). Colaboratory has been built on top of Jupyter Notebook.

Example

Following is the code snippet:
print(“The two models are compared")
print("Linear model on binary vectorized data:")
print(binary_model.summary())
print("ConvNet model on int vectorized data:")
print(int_model.summary())

Code credit − https://www.tensorflow.org/tutorials/load_data/text

Output

The two models are compared
Linear model on binary vectorized data:
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
dense (Dense) (None, 4) 40004
=================================================================
Total params: 40,004
Trainable params: 40,004
Non-trainable params: 0
_________________________________________________________________
None
ConvNet model on int vectorized data:
Model: "sequential_1"
_________________________________________________________________
Layer (type)             Output Shape          Param #
=================================================================
embedding (Embedding) (None, None, 64)          640064
_________________________________________________________________
conv1d (Conv1D)       (None, None, 64)          20544
_________________________________________________________________
global_max_pooling1d (Global (None, 64)          0
_________________________________________________________________
dense_1 (Dense)       (None, 4)                260
=================================================================
Total params: 660,868
Trainable params: 660,868
Non-trainable params: 0
_________________________________________________________________
None

Explanation

  • The vectorized ‘binary’ model and the vectorized ‘int’ model are compared.

  • This comparison is displayed on the console using the ‘summary’ method.

raja
Published on 19-Jan-2021 07:32:14
Advertisements