Skip to content

Introduction to Deep Learning in Python

Run the hidden code cell below to import the data used in this course.


1 hidden cell

Take Notes

Add notes about the concepts you've learned and code cells with code you want to keep.

Add your notes here

import numpy as np

input_value = np.array([3,5])
weights = {'node_0_0' : np.array([2,4]),
           'node_0_1' : np.array([ 4,-5]),
           'node_1_0' : np.array([-1,2]),
           'node_1_1' : np.array([1,2]),
           'output' : np.array([2,7])}

def relu(input):
    '''Define your relu activation function here'''
    # Calculate the value for the output of the relu function: output
    output = max(0, input)
    
    # Return the value just calculated
    return(output)

# node_0_value = (input_value * weights['node_0']).sum()
# node_1_value = (input_value * weights['node_1']).sum()
# Add your code snippets here
def predict_with_network(input_data):
    # Calculate node 0 in the first hidden layer
    node_0_0_input = (input_data * weights['node_0_0']).sum()
    node_0_0_output = relu(node_0_0_input)

    # Calculate node 1 in the first hidden layer
    node_0_1_input = (input_data * weights['node_0_1']).sum()
    node_0_1_output = relu(node_0_1_input)

    # Put node values into array: hidden_0_outputs
    hidden_0_outputs = np.array([node_0_0_output, node_0_1_output])
    
    # Calculate node 0 in the second hidden layer
    node_1_0_input = (hidden_0_outputs * weights['node_1_0']).sum()
    node_1_0_output = relu(node_1_0_input)

    # Calculate node 1 in the second hidden layer
    node_1_1_input = (hidden_0_outputs * weights['node_1_1']).sum()
    node_1_1_output = relu(node_1_1_input)

    # Put node values into array: hidden_1_outputs
    hidden_1_outputs = np.array([node_1_0_output, node_1_1_output])

    # Calculate model output: model_output
    model_output = (hidden_1_outputs * weights['output']).sum()
    
    # Return model_output
    return(model_output)

output = predict_with_network(input_value)
print(output)
# The data point you will make a prediction for
input_data = np.array([0, 3])
def predict_with_network1(input_data_row, weights):

    # Calculate node 0 value
    node_0_input = (input_data_row * weights['node_0']).sum()
    node_0_output = relu(node_0_input)

    # Calculate node 1 value
    node_1_input = (input_data_row * weights['node_1']).sum()
    node_1_output = relu(node_1_input)

    # Put node values into array: hidden_layer_outputs
    hidden_layer_outputs = np.array([node_0_output, node_1_output])
    
    # Calculate model output
    input_to_final_layer = (hidden_layer_outputs * weights['output']).sum()
    model_output = relu(input_to_final_layer)
    
    # Return model output
    return(model_output)


# Sample weights
weights_0 = {'node_0': [2, 1],
             'node_1': [1, 2],
             'output': [1, 1]
            }

# The actual target value, used to calculate the error
target_actual = 3

# Make prediction using original weights
model_output_0 = predict_with_network1(input_value, weights_0)

# Calculate error: error_0
error_0 = model_output_0 - target_actual

# Create weights that cause the network to make perfect prediction (3): weights_1
weights_1 = {'node_0': [2, 1],
             'node_1': [1, -2],
             'output': [1, 1]
            }

# Make prediction using new weights: model_output_1
model_output_1 = predict_with_network1(input_value, weights_1)

# Calculate error: error_1
error_1 = model_output_1 - target_actual

# Print error_0 and error_1
print(error_0)
print(error_1)
from sklearn.metrics import mean_squared_error


def predict_with_network2(input_data_row, weights):

    # Calculate node 0 value
    node_0_input = sum((input_data_row * weights['node_0']))
    node_0_output = relu(node_0_input)

    # Calculate node 1 value
    node_1_input = sum((input_data_row * weights['node_1']))
    node_1_output = relu(node_1_input)

    # Put node values into array: hidden_layer_outputs
    hidden_layer_outputs = np.array([node_0_output, node_1_output])
    
    # Calculate model output
    input_to_final_layer = sum((hidden_layer_outputs * weights['output']))
    model_output = relu(input_to_final_layer)
    
    # Return model output
    return(model_output)




# Create model_output_0 
model_output_0 = []
# Create model_output_1
model_output_1 = []

target_actuals = [1, 3]
# Loop over input_data
for row in input_data:
    # Append prediction to model_output_0
    model_output_0.append(predict_with_network2(row, weights_0))
    
    # Append prediction to model_output_1
    model_output_1.append(predict_with_network2(row, weights_1))

# Calculate the mean squared error for model_output_0: mse_0
mse_0 = mean_squared_error(target_actuals,model_output_0)

# Calculate the mean squared error for model_output_1: mse_1
mse_1 = mean_squared_error(target_actuals,model_output_1)

# Print mse_0 and mse_1
print("Mean squared error with weights_0: %f" %mse_0)
print("Mean squared error with weights_1: %f" %mse_1)

Slope Calculation in Gradient descent

import numpy as np

weights = np.array([1,2])
input_data = np.array([3,4])
target = 6
learning_rate = 0.01
preds = (input_data*weights).sum()
error = preds-target
print(error)
gradient = 2 * input_data * error
print(gradient)
weights_updated = weights - learning_rate*gradient
preds_updated = (weights_updated*input_data).sum()
error_updated = preds_updated - target
print(error_updated)
import pandas as pd

data = pd.read_csv("datasets/titanic_all_numeric.csv")
data