# 3.24. PCA¶

In [1]:
from conx import *
import random
Using Theano backend.
conx, version 3.5.3

## 3.24.1. Non-Linearly Separable¶

In [2]:
import math
In [3]:
def distance(x1, y1, x2, y2):
return math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)
In [4]:
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])
In [5]:
symbols = {
"Positive": "bo",
"Negative": "ro"
}
In [6]:
scatter([
["Positive", positives],
["Negative", negatives],
],
symbols=symbols,
height=8.0, width=8.0)
In [7]:
net = Network("Non-Linearly Separable", 2, 5, 1, activation="sigmoid")
In [8]:
net
Out[8]:
In [9]:
ds = Dataset()
In [10]:
ds.load([(p, [ 1.0], "Positive") for p in positives] +
[(n, [ 0.0], "Negative") for n in negatives])
In [11]:
ds.shuffle()
In [12]:
ds.split(.1)
In [13]:
net.set_dataset(ds)
In [14]:
net.test(tolerance=0.4)
========================================================
Testing validation dataset with tolerance 0.4...
Total count: 900
correct: 448
incorrect: 452
Total percentage correct: 0.49777777777777776
In [15]:
net.dashboard()
In [16]:
symbols = {
"Positive (correct)": "w+",
"Positive (wrong)": "k+",
"Negative (correct)": "r_",
"Negative (wrong)": "k_",
}
In [17]:
net.plot_activation_map(scatter=net.test(interactive=False), 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 landed in a local maxima; net.retrain() may be necessary if the network just needs additional training.

In [18]:
net.reset()
In [19]:
if net.saved():
net.plot_loss_acc()
else:
net.train(epochs=10000, accuracy=1.0, report_rate=50,
tolerance=0.4, batch_size=len(net.dataset.train_inputs),
plot=True, record=100)
net.save()
In [20]:
net.plot_activation_map(scatter=net.test(interactive=False), symbols=symbols, title="After Training")
In [21]:
states = [net.propagate_to("hidden", input, visualize=False) for input in net.dataset.inputs]
pca = PCA(states)
In [22]:
symbols = {
"Positive (correct)": "b+",
"Positive (wrong)": "k+",
"Negative (correct)": "r_",
"Negative (wrong)": "k_",
}
In [23]:
pb = net.playback(lambda net, epoch: scatter(**pca.transform_network_bank(net, "hidden", test=True),
symbols=symbols,
title="Epoch %s" % epoch, interactive=False))
pb
In [24]:
movie = net.movie(lambda net, epoch: scatter(**pca.transform_network_bank(net, "hidden", test=True),
symbols=symbols,
format="pil",
title="Epoch %s" % epoch, interactive=False), step=1)
movie
Out[24]:
In [25]:
pb.goto("end")
In [26]:
def function(x, y):
outputs = net.propagate([x, y], visualize=False)
return outputs[0]
In [27]:
heatmap(function)
In [28]:
scatter(**pca.transform_network_bank(net, "hidden", test=True),
symbols=symbols)
In [29]:
matrix = [[-1 for i in range(50)] for j in range(50)]
for y in frange(0, 1, .01):
for x in frange(0, 1, .01):
hiddens = net.propagate_to("hidden", [x, y], visualize=False)
vector = pca.transform_one(hiddens, scale=True)
try:
matrix[int(vector[1] * 50)][int(vector[0] * 50)] = net.propagate([x, y], visualize=False)[0]
except:
pass
In [30]:
heatmap(matrix)