search
尋找貓咪~QQ 地點 桃園市桃園區 Taoyuan , Taoyuan

用 TensorFlow 讓你的機器人唱首原創給你聽

為了拉近和大家的距離

每篇文章的文末都有一個小話題

歡迎大家參與討論

有任何想說的都可以在評論區交流~

AI 研習社按:這篇文章會用一個簡單的模型在 TensorFlow 上來實現一個音頻生成器,GitHub 代碼鏈接詳見文末「閱讀原文」。原文作者楊熹,載於作者的個人博客,雷鋒網 AI 研習社經授權發布。

今天想來看看 AI 是怎樣作曲的。

本文會用 TensorFlow 來寫一個音樂生成器。

當你對一個機器人說:我想要一種能夠表達出希望和奇迹的歌曲時,發生了什麼呢?

計算機會首先把你的語音轉化成文字,並且提取出關鍵字,轉化成詞向量。

然後會用一些打過標籤的音樂的數據,這些標籤就是人類的各種情感。接著通過在這些數據上面訓練一個模型,模型訓練好后就可以生成符合要求關鍵詞的音樂。

程序最終的輸出結果就是一些和弦,他會選擇最貼近主人所要求的情感關鍵詞的一些和弦來輸出。

當然你不只是可以聽,也可以作為創作的參考,這樣就可以很容易地創作音樂,即使你還沒有做到刻意練習1萬小時。

機器學習其實是為了擴展我們的大腦,擴展我們的能力。

DeepMind 發表了一篇論文,叫做 WaveNet, 這篇論文介紹了音樂生成和文字轉語音的藝術。

通常來講,語音生成模型是串聯。這意味著如果我們想從一些文字的樣本中來生成語音的話,是需要非常大量的語音片段的資料庫,通過截取它們的一部分,並且再重新組裝到一起,來組成一個完整的句子。

生成音樂也是同樣的道理,但是它有一個很大的難點:就是當你把一些靜止的組件組合到一起的時候,生成聲音需要很自然,並且還要有情感,這一點是非常難的。

一種理想的方式是,我們可以把所有生成音樂所需要的信息存到模型的參數裡面。也就是那篇論文里講的事情。

我們並不需要把輸出結果傳給信號處理演算法來得到語音信號,而是直接處理語音信號的波。

他們用的模型是 CNN。這個模型的每一個隱藏層中,每個擴張因子,可以互聯,並呈指數型的增長。每一步生成的樣本,都會被重新投入網路中,並且用於產生下一步。

我們可以來看一下這個模型的圖。輸入的數據,是一個單獨的節點,它作為粗糙的音波,首先需要進行一下預處理,以便於進行下面的操作。

接著我們對它進行編碼,來產生一個 Tensor,這個 Tensor 有一些 sample 和 channel。

然後把它投入到 CNN 網路的第一層中。這一層會產生 channel 的數量,為了進行更簡單地處理。

然後把所有輸出的結果組合在一起,並且增加它的維度。再把維度增加到原來的 channel 的數量。

把這個結果投入到損失函數中,來衡量我們的模型訓練的如何。

最後,這個結果會被再次投入到網路中,來生成下一個時間點所需要的音波數據。

重複這個過程就可以生成更多的語音。

這個網路很大,在他們的 GPU 集群上需要花費九十分鐘,並且僅僅只能生成一秒的音頻。

接下來我們會用一個更簡單的模型在 TensorFlow 上來實現一個音頻生成器。

1.引入packages:

數據科學包 Numpy ,數據分析包 Pandas,tqdm 可以生成一個進度條,顯示訓練時的進度。

import numpy as np

import pandas as pd

import msgpackimport glob

import tensorflow as tf

from tensorflow.python.ops import control_flow_ops

from tqdm import tqdm

import midi_manipulation

我們會用到一種神經網路的模型 RBM-Restricted Boltzmann Machine 作為生成模型。

它是一個兩層網路:第一層是可見的,第二層是隱藏層。同一層的節點之間沒有聯繫,不同層之間的節點相互連接。每一個節點都要決定它是否需要將已經接收到的數據發送到下一層,而這個決定是隨機的。

2.定義超參數:

先定義需要模型生成的 note 的 range

lowest_note = midi_manipulation.lowerBound #the indexof the lowest note on the piano roll

highest_note = midi_manipulation.upperBound #the indexof the highest note on the piano roll

note_range = highest_note-lowest_note #the note range

接著需要定義 timestep ,可見層和隱藏層的大小。

num_timesteps = 15 #This is the number of timesteps that we will createat a time

n_visible = 2*note_range*num_timesteps #This is the sizeof the visible layer.

n_hidden = 50 #This is the sizeof the hidden layer123123

訓練次數,批量處理的大小,還有學習率。

num_epochs = 200 #The number of training epochs that we are going to run. For each epoch we go through the entire data set.

batch_size = 100 #The numberof training examples that we are going to send through the RBM at a time.

lr = tf.constant(0.005, tf.float32) #The learning rate of our model

3.定義變數:

x 是投入網路的數據

w 用來存儲權重矩陣,或者叫做兩層之間的關係

此外還需要兩種 bias,一個是隱藏層的 bh,一個是可見層的 bv

x = tf.placeholder(tf.float32, [None, n_visible], name="x") #The placeholder variable that holds our data

W = tf.Variable(tf.random_normal([n_visible, n_hidden], 0.01), name="W") #The weight matrix that stores the edge weights

bh = tf.Variable(tf.zeros([1, n_hidden], tf.float32, name="bh")) #The bias vector for the hidden layer

bv = tf.Variable(tf.zeros([1, n_visible], tf.float32, name="bv")) #The bias vector for the visible layer

接著,用輔助方法 gibbs_sample 從輸入數據 x 中建立樣本,以及隱藏層的樣本:

gibbs_sample 是一種可以從多重概率分佈中提取樣本的演算法。

它可以生成一個統計模型,其中,每一個狀態都依賴於前一個狀態,並且隨機地生成符合分佈的樣本。

#The sample of x

x_sample = gibbs_sample(1)

#The sample of the hidden nodes, starting from the visible state of x

h = sample(tf.sigmoid(tf.matmul(x, W) + bh))

#The sample of the hidden nodes, starting from the visible state of x_sample

h_sample = sample(tf.sigmoid(tf.matmul(x_sample, W) + bh))

4.更新變數:

size_bt = tf.cast(tf.shape(x)[0], tf.float32)

W_adder = tf.mul(lr/size_bt, tf.sub(tf.matmul(tf.transpose(x), h), tf.matmul(tf.transpose(x_sample), h_sample)))

bv_adder = tf.mul(lr/size_bt, tf.reduce_sum(tf.sub(x, x_sample), 0, True))

bh_adder = tf.mul(lr/size_bt, tf.reduce_sum(tf.sub(h, h_sample), 0, True))

#When we do sess.run(updt), TensorFlow will run all 3 update steps

updt = [W.assign_add(W_adder), bv.assign_add(bv_adder), bh.assign_add(bh_adder)]

5.接下來,運行 Graph 演算法圖:

5.1先初始化變數
with tf.Session as sess:

#First, we train the model

#initialize the variables of the model

init = tf.initialize_all_variables

sess.run(init)

首先需要 reshape 每首歌,以便於相應的向量表示可以更好地被用於訓練模型。

for epoch in tqdm(range(num_epochs)):

for song in songs:

#The songs are stored in a time x notes format. The size of each song is timesteps_in_song x 2*note_range

#Here we reshape the songs so that each training example is a vector with num_timesteps x 2*note_range elements

song = np.array(song)

song = song[:np.floor(song.shape[0]/num_timesteps)*num_timesteps]

song = np.reshape(song, [song.shape[0]/num_timesteps, song.shape[1]*num_timesteps])

5.2接下來就來訓練 RBM 模型,一次訓練一個樣本
for i in range(1, len(song), batch_size):

tr_x = song[i:i+batch_size]

sess.run(updt, feed_dict={x: tr_x})

模型完全訓練好后,就可以用來生成 music 了。

5.3需要訓練 Gibbs chain

其中的 visible nodes 先初始化為0,來生成一些樣本。

然後把向量 reshape 成更好的格式來 playback。

sample = gibbs_sample(1).eval(session=sess, feed_dict={x: np.zeros((10, n_visible))})

for i in range(sample.shape[0]):

if not any(sample[i,:]):

continue

#Here we reshape the vector to be time x notes, and then save the vector as a midi file

S = np.reshape(sample[i,:], (num_timesteps, 2*note_range))

5.4最後,列印出生成的和弦
midi_manipulation.noteStateMatrixToMidi(S, "generated_chord_{}".format(i))

綜上,就是用 CNN 來參數化地生成音波,

用 RBM 可以很容易地根據訓練數據生成音頻樣本,

Gibbs 演算法可以基於概率分佈幫我們得到訓練樣本。



來聊聊吧

你用得最順手的語言是什麼?

還是已經全棧了?

歡迎在評論區分享

關注 AI 研習社后,回復【1】獲取

▼▼



熱門推薦

本文由 yidianzixun 提供 原文連結

寵物協尋 相信 終究能找到回家的路
寫了7763篇文章,獲得2次喜歡
留言回覆
回覆
精彩推薦