All Projects → leopiney → Tensor Safe

leopiney / Tensor Safe

Licence: bsd-3-clause
A Haskell framework to define valid deep learning models and export them to other frameworks like TensorFlow JS or Keras.

Programming Languages

haskell
3896 projects

Projects that are alternatives of or similar to Tensor Safe

Thinc
🔮 A refreshing functional take on deep learning, compatible with your favorite libraries
Stars: ✭ 2,422 (+2422.92%)
Mutual labels:  ai, functional-programming
Expression
Pragmatic functional programming for Python inspired by F#
Stars: ✭ 94 (-2.08%)
Mutual labels:  functional-programming
Deep Dream In Pytorch
Pytorch implementation of the DeepDream computer vision algorithm
Stars: ✭ 90 (-6.25%)
Mutual labels:  ai
Fungamerefresh
好玩的下拉刷新控件,让我们一起来回味童年
Stars: ✭ 1,307 (+1261.46%)
Mutual labels:  functional-programming
Taskorama
⚙ A Task/Future data type for JavaScript
Stars: ✭ 90 (-6.25%)
Mutual labels:  functional-programming
Haskell
Stars: ✭ 91 (-5.21%)
Mutual labels:  functional-programming
Grand Challenge.org
A platform for end-to-end development of machine learning solutions in biomedical imaging
Stars: ✭ 89 (-7.29%)
Mutual labels:  ai
Toeicbert
TOEIC(Test of English for International Communication) solving using pytorch-pretrained-BERT model.
Stars: ✭ 95 (-1.04%)
Mutual labels:  ai
Emojiintelligence
Neural Network built in Apple Playground using Swift
Stars: ✭ 1,323 (+1278.13%)
Mutual labels:  ai
Atulocher
人工智能。本来是YRSSF的附属项目,结果越玩越大,现在连逻辑思维都有了。
Stars: ✭ 91 (-5.21%)
Mutual labels:  ai
Slack Machine
A sexy, simple, yet powerful and extendable Slack bot
Stars: ✭ 91 (-5.21%)
Mutual labels:  ai
Bert As Service
Mapping a variable-length sentence to a fixed-length vector using BERT model
Stars: ✭ 9,779 (+10086.46%)
Mutual labels:  ai
Micromlp
A micro neural network multilayer perceptron for MicroPython (used on ESP32 and Pycom modules)
Stars: ✭ 92 (-4.17%)
Mutual labels:  ai
Ai Dl Enthusiasts Meetup
AI & Deep Learning Enthusiasts Meetup Project & Study Sessions
Stars: ✭ 90 (-6.25%)
Mutual labels:  ai
Webots
Webots Robot Simulator
Stars: ✭ 1,324 (+1279.17%)
Mutual labels:  ai
Spotted Leopards
Proof of concept for a cats-like library built using Dotty features
Stars: ✭ 91 (-5.21%)
Mutual labels:  functional-programming
Amadeus Node
Node library for the Amadeus Self-Service travel APIs
Stars: ✭ 91 (-5.21%)
Mutual labels:  ai
Aif360
A comprehensive set of fairness metrics for datasets and machine learning models, explanations for these metrics, and algorithms to mitigate bias in datasets and models.
Stars: ✭ 1,312 (+1266.67%)
Mutual labels:  ai
Paperplane
Lighter-than-air node.js server framework
Stars: ✭ 95 (-1.04%)
Mutual labels:  functional-programming
Ai Study
人工智能学习资料超全整理,包含机器学习基础ML、深度学习基础DL、计算机视觉CV、自然语言处理NLP、推荐系统、语音识别、图神经网路、算法工程师面试题
Stars: ✭ 93 (-3.12%)
Mutual labels:  ai

Tensor Safe

tensor-safe is a dependently typed framework to define deep learning models whose structure is verified at compilation time. If the models are valid, these can be compiled to external frameworks, such as Keras framework in Python or JavaScript.

Install instructions

  1. Install tensor-safe executable with cabal new-install

    cabal new-install tensor-safe

  2. Install tensor-safe library

    cabal new-install tensor-safe --lib

Building instructions and development tools

  1. Install ghc-mod, hpack and stylish-haskell with stack install

    cd ~
    stack install ghc-mod hpack stylish-haskell
    
  2. Run stack build in project folder

  3. Install Intero

    Run stack build intero in the project folder

    Ref: https://gitlab.com/vannnns/haskero/blob/master/client/doc/installation.md

Generate .cabal file

Run hpack in the root of the project and the file tensor-safe.cabal will be generated

Model definition

Models can be defined as a type using the MkINetwork type function. The MkINetwork defines a valid instance of a Network model given a list of Layers and a spected input and iutput Shapes.

Here's an example of how to define a simple model for the MNIST dataset, using Dense layers:

type MNIST = MkINetwork
    '[
        Flatten,
        Dense 784 42,
        Relu,
        Dense 42 10,
        Sigmoid
    ]
    ('D3 28 28 1)    -- Input
    ('D1 10)         -- Output

After that, variable with the model type can be verified with the function mkINetwork like this:

mnist :: MNIST
mnist = mkINetwork

Nesting networks definitions

You can nest networks definitions easily by adding the networks as layers. For example, in the case of the MNIST model defined above, we can abstract the use of Dense and a activation function like this:

type DenseRelu i o =
    MkINetwork '[ Dense i o, Relu ] ('D1 i) ('D1 o)

type DenseSigmoid i o =
    MkINetwork '[ Dense i o, Sigmoid ] ('D1 i) ('D1 o)

type MNIST = MkINetwork
    '[
        Flatten,
        DenseRelu 784 42,
        DenseSigmoid 42 10
    ]
    ('D3 28 28 1)    -- Input
    ('D1 10)         -- Output

How to extend layers definitions

Since this library only implements a subset of features that Keras implement, it's likely that for new projects you'll need to add new layers. Due to the modularization of the library, this can be done by adding the layer definitions in specific locations of the project:

  1. First, add a new auxiliary layer entry for the data type DLayer in TensorSafe.Compile.Expr. This will make possible the compilation of the layer for all instances of Generator. Also, add to the LayerGenerator entry for the newly added layer.
  2. Secondly, add the layer definition to the TensorSafe/Layers folder. You can copy the definitions from the currently defined layers.
  3. Then, import and expose your layer definition in the TensorSafe.Layers module.
  4. Finally, declare how your layer transforms a specific Shape in the Out type function.

Command line interface

This interface will change in the near future

You can install tensor-safe command line tool by running stack build. Then you can use it by using stack exec tensor-safe -- check --path ./path-to-model.hs or stack exec tensor-safe -- compile --path ./path-to-model.hs --module-name SomeModule.

Tools for JavaScript environment

Add as development dependency the packages babel-plugin-tensor-safe and eslint-plugin-tensor-safe. These can be found in the extra/javascript folder in this project.

You can add them directly from this project like this:

yarn add --dev file/:<path-to-tensor-safe>/extra/javascript/babel-plugin-tensor-safe

yarn add --dev file/:<path-to-tensor-safe>/extra/javascript/eslint-plugin-tensor-safe

Then add to the .eslintrc.js file in your JavaScript project the plugin tensor-safe and the rule tensor-safe-model-invalid like this:

module.exports = {
  plugins: [
     ...
     "tensor-safe"
   ],
  ...
  rules: {
    ...
    "tensor-safe/invalid-model": 1
    ...
  }
};

And for the Babel plugin add "@babel/plugin-tensor-safe" to the plugins list in the .babelrc file inside your JavaScript project.

Then, you can write your deep learning model inside your JS files as in the following example:

function createConvModel() {
  safeModel`
    '[
        Conv2D 1 16 3 3 1 1,
        Relu,
        MaxPooling 2 2 2 2,
        Conv2D 16 32 3 3 1 1,
        Relu,
        MaxPooling 2 2 2 2,
        Conv2D 32 32 3 3 1 1,
        Relu,
        Flatten,
        Dense 288 64,
        Sigmoid,
        Dense 64 10,
        Sigmoid
    ]
    ('D3 28 28 1)  -- Input
    ('D1 10)       -- Output
`;

  return model;
}

Related projects

This project was highly influenced by Grenade 💣. Grenade is a cool library to define deep neural networks which are validated using dependent types. What differences TensorSafe from Grenade the most is that TensorSafe doesn't run nor train the models, instead, it compiles the model to external languages that are capable of performing all computations – like Keras for Python or JavaScript. Also, TensorSafe doesn't need to specifically declare all Shapes transformations for all the model layers, instead, it just needs the input and output Shapes to validate the model.

Another worth looking library is TensorFlow for Haskell. This library has all bindings for TensorFlow in C. The issue with this is that it doesn't perform a lot of type checkings at compilation time. However, there's an open branch that uses dependent types to solve many of these issues: https://github.com/helq/tensorflow-haskell-deptyped, but the solution still seems rather complicated for real use.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].