forked from brenns10/eecs440
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
W06: Beatiful code and figures for problem 4.
- Loading branch information
Stephen Brennan
committed
Oct 7, 2015
1 parent
dbaafd3
commit 64e2ba3
Showing
5 changed files
with
136 additions
and
2 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
""" | ||
Code to plot decision boundary of ANN. | ||
Stephen Brennan, EECS 440 Written 6, 10/06/2015. | ||
""" | ||
|
||
import numpy as np | ||
import matplotlib.pyplot as plt | ||
plt.style.use('ggplot') | ||
|
||
|
||
def sigmoid(X): | ||
return (1 + np.exp(-X)) ** -1 | ||
|
||
|
||
def output(X, weights, out_weights): | ||
hidden_outs = sigmoid(np.dot(weights, X.T)) | ||
return sigmoid(np.dot(hidden_outs.T, out_weights)) | ||
|
||
|
||
def plot_decision_boundary(wmin, wmax): | ||
hidden_weights = np.random.uniform(wmin, wmax, (2, 2)) | ||
output_weights = np.random.uniform(wmin, wmax, 2) | ||
|
||
x1 = np.arange(-5.0, 5.1, step=0.1) | ||
x2 = np.arange(-5.0, 5.1, step=0.1) | ||
X = np.transpose([np.tile(x1, len(x2)), np.repeat(x2, len(x1))]) | ||
y = output(X, hidden_weights, output_weights) | ||
|
||
fig, ax = plt.subplots() | ||
pos_idx = y > 0.5 | ||
pos = X[pos_idx] | ||
neg = X[~pos_idx] | ||
print(y) | ||
print('%r positives, %r negatives' % (len(pos), len(neg))) | ||
print(pos) | ||
print(neg) | ||
ax.scatter(*pos.T, color='b') | ||
ax.scatter(*neg.T, color='r') | ||
return ax | ||
|
||
|
||
if __name__ == '__main__': | ||
import sys | ||
if len(sys.argv) < 2: | ||
print('Need weight bounds.') | ||
else: | ||
bound = float(sys.argv[1]) | ||
ax = plot_decision_boundary(-bound, bound) | ||
ax.figure.savefig(sys.argv[1] + '.pdf') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters