Decentralized TensorFlow

Solidity AI / Deep Learning contracts
A Quick Primer on Neural Networks & Deep Learning:
  • Neural networks are a type of machine learning that are modeled after the human brain. They are essentially a set of algorithms that are designed to recognize patterns. They interpret sensory data through a kind of machine perception, labeling or clustering raw input. The patterns they recognize are numerical, contained in vectors, into which all real-world data, be it images, sound, text or time series, must be translated.
  • Deep learning is a subset of machine learning that uses multi-layered artificial neural networks to deliver state-of-the-art accuracy in tasks such as object detection, speech recognition, language translation and others.
This Machine Learning Library is a collection of smart contracts that can be used to define, deploy, train & make predictions using neural networks on the blockchain. The library is written in Solidity and is designed to be used with Hardhat.
Since a lot of models need to be of large size to be effective at predicting, it is recommended that these contracts be deployed onto some blockchain with low fees and large block size, such as Bitcoin Virtual Machine.
Deployed MVP contracts can be found here.

Notable Features

  • [✓] Define models as multi-layered neural networks. Several types of layers are supported, such as Dense, Rescale, etc. with more in the working
  • [✓] Store models on the blockchain
  • [✓] Make on-chain predictions using trained models
  • [✓] Transfer ownership of neural networks

Upcoming Features

  • [ ] Train / reinforce models

Installation

git clone https://github.com/TrustlessComputer/solearn.git
cd solearn

Compile the contracts

npm install
npx hardhat compile

Interact with Perceptrons contract

Contract interactions can be done via Hardhat tasks. Of course we will need a well-defined model with trained weights, and some images to make predictions on.
Some sample models & images can be found in the repository under the sample-models and sample-images folder. Let's use the 10x10 model (model ID: 2).

Get model info

npx hardhat get-perceptron --id 2 --contract 0xe63363d747393455d774cD3E079a79c95aeb9f56
This will write the model specs as well as all the weights in the file perceptronDesc.json. For example, you can verify that the model name is indeed sample-model.

Classify an image

npx hardhat eval-perceptron --img 'sample-images/10x10/cryptoadz/000.png' --id 2 --w 10 --h 10 --contract 0xe63363d747393455d774cD3E079a79c95aeb9f56
This should print the class name prediction to your console. In this case, it should be cryptoadz. Notice the image width & height specified in the parameters, which are both equal to 10. Make sure you have them right to avoid errors; these dimensions will differ between models.

Create new model

Instead of using the existing model, you can also use the mint-perceptron task to create your own models and specify all its weights. Make sure to choose a model ID that is not yet taken (or you will run into an "invalid token ID" error).
npx hardhat mint-perceptron --id 123 --model sample-models/<your-new-model>.json --contract 0xe63363d747393455d774cD3E079a79c95aeb9f56
That's it! You have just made a prediction using a neural network on the blockchain. Stay tuned for more exciting features like continuously training models on-chain. For now, you can go on to create and use your own models.
If you are interested in deploying your own instance of Perceptrons contract, continue reading.

Deploy the contracts

Developers can edit the source code or deploy their own Perceptrons contract. Review config file hardhat.config.ts. The network configs should look like this.
networks: {
tcbtc: {
url: "http://localhost:10002",
accounts: {
mnemonic: "<your mnemonic with funds>"
},
timeout: 100_000,
},
}
Run the deploy scripts using hardhat-deploy.
npx hardhat deploy --tags Perceptrons