6.0 KiB
6.0 KiB
Note
本文档由 WeHub 基于上游 README 翻译整理,属于社区翻译,非官方中文文档。
English · 原始项目 · 上游 README
原作者、版权与许可证归属以原始项目及本仓库 LICENSE 文件为准。
ConvNetJS
ConvNetJS 是神经网络的 JavaScript 实现,并附带精美的基于浏览器的演示。目前支持:
- 常见的神经网络模块(全连接层、非线性激活)
- 分类(SVM/Softmax)与回归(L2)代价函数(cost functions)
- 可指定并训练处理图像的卷积网络(Convolutional Networks)
- 基于 Deep Q Learning 的实验性强化学习(Reinforcement Learning)模块
更多详细信息请参阅主站 convnetjs.com
注意:我已不再积极维护 ConvNetJS,因为我实在没有时间。我认为此时 npm 仓库可能已无法正常工作。
在线演示
- MNIST 手写数字卷积神经网络
- CIFAR-10 卷积神经网络
- 玩具 2D 数据
- 玩具 1D 回归
- 在 MNIST 手写数字上训练自编码器
- Deep Q Learning 强化学习演示
- 图像回归("绘画")
- MNIST 上 SGD/Adagrad/Adadelta 对比
示例代码
下面是一个定义双层神经网络并在单个数据点上进行训练的最小示例:
// species a 2-layer neural network with one hidden layer of 20 neurons
var layer_defs = [];
// input layer declares size of input. here: 2-D data
// ConvNetJS works on 3-Dimensional volumes (sx, sy, depth), but if you're not dealing with images
// then the first two dimensions (sx, sy) will always be kept at size 1
layer_defs.push({type:'input', out_sx:1, out_sy:1, out_depth:2});
// declare 20 neurons, followed by ReLU (rectified linear unit non-linearity)
layer_defs.push({type:'fc', num_neurons:20, activation:'relu'});
// declare the linear classifier on top of the previous hidden layer
layer_defs.push({type:'softmax', num_classes:10});
var net = new convnetjs.Net();
net.makeLayers(layer_defs);
// forward a random data point through the network
var x = new convnetjs.Vol([0.3, -0.5]);
var prob = net.forward(x);
// prob is a Vol. Vols have a field .w that stores the raw data, and .dw that stores gradients
console.log('probability that x is class 0: ' + prob.w[0]); // prints 0.50101
var trainer = new convnetjs.SGDTrainer(net, {learning_rate:0.01, l2_decay:0.001});
trainer.train(x, 0); // train the network, specifying that x is class zero
var prob2 = net.forward(x);
console.log('probability that x is class 0: ' + prob2.w[0]);
// now prints 0.50374, slightly higher than previous 0.50101: the networks
// weights have been adjusted by the Trainer to give a higher probability to
// the class we trained the network with (zero)
如果你希望对图像进行预测,这里是一个小型卷积神经网络示例:
var layer_defs = [];
layer_defs.push({type:'input', out_sx:32, out_sy:32, out_depth:3}); // declare size of input
// output Vol is of size 32x32x3 here
layer_defs.push({type:'conv', sx:5, filters:16, stride:1, pad:2, activation:'relu'});
// the layer will perform convolution with 16 kernels, each of size 5x5.
// the input will be padded with 2 pixels on all sides to make the output Vol of the same size
// output Vol will thus be 32x32x16 at this point
layer_defs.push({type:'pool', sx:2, stride:2});
// output Vol is of size 16x16x16 here
layer_defs.push({type:'conv', sx:5, filters:20, stride:1, pad:2, activation:'relu'});
// output Vol is of size 16x16x20 here
layer_defs.push({type:'pool', sx:2, stride:2});
// output Vol is of size 8x8x20 here
layer_defs.push({type:'conv', sx:5, filters:20, stride:1, pad:2, activation:'relu'});
// output Vol is of size 8x8x20 here
layer_defs.push({type:'pool', sx:2, stride:2});
// output Vol is of size 4x4x20 here
layer_defs.push({type:'softmax', num_classes:10});
// output Vol is of size 1x1x10 here
net = new convnetjs.Net();
net.makeLayers(layer_defs);
// helpful utility for converting images into Vols is included
var x = convnetjs.img_to_vol(document.getElementById('some_image'))
var output_probabilities_vol = net.forward(x)
入门
主站提供了入门 教程。
完整的文档 也可在那里找到。
请参阅本项目的 releases 页面以获取压缩编译后的库;下方也提供了直接链接以方便使用(但请自行托管副本)
从 src/ 编译库到 build/
如果你想为库添加功能,需要修改 src/ 中的代码,然后将库编译到 build/ 目录。编译脚本会拼接 src/ 中的文件,然后压缩结果。
编译通过 ant 任务完成:它会拼接 src/ 中的源文件来编译 build/convnet.js,然后将结果压缩到 build/convnet-min.js。请确保已安装 ant(在 Ubuntu 上可简单地通过 sudo apt-get install 安装),然后 cd 进入 compile/ 目录并运行:
$ ant -lib yuicompressor-2.4.8.jar -f build.xml
输出文件将位于 build/
在 Node 中使用
该库也可在 node.js 中使用:
- 安装:
$ npm install convnetjs - 使用:
var convnetjs = require("convnetjs");
许可证
MIT