Merge branch 'clients/inter_client_comms' into feature/rt-queries

* clients/inter_client_comms:
  Proto-helpfile for MLPRegressor
  Declare constants as constants
  Add FluidMLPRegressor
nix
Pierre Alexandre Tremblay 6 years ago
commit 6a3273da45

@ -0,0 +1,35 @@
FluidMLPRegressor : FluidManipulationClient {
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|
var uid = UniqueID.next;
^super.new(server,*([hidden.size]++hidden++activation++maxIter++learnRate++
momentum++batchSize++uid))!?{|inst|inst.init(uid);inst}
}
init {|uid|
id = uid;
}
fit{|sourceDataset, targetDataset, action|
this.prSendMsg(\fit,
[sourceDataset.asSymbol, targetDataset.asSymbol],
action,numbers(FluidMessageResponse,_,1,_)
);
}
predict{ |sourceDataset, targetDataset, layer, action|
this.prSendMsg(\predict,
[sourceDataset.asSymbol, targetDataset.asSymbol,layer],
action);
}
predictPoint { |sourceBuffer, targetBuffer, layer action|
this.prSendMsg(\predictPoint,
[sourceBuffer.asUGenInput, targetBuffer.asUGenInput, layer], action);
}
}

@ -0,0 +1,127 @@
TITLE:: FluidMLPRegressor
summary:: Regression with a multi-layer perceptron
categories:: Machine learning
related:: Classes/FluidDataSet
DESCRIPTION::
Perform regression between link::Classes/FluidDataSet::s using a Multilayer Perception neural network.
CLASSMETHODS::
METHOD:: new
Creates a new instance on the server.
ARGUMENT:: server
The link::Classes/Server:: on which to run this model.
ARGUMENT:: hidden
An link::Classes/Array:: that gives the sizes of any hidden layers in the network (default is two hidden layers of three units each).
ARGUMENT:: activation
Ativation function to use for the hidden layer units.
ARGUMENT:: maxIter
Maximum number of iterations to use in training.
ARGUMENT:: learnRate
The learning rate of the network. Start small, increase slowly.
ARGUMENT:: momentum
Training momentum, default 0.9
ARGUMENT:: batchSize
Training batch size.
METHOD:: identity, relu, sigmoid, tanh
Convinience constants for the available activation functions.
INSTANCEMETHODS::
PRIVATE:: init, uid
METHOD:: fit
Train the network to map between a source and target link::Classes/FluidDataSet::
ARGUMENT:: sourceDataset
Source data
ARGUMENT:: targetDataset
Target data
ARGUMENT:: action
Function to run when training is complete
returns:: The training loss, or -1 if training failed
METHOD:: predict
Apply the learned mapping to a dataset (given a trained network)
ARGUMENT:: sourceDataset
Input data
ARGUMENT:: targetDataset
Output data
ARGUMENT:: layer
Layer whose output to return.
ARGUMENT:: action
Function to run when complete
METHOD:: predictPoint
Apply the learned mapping to a single data point in a link::Classes/Buffer::
ARGUMENT:: sourceBuffer
Input point
ARGUMENT:: targetBuffer
Output point
ARGUMENT:: layer
Layer whose output to return.
ARGUMENT:: action
A function to run when complete
EXAMPLES::
code::
(
{
~source = FluidDataSet.new(s,"mlpregressor_source");
~target = FluidDataSet.new(s,"mlpregressor_target");
~dest = FluidDataSet.new(s,"mlpregressor_dest");
~datapoint = Buffer.alloc(s,2);
~destpoint = Buffer.new(s);
~regressor = FluidMLPRegressor(s) ;
s.sync;
~source.read("/tmp/test_reg_source_200_lin.json");
~source.print;
~target.read("/tmp/test_reg_target_200_lin.json");
~target.print;
}.fork
)
//Train network to map source to target. fit() returns loss. If this is -1, then training has failed
(
~regressor.fit(~source,~target,action: {|x|
if(x != -1) {("MLP trained with loss"+x).postln;}{"Training failed. Try again (perhaps with a lower learning rate)".postln;}
});
)
//Batch predict takes a FluidDataSet source, a FluidDataSet to write netwotk output to, and layer to read from
~regressor.predict(~source,~dest,2);
~dest.dump
//Single point predict uses Buffers rater than FluidDataSet:
{
~datapoint.setn(0,[1,1]);
~regressor.predictPoint(~datapoint,~destpoint,2);
s.sync;
~destpoint.loadToFloatArray(0,action:{|a|
a.postln;
});
}.fork
::

@ -12,6 +12,7 @@
#include <clients/nrt/StandardizeClient.hpp>
#include <clients/nrt/PCAClient.hpp>
#include <clients/nrt/MDSClient.hpp>
#include <clients/nrt/MLPRegressorClient.hpp>
#include <clients/rt/AudioTransportClient.hpp>
#include <clients/rt/FluidDataSetWr.hpp>
#include <FluidSCWrapper.hpp>
@ -36,4 +37,5 @@ PluginLoad(FluidSTFTUGen)
makeSCWrapper<RTAudioTransportClient>("FluidAudioTransport",ft);
makeSCWrapper<NRTThreadedAudioTransportClient>("FluidBufAudioTransport",ft);
makeSCWrapper<NRTThreadedDataSetWriter>("FluidDataSetWr", ft);
makeSCWrapper<NRTThreadedMLPRegressorClient>("FluidMLPRegressor",ft);
}

Loading…
Cancel
Save