# Beginner Intro to Neural Networks 7: Slope of Cost + Simple Train in Python

In the last video we saw three ways of finding the derivative of our cost function

Let's look at those ways using python

Start up the environment

Let's define our cost function,

def cost(b):

return (b - 4) ** 2

Let's find the derivative numerically first:

def num_slope(b):

h = 0.0001

return (cost(b+h) - cost(b))/h

Now let's define it the way we'll ultimately use:

def slope(b):

return 2 * (b - 4)

Finally let's apply our slope to b to minimize the cost

We can start with any b we want here

b = 8

then write our update and run it over and over...

b = b - 0.1 * slope(b)

print(b)

if we run this over and over, b moves to the target!

Instead of running this ourselves, let's put our computer to work using a loop

we can instead write

for i in range(10):

b = b - 0.1 * slope(b)

print(b)

Let's break this down

we use range(x) to generate a list of numbers x numbers long.

the for i in statement iterates through the list, setting i to be equal to each element, then running the line below.

We've solved a super simple neural network! It takes no inputs and outputs a constant value of whatever our target is! If we train long enough we bring the cost to zero and our output to the desired value.

How interesting!!!!!!! Huh? No!?!?!? Well ok, you're right it's not that interesting.

In the next video we'll look at a neural network that takes an actual input and predicts an output!

Stay tuned, thanks for watching!

sortSort By