# First step towards deep learning

1. Background & Motivation
2. Building a neural network from scratch
3. Summary & Conclusions
1. Background & Motivation :
`# The activation/sigmoid function def sigma (x):    return 1/ (1.0 + np.exp(-x))# Gradients of sigmoid def grad_sigma(w, b, x):    s = sigma ( x.dot (w) + b)    grad_w = — s * (1.0 — s) * x    grad_b = — s * (1.0 — s)    return grad_w, grad_b# Mean squared  loss functiondef loss (y_true, y_pred):    return np.sum ( (y_true -y_pred) ** 2) / y_true.shape[0]# Gradients of loss function def grad_loss (w, b, x, y, y_pred):    grad_w, grad_b = grad_sigma (w, b, x)    grad_w = — (y-y_pred) * grad_w    grad_b = — (y-y_pred) * grad_b    return grad_w, grad_b`
`class Dense:    def __init__(self, dim_in, dim_out):        self.W = np.random.random([dim_in, dim_out])        self.b = np.random.random([dim_out])     def out(self, x):        return sigma(x.dot (self.W) + self.b)`
`def input_func (X):   return np.sqrt (np.mean(X))def get_data (n):   # n = number of data points    # dim_input = dimensions of the feature vector.   X = np.random.random([n, dim_input])   y = [input_func (X[i,:]) for i in range(0, X.shape[0])]   return X, y`
`class Network:    def __init__(self, dim_in, dim_out):    self.L = Dense (dim_in,dim_out)    def fit(self, X, y, niter, learning_rate, val_split):             x_train, y_train, x_val, y_val =\        train_test_split(val_split, X, y)         tr_loss = []        vl_loss = []        for i in range (0, args.niter):            # This loop is for training data           y_train_pred = np.zeros (y_train.shape[0])           y_val_pred = np.zeros (y_val.shape[0])           for j in range(0, x_train.shape[0]):               # predict y               y_train_pred[j] = self.L.out(x_train[j,:])               # get the gradients               grad_w, grad_b = grad_loss (self.L.W, self.L.b,\               x_train[j,:], y[j], y_train_pred[j])               grad_w = grad_w.reshape([grad_w.shape[0],1])               # update the weights               self.L.W = self.L.W + args.learning_rate * grad_w               self.L.b = self.L.b + args.learning_rate * grad_b          # get the loss for training data          train_loss = loss (y_train, y_train_pred)          tr_loss.append (train_loss)          # This loop is for validation data          for j in range (0, x_val.shape[0]):              y_val_pred[j] = self.L.out(x_val[j,:])              val_loss = loss (y_val, y_val_pred)           vl_loss.append (val_loss)    plt.plot(tr_loss, label=”Train data”)    plt.plot(vl_loss, label=”Validation data”)    plt.ylabel(“Loss”)    plt.xlabel(“Iterations”)    plt.legend()    plt.show()`

--

--

## More from Jayanti prasad Ph.D

Physicist, Data Scientist and Blogger.

Love podcasts or audiobooks? Learn on the go with our new app.