«neural sirens» by clarkenciel

on 02 Dec'17 20:09 in
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
(
SynthDef(\noise, { arg freq, amp, q;
    var signal = BPF.ar(
        WhiteNoise.ar,
        freq: In.kr(freq),
        mul: In.kr(amp),
        rq: In.kr(q)
    );

    Out.ar(0, signal ! 2)
}).add;

SynthDef(\line, { arg start, end, dur, bus;
    var env = Env([start, end], [dur]);
    var signal = EnvGen.kr(env, doneAction: 2);
    Out.kr(bus, signal ! bus.numChannels);
}).add;
)

(
~noiseRamper = { arg initialFreq, initialAmp, initialQ, group=nil;
    var lineGroup = Group.new(group, \addToTail);
    var soundGroup = Group.after(lineGroup);
    var freqBus = Bus.control(s, 2).set(initialFreq);
    var ampBus  = Bus.control(s, 2).set(initialAmp);
    var qBus    = Bus.control(s, 2).set(initialQ);
    var noise = Synth.head(soundGroup, \noise, [
        \freq, freqBus,
        \amp, ampBus,
        \q, qBus
    ]);
    var ramp = { arg bus, end, dur;
        lineGroup.freeAll;
        bus.get { arg value;
            Synth.head(lineGroup, \line, [
                \bus, bus,
                \start, value,
                \end, end,
                \dur, dur
            ])
        };
    };

    (
        free: { arg self;
            noise.free;
            lineGroup.freeAll;
            soundGroup.freeAll;
        },
        rampFreq: { arg self, end, dur;
            ramp.value(freqBus, end, dur);
            self
        },
        rampAmp: { arg self, end, dur;
            ramp.value(ampBus, end, dur);
            self
        },
        rampQ: { arg self, end, dur;
            ramp.value(qBus, end, dur);
            self
        },
    )
};

~noisesFromNetwork = { arg network, fundamental, masterGroup;
	network.collect { arg layer, lIndex;
		var baseIndex = lIndex + 1;

		layer.asArray.collect { arg row, rIndex;
			var rowIndex = rIndex + 1;

			row.collect { arg value, vIndex;
				var valueIndex = vIndex + 1;
				var freqOffset = (2 + value) ** (rowIndex + valueIndex + baseIndex).nextPrime;
				var freq = (fundamental * baseIndex) + freqOffset;
				var amp = value + 0.9;
				var q = value * 0.001;

				~noiseRamper.value(freq, amp, q, masterGroup)
			}
		}
	}
};
)

(
~neuralNetwork = { arg layerDimensions;
	layerDimensions.collect { arg dimensions;
		Matrix.with(Array.fill(dimensions.first, { Array.rand(dimensions.last, 0, 1.0) }))
	}
};

~forward = { arg network, input;
	var weightedInputs = [];
	var activations = [input];
	var currentWeightedInput, currentActivation = input;

	network.do { arg layer;
		currentWeightedInput = currentActivation * layer;
		currentActivation = currentWeightedInput.tanh;
		weightedInputs = weightedInputs add: currentWeightedInput;
		activations = activations add: currentActivation;
	};

	[weightedInputs, activations]
};

~guess = { arg network, input; ~forward.value(network, input).last.last };

~outputError = { arg guess, target; target - guess };

~sigmoidPrime = { arg x; 1 - (x.tanh ** 2) };

~backProp = { arg network, input, target; // calculate weight updates to improve network performance
	var result = ~forward.value(network, input);
	var weightedInputs = result.first;
	var activations = result.last;
	var guess = activations.last;
	var error = target - guess;
	var delta, updates;

	delta = Matrix.with(error.asArray * weightedInputs.last.collect(~sigmoidPrime).asArray);
	updates = [activations.drop(-1).last.flop * delta];

	(network.size - 2).to(0, -1) do: { arg index;
		var weightedInput = weightedInputs at: index;
		var activation = activations at: index;
		var layer = network at: (index + 1);
		var derivative = weightedInput.collect(~sigmoidPrime);

		delta = Matrix with: ((delta * layer.flop).asArray * derivative.asArray);
		updates = [(activation.flop * delta)] ++ updates
	};

	updates
};

~applyUpdates = { arg network, updates, learningRate=0.1;
	network collect: { arg layer, index; layer + (updates.at(index) * learningRate) }
};

~trainLoop = { arg network, observations, steps=10, learningRate=0.1, action={};
	Routine.new {
		steps do: { arg idx;
			var observation = observations.choose;
			var input = observation.first;
			var target = observation.last;
			var updates = ~backProp.value(network, input, target);

			network = ~applyUpdates.value(network, updates, learningRate);
			action.value(idx, network, input, target);
		};
		'done'.postln;
	}
};
)

(
var network = ~neuralNetwork value: [[2, 3], [3, 1]];
var observations = [
	[[[0, 1]], [[1]]],
	[[[1, 0]], [[1]]],
	[[[1, 1]], [[0]]],
	[[[0, 0]], [[0]]],
].collect { arg obs; obs collect: { arg x; Matrix.with(x) } };

~master = Group.new;
~fundamental = 300;
~stepDur = 10;
~noises = ~noisesFromNetwork.value(network, ~fundamental, ~master);

~netLoop = ~trainLoop.value(network, observations,
	steps: 100000,
	learningRate: 0.01,
	action: { arg count, net, in, t;
		~stepDur.wait;
		"-------------".postln;

		net.asArray do: { arg layer, lIndex;
			var layerIndex = lIndex + 1;

			layer do: { arg row, rIndex;
				var rowIndex = rIndex + 1;

				row do: { arg value, vIndex;
					var noise = ~noises.at(lIndex).at(rIndex).at(vIndex);
					var valueIndex = vIndex + 1;
					var freqOffset = (2 + value) ** (rowIndex + valueIndex + layerIndex).nextPrime;
					var freq = (~fundamental * layerIndex) + freqOffset + (value * 25);
					var amp = value + 0.9;
					var q = value * (0.001 + 0.009.rand);

					[freq, amp, q].postln;

					if(noise.notNil && [true, false].choose, {
						noise.rampAmp(amp, ~stepDur).rampFreq(freq, ~stepDur).rampQ(q, ~stepDur);
					})
				}
			}
		};
	}
).play
)
raw 5681 chars (focus & ctrl+a+c to copy)
reception
comments