forked from daniel-kukiela/nmt-chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
90 lines (69 loc) · 3.22 KB
/
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import sys
import os
import argparse
from setup.settings import hparams, preprocessing
import math
sys.path.append(os.path.dirname(os.path.realpath(__file__)))
sys.path.append(os.path.dirname(os.path.realpath(__file__)) + "/nmt")
from nmt import nmt
import tensorflow as tf
import colorama
from threading import Thread
from setup.custom_summary import custom_summary
colorama.init()
def train():
print('\n\n{}Training model...{}\n'.format(colorama.Fore.GREEN, colorama.Fore.RESET))
# Custom epoch training and decaying
if preprocessing['epochs'] is not None:
# Load corpus size, calculate number of steps
with open('{}/corpus_size'.format(preprocessing['train_folder']), 'r') as f:
corpus_size = int(f.read())
# Load current train progress
try:
with open('{}epochs_passed'.format(hparams['out_dir']), 'r') as f:
initial_epoch = int(f.read())
except:
initial_epoch = 0
# Iterate thru epochs
for epoch, learning_rate in enumerate(preprocessing['epochs']):
# Check if model already passed that epoch
if epoch < initial_epoch:
print('{}Epoch: {}, learning rate: {} - already passed{}'.format(colorama.Fore.GREEN, epoch + 1, learning_rate, colorama.Fore.RESET))
continue
# Calculate new number of training steps - up to the end of current epoch
num_train_steps = math.ceil((epoch + 1) * corpus_size / (hparams['batch_size'] if 'batch_size' in hparams else 128))
print("\n{}Epoch: {}, steps per epoch: {}, epoch ends at {} steps, learning rate: {} - training{}\n".format(
colorama.Fore.GREEN,
epoch + 1,
math.ceil(corpus_size / (hparams['batch_size'] if 'batch_size' in hparams else 128)),
num_train_steps,
learning_rate,
colorama.Fore.RESET
))
# Override hparams
hparams['num_train_steps'] = num_train_steps
hparams['learning_rate'] = learning_rate
hparams['override_loaded_hparams'] = True
# Run TensorFlow threaded (exits on finished training, but we want to train more)
thread = Thread(target=nmt_train)
thread.start()
thread.join()
# Save epoch progress
with open('{}epochs_passed'.format(hparams['out_dir']), 'w') as f:
f.write(str(epoch + 1))
# Standard training
else:
nmt_train()
print('\n\n{}Training finished{}\n'.format(colorama.Fore.GREEN, colorama.Fore.RESET))
def nmt_train():
# Modified autorun from nmt.py (bottom of the file)
# We want to use original argument parser (for validation, etc)
nmt_parser = argparse.ArgumentParser()
nmt.add_arguments(nmt_parser)
# But we have to hack settings from our config in there instead of commandline options
nmt.FLAGS, unparsed = nmt_parser.parse_known_args(['--'+k+'='+str(v) for k,v in hparams.items()])
# Add custom summary function (hook)
nmt.summary_callback = custom_summary
# And now we can run TF with modified arguments
tf.app.run(main=nmt.main, argv=[os.getcwd() + '\nmt\nmt\nmt.py'] + unparsed)
train()