-
Notifications
You must be signed in to change notification settings - Fork 5
/
lstm_train.py
75 lines (65 loc) · 2.63 KB
/
lstm_train.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
from lstm_model import build_graph
import numpy as np
import tensorflow as tf
from config import *
from dataset import batches
session = tf.InteractiveSession()
graph = build_graph(BATCH_STRING_LENGTH)
init = tf.global_variables_initializer() # returns operation
session.run(init)
# run through text multiple times, and run through batches itself.
def run_batch(
initial_state1,
initial_output1,
initial_state2,
initial_output2,
initial_char,
text):
result = session.run({
#will evaluate the operations or the values of the tensors
"_": graph["train_step"],
"total_ce": graph["total_ce"],
"total_accuracy": graph["total_accuracy"],
"final_state1": graph["final_state1"],
"final_output1": graph["final_output1"],
"final_state2": graph["final_state2"],
"final_output2": graph["final_output2"],
},
feed_dict={
#key is the tensor, value is the numbers to set the tensor to
graph["initial_state1"]: initial_state1,
graph["initial_output1"]: initial_output1,
graph["initial_state2"]: initial_state2,
graph["initial_output2"]: initial_output2,
graph["initial_char"]: initial_char,
graph["text"]: text})
# now we get out the numpy arrays for the tensors in result
return result
def run_epoch(epoch_idx):
initial_state1 = np.zeros([NUM_SUBTEXTS, NUM_STATE1_UNITS])
initial_output1 = np.zeros([NUM_SUBTEXTS, NUM_STATE1_UNITS])
initial_state2 = np.zeros([NUM_SUBTEXTS, NUM_STATE2_UNITS])
initial_output2 = np.zeros([NUM_SUBTEXTS, NUM_STATE2_UNITS])
initial_char = np.zeros([NUM_SUBTEXTS, NUM_CHARS])
for (batch_idx, batch) in enumerate(batches):
result = run_batch(
initial_state1,
initial_output1,
initial_state2,
initial_output2,
initial_char,
batch)
initial_state1 = result["final_state1"]
initial_output1 = result["final_output1"]
initial_state2 = result["final_state2"]
initial_output2 = result["final_output2"]
initial_char = batch[:, -1, :]
print(f'loss: {result["total_ce"]}')
print(f'accuracy: {result["total_accuracy"]}')
print(f'batch: {batch_idx}, epoch: {epoch_idx}')
saver = tf.train.Saver()
saver.restore(session, "./lstm_checkpoints/model-46")
for i in range(47, 100):
run_epoch(i)
#save the model
saver.save(session, "./lstm_checkpoints/model", global_step=i)