-
Notifications
You must be signed in to change notification settings - Fork 0
/
AssignSymmetricWeights.m
137 lines (130 loc) · 4.96 KB
/
AssignSymmetricWeights.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
function [wMatrix, realwMatrix] = AssignSparseSymmetricWeights(neurons,number_of_connections)
%number of connections is per neuron
wMatrix = zeros(number_of_connections, neurons);
for postsynaptic = 1:neurons
connections = zeros(neurons,1);
%Pretend that we’re already connected to ourselves, to
%prevent it from happening!
connections(postsynaptic, 1) = 1;
%Find all the connections that were made to this neuron
%from previous neurons
syn_count = 0;
for i = 1:number_of_connections
if wMatrix(i,postsynaptic) == 0
break;
end
syn_count = i;
connections(wMatrix(syn_count,postsynaptic), 1) = 1;
end
%Now attempt to assign connections from remaining
%neurons (previous neurons are already full)
attempts = 0;
remainingneurons = neurons - postsynaptic;
while (syn_count < number_of_connections)
presynaptic = postsynaptic + ceil(remainingneurons * rand);
attempts = attempts + 1;
if (attempts > neurons)
%Give up if we’ve tried too many times
break;
end
%If there is not already a connection to this neuron...
if connections(presynaptic,1) == 0
nextpresyn = NextSynapse(wMatrix, presynaptic, number_of_connections);
%If this neuron still has a connection
%available...
if nextpresyn > 0
%Assign a connection from here to there
%and from there to here
connections(presynaptic, 1)=1;
syn_count = syn_count + 1;
wMatrix(syn_count,postsynaptic) = presynaptic;
wMatrix(nextpresyn,presynaptic) = postsynaptic;
end
end
end
%If we enter the next loop, there were no available
%connections to be found from the remaining neurons.
%Time to be creative!
while syn_count < number_of_connections
%We only need one more connection (therefore, we
%can not be the last neuron!)
if (syn_count == number_of_connections - 1)
%Find a neuron that is after the current one
sacrificer = postsynaptic + ceil(remainingneurons * rand);
lastsacsyn = NextSynapse(wMatrix,sacrificer, number_of_connections) - 1;
if (lastsacsyn == -1)
lastsacsyn = number_of_connections;
end
%Find a connection that is not shared by the
%current neuron
for synapse = 1:number_of_connections
if (connections(wMatrix(synapse, sacrificer), 1) == 0)
sacrificerpre = wMatrix(synapse,sacrificer);
if synapse < lastsacsyn
wMatrix(synapse, sacrificer) = wMatrix(lastsacsyn, sacrificer);
end
%Cut off the synapse (it’ll grow back)
wMatrix(lastsacsyn, sacrificer) = 0;
break;
end
end
%Graft that synapse onto the current neuron
connections(sacrificerpre, 1) = 1;
syn_count = syn_count + 1;
wMatrix(syn_count,postsynaptic) = sacrificerpre;
%Fix the symmetric connection to the sacrificer
%and replace it
for findsac=1:number_of_connections
if wMatrix(findsac, sacrificerpre) == sacrificer
wMatrix(findsac, sacrificerpre) = postsynaptic;
break;
end
end
else
%Choose a neuron from anywhere
neuronA = ceil(neurons * rand);
if connections(neuronA,1) == 0
%Find a neuron that this neuron is connected to
%that we’re not connected to
for synapse = 1:number_of_connections
neuronB = wMatrix(synapse,neuronA);
if (connections(neuronB,1) == 0)
synapseA = synapse;
break;
end
end
for synapse = 1:number_of_connections
if (wMatrix(synapse,neuronB) == neuronA)
synapseB = synapse;
break;
end
end
end
%a->b and b->a, but c is left out in the cold!
%try a->c, c->a and b->c, c->b
%This requires making TWO new connections for
%our new neuron
connections(neuronA, 1) = 1;
syn_count = syn_count + 1;
wMatrix(syn_count, postsynaptic) = neuronA;
wMatrix(synapseA, neuronA) = postsynaptic;
connections(neuronB, 1) = 1;
syn_count = syn_count + 1;
wMatrix(syn_count, postsynaptic) = neuronB;
wMatrix(synapseB, neuronB) = postsynaptic;
end
end
end
realwMatrix = zeros(neurons, neurons);
for i= 1:neurons
for j = 1:number_of_connections
if i == wMatrix(j,i)
realwMatrix(i,j) = 0;
else
connect1 = wMatrix(j,i);
connect2 = i;
realwMatrix(connect1, connect2) = 1 + .001*rand(1,1);
realwMatrix(connect2, connect1) = realwMatrix(connect1, connect2);
end
end
end