Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

RMSprop.py 3.1 KB

You have to be logged in to leave a comment. Sign In
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
  1. import numpy as np
  2. from tensorflow.python.ops import control_flow_ops, state_ops
  3. from core.leras import nn
  4. tf = nn.tf
  5. class RMSprop(nn.OptimizerBase):
  6. def __init__(self, lr=0.001, rho=0.9, lr_dropout=1.0, lr_cos=0, clipnorm=0.0, name=None, **kwargs):
  7. super().__init__(name=name)
  8. if name is None:
  9. raise ValueError('name must be defined.')
  10. self.lr_dropout = lr_dropout
  11. self.lr_cos = lr_cos
  12. self.lr = lr
  13. self.rho = rho
  14. self.clipnorm = clipnorm
  15. with tf.device('/CPU:0') :
  16. with tf.variable_scope(self.name):
  17. self.iterations = tf.Variable(0, dtype=tf.int64, name='iters')
  18. self.accumulators_dict = {}
  19. self.lr_rnds_dict = {}
  20. def get_weights(self):
  21. return [self.iterations] + list(self.accumulators_dict.values())
  22. def initialize_variables(self, trainable_weights, vars_on_cpu=True, lr_dropout_on_cpu=False):
  23. # Initialize here all trainable variables used in training
  24. e = tf.device('/CPU:0') if vars_on_cpu else None
  25. if e: e.__enter__()
  26. with tf.variable_scope(self.name):
  27. accumulators = { v.name : tf.get_variable ( f'acc_{v.name}'.replace(':','_'), v.shape, dtype=v.dtype, initializer=tf.initializers.constant(0.0), trainable=False) for v in trainable_weights }
  28. self.accumulators_dict.update ( accumulators)
  29. if self.lr_dropout != 1.0:
  30. e = tf.device('/CPU:0') if lr_dropout_on_cpu else None
  31. if e: e.__enter__()
  32. lr_rnds = [ nn.random_binomial( v.shape, p=self.lr_dropout, dtype=v.dtype) for v in trainable_weights ]
  33. if e: e.__exit__(None, None, None)
  34. self.lr_rnds_dict.update ( { v.name : rnd for v,rnd in zip(trainable_weights,lr_rnds) } )
  35. if e: e.__exit__(None, None, None)
  36. def get_update_op(self, grads_vars):
  37. updates = []
  38. if self.clipnorm > 0.0:
  39. norm = tf.sqrt( sum([tf.reduce_sum(tf.square(tf.cast(g, tf.float32))) for g,v in grads_vars]))
  40. updates += [ state_ops.assign_add( self.iterations, 1) ]
  41. for i, (g,v) in enumerate(grads_vars):
  42. if self.clipnorm > 0.0:
  43. g = self.tf_clip_norm(g, self.clipnorm, tf.cast(norm, g.dtype) )
  44. a = self.accumulators_dict[ v.name ]
  45. new_a = self.rho * a + (1. - self.rho) * tf.square(g)
  46. lr = tf.constant(self.lr, g.dtype)
  47. if self.lr_cos != 0:
  48. lr *= (tf.cos( tf.cast(self.iterations, g.dtype) * (2*3.1415926535/ float(self.lr_cos) ) ) + 1.0) / 2.0
  49. v_diff = - lr * g / (tf.sqrt(new_a) + np.finfo( g.dtype.as_numpy_dtype ).resolution )
  50. if self.lr_dropout != 1.0:
  51. lr_rnd = self.lr_rnds_dict[v.name]
  52. v_diff *= lr_rnd
  53. new_v = v + v_diff
  54. updates.append (state_ops.assign(a, new_a))
  55. updates.append (state_ops.assign(v, new_v))
  56. return control_flow_ops.group ( *updates, name=self.name+'_updates')
  57. nn.RMSprop = RMSprop
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...