3.2. XOR Multiple Inputs/Targets¶

Testing…

In [1]:
from conx import Network, Layer, SGD, PCA, scatter
Using TensorFlow backend.
/usr/lib/python3.6/importlib/_bootstrap.py:219: RuntimeWarning: compiletime version 3.5 of module 'tensorflow.python.framework.fast_tensor_util' does not match runtime version 3.6
  return f(*args, **kwds)
conx, version 3.5.15
In [2]:
net = Network("XOR Network", 2, 4, 1, activation="sigmoid")
In [3]:
net.compile(loss='mean_squared_error',
            optimizer=SGD(lr=0.3, momentum=0.9))
In [4]:
dataset = [
    ([0, 0], [0], "0"),
    ([0, 1], [1], "1"),
    ([1, 0], [1], "1"),
    ([1, 1], [0], "0")
]
#net["output"].minmax = (0, 1)
In [5]:
net.dataset.clear()
net.dataset.load(dataset)
net.dataset.summary()

Dataset Split: * training : 4 * testing : 0 * total : 4

Input Summary: * shape : [(2,)] * range : [(0.0, 1.0)]

Target Summary: * shape : [(1,)] * range : [(0.0, 1.0)]

In [6]:
net.dataset.targets[0]
Out[6]:
[0.0]
In [7]:
net.dataset.inputs.shape
Out[7]:
[(2,)]
In [8]:
widget = net.dashboard()
widget
In [10]:
net.propagate([0, 1], visualize=True)
Out[10]:
[0.6359257102012634]
In [11]:
net.reset()
net.train(epochs=2000, accuracy=1.0, report_rate=25, record=True, plot=True)
_images/XOR_10_0.svg
========================================================================
       |  Training |  Training
Epochs |     Error |  Accuracy
------ | --------- | ---------
#  381 |   0.00815 |   1.00000
In [12]:
net.test()
========================================================
Testing validation dataset with tolerance 0.1...
Total count: 4
      correct: 4
      incorrect: 0
Total percentage correct: 1.0
In [13]:
net.playback(lambda net, epoch: (
    net.plot_activation_map(interactive=False,
                            scatter=[
                                ["0", [net.dataset.inputs[i] for i in range(len(net.dataset.inputs)) if net.dataset.labels[i] == "0"]],
                                ["1", [net.dataset.inputs[i] for i in range(len(net.dataset.inputs)) if net.dataset.labels[i] == "1"]]],
                            symbols={"0": "ko", "1": "k+"},
                            title="Epoch %s" % epoch),
    net.plot('all', end=epoch, interactive=False)))
WARNING: No acc data available for the specified epochs (0-0)
WARNING: No loss data available for the specified epochs (0-0)
_images/XOR_12_2.svg
_images/XOR_12_3.svg
In [15]:
states = [net.propagate_to("hidden", input) for input in net.dataset.inputs]
pca = PCA(states)
In [16]:
symbols = {
    "0 (correct)": "bo",
    "0 (wrong)": "bx",
    "1 (correct)": "ro",
    "1 (wrong)": "rx",
}
net.playback(lambda net, epoch: scatter(**pca.transform_network_bank(net, "hidden"),
                                        symbols=symbols,
                                        interactive=False))
_images/XOR_14_1.svg
In [17]:
net.propagate_to("input", [0, 1])
Out[17]:
[0.0, 1.0]
In [18]:
net.propagate([0.5, 0.5])
Out[18]:
[0.5085740685462952]
In [19]:
net.propagate_to("hidden", [1, 0])
Out[19]:
[0.38418060541152954,
 0.572746753692627,
 0.43381455540657043,
 0.6605902314186096]
In [20]:
net.propagate_to("output", [1, 1])
Out[20]:
[0.5351778268814087]
In [21]:
net.propagate_to("input", [0.25, 0.25])
Out[21]:
[0.25, 0.25]
In [22]:
net.propagate_from("input", [1.0, 1.0])
Out[22]:
[0.5351778]
In [23]:
net.propagate_from("hidden", [1.0, 0.0, 1.0, -1.0])
Out[23]:
[0.35672686]
In [24]:
net.test()
========================================================
Testing validation dataset with tolerance 0.1...
Total count: 4
      correct: 0
      incorrect: 4
Total percentage correct: 0.0
In [25]:
from conx import Network, Layer, SGD
In [26]:
net = Network("XOR2 Network")
net.add(Layer("input1", 1))
net.add(Layer("input2", 1))
net.add(Layer("hidden1", 10, activation="sigmoid"))
net.add(Layer("hidden2", 10, activation="sigmoid"))
net.add(Layer("shared-hidden", 5, activation="sigmoid"))
net.add(Layer("output1", 1, activation="sigmoid")) # , minmax=(-1,1)))
net.add(Layer("output2", 1, activation="sigmoid")) # , minmax=(-1,1)))
Out[26]:
'output2'
In [27]:
net
Out[27]:
<Network name='XOR2 Network' (uncompiled)>
In [28]:
net.connect("input1", "hidden1")
net.connect("input2", "hidden2")
net.connect("hidden1", "shared-hidden")
net.connect("hidden2", "shared-hidden")
net.connect("shared-hidden", "output1")
net.connect("shared-hidden", "output2")
In [29]:
net.layers[2].incoming_connections
Out[29]:
[<Layer name='input1', shape=(1,), act='None'>]
In [30]:
net.compile(loss='mean_squared_error',
            optimizer=SGD(lr=0.3, momentum=0.9))
In [31]:
net.config["hspace"] = 200
widget = net.dashboard()
widget
In [32]:
net.propagate_to("hidden1", [[1], [1]])
Out[32]:
[0.5891909003257751,
 0.6736063361167908,
 0.5231307148933411,
 0.41695597767829895,
 0.3708018958568573,
 0.49578002095222473,
 0.33628129959106445,
 0.5863265991210938,
 0.5555092692375183,
 0.49335378408432007]
In [33]:
net.propagate([[1], [1]])
Out[33]:
[[0.6283185482025146], [0.2138163298368454]]
In [34]:
dataset = [
    ([[0],[0]], [[0],[0]]),
    ([[0],[1]], [[1],[1]]),
    ([[1],[0]], [[1],[1]]),
    ([[1],[1]], [[0],[0]])
]
In [35]:
net.dataset.load(dataset)
In [36]:
net.get_weights("hidden2")
Out[36]:
[[[-0.17665445804595947,
   0.6080862879753113,
   -0.3233792781829834,
   -0.6478244066238403,
   -0.7108033895492554,
   0.043866515159606934,
   0.20450246334075928,
   -0.4206157326698303,
   -0.1701200008392334,
   -0.6520969271659851]],
 [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]]
In [37]:
net.propagate([[1], [1]])
Out[37]:
[[0.6283185482025146], [0.2138163298368454]]
In [38]:
widget = net.dashboard()
widget
In [39]:
import time
net.reset()
for i in range(20):
    (epoch_count, results) = net.train(epochs=100, verbose=0, report_rate=25, plot=True)
    for index in range(4):
        net.propagate(dataset[index][0])
        time.sleep(.1)
_images/XOR_37_0.svg
In [40]:
net.reset()
In [41]:
net.train(epochs=2000, accuracy=1.0, report_rate=25, plot=True)
_images/XOR_39_0.svg
========================================================================
       |  Training |   output1 |   output2
Epochs |     Error |       acc |       acc
------ | --------- | --------- | ---------
#  965 |   0.01142 |   1.00000 |   1.00000
In [42]:
net.propagate_from("shared-hidden", [0.0] * 5)
Out[42]:
[[0.7263107], [0.8060964]]
In [43]:
net.propagate_to("hidden1", [[1], [1]])
Out[43]:
[0.7431058287620544,
 0.8438336849212646,
 0.1248035803437233,
 0.14310242235660553,
 0.7969014048576355,
 0.6870737075805664,
 0.14280001819133759,
 0.7915634512901306,
 0.27689337730407715,
 0.8801047205924988]
In [44]:
net.test()
========================================================
Testing validation dataset with tolerance 0.1...
Total count: 4
      correct: 4
      incorrect: 0
Total percentage correct: 1.0
In [45]:
net.dataset.slice(2)
In [46]:
net.train(epochs=2000, accuracy=1.0, report_rate=25)
No training required: accuracy already to desired value
Training dataset status:
       |  Training |   output1 |   output2
Epochs |     Error |       acc |       acc
------ | --------- | --------- | ---------
#  965 |   0.01142 |   1.00000 |   1.00000
In [47]:
net.plot('all')
_images/XOR_45_0.png