File:MachineLearningLinearAssociativeNetwork.webm

Summary

Description
English: Training of a small linear associative network to recognize two targets when presented with a noisy version.
Date
Source https://twitter.com/j_bertolotti/status/1483034358030217219
Author Jacopo Bertolotti
Permission
(Reusing this file)
https://twitter.com/j_bertolotti/status/1030470604418428929

Mathematica 12.0 code

img1 = {
   {0, 1, 1, 1, 0},
   {0, 0, 0, 0, 1},
   {0, 1, 1, 1, 1},
   {1, 0, 0, 0, 1},
   {0, 1, 1, 1, 1}
   };
img2 = {
   {1, 0, 0, 0, 1},
   {0, 1, 1, 1, 0},
   {0, 1, 1, 1, 0},
   {0, 1, 1, 1, 0},
   {1, 0, 0, 0, 1}
   };
v1 = Flatten[img1];
v2 = Flatten[img2];
dim = Dimensions[img1][[1]];
m = Table[0.5, {dim^2}, {dim^2}];
mtmp = m;
gradientdescent[target_, input_] := (
    \[Delta] = {Total[(m . input - target)^2]}; 
    Do[randomconnection = RandomInteger[{1, dim^2}, 2];
    mtmp = m;
    mtmp[[randomconnection[[1]], randomconnection[[2]]]] = 
    mtmp[[randomconnection[[1]], randomconnection[[2]]]] + x;
    m[[randomconnection[[1]], randomconnection[[2]]]] = m[[randomconnection[[1]], randomconnection[[2]]]] - ((D[Total[(mtmp . input - target)^2], x]) /. {x -> 0})/100;
    AppendTo[\[Delta], Total[(m . input - v1)^2]];
    , 100];);
plot[input_, target_] := Grid[{
   {Text["Input"], , Text["Output"]
    }, {
    ArrayPlot@Partition[input, dim],
    Graphics[
     Flatten@Table[{Circle[{0 - 0.4, i}, 0.4], Circle[{15 + 0.4, j}, 0.4], Thickness[Abs[m[[i, j]]]/150], Line[{{0, i}, {15, j}}]}, {i, 1, dim^2}, {j, 1, dim^2}]]
    ,
    ArrayPlot@Partition[m . input, dim]
    }, {
    , , Text["Target"]
    }, {
    , ,
    ArrayPlot@Partition[target, dim]
    }}]
frames = Flatten@Table[
    {Flatten@Table[\[Eta] = RandomReal[{-1, 1}, {dim^2}];
       target = v1; input = v1 + \[Eta];
       Table[
        gradientdescent[target, input];
        plot[input, target]
        , 10], 2]
     ,
     Flatten@Table[\[Eta] = RandomReal[{-1, 1}, {dim^2}];
       target = v2; input = v2 + \[Eta];
       Table[
        gradientdescent[target, input];
        plot[input, target]
        , 10], 2]}, 50];
Dimensions[frames]
ListAnimate[frames]

Licensing

I, the copyright holder of this work, hereby publish it under the following license:
Creative Commons CC-Zero This file is made available under the Creative Commons CC0 1.0 Universal Public Domain Dedication.
The person who associated a work with this deed has dedicated the work to the public domain by waiving all of their rights to the work worldwide under copyright law, including all related and neighboring rights, to the extent allowed by law. You can copy, modify, distribute and perform the work, even for commercial purposes, all without asking permission.

Category:CC-Zero#MachineLearningLinearAssociativeNetwork.webm
Category:Self-published work Category:Uploaded with video2commons Category:Videos of machine learning Category:Images with Mathematica source code
Category:CC-Zero Category:Images with Mathematica source code Category:Pages using deprecated source tags Category:Self-published work Category:Uploaded with video2commons Category:Videos of machine learning