3.2. XOR Multiple Inputs/Targets¶

Testing…

In [1]:
from conx import Network, Layer, SGD, PCA, scatter
Using Theano backend.
conx, version 3.5.3
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()
Input Summary:
   count  : 4 (4 for training, 0 for testing)
   shape  : [(2,)]
   range  : (0.0, 1.0)
Target Summary:
   count  : 4 (4 for training, 0 for testing)
   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 [9]:
net.propagate([0, 0])
Out[9]:
[0.7442447543144226]
In [14]:
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
------ | --------- | ---------
#  415 |   0.00891 |   1.00000
In [15]:
net.test()
========================================================
Testing validation dataset with tolerance 0.1...
Total count: 4
      correct: 4
      incorrect: 0
Total percentage correct: 1.0
In [16]:
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
None
In [17]:
states = [net.propagate_to("hidden", input) for input in net.dataset.inputs]
pca = PCA(states)
In [18]:
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 [19]:
net.propagate_to("input", [0, 1])
Out[19]:
[0.0, 1.0]
In [20]:
net.propagate([0.5, 0.5])
Out[20]:
[0.5670818090438843]
In [21]:
net.propagate_to("hidden", [1, 0])
Out[21]:
[0.7143576741218567,
 0.38777992129325867,
 0.3105054795742035,
 0.31123819947242737]
In [22]:
net.propagate_to("output", [1, 1])
Out[22]:
[0.5683605074882507]
In [23]:
net.propagate_to("input", [0.25, 0.25])
Out[23]:
[0.25, 0.25]
In [24]:
net.propagate_from("input", [1.0, 1.0])
Out[24]:
[0.5683605]
In [25]:
net.propagate_from("hidden", [1.0, 0.0, 1.0, -1.0])
Out[25]:
[0.536074]
In [26]:
net.test()
========================================================
Testing validation dataset with tolerance 0.1...
Total count: 4
      correct: 0
      incorrect: 4
Total percentage correct: 0.0
In [27]:
from conx import Network, Layer, SGD
In [28]:
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)))
In [29]:
net
Out[29]:
<Network name='XOR2 Network' (uncompiled)>
In [30]:
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 [31]:
net.layers[2].incoming_connections
Out[31]:
[<Layer name='input1', shape=(1,), act='None'>]
In [32]:
net.compile(loss='mean_squared_error',
            optimizer=SGD(lr=0.3, momentum=0.9))
In [33]:
net.config["hspace"] = 200
widget = net.dashboard()
widget
In [34]:
net.propagate_to("hidden1", [[1], [1]])
Out[34]:
[0.5083116888999939,
 0.48068660497665405,
 0.3853483200073242,
 0.42819494009017944,
 0.5566208362579346,
 0.38645774126052856,
 0.40120041370391846,
 0.6385073065757751,
 0.6055800914764404,
 0.3469994068145752]
In [35]:
net.propagate([[1], [1]])
Out[35]:
[[0.5314915180206299], [0.38976094126701355]]
In [36]:
dataset = [
    ([[0],[0]], [[0],[0]]),
    ([[0],[1]], [[1],[1]]),
    ([[1],[0]], [[1],[1]]),
    ([[1],[1]], [[0],[0]])
]
In [37]:
net.dataset.load(dataset)
In [38]:
net.get_weights("hidden2")
Out[38]:
[[[-0.21534188091754913,
   0.5022379755973816,
   -0.3664602041244507,
   0.36815139651298523,
   -0.6458175778388977,
   -0.6277430057525635,
   -0.5149113535881042,
   -0.2998758852481842,
   -0.3776269555091858,
   0.2083292156457901]],
 [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]]
In [39]:
net.propagate([[1], [1]])
Out[39]:
[[0.5314915180206299], [0.38976094126701355]]
In [40]:
widget = net.dashboard()
widget
In [41]:
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 [49]:
net.reset()
In [42]:
net.train(epochs=2000, accuracy=1.0, report_rate=25, plot=True)
No training required: accuracy already to desired value
Training dataset status:
       |  Training |   output1 |   output2
Epochs |     Error |       acc |       acc
------ | --------- | --------- | ---------
# 2000 |   0.00123 |   1.00000 |   1.00000
In [43]:
net.propagate_from("shared-hidden", [0.0] * 5)
Out[43]:
[[0.8763624], [0.8590942]]
In [44]:
net.propagate_to("hidden1", [[1], [1]])
Out[44]:
[0.22696572542190552,
 0.9065141081809998,
 0.624834418296814,
 0.8558169007301331,
 0.18792618811130524,
 0.09951143711805344,
 0.26284801959991455,
 0.1613537222146988,
 0.15783710777759552,
 0.21902421116828918]
In [45]:
net.test()
========================================================
Testing validation dataset with tolerance 0.1...
Total count: 4
      correct: 4
      incorrect: 0
Total percentage correct: 1.0
In [46]:
net.dataset.slice(2)
In [47]:
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
------ | --------- | --------- | ---------
# 2000 |   0.00123 |   1.00000 |   1.00000
In [48]:
net.plot('all')
_images/XOR_45_0.png