用 TensorFlow 让你的机器人唱首原创给你听

AI 研习社按：这篇文章会用一个简单的模型在 TensorFlow 上来实现一个音频生成器，GitHub 代码链接详见文末“阅读原文”。原文作者杨熹，载于作者的个人博客，雷锋网 AI 研习社经授权发布。

DeepMind 发表了一篇论文，叫做 `WaveNet`, 这篇论文介绍了音乐生成和文字转语音的艺术。

1.引入packages:

```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```

2.定义超参数：

```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```

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 是一种可以从多重概率分布中提取样本的算法。

```#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))```

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)```

```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})```

5.3需要训练 Gibbs chain

```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))`

http://t.cn/RKSbhYm

Github 源码链接：

http://t.cn/RKSbZl1

0 条评论

相关文章

用 TensorFlow 让你的机器人唱首原创给你听

Siraj 的视频 源码 今天想来看看 AI 是怎样作曲的。 本文会用 TensorFlow 来写一个音乐生成器。 当你对一个机器人说：我想要一种能够表达出希...

35660

21350

1K80

42290

36780

9630

93170

30860

13320

41890