| 26 | const PORT = 8001; |
| 27 | |
| 28 | async function run() { |
| 29 | const port = process.env.PORT || PORT; |
| 30 | const server = http.createServer(); |
| 31 | const io = socketio(server); |
| 32 | let useTestData = true; |
| 33 | |
| 34 | server.listen(port, () => { |
| 35 | console.log(`Running socket on port: ${port}`); |
| 36 | }); |
| 37 | |
| 38 | io.on('connection', (socket) => { |
| 39 | socket.on('test_data', (value) => { |
| 40 | useTestData = value === 'true' ? true : false; |
| 41 | }); |
| 42 | |
| 43 | socket.on('predictSample', async (sample) => { |
| 44 | console.log('received predict request'); |
| 45 | io.emit('predictResult', await pitch_type.predictSample(sample)); |
| 46 | }); |
| 47 | }); |
| 48 | |
| 49 | io.emit('accuracyPerClass', await pitch_type.evaluate(useTestData)); |
| 50 | await sleep(TIMEOUT_BETWEEN_EPOCHS_MS); |
| 51 | |
| 52 | let numTrainingIterations = 10; |
| 53 | for (var i = 0; i < numTrainingIterations; i++) { |
| 54 | console.log(`Training iteration : ${i + 1} / ${numTrainingIterations}`); |
| 55 | await pitch_type.model.fitDataset(pitch_type.trainingData, { epochs: 1 }); |
| 56 | io.emit('accuracyPerClass', await pitch_type.evaluate(useTestData)); |
| 57 | await sleep(TIMEOUT_BETWEEN_EPOCHS_MS); |
| 58 | } |
| 59 | |
| 60 | io.emit('trainingComplete', true); |
| 61 | console.log('training complete'); |
| 62 | |
| 63 | } |
| 64 | |
| 65 | run(); |