Machine Learning and Deep Learning for Fullstack Dev

Technocrat
CoderHack.com
Published in
4 min readSep 14, 2023
Photo by Hitesh Choudhary on Unsplash

Machine learning is a field of artificial intelligence that uses statistical techniques to give computer systems the ability to “learn” with data, without being explicitly programmed. Deep learning is a type of machine learning that uses neural networks, which are algorithms that are inspired by the human brain.

Some common use cases of machine learning are:

•Image Recognition: Identifying objects, scenes, people, etc. in images. Used for face recognition, object detection, etc.

•Natural Language Processing: Understanding, interpreting and generating human language. Used for machine translation, sentiment analysis, speech recognition, etc.

•Forecasting: Predicting future events based on past data. Used for stock market prediction, sales forecasting, etc.

•And many more! Machine learning has a wide range of applications.

Getting started with TensorFlow and Keras

TensorFlow is one of the most popular open source machine learning frameworks developed by Google. Keras is a high-level neural network API written in Python and capable of running on top of TensorFlow. Keras makes it easy to build neural networks and complex machine learning models.

To install TensorFlow and Keras:

pip install tensorflow keras

The core data structure of Keras is a model, which is a directed acyclic graph of layers. Layers are nodes in the graph that take in tensors, process them and output new tensors. Let’s look at a simple example of a Keras model:

from keras.models import Sequential
from keras.layers import Dense

model = Sequential()
model.add(Dense(32, input_shape=(784,))) # 784 input nodes, 32 hidden nodes
model.add(Dense(10, activation='softmax')) # 10 output nodes with softmax

This defines a basic neural network with an input layer, one hidden layer and an output layer. The model API is simple and intuitive to allow you to rapidly experiment with different model architectures.

Building a Simple Model

Let’s build our first machine learning model using Keras to recognize handwritten digits. We will use the MNIST dataset which contains 60,000 examples of handwritten digits 0–9.

First we load and preprocess the data:

from keras.datasets import mnist

(x_train, y_train), (x_test, y_test) = mnist.load_data()

x_train = x_train.astype('float32') / 255.
x_test = x_test.astype('float32') / 255.

Next we define a simple model with 2 dense layers:

model = Sequential()
model.add(Dense(512, input_shape=(784,)))
model.add(Dense(10, activation='softmax'))

We compile the model with the categorical cross entropy loss function and Adam optimizer.

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

Then we train the model by fitting the training data.

model.fit(x_train, y_train, validation_data=(x_test, y_test), epochs=10, batch_size=128)

Finally we evaluate the accuracy of our trained model on the test set:

test_loss, test_acc = model.evaluate(x_test, y_test)
print('Test accuracy:', test_acc) # Test accuracy: 0.9774000144004822

Our model achieves 97.74% accuracy on recognizing handwritten digits! We have successfully built and trained our first machine learning model using TensorFlow and Keras.

Improving the Model

Some ways to improve our model are:

•Add dropout regularization to prevent overfitting. We can add dropout layers in between dense layers. For example:

model.add(Dense(512, input_shape=(784,)))  
model.add(Dropout(0.2))
model.add(Dense(10, activation='softmax'))

•Use more advanced activation functions like ReLU instead of the default sigmoid. ReLU is faster and helps prevent vanishing gradient problem. We can specify activation like this:

model.add(Dense(512, activation='relu', input_shape=(784,)))

•For image data, use convolutional neural networks (CNNs) which are very effective. We can add CNN layers like this:

model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
model.add(MaxPooling2D((2, 2)))
model.add(Conv2D(64, (3, 3), activation='relu'))

•Tune hyperparameters like learning rate, number of layers/nodes, batch size, epochs, etc. This can be done manually or using tools like Keras Tuner.

•Use data augmentation techniques like image scaling, rotation, flip, crop, etc. to generate more data and prevent overfitting.

•And many more! There are endless possibilities to build better machine learning models. With TensorFlow and Keras you have the flexibility and power to create highly accurate and complex models.

Saving and Deploying the Model

To save a Keras model, simply call the .save() method:

model.save('handwriting_model.h5')

You can reload the model from file like this:

new_model = keras.models.load_model('handwriting_model.h5')

To make predictions on new data with the reloaded model:

predictions = new_model.predict(x_test)

Some ways to deploy a Keras model are:

•Export to TensorFlow JS — Run in web browser with JavaScript •Convert to TensorFlow Lite — Run on mobile/embedded devices •Serve with TensorFlow Serving — Expose as API endpoint for production •And more… There are many options to deploy machine learning models built with TensorFlow and Keras!

Conclusion

In this article, we discussed the basics of machine learning and deep learning. We built a simple neural network model using TensorFlow and Keras to recognize handwritten digits. We explored various ways to improve and optimize the model by techniques like regularization, tuning hyperparameters, data augmentation, etc. Finally we looked at options to save and deploy machine learning models for production use.

Keras and TensorFlow provide a powerful environment to get started with deep learning. With Keras it is easy to build models, but TensorFlow also gives you full control to design complex neural network architectures if needed. I hope you found this article helpful as an introduction to machine learning and building models using TensorFlow and Keras! Let me know if you have any other questions.

--

--