import conx as cx
import random
import math
def distance(x1, y1, x2, y2):
return math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
negatives = []
while len(negatives) < 500:
x = random.random()
y = random.random()
d = distance(x, y, 0.5, 0.5)
if d > 0.375 and d < 0.5:
negatives.append([x, y])
positives = []
while len(positives) < 500:
x = random.random()
y = random.random()
d = distance(x, y, 0.5, 0.5)
if d < 0.25:
positives.append([x, y])
symbols = {
"Positive": "bo",
"Negative": "ro"
}
cx.scatter([["Positive", positives],
["Negative", negatives]],
symbols=symbols,
height=6.0, width=6.0)
net = cx.Network("Non-Linearly Separable", 2, 5, 1, activation="sigmoid")
net.compile(error="mean_absolute_error", optimizer="adam")
net.picture()
ds = cx.Dataset()
ds.load([(p, [ 1.0], "Positive") for p in positives] +
[(n, [ 0.0], "Negative") for n in negatives])
ds.shuffle()
ds.split(0.1)
net.set_dataset(ds)
net.evaluate(tolerance=0.4)
======================================================== Testing validation dataset with tolerance 0.4... Total count: 900 correct: 454 incorrect: 446 Total percentage correct: 0.5044444444444445
net.dashboard()
Failed to display Jupyter Widget of type Dashboard
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
symbols = {
"Positive (correct)": "w+",
"Positive (wrong)": "k+",
"Negative (correct)": "r_",
"Negative (wrong)": "k_",
}
net.plot_activation_map(scatter=net.evaluate_and_label(), symbols=symbols, title="Before Training")
You may want to either net.reset()
or net.retrain()
if the following cell doesn't complete with 100% accuracy. Calling net.reset()
may be needed if the network has gotten stuck in a local minimum; net.retrain()
may be necessary if the network just needs additional training.
if net.saved():
net.load()
net.plot_results()
else:
net.train(epochs=10000, accuracy=1.0, report_rate=50,
tolerance=0.4, batch_size=128, record=100)
net.save()
cx.scatter(net.evaluate_and_label())
net.plot_activation_map(scatter=net.evaluate_and_label(), symbols=symbols, title="After Training")
states = [net.propagate_to("hidden", input) for input in net.dataset.inputs]
pca = cx.PCA(states)
symbols = {
"Positive (correct)": "b+",
"Positive (wrong)": "k+",
"Negative (correct)": "r_",
"Negative (wrong)": "k_",
}
pb = net.playback(lambda net,epoch: cx.scatter(**pca.transform_network_bank(net, "hidden", test=True),
symbols=symbols,
title="Epoch %s" % epoch))
pb
Failed to display Jupyter Widget of type SequenceViewer
.
If you're reading this message in the Jupyter Notebook or JupyterLab Notebook, it may mean that the widgets JavaScript is still loading. If this message persists, it likely means that the widgets JavaScript library is either not installed or not enabled. See the Jupyter Widgets Documentation for setup instructions.
If you're reading this message in another frontend (for example, a static rendering on GitHub or NBViewer), it may mean that your frontend doesn't currently support widgets.
pb.goto("end")
movie = net.movie(lambda net,epoch: cx.scatter(**pca.transform_network_bank(net, "hidden", test=True),
symbols=symbols,
format="image",
title="Epoch %s" % epoch),
step=1)
movie
def function(x, y):
outputs = net.propagate([x, y])
return outputs[0]
cx.heatmap(function)
cx.scatter(**pca.transform_network_bank(net, "hidden", test=True),
symbols=symbols)
matrix = [[-1 for i in range(50)] for j in range(50)]
for y in cx.frange(0, 1, 0.01):
for x in cx.frange(0, 1, 0.01):
hiddens = net.propagate_to("hidden", [x, y])
vector = pca.transform_one(hiddens, scale=True)
try:
matrix[int(vector[1] * 50)][int(vector[0] * 50)] = net.propagate([x, y])[0]
except:
pass
cx.heatmap(matrix)