So the model from yesterday performs pretty well. Next step is to get it running in the browser for a simple demo app. As text is typed the model will output the parts of speech.
Converting TensorFlow models to TensorFlow.js is very straightforward. First install tensorflowjs at the command line using pip.
pip install tensorflowjs
Then run a single command to convert your model. You can find the details here. There are plenty of options that you can set, but the vanilla version worked fine for me.
tensorflowjs_converter data/model ./output_dir --input_format=tf_saved_modelNext I needed a bit of JavaScript to load the model into the page and make a prediction. You can see that I have stopped this in the Chrome debugger so that I could fiddle with the shape of the tensor for prediction.
So I just chose some numbers to fill the tensor and run a prediction:

Just to check that everything is working as I expect I ran the same tensor into the python version of the model:
So both of the models return 38. But what does that mean? That brings me to my next task. I need to write the text preprocessing that I used in the python as javascript.
Hopefully soon the need for this tricky - and hard to maintain - step in the process will be obviated by using TensorFlow Preprocessing Layers. You can find details
here. Not available in TensorFlow.js as at time of writing, but they look really great especially for text models.
I originally found out about Preprocessing Layers by watching
this talk on youtube. It is an excellent talk and there are more new ideas in there which I will come back to in subsequent posts.
And
this talk from TensorFlow Fall 2020 is excellent too. It covers a wide range of new features in TF.
So my next task is to write the preprocessing (and prediction decoding) logic for the model in JavaScript.
Comments
Post a Comment