simple-socket-router

Simple express-like socket routing for websockets, event emitters and socket.io
This is a simple way to avoid having to write a bunch of event.on
handlers for larger applications. You can simply define routes and pass in your event emitter/websocket/socket.io(client|sever) and handle routes that way
Example
This works for both socket servers and clients
// SERVER CODE
import {Router,EventRouter} from 'simple-socket-router';
import Server from 'socket.io';
const ioServer = new Server();
// Router - npm module 'routes';
const router = new Router();
router.addRoute('/test/users', function(req, res){
console.log('Got data from client Handler');
res.send('ok');
});
router.addRoute('/account/:username', async (req, res)=>{
console.log('In Account Handler');
const user = await db.getUser(req.params.user);
res.send({user);
});
ioServer.on('connection',socket=>{
EventRouter({router,socket});
})
// Client CODE
import {Router,EventRouter} from 'simple-socket-router';
import io from 'socket.io-client';
const socket = io('http://localhost');
// Router - npm module 'routes';
const router = new Router();
router.addRoute('/live/updates', function(req, res){
console.log('Got message from server',{update:req.body});
});
socket.on('connect',()=>{
EventRouter({router,socket});
})
Overview
All of the model building classes are structured similarly to ml.js models. Each model has three shared components regardless if it's a regression, classification or time series model.
This library was built to quickly implement Tensorflow and Keras models in JavaScript.
Configuration
All of the model configuration is managed during creating an instance of the class. All models have fit
and compile
properties that are assigned to the instance settings
property.
You can also pass the Tensorflow reference to be used, as a configuration property if you want to use the compiled C++ or GPU via CUDA versions on the tf
property;
this.settings.compile
is passed totensorflow.model.compile
and contains the configuration for optimizers (read more
).this.settings.fit
is passed totensorflow.model.fit
and contains the configuration for model fitting (read more
)
Model constructor example
import { MultipleLinearRegression, } from 'tensorscript';
import tf from '@tensorflow/tfjs-node-gpu';
const MLR = new MultipleLinearRegression({
fit:{
epochs:500,
batchSize:5,
},
compile:{
loss: 'meanSquaredError',
optimizer: 'adam',
},
},{
tf,
});
Each constructor takes two parameters settings
and properties
. As in the example above, settings are used to configure tensorflow objects and properties are used to configure tensorscript (like which version of tensorflow to use).
Training
All tensorflow models train asynchronously, therefore all tensorscript model train functions are all asynchronous. Always pass dependent and independent variables as matrix inputs.
Time series models can be trained with only one input matrix.
Training example
import { MultipleLinearRegression, } from 'tensorscript';
async function main(){
const MLR = new MultipleLinearRegression();
const x = [ [1], [2] ];
const y = [ [3], [4] ];
await MLR.train(x,y);
}
Predicting
All prediction inputs must be the same shape as training inputs. TensorScript provides an asynchronous predict method that converts tensor values to javascript objects. If you want the tensor from tensorflow, use the calculate method instead.
Predicting example
import { MultipleLinearRegression, } from 'tensorscript';
async function main(){
const MLR = new MultipleLinearRegression();
const x = [ [1], [2] ];
const y = [ [3], [4] ];
await MLR.train(x,y);
await MLR.predict([[3]]) //=> [[5]];
const tensorPrediction = await MLR.calculate([[3]]) //=> [[5]];
tensorPrediction.data()
.then(tensors=> /*Typed Array*/)
}
Examples
For more examples on how to do feature scaling and more, check out the Examples folder.
Frequently Asked Questions
Scaling
- How do I scale and descale my inputs?
- The library is only for model create, data engineering and modeling is typically handled elsewhere
- Check out the regression examples (hint: use ModelScript).
- How do I cross validate, test/train split, K-fold, etc?
- (See above) The library is only for model create, data engineering and modeling is typically handled elsewhere
- Check out the code examples (hint: use ModelScript).
Frequently Asked Questions
Scaling
- How do I scale and descale my inputs?
- The library is only for model create, data engineering and modeling is typically handled elsewhere
- Check out the regression examples (hint: use ModelScript).
- How do I cross validate, test/train split, K-fold, etc?
- (See above) The library is only for model create, data engineering and modeling is typically handled elsewhere
- Check out the code examples (hint: use ModelScript).