深度學習零基礎?沒關係——麵向藝術家的RNN教程
首發地址:https://yq.aliyun.com/articles/68606
更多深度文章,請關注:https://yq.aliyun.com/cloud
hardmaru
Twitterhttps://twitter.com/hardmaru
Githubhttps://github.com/hardmaru
這篇文章適用於沒有任何機器學習背景的讀者,目標是向藝術家和設計師展示如何使用一個預訓練的神經網絡並Javascript生成交互式的數字作品。
介紹
近年來,機器學習已經成為創意社區的流行工具。比如、T-SNE以及無數其他的方法,藝術家們采用這些技術生成文本、音樂以及聲音。
distill.pub
手寫大腦建模
當寫信時,大腦中有許多事情發生。比如寫什麼、選擇詞匯、動筆寫等過程。而JavaScript——筆的位置,以及筆與紙張是否接觸。
對模型做兩個假設。第一個假設是模型接下來要寫的內容隻取決於它過去寫的內容。內容的記憶可以通過構建回歸神經網絡(RNN)完成。
RNN記錄每個神經元的活躍程度,隱藏狀態對象會隨著寫入內容而不斷更新。
第二個假設是該模型不完全確定它接下來應該寫什麼。這兩個假設可以總結為下圖,描述了使用具有隱藏狀態的循環神經網絡模型來生成隨機序列的過程。
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函數能夠產生一代手寫字跡,60,RNN
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) 的新形式。基於這一新的變量集合,產生新的手寫體。
變化溫度的概率分布采樣
pdf段應該儲存下一個筆劃的概率分布,它實際上隻是包含了複雜的概率分布的參數。
處理這個問題的直接方式是將概率分布建模為許多正態分布加在一起的總和,更多的技術細節可以在之前得到。
利用概率分布以及從分布中采樣得到的 (dx, dy, pen) 以確定接下來 temperature
在下麵的草圖中,可以通過改變溫度參數來可視化概率分布是如何增加。

為了簡單起見,上述演示模擬了二十個一維正態分布與溫度參數的混合。在手寫模型中,概率分布是二十個二維正態分布的混合。在下一個草圖中,您可以在手寫模型正在寫入內容時修改其溫度,以查看手寫隨溫度變化的情況。

當溫度保持低時,手寫模型變得非常確定,所以手寫通常更整潔和更現實。
demo
+ python

一個可能的是從基本的手寫演示中得到讓用戶在屏幕上以交互的方式編寫一些字跡,另一個是可以建立類似的distill.pub。

使用此代碼!
如果你是一個藝術家或有意於機器學習的設計師,你可以據自己的喜好使用包含這些代碼的GitHub
這篇文章隻從表麵分析了循環神經網絡。還有其他的資源,比如TensorFlow或keras等。
更新:
該模型已經被移植到bl.ocks。
本文由北郵@組織翻譯。
文章原標題《Recurrent Neural Network Tutorial for Artistshardmaru
原文
最後更新:2017-07-12 22:09:47