資源簡介
基于深度學習生成音樂(mid格式的音樂) 附代碼,絕對可用,我自己調試過,python3環(huán)境

代碼片段和文件信息
‘‘‘
Author:?????Ji-Sung?Kim
Project:????deepjazz
Purpose:????Generate?jazz?using?a?deep?learning?model?(LSTM?in?deepjazz).
Some?code?adapted?from?Evan?Chow‘s?jazzml?https://github.com/evancchow/jazzml?
with?express?permission.
Code?was?built?while?significantly?referencing?public?examples?from?the
Keras?documentation?on?GitHub:
https://github.com/fchollet/keras/blob/master/examples/lstm_text_generation.py
GPU?run?command:
????THEANO_FLAGS=mode=FAST_RUNdevice=gpufloatX=float32?python?generator.py?[#?of?epochs]
????Note:?running?Keras/Theano?on?GPU?is?formally?supported?for?only?NVIDIA?cards?(CUDA?backend).
‘‘‘
from?__future__?import?print_function
import?sys
from?music21?import?*
import?numpy?as?np
from?grammar?import?*
from?preprocess?import?*
from?qa?import?*
import?lstm
#----------------------------HELPER?FUNCTIONS----------------------------------#
‘‘‘?Helper?function?to?sample?an?index?from?a?probability?array?‘‘‘
def?__sample(a?temperature=1.0):
????a?=?np.log(a)?/?temperature
????#?a?=?np.exp(a)?/?np.sum(np.exp(a))
????#?return?np.argmax(np.random.multinomial(1?a?1))
????dist?=?np.exp(a)/np.sum(np.exp(a))
????choices?=?range(len(a))
????return?np.random.choice(choices?p=dist)
‘‘‘?Helper?function?to?generate?a?predicted?value?from?a?given?matrix?‘‘‘
def?__predict(model?x?indices_val?diversity):
????preds?=?model.predict(x?verbose=0)[0]
????next_index?=?__sample(preds?diversity)
????next_val?=?indices_val[next_index]
????return?next_val
‘‘‘?Helper?function?which?uses?the?given?model?to?generate?a?grammar?sequence?
????from?a?given?corpus?indices_val?(mapping)?abstract_grammars?(list)?
????and?diversity?floating?point?value.?‘‘‘
def?__generate_grammar(model?corpus?abstract_grammars?values?val_indices
???????????????????????indices_val?max_len?max_tries?diversity):
????curr_grammar?=?‘‘
????#?np.random.randint?is?exclusive?to?high
????start_index?=?np.random.randint(0?len(corpus)?-?max_len)
????sentence?=?corpus[start_index:?start_index?+?max_len]????#?seed
????running_length?=?0.0
????while?running_length?<=?4.1:????#?arbitrary?from?avg?in?input?file
????????#?transform?sentence?(previous?sequence)?to?matrix
????????x?=?np.zeros((1?max_len?len(values)))
????????for?t?val?in?enumerate(sentence):
????????????if?(not?val?in?val_indices):?print(val)
????????????x[0?t?val_indices[val]]?=?1.
????????next_val?=?__predict(model?x?indices_val?diversity)
????????#?fix?first?note:?must?not?have?>?and?not?be?a?rest
????????if?(running_length?0.00001):
????????????tries?=?0
????????????while?(next_val.split(‘‘)[0]?==?‘R‘?or?
????????????????len(next_val.split(‘‘))?!=?2):
????????????????#?give?up?after?1000?tries;?random?from?input‘s?first?notes
????????????????if?tries?>=?max_tries:
????????????????????print(‘Gave?up?on?first?note?generation?after‘?max_tries?
????????????????????????‘tries‘)
????????????????????#?np.random?is?exclusive?to?high
????????????????????rand?=?np.random.randint(0?len(abstrac
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2018-10-28?23:30??deepjazz\
?????文件?????????862??2017-08-02?09:43??deepjazz\.gitignore
?????文件???????11357??2017-08-02?09:43??deepjazz\LICENSE
?????文件?????????309??2017-08-02?09:43??deepjazz\NOTICE
?????文件????????2368??2017-08-02?09:43??deepjazz\README.md
?????目錄???????????0??2018-10-28?22:42??deepjazz\__pycache__\
?????文件????????7182??2018-10-28?22:42??deepjazz\__pycache__\grammar.cpython-36.pyc
?????文件????????1624??2018-10-28?22:11??deepjazz\__pycache__\lstm.cpython-36.pyc
?????文件????????3983??2018-10-28?22:30??deepjazz\__pycache__\preprocess.cpython-36.pyc
?????文件????????2131??2018-10-28?22:27??deepjazz\__pycache__\qa.cpython-36.pyc
?????文件????????6999??2018-10-28?23:30??deepjazz\generator.py
?????文件???????15244??2018-10-28?22:41??deepjazz\grammar.py
?????文件????????1815??2017-08-02?09:43??deepjazz\lstm.py
?????目錄???????????0??2018-10-28?23:47??deepjazz\midi\
?????文件????????4320??2018-10-28?23:33??deepjazz\midi\deepjazz_on_metheny...128_epochs.midi
?????文件???????92549??2017-08-02?09:43??deepjazz\midi\original_metheny.mid
?????文件????????5629??2018-10-28?22:30??deepjazz\preprocess.py
?????文件????????2861??2018-10-28?22:27??deepjazz\qa.py
評論
共有 條評論