您的位置:首页 > Web前端 > JavaScript

使用electron+deeplearnjs构建深度学习环境

2017-09-05 15:42 567 查看

前言

目前javascript的功能越来越强大,除了可以用js+html+css来做网页之外,还可以依赖一些框架和库来做app应用,使用html+css+js来做手机应用比较流行的是react native和weex。html+css+js实现桌面应用比较流行的是electron和nw.js。切入今天的主题,使用js来做深度学习。

js深度学习库

已经有很多牛人在尝试将js用于深度学习,作为一个前端的开发者,也可以尝试着了解这方面的知识,在目前,这是一个趋势,一切都在向人工智能靠拢。下面是一些js用于机器学习的库:

* brain

* playground

* FlappyLearning

* synaptic

* Land Lines

* convnetjs

* thing-translator

* neurojs

* machine_learning

* deepforge

* deeplearnjs

deeplearnjs

deeplearnjs是google开源的一套深度学习库,支持typescript和es6等语法。它提供了很丰富的API,如果了解过tensorflow的人,学习deeplearnjs会感觉很亲切,因为deeplearnjs里面的很多思想和tensorflow是很相似的。deeplearnjs可以在浏览器里面训练机器学习模型,同时支持CPU和GPU。这里需要一个前提是浏览器需要支持WebGL才能使用该库,可以下载最新的chrome浏览器。可以在这里查看官网提供的demo演示。

让deeplearnjs在服务端运行

目前deeplearnjs推荐的运行环境的chrome浏览器,但也可以在node上运行deeplearnjs,这儿尝试的一种办法是使用electron+deeplearnjs+node.js的方式。

环境搭建步骤:

安装node V8.4.0

安装electron v1.8.0 beta ,需要支持WebGL,所以需要高版本的electron。使用命令:npm install electron@beta, or npm i electron@1.8.0

安装deeplearnjs 命令:npm install deeplearn

到目前为止环境已经搭建完成。

测试:

在上面环境搭建目录下,新建文件:

package.json

{
"name": "dp",
"version": "1.0.0",
"description": "",
"main": "main.js",
"dependencies": {
"deeplearn": "^0.1.2"
},
"devDependencies": {},
"scripts": {
"start":"electron ."
},
"author": "",
"license": "ISC"
}


main.js

const {app, BrowserWindow} = require('electron')
const path = require('path')
const url = require('url')

// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let win

function createWindow () {
// Create the browser window.
win = new BrowserWindow({width: 800, height: 600})

// and load the index.html of the app.
win.loadURL(url.format({
pathname: path.join(__dirname, 'index.html'),
protocol: 'file:',
slashes: true
}))

// Open the DevTools.
//  win.webContents.openDevTools()

// Emitted when the window is closed.
win.on('closed', () => {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
win = null
})
}

// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow)

// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit()
}
})

app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (win === null) {
createWindow()
}
})


index.html

<!DOCTYPE html>
<html>
<head>
<title>deeplearn demo1</title>
</head>

<body>
<script>
const deeplearn=require('deeplearn');
const util=require('util');
const math = new deeplearn.NDArrayMathGPU();
const a=deeplearn.Array1D.new([1,2,3]);
const b=deeplearn.Scalar.new(2);

math.scope(()=>{
const result=math.add(a,b);
document.body.append(util.inspect(result));
});
</script>
</body>
</html>


运行命令npm run start,然后看见如下界面出现,则环境搭建成功。



可以在此处下载demo直接运行。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息