Models based on code
By creating a model that executes code you can create any behaviour you'd like. In this section we will go into the details about how models that execute code work, and how you can create one.
Filesystem
When you create a code model it is assigned a cloud-based filesystem. The code, dependencies you install, and other necessary files are placed inside this file system. Modifying the files can be done using the browser-based editor or a terminal.
The size of your filesystem is limited at 32 MiB by default. This is enough to contain lots of code, but not enough to install many dependencies. If you see errors while attempting to install dependencies, try increasing the limit in the model's settings. There is also a limit on the number of files and folders you can create within your filesystem. The larger the filesystem, the higher the file limit. It is not possible to accidentally exceed the limit, because if the filesystem is at max capacity, any additional writes will simply return an error. If this happens, try to increase the filesystem size.
When you execute your model, the filesystem is made available to the launcher that is executing the model, and a file within the filesystem is executed. For example, in Python, the file "main.py" is executed.
Launchers
For security, the model code is executed inside isolated virtual machines, in Decthings called launchers. See the launchers page for more information about how this works.
Code structure
When a request to execute an operation (such as an evaluation or training) reaches Decthings, a launcher will be started and it will execute the code that you have stored in your filesystem (in Python, the file "main.py"). Once the code has been loaded, Decthings will call a function in your code, and this function should perform the desired calculations. In order for Decthings to be able to execute your code and all functions within your code, there are some things to consider.
Firstly, Decthings must be able to find your code. Exactly how this works depends on which language is used. For Python, this means placing your code entry point in a file called "main.py".
Additionally, Decthings must be able to call functions within your code. This means that there are a certain set of functions that must be implemented, and these functions must return values that Decthings can interpret and send back to the person who started the operation.
The following functions can be implemented:
createModelState (parameters)
This function takes some user-defined input parameters, and uses that to initialize a new model state. For example, a neural net would create a randomized set of weights and biases. The new state is returned, in a binary format.
instantiateModel (state)
This function takes a state as input and prepares it for evaluation or training. For example, a neural net would load all the weights and biases from the state data.
evaluate (parameters)
This function takes user-provided input parameters and returns some output parameters. This function will be called after instantiateModel, which means that it has access to the loaded state.
train (parameters, tracker)
This function takes some user-provided input parameters and performs calculations that update the model state. For example, a neural net would set the new weights and biases. This function will be called after instantiateModel, which means that it has access to the loaded state.
getModelState ()
This function returns a new state. This function is usually called after train, and the returned state should include the modifications that was made by train.
dispose ()
This function is called when a loaded state will no longer be used. When this is called, the loaded state can be discarded to free up memory.
Note that not all of these functions have to be implemented - if you don't want to train your model, there is no need to implement "train".
Exactly how you implement the functions differs slightly between programming languages. For details, take a look at write the model code.
Terminal
You can start a terminal which runs in the Decthings cloud by going to the browser-based code editor and clicking "terminal" at the bottom. This terminal will have access to the files within your model, which means that you can for example use it to dependencies things into your model.
The terminal runs an interactive shell using Bash. For example, to view the files of your model, type "ls" and hit enter. You will have access to pip and npm, which allows you to install packages for Python and Node.
Package versions
In the model settings dialog (found by clicking "Model settings" on the details panel on the model page), you can select between different versions of globally installed packages and languages. You can for example choose which version of Python and Node you want your model to use.
In addition to selecting language versions, you can also select version of globally installed packages. Currently, the pip packages TensorFlow and Torch are pre-installed within your launcher. This means that you can simply do "import torch" in python, without installing anything. If you want to use a specific version of these packages, you can select it from the list. If the specific version is not available, you can always install it yourself into your model filesystem by using pip.