-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserverMLP.js
More file actions
85 lines (62 loc) · 2.92 KB
/
Copy pathserverMLP.js
File metadata and controls
85 lines (62 loc) · 2.92 KB
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
// Demo for supervised machine learning - Multi-Layer Perceptron (Neural Network) (For more info: http://joonku.com/project/machine_learning/apidoc#multi_layer_perceptron )
// GLOBALS
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io').listen(server);
var port = 80;
// SET SERVER ON PORT
server.listen(port);
console.log('server running on http://127.0.0.1');
console.log('server listening on port '+port);
// SET APPLICATON PATH
app.configure(function(){ app.use(express.static(__dirname + '/multiLayerPerceptron')); });
app.get('/', function (req, res){ res.sendfile(__dirname + '/multiLayerPerceptron/index.html'); });
// SET SOCKET CONNECTION(S)
io.sockets.on('connection', function (socket) {
// listen to socket with stream name 'music'
socket.on('music', function (data) {
// calculate/classify data
var result = mlp.predict([[data.rhcp, data.bieber, data.soad, data.eminem, data.dg, data.wtc, data.gaga, data.dp]]);
// send result to client
console.log(result);
io.sockets.emit('music', { result});
});
});
/**-- Important Stuff for ML starts here --**/
var ml = require('machine_learning');
// Training Data Inputs; 1.0=best rating 0.0=worst rating
var data = [
// Red Hot Chilli Peppers, Justin Bieber, System of a down, Eminem, David Guetta, WuTangClan, Lady Gaga, Daft Punk
[0.8, 0.0, 0.8, 0.4, 0.2, 0.4, 0.0, 0.2],
[1.0, 0.2, 0.2, 0.6, 0.0, 0.2, 0.0, 0.6],
[0.2, 0.0, 0.2, 0.8, 0.2, 1.0, 0.0, 0.6],
[0.8, 0.2, 0.6, 0.8, 0.4, 0.8, 0.0, 0.8],
[0.2, 0.0, 0.0, 1.0, 0.2, 1.0, 0.0, 0.0],
[0.2, 0.2, 0.0, 0.4, 0.8, 0.0, 0.2, 1.0],
[0.0, 1.0, 0.0, 0.2, 0.0, 0.0, 0.6, 0.0],
[1.0, 1.0, 0.2, 0.0, 0.2, 0.6, 0.0, 0.6],
];
var result = [
// Training Data Outputs: The Inputs of var data make the following outputs
// Linkin Park, Rihanna, ACDC, Kanye West, Martin Garrix, 2Pac, Avicii
[0.8, 0.0, 1.0, 0.2, 0.1, 0.6, 0.2],
[1.0, 0.2, 0.4, 0.4, 0.2, 0.4, 0.6],
[0.6, 0.0, 0.2, 0.8, 0.2, 0.8, 0.6],
[0.8, 0.2, 0.6, 0.8, 0.6, 0.6, 0.8],
[0.2, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0],
[0.2, 0.2, 0.0, 0.2, 0.8, 0.0, 1.0],
[0.0, 1.0, 0.0, 0.2, 0.0, 0.0, 0.0],
[0.8, 0.8, 0.2, 0.4, 0.2, 0.6, 0.6],
];
var mlp = new ml.MLP({
'input' : data,
'label' : result,
'n_ins' : 8, // Number Artists (Input)
'n_outs' : 7, // Number Artists (Outputs)
'hidden_layer_sizes' : [9,8,8] //number of hidden unit in each hidden layer
});
mlp.train({
'lr' : 0.6, // learn rate
'epochs' : 20000
});