1. 程式人生 > >吳恩達 Coursera Deep Learning 第五課 Sequence Models 第一週程式設計作業 3

吳恩達 Coursera Deep Learning 第五課 Sequence Models 第一週程式設計作業 3

Improvise a Jazz Solo with an LSTM Network

Welcome to your final programming assignment of this week! In this notebook, you will implement a model that uses an LSTM to generate music. You will even be able to listen to your own music at the end of the assignment.

You will learn to:

  • Apply an LSTM to music generation.
  • Generate your own jazz music with deep learning.

Please run the following cell to load all the packages required in this assignment. This may take a few minutes.

from __future__ import print_function
import IPython
import sys
from music21 import *
import numpy as np
from grammar import *
from qa import *
from preprocess import * 
from music_utils import *
from data_utils import *
from keras.models import load_model, Model
from keras.layers import Dense, Activation, Dropout, Input, LSTM, Reshape, Lambda, RepeatVector
from keras.initializers import glorot_uniform
from keras.utils import to_categorical
from keras.optimizers import Adam
from keras import backend as K

1 - Problem statement

You would like to create a jazz music piece specially for a friend's birthday. However, you don't know any instruments or music composition. Fortunately, you know deep learning and will solve this problem using an LSTM netwok.

You will train a network to generate novel jazz solos in a style representative of a body of performed work.



1.1 - Dataset

You will train your algorithm on a corpus of Jazz music. Run the cell below to listen to a snippet of the audio from the training set:

We have taken care of the preprocessing of the musical data to render it in terms of musical "values." You can informally think of each "value" as a note, which comprises a pitch and a duration. For example, if you press down a specific piano key for 0.5 seconds, then you have just played a note. In music theory, a "value" is actually more complicated than this--specifically, it also captures the information needed to play multiple notes at the same time. For example, when playing a music piece, you might press down two piano keys at the same time (playng multiple notes at the same time generates what's called a "chord"). But we don't need to worry about the details of music theory for this assignment. For the purpose of this assignment, all you need to know is that we will obtain a dataset of values, and will learn an RNN model to generate sequences of values.

Our music generation system will use 78 unique values. Run the following code to load the raw music data and preprocess it into values. This might take a few minutes.

X, Y, n_values, indices_values = load_music_utils()
print('shape of X:', X.shape)
print('number of training examples:', X.shape[0])
print('Tx (length of sequence):', X.shape[1])
print('total # of unique values:', n_values)
print('Shape of Y:', Y.shape)

You have just loaded the following:

  • X: This is an (m, TxTx, 78) dimensional array. We have m training examples, each of which is a snippet of Tx=30Tx=30 musical values. At each time step, the input is one of 78 different possible values, represented as a one-hot vector. Thus for example, X[i,t,:] is a one-hot vector representating the value of the i-th example at time t.

  • Y: This is essentially the same as X, but shifted one step to the left (to the past). Similar to the dinosaurus assignment, we're interested in the network using the previous values to predict the next value, so our sequence model will try to predict yty⟨t⟩ given x1,,xtx⟨1⟩,…,x⟨t⟩. However, the data in Y is reordered to be dimension (Ty,m,78)(Ty,m,78), where Ty=TxTy=Tx. This format makes it more convenient to feed to the LSTM later.

  • n_values: The number of unique values in this dataset. This should be 78.

  • indices_values: python dictionary mapping from 0-77 to musical values.

1.2 - Overview of our model

Here is the architecture of the model we will use. This is similar to the Dinosaurus model you had used in the previous notebook, except that in you will be implementing it in Keras. The architecture is as follows:

We will be training the model on random snippets of 30 values taken from a much longer piece of music. Thus, we won't bother to set the first input 

x1=0⃗ x⟨1⟩=0→, which we had done previously to denote the start of a dinosaur name, since now most of these snippets of audio start somewhere in the middle of a piece of music. We are setting each of the snippts to have the same length Tx=30Tx=30 to make vectorization easier.

2 - Building the model

In this part you will build and train a model that will learn musical patterns. To do so, you will need to build a model that takes in X of shape (m,Tx,78)(m,Tx,78) and Y of shape (Ty,m,78)(Ty,m,78). We will use an LSTM with 64 dimensional hidden states. Lets set n_a = 64.

Here's how you can create a Keras model with multiple inputs and outputs. If you're building an RNN where even at test time entire input sequence x1,x2,,xTxx⟨1⟩,x⟨2⟩,…,x⟨Tx⟩ were given in advance, for example if the inputs were words and the output was a label, then Keras has simple built-in functions to build the model. However, for sequence generation, at test time we don't know all the values of xtx⟨t⟩ in advance; instead we generate them one at a time using xt=yt

相關推薦

Coursera Deep Learning Sequence Models 第一程式設計作業 3

Improvise a Jazz Solo with an LSTM Network Welcome to your final programming assignment of this week! In this notebook, you will im

Coursera Deep Learning 卷積神經網路 程式設計作業: Convolutional Model: Application

Convolutional Neural Networks: Application Welcome to Course 4’s second assignment! In this notebook, you will: Implement helper

Deeplearning.ai Sequence Model 第一------Deep RNNs

這一節主要講解了深度RNN網路的結構。 左邊是在一般的神經網路中DNN的結構,由輸入經過多層網路最終得到輸出 與此類似,Deep RNN也有類似的結構,之前的RNN網路都只是一層,如圖畫出了三層。 用a[l]<t>來表示第l層激勵的第t個t

Deeplearning.ai Sequence Model 第一------Sampling novel sequence

這一節主要講了如何從一個訓練好的RNN中進行取樣得到序列 從訓練好的RNN中取樣出一個序列 之前在序列產生中講到,首先RNN輸入a<0>(0向量)和x<1>(0向量),通過一個RNN cell產生一個輸出y。 y = softm

v2 老師深度學習第二程式設計作業2

吳恩達老師深度學習第五課第二週程式設計作業2,包含答案! Emojify! Welcome to the second assignment of Week 2. You are going to use word vector representation

Deeplearning.ai Sequence Model 第一------Recurrent Neural Network Model

這一節內容比較多,主要講述瞭如何搭建一個RNN標準單元 使用標準神經網路的不足: 1.不同樣本的輸入輸出長度不等(雖然都可以padding成最大長度的樣本) 2.(更主要的原因)text不同的位置之間不共享學習到的引數 RNN模型,可以用左邊也可

Deeplearning.ai Sequence Model 第一------Long Short Term Memory(LSTM)

這一節主要講解了LSTM單元 LSTM和GRU略有區別,可以說是一種更加通用的GRU模型 在LSTM中,c<t>不再等於a<t>,因此原來公式中的c<t-1>要改成a<t-1>,同時在LSTM中,也沒有了Γ

Deeplearning.ai Sequence Model 第一------Backpropagation through time

這一節主要講了RNN的反向傳播過程 BP即從輸出到輸入,因此和FP是相反的一些箭頭 計算a的引數Wa和ba,計算y的引數Wy和by,綠色的箭頭表示在這些不同的a和y的計算中都要用到這些引數(這裡展示的是一層RNN,後面會講到多層,在這一層中,不同的c

Coursera機器學習課程-

五、Octave教程(Octave Tutorial) 5.1 基本操作 參考視訊: 5 - 1 - Basic Operations (14 min).mkv 在這段視訊中,我將教你一種程式語言:Octave語言。你能夠用它來非常迅速地實現這門課中我們已經學過的,或者

機器學習(章)--特徵縮放和學習率

一、特徵縮放  ----(1) 對於我們假設的式子(1),可能存在這樣一種情況就是有些資料遠大於另一些資料(eg:x_1>>x_2) 比如房子價格的例子: 房子的面積要遠大於房子的層數和房間數。在這種情況下可以看下圖,所產生的等高線的圈會很窄,在做梯度下降

Coursera Deep Learning 卷積神經網路 第二 程式設計作業 殘差神經網路 Residual Networks

Coursera Deep Learning 第四課 卷積神經網路 第二週 程式設計作業 殘差神經網路 Residual Networks Welcome to the second assignment of this week! You will l

Coursera Deep Learning 2 Improving Deep Neural Networks 第一 程式設計作業程式碼 Regularization

2 - L2 Regularization # GRADED FUNCTION: compute_cost_with_regularization def compute_cost_with_reg

Coursera Deep Learning 2 Improving Deep Neural Networks 第一 程式設計作業程式碼 Initialization

2 - Zero initialization # GRADED FUNCTION: initialize_parameters_zeros def initialize_parameters_z

Coursera--機器學習-(5周筆記)Neural Networks——Learning

Week 5 —— Neural Networks : Learning 目錄 一代價函式和反向傳播 1-1 代價函式 首先定義一些我們需要使用的變數: L =網路中的總層數 sl =第l層中的單位數量(不

Coursera DeepLearning.AI sequence model 序列模型 第一 Improvise a Jazz Solo with an LSTM Network

We have taken care of the preprocessing of the musical data to render it in terms of musical "values." You can informally think of each "value" as a note,

-coursera-機器學習測試題

      如果我們使用梯度下降作為我們的優化演算法,那麼梯度檢查是很有用的。但是,如果我們使用的是高階優化方法之一(比如fminunc),它幾乎沒有什麼作用。   假設我們有一個正確的反向傳播的實現,並且使用梯度下降來訓練一個神經網路。假設我們

Coursera深度學習課程】 Neural Networks and Deep Learning 第一課後習題

因為這學期選了深度學習的課程,所以也會自學一下相關內容,在網上選擇了吳恩達的深度學習來進行學習,首先是第一門課程,Neural Networks and Deep Learning。第一週的內容是深度學習概論,課後習題為十個選擇題,個人觀點,僅供參考,歡迎指正。這題問為什麼稱

Coursera DeepLearning.AI sequence model 序列模型 第二 Emofify

這個Emojify裡最坑的一個就是,avg初始化的時候一定要是 (50,) ,如果你用(word_to_vec_map["a"]).shape 就死活過不了。Emojify!Welcome to the second assignment of Week 2. You are going to use wor

Coursera-AndrewNg()機器學習筆記——三周

訓練 ros 方便 font 就是 梯度下降 全局最優 用法 郵件 一.邏輯回歸問題(分類問題) 生活中存在著許多分類問題,如判斷郵件是否為垃圾郵件;判斷腫瘤是惡性還是良性等。機器學習中邏輯回歸便是解決分類問題的一種方法。二分類:通常表示為y?{0,1},0:“Negat

機器學習(章)---降維PCA

一、目標 1.資料壓縮 在機器學習中,會用到大量資料進行學習訓練,當資料量很大,維度高時,對電腦記憶體的壓力會很大,並且學習的速度也會很慢。 2.視覺化 我們將一些高維的資料降維到1維,2維,3維的話,就可以進行視覺化,將資料以圖表的方式展示出來。 二、主成分分析方法 主成分