oceanai-js
TypeScript icon, indicating that this package has built-in type declarations

1.0.5 • Public • Published

banner

Oceanai-js

Wrapper library that allows a direct integration between the Ocean marketplace and artificial intelligence tools like tensorflow

With Oceanai-js, you can:

  • Create artificial intelligence models: the current state of the library uses Tensorflow.js for the implementation of the AI models, anything that you can do with Tensorflow.js you can also do it with Oceanai-js

  • Buy OCEAN marketplace datasets to use them on your models

  • Publish OCEAN marketplace datasets

  • Consume OCEAN datatokens, to access the services of the Ocean environment.

Every function related to the OCEAN environment is fullfiled using Ocean.js. Ocean.js is part of the Ocean Protocol toolset.

This library is still in alpha state and you can expect running into problems. If you run into them, please open up a new issue.

📚 Prerequisites

🏗 Installation

npm install oceanai-js

🏄 Quickstart

The source code of this example is also included on the folder samples/intro of the library

index.ts This file explains how to implement several types of AI models for the selected Ocean dataset

import  *  as  tf  from  '@tensorflow/tfjs-node';
import { MultiLayerPerceptronSpace, MultilayerPerceptronTraining, MultilevelSequentialTraining, MultilayerIOParser, SequentialSpace } from  'oceanai-js';
import { ModelGoal } from  './ModelGoal';

let  inputs = [
	[ 0.1, 0.3, 0.4, 0.4 ],
	[ 0.8, 0.2, 0.2, 0.6 ],
	[ 0.2, 0.6, 0.4, 0.3 ],
	[ 0.3, 0.8, 0.7, 0.9 ],
	[ 0.1, 0.2, 0.3, 0.2 ],
	[ 0.4, 0.4, 0.9, 0.1 ],
	[ 0.1, 0.3, 0.2, 0.5 ],
	[ 0.5, 0.5, 0.3, 0.3 ],
	[ 0.7, 0.6, 0.2, 0.5 ],
];

let  outputs = [
	[ 0.25, 0.4 ],
	[ 0.5, 0.4 ],
	[ 0.5, 0.35 ],
	[ 0.55, 0.8 ],
	[ 0.15, 0.25 ],
	[ 0.4, 0.5 ],
	[ 0.4, 0.35 ],
	[ 0.5, 0.3 ],
	[ 0.65, 0.35 ],
];

  
//Arguments for the creation of the model
let  args = {
	inputs:  4,
	outputs:  2,
	hidden: [20, 20]
};
let  goal = new  ModelGoal(inputs, outputs);
  
// tensorflow compiling args (class ModelCompileArgs in tensorflow)
let  compileArgs = {
	optimizer:  'Adamax',
	loss:  'meanSquaredError',
	metrics: ['mse']
};

// tensorflow fit args (class ModelFitArgs in tensorflow)
let  fitArgs = {
	epochs:  100,
	batchSize:  32
}

let  iters = 10;

// create a new model using a specific training (multilayer perceptron)
async  function  sample1() {
	let  training = new  MultilayerPerceptronTraining(args, goal, compileArgs, fitArgs, iters);
	let  model = await  training.apply();
	console.log("> outputs:", outputs);
	console.log("> predictions:", await model.apply(inputs));
}

// create a new model specifing the model generation (extends Space or SequentialSpace for tensorflow) and Data parsing (extends DataParse)
async  function  sample2() {

	let  parser = new  MultilayerIOParser(2); // Extends DataParse
	let  space = new  MultiLayerPerceptronSpace({ // Extends SequentialSpace
		inputs:  4,
		outputs:  2,
		hidden: [20, 20]
	});

	let  training = new  MultilevelSequentialTraining(goal, parser, compileArgs, fitArgs, iters);
	let  model = await  training.apply(space);
	console.log("> outputs:", outputs);
	console.log("> predictions:", await model.apply(inputs));
}

// create a new model and train it using the tensorflow functions
async  function  sample3() {
	let  parser = new  MultilayerIOParser(2);
	let  space = new  MultiLayerPerceptronSpace({
		inputs:  4,
		outputs:  2,
		hidden: [20, 20]
	});

	let  model = space.generate();
	model.model.compile(compileArgs);

	let  data = parser.parseInputs(inputs);
	let  labels = parser.parseOutputs(outputs);

	await  model.model.fit(data, labels, fitArgs);

	let  predictions = await  model.apply(data);
	console.log("> outputs:", outputs);
	console.log("> predictions:", await parser.restoreOutputs(predictions));
}

// Personal network designed using tensorflow layer definitions
async function sample4() {
    // defining a personalized network in this case multilayer perceptron
    let layers = [
        tf.layers.dense({ units: args.inputs, inputShape: [ args.inputs ] }), // input layer
        tf.layers.dense({ units: 20 }), // hidden layer 1
        tf.layers.dense({ units: 20 }), // hidden layer 2
        tf.layers.dense({ units: args.outputs }) // output layer
    ];
    let space = new SequentialSpace(layers);
    let parser = new MultilayerIOParser(2);
    let training = new MultilevelSequentialTraining(goal, parser, compileArgs, fitArgs, iters);
    let model = await training.apply(space);
    console.log("> outputs:", outputs);
    console.log("> predictions:", await model.apply(inputs));
}

// this function defines which of sample do you want to run
async  function  run() {
	await sample1();
	await sample2();
	await sample3();
	await sample4();
}

run();

Obtaining Ocean datasets

In here we will explain how to obtain OCEAN datasets using oceanai-js. Because there are multiple forms in which the data is stored on ocean (files, google spreadsheets, urls) we implement classes that help you to manage this data.

import fs from 'fs';
import { StandardCSVReader, GoogleAPIConnection, SpreadsheetConnection } from 'oceanai-js';

async function csvDataRead() {
    let reader = new StandardCSVReader();
    let result = await reader.read({
        file: './src/dataset.csv',
        separator: ','
    });
    console.log("> result:", result);
}

function googleDataRead() {
    fs.readFile('./src/credentials.json', 'utf-8', async (err: any, content: any) => {
        if (err)
            return console.log('Error loading client secret file:', err);
        let credentials = JSON.parse(content);
        let spreadsheetId = '1VdP4j5cUXBwx8jV8c3FmVBEwP4VmtDxRHVRvEWhcx2A';
        let google = new GoogleAPIConnection('./src/token.json', [
            'https://www.googleapis.com/auth/spreadsheets'
        ]);
        let spreadsheets = new SpreadsheetConnection(google);
        await spreadsheets.init(credentials.web);
        let details = await spreadsheets.getSpreadsheetDetails(spreadsheetId);
        console.log("> details:", details);
        let data = await spreadsheets.getSheetData({
            spreadsheetId: spreadsheetId,
            sheet: 'data',
            fromColumn: 'A',
            toColum: 'E',
            formRow: 1,
            toRow: 401
        });
        console.log("> data:", data, data.length);
    });
}

async function run() {
    await csvDataRead();
    await googleDataRead();
}

run();

📖 Learn more

If you have any difficulties with the quickstarts, or if you have further questions about how to use oceanai.js please reach out to us on Github.

This also applies If you notice any bugs or issues with the library, in that case please open an issue on github.

🦑 Development

The project is authored with TypeScript and compiled with tsc.

To start compiler in watch mode:

npm install
npm start

⬆️ Releases

Coming soon.

🏛 License

Copyright ((C)) 2021 Intelligent Trading Machines

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Readme

Keywords

none

Package Sidebar

Install

npm i oceanai-js

Weekly Downloads

2

Version

1.0.5

License

ISC

Unpacked Size

223 kB

Total Files

291

Last publish

Collaborators

  • itrmachines