The parts of code that look like
pass # ⬅️✏️
need to be filled in before moving to the next cell.
Check out the slides and / or corresponding video lecture for more instructions.
# Define two vectors (lists): input my_x, weights my_w
my_x = [0, 1, 1]
my_w = [-10, 10, 5]
# Multiply two lists element wise
def mul(a, b):
"""
return a list c, same length as a, element c[i] = a[i] * b[i]
"len(list)" and "[0] * n" may be useful
"""
pass # ⬅️✏️
# Test mul() function
mul(my_x, my_w)
# Define a scalar: bias b
my_b = -5
# Neuron linear (internal) potential
def linear_neuron(x, weights, bias):
"""
return a scalar value representing a scalar neuron
"sum(list)" computes the sum of the elements in a list
"""
pass # ⬅️✏️
# Test linear_neuron() function
linear_neuron(my_x, my_w, my_b)
# Import exp() function (safe on lists)
from numpy import exp
# Logistic sigmoid
def sigmoid(z):
"""
return the logistic sigmoid (use exp())
"""
pass # ⬅️✏️
# Try out σ() for different z values
for z in (-5, 0, 5): print(sigmoid(z))
# Import some plotting stuff to graph sigmoid()
from numpy import linspace
from matplotlib.pyplot import plot, axhline, axvline, grid, style
style.use('dark_background')
# Get a pair of axis and grid lines centred in (0, 0)
axhline(c='.2'); axvline(color='.2')
grid(c='.1', ls='--')
# Plot across a span of numbers
z = linspace(-7.5, 7.5)
plot(z, sigmoid(z));
# Full neuron function
def neuron(x, weights, bias):
"""
Return a classical neuron output (reuse code above)
"""
pass # ⬅️✏️
# Test neuron() function
neuron(my_x, my_w, my_b)
# Package OR neuron weights and bias
def or_neuron(x):
"""
Return x1 OR x2 (x1 + x2)
"""
pass # ⬅️✏️
# Defina a collection of inputs to test OR and AND
my_x_collection = [
[0, 0],
[0, 1],
[1, 0],
[1, 1],
]
print('Checking OR neuron output')
for my_x in my_x_collection:
print(my_x, f'{or_neuron(my_x):.3f}')
# Package AND neuron weights and bias
def and_neuron(x):
"""
Return x1 AND x2 (x1 * x2)
"""
pass # ⬅️✏️
print('Checking AND neuron output')
for my_x in my_x_collection:
print(my_x, f'{and_neuron(my_x):.3f}')
# Package NOT neuron weight and bias
def not_neuron(x):
"""
Return NOT x1 (x1_)
"""
pass # ⬅️✏️
print('Checking NOT neuron output')
for my_x in [[0], [1]]:
print(my_x, f'{not_neuron(my_x):.3f}')
# Combine OR, AND, and NOT into a XOR neuron
def xor_neuron(x):
"""
Return x1_ * x2 + x1 * x2_
"""
pass # ⬅️✏️
print('Checking XOR neuron output')
for my_x in my_x_collection:
print(my_x, f'{xor_neuron(my_x):.3f}')
# Combine a XOR and an AND into a half-adder
# ⬅️✏️
# Combine two half-adders and an OR into a (full) adder
# ⬅️✏️