文本生成的简单demo

import numpy as np
import pandas as pd

import numpy as np
import pandas as pd
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import LSTM
from keras.layers import RNN
from keras.utils import np_utils

text=(open("./sonnets.txt").read())
text=text.lower()

characters = sorted(list(set(text)))

n_to_char = {n:char for n, char in enumerate(characters)}
char_to_n = {char:n for n, char in enumerate(characters)}

X = []
Y = []
length = len(text)
seq_length = 100

for i in range(0, length-seq_length, 1):    #sequence_lengt的长度设置为100,步长设置为1
    sequence = text[i:i + seq_length]       #取不到i + seq_length,这里作为label
    label =text[i + seq_length]             #label是sequence的后一个char
    X.append([char_to_n[char] for char in sequence]) #进行编码
    Y.append(char_to_n[label])

X_modified = np.reshape(X, (len(X), seq_length, 1))
X_modified = X_modified / float(len(characters))
Y_modified = np_utils.to_categorical(Y)

model = Sequential()
model.add(LSTM(400, input_shape=(X_modified.shape[1], X_modified.shape[2]), return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(400))
model.add(Dropout(0.2))
model.add(Dense(Y_modified.shape[1], activation='softmax'))

model.compile(loss='categorical_crossentropy', optimizer='adam')

model.fit(X_modified, Y_modified, epochs=1, batch_size=100)

model.save_weights('/Users/pranjal/Desktop/text_generator/models/text_generator_400_0.2_400_0.2_baseline.h5')

model.load_weights('/Users/pranjal/Desktop/text_generator/models/text_generator_400_0.2_400_0.2_baseline.h5')

string_mapped = X[99]                                      #取一段句子
full_string = [n_to_char[value] for value in string_mapped]#将句子转化为char,作为最后的输出
# generating characters
for i in range(400):                                       #预测400个char
    x = np.reshape(string_mapped,(1,len(string_mapped), 1))
    x = x / float(len(characters))

    pred_index = np.argmax(model.predict(x, verbose=0))    #取预测最大的值的索引
    #seq = [n_to_char[value] for value in string_mapped]   #临时的char级别的句子,将后面的预测输出加入到句子中去
    full_string.append(n_to_char[pred_index])              #将预测值加入到输入中,递归进行token的预测

    string_mapped.append(pred_index)
    string_mapped = string_mapped[1:len(string_mapped)]    #保持输入的长度不变

#combining text
txt=""#打印预测出来的400个char
for char in full_string:
    txt = txt+char
txt
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值