deeplearn.js入门(二)

来源:互联网 发布:linux查询端口命令 编辑:程序博客网 时间:2024/05/22 17:20

deeplearn.js是一个开源的项目,项目托管在GitHub上。基本介绍可以查看之前写的博客译文。

deeplearn.js本地环境搭建

  • 先从GitHub网站上找到deeplearn.js,然后Fork到您自己的仓库。
  • windows下打开cmd,输入npm install deeplearn 安装deeplearn(假设您电脑上已有node环境)
  • 将仓库克隆到本地
    Git Bash中输入如下命令:
$ git clone https://github.com/PAIR-code/deeplearnjs.git$ cd deeplearnjs # 进入到deeplearn根目录下$ npm run prep 

运行一个Demo程序

输入:

$ ./scripts/watch-demo demos/adder/adder.ts

运行成功后,会提供一个服务器的网址,打开浏览器,输入网址,找到demos下的adder文件夹,打开index.html,就可看到运行效果。

自己写一个Demo运行

在本地clone下来的deeplearn文件夹下,找到demos文件夹,里面有很多文件夹,每个文件夹里就是一个demo。我们在demos文件夹下建立一个test文件夹,创建一个文本文档,命名为test.ts,用记事本打开该文件,复制以下内容到test.ts中:

import {Array1D, NDArrayMathGPU, Scalar} from '../deeplearn';const math = new NDArrayMathGPU();const a = Array1D.new([1, 2, 3]);const b = Scalar.new(2);math.scope(() => {  const result = math.add(a, b);  console.log(result.getValues());  // Float32Array([3, 4, 5])});

保存关闭。
新建一个index.html的文件,复制以下内容到文件中:

<!doctype html><body><script src="bundle.js"></script></body>

输入

$ ./scripts/watch-demo demos/test/test.ts

运行该demo,浏览器中打开,找到index.html,打开,浏览器进入开发者模式(F12),可以在console控制台看到输出结果为[3,4,5].

原创粉丝点击