阅读361 返回首页    go 京东网上商城


深度学习零基础?没关系——面向艺术家的RNN教程

首发地址:https://yq.aliyun.com/articles/68606


更多深度文章,请关注:https://yq.aliyun.com/cloud


hardmaru

Twitterhttps://twitter.com/hardmaru

Githubhttps://github.com/hardmaru

博客地址:https://blog.otoro.net/

3255170d60d5b67bb7e3295be9c001211f84e115

这篇文章适用于没有任何机器学习背景的读者目标是艺术家和设计师展示如何使用一个预训练的神经网络Javascript生成交互式数字作品。

72bd0bc283ba5588d4d29901befe2a420fbaf314

介绍

近年来,机器学习已经成为创意社区的流行工具。比如T-SNE以及无数其他的方法,艺术家们采用这些技术生成文本、音乐以及声音。

distill.pub

手写大脑建模

当写信时,大脑中有许多事情发生。比如写什么、选择词汇动笔写等过程JavaScript——笔的位置,以及笔与纸张是否接触。

对模型做两个假设。第一个假设是模型接下来要写的内容只取决于它过去写的内容。内容的记忆可以通过构建回归神经网络(RNN)完成。

RNN记录每个神经元的活跃程度,隐藏状态对象会随着写入内容而不断更新。

第二个假设是该模型不完全确定它接下来应该写什么。这两个假设可以总结为下图,描述了使用具有隐藏状态的循环神经网络模型来生成随机序列的过程。

   2c787d9544dd3654e46fe2af76be7c1ac785fd5d

RNN

RNNp5.j s的框架


x,ydx,dyx,y

var x, y; // absolute coordinates of where the pen is
var dx, dy; // offsets of the pen strokes, in pixels

pen相互pen

// keep track of whether pen is touching paper. 0 or 1.
var pen;
var prev_pen; // pen at the previous timestep

模型每一时刻(dx, dy, pen)变量列表,那么使用这些数据可以绘制出模型在屏幕上生成的内容。开始时将dx, dy, x, y, pen,prev_pen

var rnn_state; // store the hidden states the rnn

// store all the parameters of a mixture-density distribution
var pdf;

// controls the amount of uncertainty of the model
// the higher the temperature, the more uncertainty.
var temperature = 0.65; // a non-negative number.

 rnn_state update

rnn_state = Model.update([dx, dy, prev_pen], rnn_state);

 rnn_state产生模型将要写内容的概率分布。该概率分布表示作为主体,称作pdf就能rnn_state

pdf = Model.get_pdf(rnn_state);

 temperature该模型是否为想要的pdf sample去采样下(dx, dy, pen)在稍后使用以下函数:

[dx, dy, pen] = Model.sample(pdf, temperature);

现在唯一需要的其他变量是控制手写体的颜色,并且跟踪浏览器的屏幕尺寸:

// stores the browser's dimensions
var screen_width = window.innerWidth;
var screen_height = window.innerHeight;

// colour for the handwriting
var line_color;

现在准备初始化所有这些声明的变量restart通过重复调用以初始化很多次。


function restart() {
  // set x to be 50 pixels from the left of the canvas
  x = 50;
  // set y somewhere in middle of the canvas
  y = screen_height/2;

  // initialize pen's states to zero.
  dx = 0;
  dy = 0;
  prev_pen = 0;
  // note: we draw lines based off previous pen's state

  // randomise the rnn's initial hidden states
  rnn_state = Model.random_state();

  // randomise colour of line by choosing RGB values
  line_color = color(random(255), random(255), random(255))
}

restar函数后,定义p5.js setup框架


function setup() {
  restart(); // initialize variables for this demo
  createCanvas(screen_width, screen_height);
  frameRate(60); // 60 frames per second
  // clear the background to be blank white colour
  background(255);
  fill(255);
}

p5.j s中的 draw函数能够产生一代手写字迹,60RNN


function draw() {

  // using the previous pen states, and hidden state
  // to get next hidden state 
  rnn_state = Model.update([dx, dy, prev_pen], rnn_state);

  // get the parameters of the probability distribution
  // from the hidden state
  pdf = Model.get_pdf(rnn_state);

  // sample the next pen's states
  // using our probability distribution and temperature
  [dx, dy, pen] = Model.sample(pdf, temperature);

  // only draw on the paper if pen is touching the paper
  if (prev_pen == 0) {
    // set colour of the line
    stroke(line_color);
    // set width of the line to 2 pixels
    strokeWeight(2.0);
    // draw line connecting prev point to current point.
    line(x, y, x+dx, y+dy);
  }

  // update the absolute coordinates from the offsets
  x += dx;
  y += dy;

  // update the previous pen's state
  // to the current one we just sampled
  prev_pen = pen;

  // if the rnn starts drawing close to the right side
  // of the screen, restart our demo
  if (x > screen_width - 50) {
    restart();
    // reset screen
    background(255);
    fill(255);
  }

}

在每一帧draw函数将更新基于它先前在屏幕上绘制的模型隐藏的状态。从这个隐藏状态,模型将生成接下来的概率分布。基于此temperature将随机抽样(dx, dy, pen) 新形式。基于这一新的变量集合,产生新的手写体

ba2ebaab03e8a46d921f9173e6bbe79d2d0c748c

变化温度的概率分布采样

 pdf应该储存下一个笔划的概率分布,它实际上只是包含了复杂的概率分布的参数。

处理这个问题的直接方式是将概率分布建模为许多正态分布加在一起的总和更多的技术细节可以在之前得到

利用概率分布以及从分布中采样得到的 (dx, dy, pen) 确定接下来 temperature 

在下面的草图中,可以通过改变温度参数来可视化概率分布如何增加。

6490570257918dcc04d5add3d69d79d53a84061d

为了简单起见,上述演示模拟了二十个一维正态分布与温度参数的混合。在手写模型中,概率分布是二十个二维正态分布的混合。在下一个草图中,您可以在手写模型正在写入内容时修改其温度,以查看手写随温度变化的情况。

c9148aa9785b9dcbd9d0f672947cc2bc5548c6a0

当温度保持低时,手写模型变得非常确定,所以手写通常更整洁和更现实。

demo

+ python

74fb16f0922dfe625091db7ee0084b48f6b7b837

一个可能的从基本的手写演示中得到让用户在屏幕上交互的方式编写一些字迹另一个可以建立类似distill.pub

978815fa1ee3132f4bb5dd4407c872e24f744899

使用此代码!

如果你是一个艺术家或有意机器学习设计师,你可以自己的喜好使用包含这些代码GitHub

这篇文章只表面分析了循环神经网络。还有其他的资源,比如TensorFlowkeras等。

更新:

该模型已经被移植bl.ocks


       本文由北邮@组织翻译。


      文章原标题《Recurrent Neural Network Tutorial for Artistshardmaru

原文

最后更新:2017-07-12 22:09:47

  上一篇:go  没有任何公式——直观的理解变分自动编码器VAE
  下一篇:go  科普“三伏天”