-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
86 lines (67 loc) · 2.61 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import random
import numpy as np
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM,Dense,Activation
from tensorflow.keras.optimizers import RMSprop
filepath = tf.keras.utils.get_file('shakespeare.txt','https://storage.googleapis.com/download.tensorflow.org/data/shakespeare.txt')
text = open(filepath,'rb').read().decode(encoding='utf-8').lower()
characters = sorted(set(text))
char_to_index = dict((c,i) for i,c in enumerate(characters))
index_to_char = dict((i,c) for i,c in enumerate(characters))
SEQ_LENGTH = 40
STEP_SIZE = 3
"""
# textgenerator.model
sentences = []
next_characters = []
for i in range(0,len(text)-SEQ_LENGTH,STEP_SIZE):
sentences.append(text[i:i+SEQ_LENGTH])
next_characters.append(text[i+SEQ_LENGTH])
x = np.zeros((len(sentences),SEQ_LENGTH,len(characters)),dtype=np.bool_)
y = np.zeros((len(sentences),len(characters)),dtype=np.bool_)
for i ,sentence in enumerate(sentences):
for t,character in enumerate(sentence):
x[i,t,char_to_index[character]] = 1
y[i,char_to_index[next_characters[i]]] = 1
model = Sequential()
model.add(LSTM(128,input_shape = (SEQ_LENGTH,len(characters))))
model.add(Dense(len(characters)))
model.add(Activation('softmax'))
model.compile(loss='categorical_crossentropy',optimizer=RMSprop(lr=0.01))
model.fit(x,y,batch_size=256,epochs=5)
model.save('textgenerator.model')
"""
model = tf.keras.models.load_model('textgenerator.model')
def sample(preds,temperature=1.0):
preds = np.asarray(preds).astype('float64')
preds = np.log(preds) / temperature
exp_preds = np.exp(preds)
preds = exp_preds / np.sum(exp_preds)
probas = np.random.multinomial(1,preds,1)
return np.argmax(probas)
def generate_text(length,temperature):
start_index = random.randint(0,len(text)-SEQ_LENGTH-1)
generated = ''
sentence = text[start_index:start_index + SEQ_LENGTH]
generated += sentence
for i in range(length):
x = np.zeros((1,SEQ_LENGTH,len(characters)))
for t ,character in enumerate(sentence):
x[0,t,char_to_index[character]] = 1
predictions = model.predict(x,verbose=0)[0]
next_index = sample(predictions,temperature)
next_character = index_to_char[next_index]
generated += next_character
sentence = sentence[1:] + next_character
return generated
print('-----0.2-------')
print(generate_text(300,0.2))
print('-----0.4-------')
print(generate_text(300,0.4))
print('-----0.6-------')
print(generate_text(300,0.6))
print('-----0.8-------')
print(generate_text(300,0.8))
print('-----1-------')
print(generate_text(300,1.0))