-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactory.py
More file actions
32 lines (24 loc) · 1.02 KB
/
factory.py
File metadata and controls
32 lines (24 loc) · 1.02 KB
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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import tensorflow as tf
class factory(object):
def __init__(self, model):
self.model = model
self.outputs = model.outputs
def loss(self, y_true, y_pred):
return self.loss_fn(y_true= y_true, y_pred = y_pred)
def training_iter_step(self, inputs):
x, labels = inputs
with tf.GradientTape() as tape:
y_ = self.model(x)
loss = self.loss_fn(y_true= labels, y_pred = y_)
grads = tape.gradient(loss, self.model.trainable_variables)
self.optimizer.apply_gradients(zip(grads, self.model.trainable_variables))
return loss
def training_step(self, iterator, num_steps):
if not isinstance(num_steps, tf.Tensor):
num_steps = tf.convert_to_tensor(num_steps, dtype = tf.int32)
for _ in tf.range(num_steps -1):
print(self.training_iter_step(next(iterator)))
return