Skip to content

Instantly share code, notes, and snippets.

@michelkana
Created July 17, 2019 21:55
Show Gist options
  • Save michelkana/e811898bbbe4cc3cfc7dec5eb841f4ca to your computer and use it in GitHub Desktop.
Save michelkana/e811898bbbe4cc3cfc7dec5eb841f4ca to your computer and use it in GitHub Desktop.
# affine transformation of an input x
# using weight w and biais b
def affine(x, w, b):
return w * x + b
# sigmoidal activation
# on output from affine transformation
def sigmoid(z):
return 1.0 / (1.0 + np.exp(-z))
# neuron that takes input x
# applies affine transformation
# and activation
def perceptron(x, w, b):
z = affine(x, w, b) # Affine transformation
h = sigmoid(z) # Sigmoid activation
return h
# first neuron
one_hump_h1 = perceptron(one_hump_df.x, w=14, b=-28.0)
# second neuron
one_hump_h2 = perceptron(one_hump_df.x, w=-8.9, b=54.3)
# third neuron
wout = [1, 1]
bout = -1
one_hump_yout = wout[0] * one_hump_h1 + wout[1] * one_hump_h2 + bout
# plot data vs ANN prediction
def plot_data(x, y, ax, label, title):
SIZE = 10
ax.scatter(x, y, lw=0.5, label=label)
ax.set_xlabel('$x$', fontsize=SIZE)
ax.set_ylabel('$y$', fontsize=SIZE)
ax.tick_params(labelsize=SIZE)
ax.legend(fontsize=SIZE, loc='best')
ax.set_title(title)
fig, ax = plt.subplots(1,1, figsize=(11,5))
plot_data(one_hump_df.x, one_hump_df.y, ax=ax, title='', label='Original Data')
plot_data(one_hump_df.x, one_hump_yout, ax=ax, title='', label='ANN prediction')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment