-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·48 lines (40 loc) · 1.31 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
#!/usr/bin/env python3
import torch
import sys
from model import Model
from data import DataSet, one_hot
from random import choice
batch_size = 150
def output(startChar, length, model, dataset):
def fakeDataset(character):
x = torch.zeros(batch_size, 1, len(dataset.corpus))
x[0][0] = one_hot(dataset.getLabelFromChar(character), len(dataset.corpus))
y = None
return [x,y]
def getChar(x):
label = list(x[0])
label = label.index(max(label))
char = dataset.getCharFromLabel(label)
return char
x = fakeDataset(startChar)
outputString = startChar
while len(outputString) < length:
x = model.train(x , adjust=False)
c = getChar(x)
outputString += c
x = fakeDataset(c)
return outputString
def main():
dataset = DataSet('Datafiles/tiny-shakespeare.txt')
corpus = dataset.corpus
model = Model(len(corpus), batch_size)
i = 0
while True:
for j, datapoint in enumerate(dataset.sequentialSampler(batch_size)):
if datapoint[0].size(0) == batch_size:
model.train(datapoint)
if i % 1500 == 0 and i > 0:
print(output(choice(corpus), 1000, model, dataset),
file=sys.stderr)
i += 1
main()