Reading Time:- 3 min 34 sec

Reading Time:- 3 min 34 sec

#tensorflow #what-is-tensorflow #machine-learning #deep-learningBY TEJAS

TensorFlow is the end-to-end machine learning library for everyone. Anyone can use this openly. This is really the finest machine learning library in the world.

In today's world, we all know the importance of Deep Learning and Machine Learning. And for that, there is a famous library called TensorFlow. TensorFlow is the product of Google. Because it is the product of Google so of-course it is best. And other best part of Tensorflow is it is an open-source machine learning and deep learning library.

What is TensorFlow

TensorFlow is developed by the Google Brain team for fast machine learning and deep
learning.

TensorFlow is run on any devices and it has some wrappers in some languages like Python, Java,
C++,
Swift, and JavaScript. TensorFlow is also used in IOT platforms.

These are some companies that are using TensorFlow (According to Tensorflow Website).

ADVERTISEMENT

TensorFlow's first stable version is released in 2017. As we have seen earlier TensorFlow is an open-source library. It is open-source under Apache open source license.

ADVERTISEMENT

**For Python**

To run TensorFlow in python we have to download TensorFlow's python module using
`pip`

.

For this use this command:

` `` pip install tensorflow`

- A Begineer's Guide to Data Visualization in Python
- Top Reasons Why Python is so Popular in programming Languages ?

**For JavaScript**

The best thing about TensorFlow is it run in javascript using the CDN link. This is quite interesting. To run TensorFlow in javascript we have to use the CDN link of TensorFlow.

` ````
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]/dist/tf.min.js"></script>
```

You can install TensorFlow on different platforms please see TensorFlow's installation guide for how to install TensorFlow in different devices and frameworks?.

ADVERTISEMENT

So basically, TensorFlow works in three different processes.

- Processing the data
- Building the model
- Training and testing the model

Tensorflow takes the multi-dimensional arrays and process that arrays. Because it works on
arrays
and processes that array the name is TensorFlow. "Tensor" means arrays.

When we give some input into arrays it starts processing that array at one end and gives output
on
the second end.

ADVERTISEMENT

We are creating some simple programs of TensorFlow which also can be called the Hello-World
program.
The Programs we are covering in this article that are very basic. We see 2 programs one is for
Python and the other is for javascript.

I used `Google Colab`

for executing the example of Python.

**For Python**

First, we import TensorFlow using

` ````
import tensorflow as tf
```

Load the MNIST dataset in the program using

` ````
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
```

Convert the sample data from integer to floating-point number by dividing it with 255.0

` ````
x_train, x_test = x_train / 255.0, x_test / 255.0
```

Build the model by stacking layers and choose the optimizer and loss function for training.

` ````
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)
])
```

for each example model returns a vector

` ````
predictions = model(x_train[:1]).numpy()
predictions
```

Predictions

tf.nn.softmax() function converts vector into probabilities.

` ````
tf.nn.softmax(predictions).numpy()
```

tf.nn.softmax()

tf.keras.losses.SparseCategoricalCrossentropy() returns loss for each example.

` ````
loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
```

model.compile() function compiles the model

` ````
model.compile(optimizer='adam',
loss=loss_fn,
metrics=['accuracy'])
```

model.fit() adjust the parameters to minimize the loss

` ````
model.fit(x_train, y_train, epochs=5)
```

model.fit()

model.evaluate() function checks the performance of the model

` ````
model.evaluate(x_test, y_test, verbose=2)
```

model.evaluate()

Now time is to see the full program of TensorFlow in python is

` ````
# importing tensorflow
import tensorflow as tf
# load mnist dataset
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# convert integers into float
x_train, x_test = x_train / 255.0, x_test / 255.0
# building the model
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)
])
# return a vector
predictions = model(x_train[:1]).numpy()
predictions
# converting into probabilities
tf.nn.softmax(predictions).numpy()
# returns loss
loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
# compiling the model
model.compile(optimizer='adam',
loss=loss_fn,
metrics=['accuracy'])
# fitting model
model.fit(x_train, y_train, epochs=5)
# check models performance
model.evaluate(x_test, y_test, verbose=2)
```

In this model we get a loss is nearly about ~7% and accuracy is about ~97%

ADVERTISEMENT

**For JavaScript**

For JavaScript, there are some different functions in TensorFlow. The output of this program is
shown in the console of the browser and also in the `<H1>`

tag.

First, we are adding the CDN link of TensorFlow in the `<head>`

tag.
After the addition of the link now we have created an `array`

and inserted fifteen
random
values
into the array and declared it into a `values`

variable.

Then we have created a second array in which
there are two values in that array `[5, 3]`

and declared it into a `shape`

variable.

And after that, we create a `data`

variable and in that variable, we pass the
`tf.tensor(values, shapes)`

function. and then in last, we use
`console.log()`

for printing the output into the console.

And `data.toString()`

function converts data into a string and displays the string
values
in the console.

And by using `document.getElementById("demo").innerHTML = data.toString();`

we show
output in `<H1>`

> tag (It is just a basic HTML and JavaScript).

This is console output of program.

Console Output

And this is output of program by using `<H1>`

tag.

Page Output

So this is full HTML and JavaScript code for TensorFlow in JavaScript example.

` ````
<!DOCTYPE html>
<head>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/[email protected]/dist/tf.min.js"></script>
</head>
<body>
<h1 id="demo"></h1>
<script>
const values = [10.5, 6.5, 55.5, 96.96, 36.25, 12.25, 87.00, 35.65, 33.96, 25.14, 8.00, 9.66, 22.12, 66.20, 45.33];
const shape = [5, 3];
const data = tf.tensor(values, shape);
console.log(data.toString());
document.getElementById("demo").innerHTML = data.toString();
</script>
</body>
</html>
```

The following videos will inspire you to learn artificial intelligence. These videos are on TensorFlow's official YouTube channel.

Videos by TensorFlow

ADVERTISEMENT

**Conclusion:-**

We have covered in this article "What is TensorFlow?", "History of TensorFlow", "How to run
Tensorflow in Python and Javascript?", "How TensorFlow works?", "A simple programs in TensorFlow
(For Python and JavaScript)".

If You like this article, please comment and share.
also, you have any doubts about TensorFlow then please comment below.

Thank you.

ADVERTISEMENT

- A 3 Step guide to download and install Node.Js
- Inheritance In Java: A Simple But Complete Guide
- Constructor In Java: A Simple But Complete Guide
- A Begineer's Guide to Data Visualization in Python
- What is TensorFlow? A Beginner's Guide to Know about it.
- Top Reasons Why Python is so Popular in programming Languages ?

- A 3 Step guide to download and install Node.Js
- Constructor In Java: A Simple But Complete Guide
- A Begineer's Guide to Data Visualization in Python
- What is TensorFlow? A Beginner's Guide to Know about it.
- Read (*.txt) file using C Program
- Top Reasons Why Python is so Popular in programming Languages ?