-
Notifications
You must be signed in to change notification settings - Fork 2
/
generate_INFINITE_RANDOM-SEED.py
executable file
·211 lines (134 loc) · 6.81 KB
/
generate_INFINITE_RANDOM-SEED.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
###############################################################################
# Language Modeling on Penn Tree Bank
#
# This file generates new sentences sampled from the language model
#
###############################################################################
import argparse
import torch
from torch.autograd import Variable
import data
import time
import sys
import math
from random import randint
import random
#############
# TEMPERATURE
#### RANGE ##
MIN_TEMP=0.5
MAX_TEMP=1.0
parser = argparse.ArgumentParser(description='PyTorch Poetry Language Model')
from random import randint
from datetime import datetime
started_datestring = "{0:%Y-%m-%dT%H-%M-%S}".format(datetime.now())
# Model parameters.
parser.add_argument('--data', type=str, default='./data/pf',
help='location of the data corpus')
parser.add_argument('--model', type=str, default='LSTM',
help='type of recurrent net (LSTM, QRNN)')
parser.add_argument('--checkpoint', type=str, default='./model.pt',
help='model checkpoint to use')
parser.add_argument('--outf', type=str, default='GENERATED/generated-'+ started_datestring +'.txt',
help='output file for generated text')
parser.add_argument('--words', type=int, default='1000',
help='number of words to generate')
parser.add_argument('--seed', type=int, default=1111,
help='random seed')
parser.add_argument('--cuda', action='store_true',
help='use CUDA')
parser.add_argument('--temperature', type=float, default=1.0,
help='temperature - higher will increase diversity')
parser.add_argument('--log-interval', type=int, default=100,
help='reporting interval')
args = parser.parse_args()
##########################################
############ DISPLAY ###################
##########################################
# GET TECH DETAILS
# md=args.checkpoint.split("/")[-1]
# style = md.split("-")[1]
# emsize= md.split("-")[3]
# nhid= md.split("-")[4].split("_")[1]
# nlay= md.split("-")[5].split("_")[1]
# bs = md.split("-")[6].split("_")[2]
# ep= md.split("-")[7].split("_")[1]
# loss= md.split("-")[8].split("_")[1]
# ppl= md.split("-")[9].split("_")[1]
det = "\tAveraged Stochastic Gradient Descent \n\twith Weight Dropped QRNN \n\tPoetry Generation \n\n\nTrained on 197,923 lines of poetry & pop lyrics. \n\nPoetry sources: a subset of Poetry Magazine, Jacket2, 2 River, Capa, Evergreen Review, Cathay by Li Bai, Kenneth Patchen, Maurice Blanchot, and previous Rerites.\nLyric sources: Bob Marley, Bob Dylan, David Bowie, Tom Waits, Patti Smith, Radiohead.\n\n+Tech-terminology source: jhavelikes.tumblr.com, \n\n\n+~+Library: PyTorch (word-language-model modified by Salesforce Research)+~+\n\nMode: QRNN\nEmbedding size: 400\nHidden Layers: 1550\nBatch size: 20\nEpoch: 478\nLoss: 3.62\nPerplexity: 37.16\n\nTemperature range: "+str(MIN_TEMP)+" to "+str(MAX_TEMP)
print("\n\n\n\n"+det)
print("\n\nSystem will generate "+str(args.words)+" word bursts, perpetually, until stopped.")
print ("\nInitializing.Please be patient.\n\nSYSTEM OUTPUT : REAL-TIME generation on TitanX GPU \nre-loading model every poem \nfresh with a new RANDOM SEED.\n")
#############################################
########### INFINITE LOOP #################
#############################################
while(True):
# Set the random seed RANDOMLY for UNreproducibility.
args.seed=randint(0,9999999999)
torch.manual_seed(args.seed)
if torch.cuda.is_available():
if not args.cuda:
print("WARNING: You have a CUDA device, so you should probably run with --cuda")
else:
torch.cuda.manual_seed(args.seed)
if args.temperature < 1e-3:
parser.error("--temperature has to be greater or equal 1e-3")
with open(args.checkpoint, 'rb') as f:
model = torch.load(f)
model.eval()
if args.model == 'QRNN':
model.reset()
if args.cuda:
model.cuda()
else:
model.cpu()
corpus = data.Corpus(args.data)
ntokens = len(corpus.dictionary)
hidden = model.init_hidden(1)
input = Variable(torch.rand(1, 1).mul(ntokens).long(), volatile=True)
if args.cuda:
input.data = input.data.cuda()
# SLEEP time.sleep(5)
print ("\n\n\n\t\t~ + ~\n\n")
#torch.manual_seed(randint(0,9999999999))
words=''
###########################################
######### RANDOM TEMPERATURE ##############
args.temperature = random.uniform(MIN_TEMP, MAX_TEMP)
with open(args.outf, 'a') as outf:
for i in range(args.words):
output, hidden = model(input, hidden)
word_weights = output.squeeze().data.div(args.temperature).exp().cpu()
word_idx = torch.multinomial(word_weights, 1)[0]
input.data.fill_(word_idx)
if word_idx<=len(corpus.dictionary.idx2word)-1:
word = corpus.dictionary.idx2word[word_idx]
if word == '<eos>':
word = '\n'
if word == '&':
word = '\n'
words+=word+" "
#outf.write(word + ('\n' if i % 20 == 19 else ' '))
# Output how many created so far
print(' {}/{} words'.format(i+1, args.words), end='\r')
titl = words.split('\n', 1)[0].title()
#erase the output '88/88 words' line
print(' ', end='\r')
maxl=75
for li in words.splitlines():
if len(li)>maxl:
words = "\n".join(words.splitlines()[1:])
break
words = words.replace(" \n","\n")
words = words.replace("\r","\n")
words = words.replace("\n\n\n\n\n","\n\n")
words = words.replace("\n\n\n\n","\n")
words = words.replace("\n\n\n","\n")
# SCREEN OUTPUT
for char in words:
#time.sleep(0.01)
sys.stdout.write(char)
print("\n\n\n\t\t\t\tTemperature= "+ str(math.ceil(args.temperature*100)/100)+"\tSeed:"+str(args.seed))
words+="\n\n\n\t\tTemperature="+ str(math.ceil(args.temperature*100)/100)+"\tSeed:"+str(args.seed)+"\n\n\n\t\t\t\t~ + ~\n\n\n"#--------------------------------------------------------------------------------------------\nGenerated on : "+str(started_datestring)+"\n--------------------------------------------------------------------------------------------\n\nTech details\n------------- \n\nInfo: http://bdp.glia.ca/\nCode: https://github.com/jhave/pytorch-poetry-generation\n\n"+det+"\n\n--------------------------------------------------------------------------------------------\n"+args.checkpoint
outf.write(words)
outf.close()