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

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

#### 1.引入packages:

import numpy as np
import pandas as pd
import msgpack
import 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 index of the lowest note on the piano roll
highest_note = midi_manipulation.upperBound #the index of 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 create at a time
n_visible      = 2*note_range*num_timesteps #This is the size of the visible layer.
n_hidden       = 50 #This is the size of the hidden layer

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 number of 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 用来存储权重矩阵，或者叫做两层之间的关系

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

#### 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 算法图：

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

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


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

##### 4.最后，打印出生成的和弦
       midi_manipulation.noteStateMatrixToMidi(S, "generated_chord_{}".format(i))


Gibbs 算法可以基于概率分布帮我们得到训练样本。

#### 【深度学习】写诗机器人tensorflow实现

2017-06-29 14:51:02

#### 用 TensorFlow 做个聊天机器人

2017-05-01 11:49:58

#### 用 Tensorflow 建立 CNN

2017-05-01 11:58:20

#### 一文学会用 Tensorflow 搭建神经网络

2017-04-26 10:48:55

#### TensorFlow 入门

2017-04-26 10:50:24

#### 用 TensorFlow 创建自己的 Speech Recognizer

2017-04-26 11:06:37

#### 深度学习动手实践：用 TensorFlow 打造“会看”的机器人

2016-10-21 10:41:20

#### 利用tensorflow制作一个简单的聊天机器人

2016-12-22 16:22:12

#### 把照片唱给你听 | 腾讯AI Lab国际领先技术邀你「趣」体验

2017-10-17 00:00:00

#### 用 TensorFlow 让机器人唱首歌给你听

2018-04-20 00:00:00