In [1]:
import numpy as np

Step 1: Collect Data


In [22]:
X = np.array([[0,0,1],
            [0,1,1],
            [1,0,1],
            [1,1,1]])
                
Y = np.array([[0],
            [1],
            [1],
            [0]])

In [23]:
X


Out[23]:
array([[0, 0, 1],
       [0, 1, 1],
       [1, 0, 1],
       [1, 1, 1]])

In [24]:
Y


Out[24]:
array([[0],
       [1],
       [1],
       [0]])

Step 2: build model


In [12]:
num_epochs = 60000

#initialize weights
syn0 = 2*np.random.random((3,4)) - 1
syn1 = 2*np.random.random((4,1)) - 1

syn0


Out[12]:
array([[-0.17650874, -0.41777943,  0.2489118 ,  0.69412528],
       [-0.81454024, -0.5201076 , -0.73099017, -0.94322397],
       [-0.42774686,  0.96476856,  0.97950995, -0.88146306]])

In [13]:
syn1


Out[13]:
array([[ 0.23005787],
       [-0.66015815],
       [-0.86986886],
       [-0.3625419 ]])

In [15]:
def nonlin(x,deriv=False):
    if(deriv==True):
        return x*(1-x)
    return 1/(1+np.exp(-x))

Step 3: train model


In [33]:
for epoch in range(num_epochs):
    #feed forward through layers 1, 2 & 3
    l0 = X                        # the input layer
    l1 = nonlin(np.dot(l0, syn0)) # hidden layer
    l2 = nonlin(np.dot(l1, syn1)) # final, hence output layer
    
    # by how much did we miss the target value?
    l2_error = Y - l2
    l2_delta = l2_error * nonlin(l2, deriv=True)
    
    # propgating to the next layer
    l1_error = np.dot(l2_delta, syn1.T)
    l1_delta = l1_error * nonlin(l1, deriv=True)
    
    # updating weights
    syn1 += l1.T.dot(l2_delta)
    syn0 += l0.T.dot(l1_delta)
    
    if (epoch% 10000) == 0:
        print("Error:", (np.mean(np.abs(l2_error))))
        #print(l1_error)
        print('============')


Error: 0.00297082593254
============
Error: 0.00284921240072
============
Error: 0.00274104093076
============
Error: 0.00264400921498
============
Error: 0.00255633125314
============
Error: 0.00247659751167
============

The challenge for this video is to build a neural network to predict the magnitude of an Earthquake given the date, time, Latitude, and Longitude as features. This is the dataset. Optimize at least 1 hyperparameter using Random Search. See this example for more information.

You can use any library you like, bonus points are given if you do this using only numpy.


In [ ]: