Add MLPClassifier + help example

nix
Owen Green 6 years ago
parent 73e70f4676
commit ae74be947d

@ -1,5 +1,11 @@
FluidBaseMLP : FluidDataClient {
*new {|server, hidden = #[3,3] , activation = 0, maxIter = 100, learnRate = 0.0001, momentum = 0.9, batchSize = 50, validation = 0.2|
const <identity = 0;
const <sigmoid = 1;
const <relu = 2;
const <tanh = 3;
*new {|server, hidden = #[3,3] , activation = 0, maxIter = 100, learnRate = 0.0001, momentum = 0.9, batchSize = 50, validation = 0.2|
var hiddenCtrlLabels;
hidden = [hidden.size]++hidden;
@ -19,12 +25,6 @@ FluidBaseMLP : FluidDataClient {
}
FluidMLPRegressor : FluidBaseMLP {
const <identity = 0;
const <sigmoid = 1;
const <relu = 2;
const <tanh = 3;
*new {|server, hidden = #[3,3] , activation = 0, maxIter = 100, learnRate = 0.0001, momentum = 0.9, batchSize = 50, validation = 0.2|
^super.new(server,hidden,activation, maxIter,learnRate, momentum, batchSize,validation)
}
@ -47,3 +47,27 @@ FluidMLPRegressor : FluidBaseMLP {
[sourceBuffer.asUGenInput, targetBuffer.asUGenInput, layer], action);
}
}
FluidMLPClassifier : FluidBaseMLP {
*new {|server, hidden = #[3,3] , activation = 0, maxIter = 100, learnRate = 0.0001, momentum = 0.9, batchSize = 50, validation = 0.2|
^super.new(server,hidden,activation, maxIter,learnRate, momentum, batchSize,validation)
}
fit{|sourceDataSet, targetLabelSet, action|
this.prSendMsg(\fit,
[sourceDataSet.asSymbol, targetLabelSet.asSymbol],
action,numbers(FluidMessageResponse,_,1,_)
);
}
predict{ |sourceDataSet, targetLabelSet, action|
this.prSendMsg(\predict,
[sourceDataSet.asSymbol, targetLabelSet.asSymbol],
action);
}
predictPoint { |sourceBuffer, action|
this.prSendMsg(\predictPoint,
[sourceBuffer.asUGenInput], action,string(FluidMessageResponse,_,_));
}
}

@ -0,0 +1,152 @@
TITLE:: FluidMLPClassifier
summary:: Classification with a neural network
categories:: Undocumented classes
related:: Classes/FluidMLPRegressor, Classes/FluidDataSet
DESCRIPTION::
CLASSMETHODS::
METHOD:: new
(describe method here)
ARGUMENT:: server
(describe argument here)
ARGUMENT:: hidden
(describe argument here)
ARGUMENT:: activation
(describe argument here)
ARGUMENT:: maxIter
(describe argument here)
ARGUMENT:: learnRate
(describe argument here)
ARGUMENT:: momentum
(describe argument here)
ARGUMENT:: batchSize
(describe argument here)
ARGUMENT:: validation
(describe argument here)
returns:: (describe returnvalue here)
INSTANCEMETHODS::
METHOD:: predictPoint
(describe method here)
ARGUMENT:: sourceBuffer
(describe argument here)
ARGUMENT:: action
(describe argument here)
returns:: (describe returnvalue here)
METHOD:: fit
(describe method here)
ARGUMENT:: sourceDataSet
(describe argument here)
ARGUMENT:: targetLabelSet
(describe argument here)
ARGUMENT:: action
(describe argument here)
returns:: (describe returnvalue here)
METHOD:: predict
(describe method here)
ARGUMENT:: sourceDataSet
(describe argument here)
ARGUMENT:: targetLabelSet
(describe argument here)
ARGUMENT:: action
(describe argument here)
returns:: (describe returnvalue here)
EXAMPLES::
code::
(
~classifier = FluidMLPClassifier(s,hidden:[6],validation:0,momentum:0.1,learnRate:0.01);
~sourcedata= FluidDataSet(s,\mlpclassify_help_examples);
~labels = FluidLabelSet(s,\mlpclassify_help_labels);
~testdata = FluidDataSet(s,\mlpclassify_help_test);
~predictedlabels = FluidLabelSet(s,\mlpclassify_help_mapping);
)
//Make some clumped 2D points and place into a DataSet
(
~centroids = [[0.5,0.5],[-0.5,0.5],[0.5,-0.5],[-0.5,-0.5]];
~categories = [\red,\orange,\green,\blue];
~trainingset = Dictionary();
~labeldata = Dictionary();
4.do{ |i|
64.do{ |j|
~trainingset.put("mlpclass"++i++\_++j, ~centroids[i].collect{|x| x.gauss(0.5/3)});
~labeldata.put("mlpclass"++i++\_++j,[~categories[i]]);
}
};
~sourcedata.load(Dictionary.with(*[\cols->2,\data->~trainingset]));
~labels.load(Dictionary.with(*[\cols->1,\data->~labeldata]));
)
//Fit the classifier to the example DataSet and labels, and then run prediction on the test data into our mapping label set
~classifier.fit(~sourcedata,~labels,action:{|loss| ("Trained"+loss).postln});
//make some test data
(
~testset = Dictionary();
4.do{ |i|
64.do{ |j|
~testset.put("mlpclass_test"++i++\_++j, ~centroids[i].collect{|x| x.gauss(0.5/3)});
}
};
~testdata.load(Dictionary.with(*[\cols->2,\data->~testset]));
)
//Run the test data through the network, into the predicted labelset
~classifier.predict(~testdata,~predictedlabels,action:{"Test complete".postln});
//get labels from server
~predictedlabels.dump(action:{|d| ~labelsdict = d["data"]});
//Visualise: we're hoping to see colours neatly mapped to quandrants...
(
c = Dictionary();
c.add("red"->Color.red);
c.add("blue"->Color.blue);
c.add("green"->Color.green);
c.add("orange"->Color.new255(255, 127, 0));
e = 200 * ((~centroids + 1) * 0.5).flatten(1).unlace;
w = Window("scatter", Rect(128, 64, 200, 200));
w.drawFunc = {
Pen.use {
~testset.keysValuesDo{|k,v|
var x = v[0].linlin(-1,1,200,0).asInteger;
var y = v[1].linlin(-1,1,200,0).asInteger;
var r = Rect(x,y,5,5);
Pen.fillColor = c.at(~labelsdict[k][0]);
Pen.fillOval(r);
}
}
};
w.refresh;
w.front;
)
::

@ -13,6 +13,7 @@
#include <clients/nrt/PCAClient.hpp>
#include <clients/nrt/MDSClient.hpp>
#include <clients/nrt/MLPRegressorClient.hpp>
#include <clients/nrt/MLPClassifierClient.hpp>
#include <clients/rt/AudioTransportClient.hpp>
#include <clients/rt/FluidDataSetWr.hpp>
#include <FluidSCWrapper.hpp>
@ -38,4 +39,5 @@ PluginLoad(FluidSTFTUGen)
makeSCWrapper<NRTThreadedAudioTransportClient>("FluidBufAudioTransport",ft);
makeSCWrapper<NRTThreadedDataSetWriter>("FluidDataSetWr", ft);
makeSCWrapper<NRTThreadedMLPRegressorClient>("FluidMLPRegressor",ft);
makeSCWrapper<NRTThreadedMLPClassifierClient>("FluidMLPClassifier",ft);
}

Loading…
Cancel
Save