# 用 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 算法可以基于概率分布帮我们得到训练样本。

## tensorflow1.x版本rnn生成cell 报错解决方案

tensorflow1.x版本叫以前有很大改动，那个rnn的cell和别的一些地方有了作用域，具体可以看官网，下面是报错和解决办法。 ValueError: Variable hello/rnn/ba...
• u014283248
• 2017年03月21日 12:50
• 9531

## tensorflow 变量生成 变量管理 tf.Variable & tf.get_variable & tf.variable_scope

____tz_zs学习笔记 tf.Variable 官网api：https://www.tensorflow.org/api_docs/python/tf/Variable def __...
• tz_zs
• 2017年07月13日 17:27
• 1514

## TensorFlow 从入门到精通（六）：tensorflow.nn 详解

• kkk584520
• 2016年05月23日 14:30
• 50310

## bugku ctf 听首音乐 wirteup

• pdsu161530247
• 2017年07月06日 19:11
• 1273

## 【bzoj2141】排队

Description排排坐，吃果果，生果甜嗦嗦，大家笑呵呵。你一个，我一个，大的分给你，小的留给我，吃完果果唱支歌，大家乐和和。红星幼儿园的小朋友们排起了长长地队伍，准备吃果果。不过因为小朋友们的身...
• w_yqts
• 2017年05月05日 21:16
• 108

## tf生成随机数

tf.random_normal 从正态分布输出随机值。 [python] view plain copy print? random_normal(shap...
• Touch_Dream
• 2017年11月13日 14:12
• 182

## Tensorflow 自动文摘: 基于Seq2Seq+Attention模型的Textsum模型

Github下载完整代码 https://github.com/rockingdingo/deepnlp/tree/master/deepnlp/textsum 简介 ...
• c2a2o2
• 2018年01月23日 15:07
• 90

## 从我失败的“处女面”谈谈面试感受

• shine_of_sun
• 2014年04月18日 12:55
• 1359

## TensorFlow 聊天机器人

• u014365862
• 2017年02月26日 22:15
• 2229

## seq2seq.py

• sjqyjs
• 2017年11月23日 11:31
• 58

举报原因： 您举报文章：用 TensorFlow 让你的机器人唱首原创给你听 色情 政治 抄袭 广告 招聘 骂人 其他 (最多只允许输入30个字)