forked from USGS-R/river-dl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Snakefile_pretrain_LSTM.smk
211 lines (187 loc) · 7.13 KB
/
Snakefile_pretrain_LSTM.smk
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
import os
import tensorflow as tf
import numpy as np
code_dir = config['code_dir']
# if using river_dl installed with pip this is not needed
import sys
sys.path.insert(1, code_dir)
from river_dl.preproc_utils import asRunConfig
from river_dl.preproc_utils import prep_all_data
from river_dl.evaluate import combined_metrics
from river_dl.postproc_utils import plot_obs
from river_dl.predict import predict_from_io_data
from river_dl.train import train_model
from river_dl import loss_functions as lf
from river_dl.tf_models import LSTMModel
out_dir = config['out_dir']
loss_function = lf.multitask_rmse(config['lambdas'])
pred_weights = config['pred_weights']
rule all:
input:
expand("{outdir}/{metric_type}_metrics.csv",
outdir=out_dir,
metric_type=['overall', 'month', 'reach', 'month_reach'],
),
expand("{outdir}/asRunConfig.yml", outdir=out_dir)
rule as_run_config:
output:
"{outdir}/asRunConfig.yml"
run:
asRunConfig(config, code_dir, output[0])
rule prep_io_data:
input:
config['sntemp_file'],
config['obs_file'],
config['dist_matrix_file']
output:
"{outdir}/prepped.npz"
run:
prep_all_data(
x_data_file=input[0],
pretrain_file=input[0],
y_data_file=input[1],
distfile=input[2],
x_vars=config['x_vars'],
y_vars_pretrain=config['y_vars_pretrain'],
y_vars_finetune=config['y_vars_finetune'],
spatial_idx_name='segs_test',
time_idx_name='times_test',
catch_prop_file=None,
train_start_date=config['train_start_date'],
train_end_date=config['train_end_date'],
val_start_date=config['val_start_date'],
val_end_date=config['val_end_date'],
test_start_date=config['test_start_date'],
test_end_date=config['test_end_date'],
segs=None,
out_file=output[0],
trn_offset = config['trn_offset'],
tst_val_offset = config['tst_val_offset'])
# Pretrain the model on process based model
rule pre_train:
input:
"{outdir}/prepped.npz"
output:
directory("{outdir}/pretrained_weights/"),
"{outdir}/pretrain_log.csv",
"{outdir}/pretrain_time.txt",
params:
# getting the base path to put the training outputs in
# I omit the last slash (hence '[:-1]' so the split works properly
weight_dir=lambda wildcards, output: os.path.split(output[0][:-1])[0],
run:
data = np.load(input[0])
optimizer = tf.optimizers.Adam(learning_rate=config['pretrain_learning_rate'])
model = LSTMModel(
config['hidden_size'],
recurrent_dropout=config['recurrent_dropout'],
dropout=config['dropout'],
num_tasks=len(config['y_vars_pretrain']),
)
model.compile(optimizer=optimizer, loss=loss_function)
train_model(model,
x_trn = data['x_pre_full'],
y_trn = data['y_pre_full'],
epochs = config['pt_epochs'],
batch_size = 2,
seed=config['seed'],
# I need to add a trailing slash here. Otherwise the wgts
# get saved in the "outdir"
weight_dir = output[0] + "/",
log_file = output[1],
time_file = output[2])
# Finetune/train the model on observations
rule finetune_train:
input:
"{outdir}/prepped.npz",
"{outdir}/pretrained_weights/"
output:
directory("{outdir}/finetune_weights/"),
directory("{outdir}/best_val_weights/"),
"{outdir}/finetune_log.csv",
"{outdir}/finetune_time.txt",
run:
data = np.load(input[0])
optimizer = tf.optimizers.Adam(learning_rate=config['finetune_learning_rate'])
model = LSTMModel(
config['hidden_size'],
recurrent_dropout=config['recurrent_dropout'],
dropout=config['dropout'],
num_tasks=len(config['y_vars_pretrain']),
)
model.compile(optimizer=optimizer, loss=loss_function)
model.load_weights(input[1] + "/")
train_model(model,
x_trn = data['x_trn'],
y_trn = data['y_obs_trn'],
epochs = config['pt_epochs'],
batch_size = 2,
seed=config['seed'],
x_val = data['x_val'],
y_val = data['y_obs_val'],
# I need to add a trailing slash here. Otherwise the wgts
# get saved in the "outdir"
weight_dir = output[0] + "/",
best_val_weight_dir = output[1] + "/",
log_file = output[2],
time_file = output[3],
early_stop_patience=config['early_stopping'])
rule make_predictions:
input:
"{outdir}/"+pred_weights+'/',
"{outdir}/prepped.npz"
output:
"{outdir}/{partition}_preds.feather",
group: 'train_predict_evaluate'
run:
model = LSTMModel(
config['hidden_size'],
recurrent_dropout=config['recurrent_dropout'],
dropout=config['dropout'],
num_tasks=len(config['y_vars_pretrain']),
)
weight_dir = input[0] + '/'
model.load_weights(weight_dir)
predict_from_io_data(model=model,
io_data=input[1],
partition=wildcards.partition,
outfile=output[0],
trn_offset = config['trn_offset'],
spatial_idx_name='segs_test',
time_idx_name='times_test',
tst_val_offset = config['tst_val_offset'])
def get_grp_arg(wildcards):
if wildcards.metric_type == 'overall':
return None
elif wildcards.metric_type == 'month':
return 'month'
elif wildcards.metric_type == 'reach':
return 'seg_id_nat'
elif wildcards.metric_type == 'month_reach':
return ['seg_id_nat', 'month']
rule combine_metrics:
input:
config['obs_file'],
"{outdir}/trn_preds.feather",
"{outdir}/val_preds.feather"
output:
"{outdir}/{metric_type}_metrics.csv"
group: 'train_predict_evaluate'
params:
grp_arg = get_grp_arg
run:
combined_metrics(obs_file=input[0],
pred_trn=input[1],
pred_val=input[2],
spatial_idx_name='segs_test',
time_idx_name='times_test',
group=params.grp_arg,
outfile=output[0])
rule plot_prepped_data:
input:
"{outdir}/prepped.npz",
output:
"{outdir}/{variable}_{partition}.png",
run:
plot_obs(input[0], wildcards.variable, output[0],
partition=wildcards.partition)