# 3.24. PCA¶

[31]:

import conx as cx
import random


## 3.24.1. Non-Linearly Separable¶

[2]:

import math

[3]:

def distance(x1, y1, x2, y2):
return math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2)

[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])

[5]:

symbols = {
"Positive": "bo",
"Negative": "ro"
}

[6]:

cx.scatter([["Positive", positives],
["Negative", negatives]],
symbols=symbols,
height=6.0, width=6.0)

[6]:

[7]:

net = cx.Network("Non-Linearly Separable", 2, 5, 1, activation="sigmoid")

[8]:

net.picture()

[8]:

[9]:

ds = cx.Dataset()

[10]:

ds.load([(p, [ 1.0], "Positive") for p in positives] +
[(n, [ 0.0], "Negative") for n in negatives])

[11]:

ds.shuffle()

[12]:

ds.split(0.1)

[13]:

net.set_dataset(ds)

[14]:

net.evaluate(tolerance=0.4)

========================================================
Testing validation dataset with tolerance 0.4...
Total count: 900
correct: 454
incorrect: 446
Total percentage correct: 0.5044444444444445

[15]:

net.dashboard()

[16]:

symbols = {
"Positive (correct)": "w+",
"Positive (wrong)": "k+",
"Negative (correct)": "r_",
"Negative (wrong)": "k_",
}

[17]:

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.

[18]:

if net.saved():
net.plot_results()
else:
net.train(epochs=10000, accuracy=1.0, report_rate=50,
tolerance=0.4, batch_size=128, record=100)
net.save()

[19]:

cx.scatter(net.evaluate_and_label())

[19]:

[20]:

net.plot_activation_map(scatter=net.evaluate_and_label(), symbols=symbols, title="After Training")

[21]:

states = [net.propagate_to("hidden", input) for input in net.dataset.inputs]
pca = cx.PCA(states)

[22]:

symbols = {
"Positive (correct)": "b+",
"Positive (wrong)": "k+",
"Negative (correct)": "r_",
"Negative (wrong)": "k_",
}

[23]:

pb = net.playback(lambda net,epoch: cx.scatter(**pca.transform_network_bank(net, "hidden", test=True),
symbols=symbols,
title="Epoch %s" % epoch))
pb

[24]:

pb.goto("end")

[25]:

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

[25]:

[26]:

def function(x, y):
outputs = net.propagate([x, y])
return outputs[0]

[27]:

cx.heatmap(function)

[28]:

cx.scatter(**pca.transform_network_bank(net, "hidden", test=True),
symbols=symbols)

[28]:

[29]:

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

[30]:

cx.heatmap(matrix)

[ ]: