Tag: python
-
Tensorflow: training loop from scratch
The deep learning model has model weights which start from a random state wen the model object is created. As we expose the model with the training data, the weights are modified such that their mathematical operations produce values close to the training labels. Following are the major things that happen during training: If we…
-
How to save python objects using joblib
Often times you would want to save python objects for later use. For example, a dataset you constructed which could be used for several projects, or a transformer object with specific parameters you want to apply for different data, or even a machine learning learning model you trained. This is how to do it. First,…
-
Change elements of an array based on a condition using np.where
Let’s say we want to convert multiple categorical variables into binary variables by selecting one category as “0” and the rest as “1”. Or we want to change the values of an array based on a condition, such as in RELU function where all negative values are converted to zero and rest stay the same.…
-
Convert class labels to categories using keras
Class labels can be converted to OneHot encoded array using keras.utils.to_categorical.The resultant array has number of rows equal to the number of samples, and number of columns equal to the number of classes. Let’s take an example of an arrray containing labels.First we need to import numpy to create the labels array and then define the labels array. The labels contain four…
-
Join two or more arrays using `numpy.concatenate`
np.concatenate is used for concatenating numpy arrays.We will discuss here some of the functionalities of this method of numpy arrays.It takes a list of two or more arrays as input argument, and some other keyword arguments, one of which we will cover here. Simplest usage of the concatenate method is as follows: Python Both of the inputs in…
-
Python List
Lists in python are collection of objects. They can include any python objects such as numerals, strings, other lists and dictionaries. The list is defined by square bracket as follows: Python The above example shows an empty list. Following is an example of a list with numbers: Python Lists can also contain strings. Python Single…
-
Loops in Python: For Loop
Loops are used when one wants to repeat a set of statements several times (iterations). For example, when you want to apply a set of operations on elements of a list, by choosing one element at a time, you can use a python loop. There are two types of loops: For loop: For loop is…
-
While loop
Many a times we will encounter a situation where we have to iterate over a group of statements until a specific condition is met. The number iterations that the loop should run is not fixed or known. In such cases, while loop is used. Statements inside a while loop are executed iteratively until a given…
-
Python functions: args and kwargs
Earlier we saw how to write functions in python and how to call them in our program. For those functions, the number of inputs or arguments were defined. They took fixed number of positional or keyword arguments or had a default value assigned to one or more of the arguments. But how to handle situations…
-
Functions in python
Functions are sets of statements that perform a specific task. They can be called by their names, more than once in a program. Inputs can be given to functions based on which they will perform a task and can give back the result. Functions avoid same code to be written over and over again, thus,…
-
Tensorflow: The History object
To train a tensorfow model, we call the fit() method of the model as follows: Python The fit() method returns something called as a History object. It stores the epochwise values of the loss and any metrics we have asked the model to track. These records can be used to visualize how the training process occurred and potentially help us modify…
-
Tensorflow: Basic deep learning workflow for classification task
In our first project of tensorflow, we used the fashion-mnist data to make a simple deep learning model to identify whether any given image is of a trouser or a bag. We labelled the trouser images as class 0, and images of bag as class 1. In our neural network, the last output layer was as follows: Python As…