用AI生成霉霉(Taylor Swift)的歌词(下)

2019年10月07日 由 sunlei 发表 83460 0
前文回顾:用AI生成霉霉(Taylor Swift)的歌词(上)

建立模型:


第一条路:从头开始


我们将首先确定模型将具有多少层,以及每层将具有多少节点:
LSTM_layer_num = 4 # number of LSTM layers
layer_size = [256,256,256,256] # number of nodes in each layer

定义序列模型:
model = Sequential()

LSTM层与CUDNNLSTM层:


主要区别是LSTM使用CPU,而CuDNNLSTM使用GPU,这就是为什么CuDNNLSTM比LSTM快很多的原因,它比LSTM快x15。

这就是为什么我使用CuDNNLTSM而不是LSTM。

注意:请确保将colab的运行时设置更改为使用其GPU。

添加输入层:
model.add(CuDNNLSTM(layer_size[0], input_shape =(X.shape[1],

X.shape[2]), return_sequences = True))

添加一些隐藏层:
for i in range(1,LSTM_layer_num) :
model.add(CuDNNLSTM(layer_size[i], return_sequences=True))

将来自最后一个隐藏层的数据平铺到输出层:
model.add(Flatten())

添加输出层并将其激活功能定义为“softmax”

然后使用下一个参数编译模型:

  • 损失= ' categorical_crossentropy '

  • 优化器=‘adam’


model.add(Dense(y.shape[1]))
model.add(Activation('softmax'))
model.compile(loss = 'categorical_crossentropy', optimizer = 'adam')

打印模型摘要以查看一些详细信息:
model.summary()



定义模型之后,我们将定义所需的回调。

什么是回调?


回调函数是在每个历元之后调用的函数

在我们的例子中,我们将调用检查点回调,检查点回调所做的是在模型每次变得更好时保存模型的权重。
# Configure the checkpoint :
checkpoint_name = 'Weights-LSTM-improvement-{epoch:03d}-{loss:.5f}-

bigger.hdf5'
checkpoint = ModelCheckpoint(checkpoint_name, monitor='loss', verbose

= 1, save_best_only = True, mode ='min')
callbacks_list = [checkpoint]

训练


一个模型如果不经过训练就一事无成。

就像他们说的“没有耕耘就没有收获”

请随意调整模型参数以获得更好的模型
# Fit the model :
model_params = {'epochs':30,
'batch_size':128,
'callbacks':callbacks_list,
'verbose':1,
'validation_split':0.2,
'validation_data':None,
'shuffle': True,
'initial_epoch':0,
'steps_per_epoch':None,
'validation_steps':None}model.fit(X,
y,
epochs = model_params['epochs'],
batch_size = model_params['batch_size'],
callbacks= model_params['callbacks'],
verbose = model_params['verbose'],
validation_split = model_params['validation_split'],
validation_data = model_params['validation_data'],
shuffle = model_params['shuffle'],
initial_epoch = model_params['initial_epoch'],
steps_per_epoch = model_params['steps_per_epoch'],
validation_steps = model_params['validation_steps'])



 

我们可以看到已经下载了一些文件,我们可以使用这些文件来加载未经训练的模型中使用的训练权重(也就是说,我们不必每次都要训练模型)

如何装载重物?


# Load wights file :
wights_file = './models/Weights-LSTM-improvement-004-2.49538-

bigger.hdf5' # weights file path
model.load_weights(wights_file)
model.compile(loss = 'categorical_crossentropy', optimizer = 'adam')

现在,在我们训练了模型之后,我们可以用它来生成假泰勒·斯威夫特的歌词。

生成歌词


我们首先选择一个随机种子,然后用它来生成歌词的字符。
# set a random seed :
start = np.random.randint(0, len(data_X)-1)
pattern = data_X[start]
print('Seed : ')
print("\"",''.join([int_chars[value] for value in pattern]), "\"\n")# How many characters you want to generate
generated_characters = 300# Generate Charachters :
for i in range(generated_characters):
x = np.reshape(pattern, ( 1, len(pattern), 1))
x = x / float(n_vocab)
prediction = model.predict(x,verbose = 0)
index = np.argmax(prediction)
result = int_chars[index]
#seq_in = [int_chars[value] for value in pattern]
sys.stdout.write(result)
pattern.append(index)
pattern = pattern[1:len(pattern)]
print('\nDone')

输出:


Seed : " once, i've been waiting, waiting ooh whoa, ooh whoa and all at once, you are the one, i have been w " 
eu h mool shoea a eir, bo ly lean on the sast is tigm's the noen uo doy, fo shey stant tas you fot you srart aoo't you tein so my liost i spaye somethppel' cua iy yas tn mu, io' me ohehip in the uorlirs tiines ho a ban't teit dven aester, tee tame mnweiny you'd be pe k bet thing oe eowt the light i Done

您可能会注意到生成的歌词并不真实,并且有许多拼写错误。

您可以调整一些参数,并添加一个Dropout层,以避免过度拟合,然后该模型可以更好地生成可容忍的歌词。但是如果你很懒,不想费心尝试这些步骤,可以尝试使用textgenrnn

第二种方法:使用textgenrnn


导入依赖项:


!pip install -q textgenrnn
from google.colab import files
from textgenrnn import textgenrnn
import os

配置模型:


model_cfg = {
'rnn_size': 500,
'rnn_layers': 12,
'rnn_bidirectional': True,
'max_length': 15,
'max_words': 10000,
'dim_embeddings': 100,
'word_level': False,
}train_cfg = {
'line_delimited': True,
'num_epochs': 100,
'gen_epochs': 25,
'batch_size': 750,
'train_size': 0.8,
'dropout': 0.0,
'max_gen_length': 300,
'validation': True,
'is_csv': False
}

上传数据集:


uploaded = files.upload()
all_files = [(name, os.path.getmtime(name)) for name in os.listdir()]
latest_file = sorted(all_files, key=lambda x: -x[1])[0][0]

培训模型:


model_name = '500nds_12Lrs_100epchs_Model'
textgen = textgenrnn(name=model_name)train_function = textgen.train_from_file if train_cfg['line_delimited'] else textgen.train_from_largetext_filetrain_function(
file_path=latest_file,
new_model=True,
num_epochs=train_cfg['num_epochs'],
gen_epochs=train_cfg['gen_epochs'],
batch_size=train_cfg['batch_size'],
train_size=train_cfg['train_size'],
dropout=train_cfg['dropout'],
max_gen_length=train_cfg['max_gen_length'],
validation=train_cfg['validation'],
is_csv=train_cfg['is_csv'],
rnn_layers=model_cfg['rnn_layers'],
rnn_size=model_cfg['rnn_size'],
rnn_bidirectional=model_cfg['rnn_bidirectional'],
max_length=model_cfg['max_length'],
dim_embeddings=model_cfg['dim_embeddings'],
word_level=model_cfg['word_level'])

 

打印模型摘要:


print(textgen.model.summary())

下载训练重量:


files.download('{}_weights.hdf5'.format(model_name))
files.download('{}_vocab.json'.format(model_name))
files.download('{}_config.json'.format(model_name))

加载训练模型并使用:


textgen = textgenrnn(weights_path='6layers30EpochsModel_weights.hdf5',
vocab_path='6layers30EpochsModel_vocab.json',
config_path='6layers30EpochsModel_config.json')generated_characters = 300textgen.generate_samples(300)
textgen.generate_to_file('lyrics.txt', 300)

 

使用textgenrnn创建的模型生成的一些歌词:

i ' m not your friends
and it rains when you ' re not speaking
but you think tim mcgraw
and i ' m pacing down
i ' m comfortable
i ' m not a storm in mind
you ' re not speaking
and i ' m not a saint and i ' m standin ' t know you ' re
i ' m wonderstruck
and you ' re gay

i ' ve been giving out
but i ' m just another picture to pay
you ' re not asking myself , oh , i ' d go back to december , don ' t know you
it ' s killing me like a chalkboard
it ' s the one you
can ' t you ' re jumping into of you ' re not a last kiss
and i ' m just a girl , baby , i ' m alone for me
i ' m not a little troubling

won ' t you think about a . steps , you roll the stars mind
you ' s killing me ? )
and i ' m say i won ' t stay beautiful at onto the first page
you ' s 2 : pretty
and you said real
?
change makes and oh , who baby , oh , and you talk away
and you ' s all a minute , ghosts your arms page
these senior making me tough , so hello growing up , we were liar , no one someone perfect day when i came
' re not sorry
you ' re an innocent
on the outskirts

ight , don ' t say a house and he ' round
she ' re thinking to december all that baby , all everything now
and let me when you oh , what to come back my dress
always
i close both young before
at ?
yeah

我不是你的朋友

当你不说话的时候,天就下雨了

但你认为是蒂姆·麦格劳

我正在放慢脚步

我很舒服

我心里没有风暴

你没有说话

我不是圣人,我站在这里却不知道你是圣人

我很惊讶

你是同性恋

 

我已经放弃了

但我只是另一张要付钱的照片

你不会问自己,哦,我会回到不认识你的十二月

它就像黑板上的难题一样把我折磨死了。

就是你

你就不能投入这最后一个吻吗

我只是一个女孩,宝贝

我独自一人

我一点也不麻烦你

你不考虑下一步吗

你的心像星星一样忽明忽暗

你是要杀死我吗

我不想美好只停留在这一页

你很美你说是的

谁改变了,你说个不停

你的每一分钟像个幽灵一样

这些经历让我坚强

我们都是骗子,没有人是完美的

你不后悔

你是无辜的

不再回到有他的家

她想用现在的一切交换可以回到那个12月

让我当你不在的时候,把衣服拿回来

我以前都很年轻

……

我们看到使用textgenrnn是多么容易和方便,是的,歌词仍然不现实,但是拼写错误比我们从头开始构建的模型要少得多。

textgenrnn的另一个好处是,它不需要处理任何数据集处理,只需上传文本数据集,然后喝杯咖啡,看着你的模型训练,然后变得更好。

下一个步骤:


现在,在您学习了如何从头开始创建LSTM RNN生成文本,以及如何使用Pyhton模块,如textgenrnn,您可以使用这些知识做很多事情:

  • 尝试使用其他数据集(维基百科文章、莎士比亚小说等)生成小说或文章。

  • 在文本生成之外的其他应用程序中使用LSTM RNN。

  • 阅读更多关于LSTM RNN的信息


参考文献:

用LSTM递归神经网络生成Python中的文本

应用介绍LSTMs与GPU的文本生成

使用LSTM RNN生成文本

textgenrnn

用textgenrnn免费训练一个文本生成神经网络

理解LSTM网络

长短期记忆
欢迎关注ATYUN官方公众号
商务合作及内容投稿请联系邮箱:bd@atyun.com
评论 登录
热门职位
Maluuba
20000~40000/月
Cisco
25000~30000/月 深圳市
PilotAILabs
30000~60000/年 深圳市
写评论取消
回复取消