-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
80 lines (53 loc) · 2.17 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
import time
import sys
import random as rnd
import matplotlib
import matplotlib.pyplot as plt
from src.twitterBot import *
sleepTimes = [ 1800, 3600, 7200, 14400 ]
batchSize = 256
nh = 256
nl = 3
do = 0.25
rdo = 0.25
nEpochs = 3000
matplotlib.rcParams.update({'font.size': 24, 'text.usetex': True})
def plotLosses( losses ):
"""Plots training loss as a fucntion of epoch."""
fig = plt.figure(1, figsize = (18,10))
plt.plot( range(1, len(losses["loss"]) + 1), losses["loss"], "b-",
linewidth = 3, label = "$\mathrm{training}$")
plt.plot( range(1, len(losses["val_loss"]) + 1), losses["val_loss"], "g-",
linewidth = 3, label = "$\mathrm{validation}$")
plt.ylabel("$\mathrm{Loss}$")
plt.xlabel("$\mathrm{Epoch}$")
plt.legend( loc = "best" )
fig.savefig( "lossPlot.eps", format = 'eps', dpi = 20000, bbox_inches='tight' )
return
if __name__ == "__main__":
assert len(sys.argv) == 3, "Invalid command line arguments."
if (sys.argv[1] == "train"):
bot = TwitterBot( sys.argv[2], True, nHidden = nh, numLayers = nl,
dropout = do, recurrent_dropout = rdo )
print( bot.model.summary() )
#Uncomment to continue training:
#bot.load( "./weights/trained.hdf5" )
losses = bot.trainBot( batchSize = batchSize, nEpochs = nEpochs,
savePath = "./weights/trained.hdf5" )
plotLosses( losses )
elif (sys.argv[1] == "tweet"):
bot = TwitterBot( sys.argv[2], False, nHidden = nh, numLayers = nl )
bot.load( "./weights/trained.hdf5" )
while (True):
tweet = bot.genTweet()
bot.sendTweet( tweet )
print( "\n\nPosted tweet:\n ", tweet )
time.sleep( rnd.choice(sleepTimes) )
elif (sys.argv[1] == "test"):
bot = TwitterBot( sys.argv[2], False, nHidden = nh, numLayers = nl )
print (bot.model.summary() )
bot.load( "./weights/trained.hdf5" )
for _ in range(5):
print( "\n\nSample tweet:\n ", bot.genTweet( rnd.random() ), "\n" )
else:
print ( "Invalid argument, must be either 'tweet', 'train', or 'test'." )