-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtrainer.py
66 lines (47 loc) · 2.32 KB
/
trainer.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
import os
import numpy as np
import tensorflow as tf
import soundfile as sf
from tqdm import tqdm
import pandas as pd
class Trainer():
def __init__(self, model, optimizer,loss, strategy, path_experiment, args):
self.model=model
print(self.model.summary())
self.strategy=strategy
self.optimizer=optimizer
self.path_experiment=path_experiment
self.args=args
#self.metrics=[]
with self.strategy.scope():
#loss_fn=tf.keras.losses.mean_absolute_error
loss.reduction=tf.keras.losses.Reduction.NONE
self.loss_object=loss
self.train_mae_s1=tf.keras.metrics.MeanAbsoluteError(name="train_mae_s1")
self.train_mae=tf.keras.metrics.MeanAbsoluteError(name="train_mae_s2")
self.val_mae=tf.keras.metrics.MeanAbsoluteError(name="validation_mae")
self.val_loss = tf.keras.metrics.Mean(name='test_loss')
def train_step(self,inputs):
noisy, clean= inputs
with tf.GradientTape() as tape:
logits_2,logits_1 = self.model(noisy, training=True) # Logits for this minibatch
loss_value = tf.reduce_mean(self.loss_object(clean, logits_2) + tf.reduce_mean(self.loss_object(clean, logits_1)))
grads = tape.gradient(loss_value, self.model.trainable_weights)
self.optimizer.apply_gradients(zip(grads, self.model.trainable_weights))
self.train_mae.update_state(clean, logits_2)
self.train_mae_s1.update_state(clean, logits_1)
return loss_value
def test_step(self,inputs):
noisy,clean = inputs
predictions_s2, predictions_s1 = self.model(noisy, training=False)
t_loss = self.loss_object(clean, predictions_s2)+self.loss_object(clean, predictions_s1)
self.val_mae.update_state(clean,predictions_s2)
self.val_loss.update_state(t_loss)
@tf.function()
def distributed_training_step(self,inputs):
per_replica_losses=self.strategy.run(self.train_step, args=(inputs,))
reduced_losses=self.strategy.reduce(tf.distribute.ReduceOp.MEAN, per_replica_losses, axis=None)
return reduced_losses
@tf.function
def distributed_test_step(self,inputs):
return self.strategy.run(self.test_step, args=(inputs,))