Load trained Machine Learning model in javascript - javascript

I am using sklearn in python to build/train some models (Random Forest Regressor, Kmeans, SVM, ...), and I would like to use those trained models in a webapp Javascript/html. Is there a way to do so?
I've seen that tensorflow.js allows to do such things with keras model. But it seems to me that TF/keras in python are limited to neural network.
I've also seen sklearn-porter, but it seems to be limited to some specific models (classification mostly). If someone managed to use it, can he tells me more?
Thanks in advance for any help

As an option you can simply build RESTfull API in Python (for instance using Flask). After it would be easy just request a prediction from Javascript.
There are few benefits for such approach:
except the model, you still maybe need data preparation functions;
you have to separated projects;
with REST API you can easy add new version of the model;

Related

Sklearn Model to JS

I am working on a sentiment analysis project, where the backbone is ofc a model. This was developed using sklearn's off the shelf solutions (MLP) trained with my data. I would like to "save" this model and use it again in JavaScript.
Adam
I have looked at pickle for python but I'm not sure how i could use this for JS. This is a chrome extension I am developing so I would rather not set up and server. I should add this is course work, so spending money is a no!
After some research I pretty much determined its not possible using sklearn in JS. My solution was to use keras and use tensorflow JS.
Alternatively, I have learnt the maths behind the network and "raw" code it using no libraries. This took a lot longer than just converting everything to keras although.

Is there a way to share data (ex. enums) between a NodeJS Backend and a WPF C# Windows Application Frontend?

I've noticed that I've been writing a small, but non-negligible amount of repeat code between my NodeJS Backend and C# WPF Frontend.
Most of the code would be things like Enums or Model classes where a backend endpoint would require an enum or properties of a Model, I would have to write an equivalent Enum or Model on the WPF application to properly do the insertion. This is okay as it works, but I'm simply wondering is there a better approach to keeping these 2 projects in sync?
Thanks!
I am not sure if you are looking for tools to create the interoperatbility or for libraries that will do this at runtime. I think there are tools that can help you with this though.
Personally I have experience with generating a C# client using NSwagStudio. You can (among other options) use a "Swagger file" to generate a C# client.
In order to generate a swagger file from your NodeJS endpoint you might be able to use a tool like https://github.com/swagger-api/swagger-node to complete your tool chain. I have no experience with the latter so I don't know how it works.

Use/productionize trained machine learning model in client-side javascript?

So this is a slightly insane idea that I've been kicking around. I'm thinking of building a classifier for (a specialized subset of) some web content, and then supplying it to users in the form of a chrome extension. (So: user goes to web page, clicks button on chrome extension, extension applies pre-trained model to generate prediction, presents result to user.)
But I'm cheap, and I don't want to run a server to receive data from the chrome extension, generate a prediction, and send it back to the user. It occurs to me: why not just find some way to serialize the trained model or something, and do it all on the client side.
So if I end up fitting a very simple model, like a logistic regression or something, then it would be easy to transfer to the client-side. Ultimately, a trained logistic regression is just expressible as an ordinary mathematical function mapping features to predictions, it's like one line of code. And I guess that's true of some fancier models as well (linear SVM). Under those circumstances, the chrome extension could do all the data cleaning and such, and then generating predictions is just a matter of calculating a dot product. But the same isn't true if I end up using, say, random forests or something.
Tragically, there doesn't appear to be a PMML library or anything like that for javascript.
So, I turn to the wisdom of the internet. Are there any other methods for productionizing ML models that might be usable on client-side javascript? Or are the options really just (1) run a server, (2) write a PMML library for JS, (3) there is no third option?
Thanks!
Do you have your models available in PMML data format already?
If so, then:
Translate PMML to Java source code. This is easy using JPMML libraries.
Translate Java source code to JavaScript source code. This is even easier using Google Web Toolkit.
As a result, you would have a self-contained JavaScript model object that exposes model schema query and scoring API similar to the JPMML-Evaluator library.

How to integrate Golang backend and Javascript (three.js) frontend?

I would like to write a 3D application using Golang, my favorite programming language. However, I would prefer not to use native OpenGL directly as the graphical frontend as this would entail a larger learning curve than I'm willing to tolerate. Additionally, I don't really want to use a Golang game engine like go:ngine.
After doing some research I found out about the amazing WebGL Javascript library three.js and I was so intrigued that I completed an introductory Javascript course in a few hours. I'm not really a web developer at all, so I'm wondering how practical it would be for me to write my application primarily in Go but with a three.js frontend.
Some specific questions:
Is it worthwhile / practical to use Javascript/three.js as a frontend to a Golang server like this?
If it is worthwhile, where can I look to learn how to integrate the two cleanly?
If it isn't really worthwhile, which alternatives do I have?
Thanks, any response appreciated.
Edit:
Do you plan to make operations on geometry in Go, possibly in realtime and communicate with your Javascript layer using Websockets?
Yes, this is what I'm thinking now. I'm intending my (Golang) program to generate streams of vertices and triangles based on a few parameters from the user. Each "structure" is generated all at once after the users supply their parameters (which I see as slider bars + input boxes on a Javascript frontend).
Here's an example of something the Go program might generate, plotted in GNUPlot:
(source: alexozer.com)
If this is the case you underestimate Javascript a bit. You should write most of your application in Javascript and use Go as a classical backend service layer like user accounts, persisting state, etc.
I'd be fine with doing that, except for these reasons:
The core generating process fundamentally depends on Goroutines and channels
I know close to nothing about web development, including frontend and backend
So I suppose I'm asking about the implementation details of one of these three possibilities:
Writing most of the program, including the generator, in JS, and a minimal backend in Go
Writing most of the program in Go and using JS just as a graphical frontend
Something else?
I recommend leaving out the whole Websocket aspect because it's only a possible optimization (and maybe not even that).
If you need to depend on Go's concurrency model then go for it, write your component in Go, then a Webserver in Go that takes parameters coming from an HTML request, uses them to compute the result and sends back the data in a JSON format.
On the frontend you will then only focus on sending this request when the user changed a parameter and for displaying of the JSON data you can use ThreeJS right away.
You'll still have to learn a bit of web development and Javascript though. But hey, they say the web is the future ;-)
I think it's the way to go for your case because your application operates in a quite strict request-result way.
The answer to your question depends a lot on where exactly you plan to draw the line between your Go component and the Javascript component. From the way you describe the problem I get the feeling that you want to write most of your application in Go and use JS only as a display layer? I wouldn't recommend doing that.
To rephrase my question: Do you plan to make operations on geometry in Go, possibly in realtime and communicate with your Javascript layer using Websockets? If this is the case you underestimate Javascript a bit. You should write most of your application in Javascript and use Go as a classical backend service layer like user accounts, persisting state, etc.
I might be able to give a more specific answer if I know what exactly you plan to do.

Do I have to use a Backend when using Backbone.js?

I want to develop a relatively simple application that calculates some value based on several inputs. I dont want a backend, all the calculation can be done in the browser.
Im a little new to JavaScript and WebApps and I came across Backbone.js.
I really like the MVC design, however, they mention a backend a lot. My question:
Is a backend server absolutely required?
Is a backend server optional but without one there isn't much point in backbone.
Or will backbone will really help me out?
Backend is not required.
Backbone can fully work without any backend if your application doesn't require one.
That depends on your application. If you want to retrieve value of some inputs and calculate a result then Backbone won't do that for you - it will help you structure your code. If you app is simple and don't need support for models, views and collections or routing, then there is no point in using Backbone. Hard to answer this question.
For example: Classic todo example application doesn't use any backend.
Backbone.js implements fetch(), save(), destroy() etc. methods on models automatically performing appropriate AJAX requests and parsing response. So it has a strong support for backend via REST services, but it is optional.
You can still use models, views, routers and events without any server-side code. Just don't call REST methods (or override them at your wish).
You can use localStorage for persistence (you'd have to implement this yourself or find it on the web, like here) but if you don't even need that then you don't need to use any of the persistence methods in backbone.
Backbone is meant to help you structure a medium-large sized application (js-wise), so it doesn't become unmaintainable jQuery spaghetti. With short applications (js-wise) it's really an overkill unless you are trying to learn how backbone works.
Note with js-wise I mean the client side code, if you had a huge backend but the only js would be something that focuses some form, it would not even count as a short application (js-wise).
You can use backbone.js without a backend. However you obviously won't be able to store or retrieve data. Backbone may still be useful for keeping your code organized, however it really shines when you want to separate presentation logic from logic that manipulates your data, which is a goal of the MVC pattern. Generally your data will be stored on and retrieved from a backend.
If you want to play around with data persistence, try out backlift.com. [disclosure, I work on backlift.com] We've tried to make it easy to get a backbone app up-and-running without having to setup a server or deal with compiling templates.

Categories

Resources