FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
tensorflow/tensorflow/python/training/optimizer.py at master · codeplaysoftware/tensorflow · GitHub
codeplaysoftware
tensorflow
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
tensorflow
/
tensorflow
/
python
/
training
/
optimizer.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
1213 lines (1016 loc) · 47.3 KB
Breadcrumbs
tensorflow
/
tensorflow
/
python
/
training
/
optimizer.py
Copy path
File metadata and controls
1213 lines (1016 loc) · 47.3 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Copyright 2015 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Base class for optimizers."""
# pylint: disable=g-bad-name
from
__future__
import
absolute_import
from
__future__
import
division
from
__future__
import
print_function
import
abc
from
tensorflow
.
python
.
eager
import
backprop
from
tensorflow
.
python
.
eager
import
context
from
tensorflow
.
python
.
framework
import
dtypes
from
tensorflow
.
python
.
framework
import
ops
from
tensorflow
.
python
.
ops
import
array_ops
from
tensorflow
.
python
.
ops
import
control_flow_ops
from
tensorflow
.
python
.
ops
import
gradients
from
tensorflow
.
python
.
ops
import
math_ops
from
tensorflow
.
python
.
ops
import
resource_variable_ops
from
tensorflow
.
python
.
ops
import
state_ops
from
tensorflow
.
python
.
ops
import
variable_scope
from
tensorflow
.
python
.
ops
import
variables
from
tensorflow
.
python
.
training
import
distribute
as
distribute_lib
from
tensorflow
.
python
.
training
import
slot_creator
from
tensorflow
.
python
.
training
.
checkpointable
import
base
as
checkpointable
from
tensorflow
.
python
.
util
import
nest
from
tensorflow
.
python
.
util
.
tf_export
import
tf_export
def
get_filtered_grad_fn
(
grad_fn
):
# `distributed_context.join()` requires that its arguments are parallel
# across threads, and in particular that `grads_and_vars` has the same
# variables in the same order.
# When computing gradients in eager mode with multiple threads, you
# can get extra variables with a gradient of `None`. This happens when
# those variables are accessed in another thread during the gradient
# computation. To get a consistent set of variables, we filter out
# those with `None` gradients.
def
filtered_grad_fn
(
x
=
None
):
return
[(
g
,
v
)
for
g
,
v
in
grad_fn
(
x
)
if
g
is
not
None
]
return
filtered_grad_fn
def
_deduplicate_indexed_slices
(
values
,
indices
):
"""Sums `values` associated with any non-unique `indices`.
Args:
values: A `Tensor` with rank >= 1.
indices: A one-dimensional integer `Tensor`, indexing into the first
dimension of `values` (as in an IndexedSlices object).
Returns:
A tuple of (`summed_values`, `unique_indices`) where `unique_indices` is a
de-duplicated version of `indices` and `summed_values` contains the sum of
`values` slices associated with each unique index.
"""
unique_indices
,
new_index_positions
=
array_ops
.
unique
(
indices
)
summed_values
=
math_ops
.
unsorted_segment_sum
(
values
,
new_index_positions
,
array_ops
.
shape
(
unique_indices
)[
0
])
return
(
summed_values
,
unique_indices
)
def
_var_key
(
var
):
if
context
.
executing_eagerly
():
return
var
.
_unique_id
# pylint: disable=protected-access
return
(
var
.
op
.
graph
,
var
.
op
.
name
)
class
_OptimizableVariable
(
object
):
"""Interface for abstracting over variables in the optimizers."""
@
abc
.
abstractmethod
def
target
(
self
):
"""Returns the optimization target for this variable."""
raise
NotImplementedError
(
"Calling an abstract method."
)
@
abc
.
abstractmethod
def
update_op
(
self
,
optimizer
,
g
):
"""Returns the update ops for updating the variable."""
raise
NotImplementedError
(
"Calling an abstract method."
)
class
_RefVariableProcessor
(
_OptimizableVariable
):
"""Processor for Variable."""
def
__init__
(
self
,
v
):
self
.
_v
=
v
def
__str__
(
self
):
return
"<_RefVariableProcessor(%s)>"
%
self
.
_v
def
target
(
self
):
return
self
.
_v
.
_ref
()
# pylint: disable=protected-access
def
update_op
(
self
,
optimizer
,
g
):
if
isinstance
(
g
,
ops
.
Tensor
):
update_op
=
optimizer
.
_apply_dense
(
g
,
self
.
_v
)
# pylint: disable=protected-access
if
self
.
_v
.
constraint
is
not
None
:
with
ops
.
control_dependencies
([
update_op
]):
return
self
.
_v
.
assign
(
self
.
_v
.
constraint
(
self
.
_v
))
else
:
return
update_op
else
:
assert
isinstance
(
g
,
ops
.
IndexedSlices
), (
"Gradient "
,
g
,
" is neither a "
"tensor nor IndexedSlices."
)
if
self
.
_v
.
constraint
is
not
None
:
raise
RuntimeError
(
"Cannot use a constraint function on a sparse variable."
)
# pylint: disable=protected-access
return
optimizer
.
_apply_sparse_duplicate_indices
(
g
,
self
.
_v
)
class
_DenseReadResourceVariableProcessor
(
_OptimizableVariable
):
"""Processor for dense ResourceVariables."""
def
__init__
(
self
,
v
):
self
.
_v
=
v
def
target
(
self
):
return
self
.
_v
def
update_op
(
self
,
optimizer
,
g
):
# pylint: disable=protected-access
update_op
=
optimizer
.
_resource_apply_dense
(
g
,
self
.
_v
.
op
.
inputs
[
0
])
if
self
.
_v
.
constraint
is
not
None
:
with
ops
.
control_dependencies
([
update_op
]):
return
self
.
_v
.
assign
(
self
.
_v
.
constraint
(
self
.
_v
))
else
:
return
update_op
class
_DenseResourceVariableProcessor
(
_OptimizableVariable
):
"""Processor for dense ResourceVariables."""
def
__init__
(
self
,
v
):
self
.
_v
=
v
def
target
(
self
):
return
self
.
_v
def
update_op
(
self
,
optimizer
,
g
):
# pylint: disable=protected-access
if
isinstance
(
g
,
ops
.
IndexedSlices
):
if
self
.
_v
.
constraint
is
not
None
:
raise
RuntimeError
(
"Cannot use a constraint function on a sparse variable."
)
return
optimizer
.
_resource_apply_sparse_duplicate_indices
(
g
.
values
,
self
.
_v
,
g
.
indices
)
update_op
=
optimizer
.
_resource_apply_dense
(
g
,
self
.
_v
)
if
self
.
_v
.
constraint
is
not
None
:
with
ops
.
control_dependencies
([
update_op
]):
return
self
.
_v
.
assign
(
self
.
_v
.
constraint
(
self
.
_v
))
else
:
return
update_op
class
_TensorProcessor
(
_OptimizableVariable
):
"""Processor for ordinary Tensors.
Even though a Tensor can't really be updated, sometimes it is useful to
compute the gradients with respect to a Tensor using the optimizer. Updating
the Tensor is, of course, unsupported.
"""
def
__init__
(
self
,
v
):
self
.
_v
=
v
def
target
(
self
):
return
self
.
_v
def
update_op
(
self
,
optimizer
,
g
):
raise
NotImplementedError
(
"Trying to update a Tensor "
,
self
.
_v
)
def
_get_processor
(
v
):
"""The processor of v."""
if
context
.
executing_eagerly
():
if
isinstance
(
v
,
ops
.
Tensor
):
return
_TensorProcessor
(
v
)
else
:
return
_DenseResourceVariableProcessor
(
v
)
if
isinstance
(
v
,
resource_variable_ops
.
ResourceVariable
)
and
not
v
.
_in_graph_mode
:
# pylint: disable=protected-access
# True if and only if `v` was initialized eagerly.
return
_DenseResourceVariableProcessor
(
v
)
if
v
.
op
.
type
==
"VarHandleOp"
:
return
_DenseResourceVariableProcessor
(
v
)
if
isinstance
(
v
,
variables
.
Variable
):
return
_RefVariableProcessor
(
v
)
if
isinstance
(
v
,
ops
.
Tensor
):
return
_TensorProcessor
(
v
)
raise
NotImplementedError
(
"Trying to optimize unsupported type "
,
v
)
@
tf_export
(
"train.Optimizer"
)
class
Optimizer
(
# Optimizers inherit from CheckpointableBase rather than Checkpointable
# since they do most of their dependency management themselves (slot
# variables are special-cased, and non-slot variables are keyed to graphs).
checkpointable
.
CheckpointableBase
):
"""Base class for optimizers.
This class defines the API to add Ops to train a model. You never use this
class directly, but instead instantiate one of its subclasses such as
`GradientDescentOptimizer`, `AdagradOptimizer`, or `MomentumOptimizer`.
### Usage
```python
# Create an optimizer with the desired parameters.
opt = GradientDescentOptimizer(learning_rate=0.1)
# Add Ops to the graph to minimize a cost by updating a list of variables.
# "cost" is a Tensor, and the list of variables contains tf.Variable
# objects.
opt_op = opt.minimize(cost, var_list=<list of variables>)
```
In the training program you will just have to run the returned Op.
```python
# Execute opt_op to do one step of training:
opt_op.run()
```
### Processing gradients before applying them.
Calling `minimize()` takes care of both computing the gradients and
applying them to the variables. If you want to process the gradients
before applying them you can instead use the optimizer in three steps:
1. Compute the gradients with `compute_gradients()`.
2. Process the gradients as you wish.
3. Apply the processed gradients with `apply_gradients()`.
Example:
```python
# Create an optimizer.
opt = GradientDescentOptimizer(learning_rate=0.1)
# Compute the gradients for a list of variables.
grads_and_vars = opt.compute_gradients(loss, <list of variables>)
# grads_and_vars is a list of tuples (gradient, variable). Do whatever you
# need to the 'gradient' part, for example cap them, etc.
capped_grads_and_vars = [(MyCapper(gv[0]), gv[1]) for gv in grads_and_vars]
# Ask the optimizer to apply the capped gradients.
opt.apply_gradients(capped_grads_and_vars)
```
### Gating Gradients
Both `minimize()` and `compute_gradients()` accept a `gate_gradients`
argument that controls the degree of parallelism during the application of
the gradients.
The possible values are: `GATE_NONE`, `GATE_OP`, and `GATE_GRAPH`.
<b>`GATE_NONE`</b>: Compute and apply gradients in parallel. This provides
the maximum parallelism in execution, at the cost of some non-reproducibility
in the results. For example the two gradients of `matmul` depend on the input
values: With `GATE_NONE` one of the gradients could be applied to one of the
inputs _before_ the other gradient is computed resulting in non-reproducible
results.
<b>`GATE_OP`</b>: For each Op, make sure all gradients are computed before
they are used. This prevents race conditions for Ops that generate gradients
for multiple inputs where the gradients depend on the inputs.
<b>`GATE_GRAPH`</b>: Make sure all gradients for all variables are computed
before any one of them is used. This provides the least parallelism but can
be useful if you want to process all gradients before applying any of them.
### Slots
Some optimizer subclasses, such as `MomentumOptimizer` and `AdagradOptimizer`
allocate and manage additional variables associated with the variables to
train. These are called <i>Slots</i>. Slots have names and you can ask the
optimizer for the names of the slots that it uses. Once you have a slot name
you can ask the optimizer for the variable it created to hold the slot value.
This can be useful if you want to log debug a training algorithm, report stats
about the slots, etc.
"""
# Values for gate_gradients.
GATE_NONE
=
0
GATE_OP
=
1
GATE_GRAPH
=
2
def
__init__
(
self
,
use_locking
,
name
):
"""Create a new Optimizer.
This must be called by the constructors of subclasses.
Args:
use_locking: Bool. If True apply use locks to prevent concurrent updates
to variables.
name: A non-empty string. The name to use for accumulators created
for the optimizer.
Raises:
ValueError: If name is malformed.
"""
if
not
name
:
raise
ValueError
(
"Must specify the optimizer name"
)
self
.
_use_locking
=
use_locking
self
.
_name
=
name
# Dictionary of slots.
# {slot_name :
# {_var_key(variable_to_train): slot_for_the_variable, ... },
# ... }
self
.
_slots
=
{}
self
.
_non_slot_dict
=
{}
# For implementing Checkpointable. Stores information about how to restore
# slot variables which have not yet been created
# (checkpointable._CheckpointPosition objects).
# {slot_name :
# {_var_key(variable_to_train): [checkpoint_position, ... ], ... },
# ... }
self
.
_deferred_slot_restorations
=
{}
# TODO(isaprykin): When using a DistributionStrategy, and when an
# optimizer is created in each tower, it might be dangerous to
# rely on some Optimer methods. When such methods are called on a
# per-tower optimizer, an exception needs to be thrown. We do
# allow creation per-tower optimizers however, because the
# compute_gradients()->apply_gradients() sequence is safe.
def
get_name
(
self
):
return
self
.
_name
def
minimize
(
self
,
loss
,
global_step
=
None
,
var_list
=
None
,
gate_gradients
=
GATE_OP
,
aggregation_method
=
None
,
colocate_gradients_with_ops
=
False
,
name
=
None
,
grad_loss
=
None
):
"""Add operations to minimize `loss` by updating `var_list`.
This method simply combines calls `compute_gradients()` and
`apply_gradients()`. If you want to process the gradient before applying
them call `compute_gradients()` and `apply_gradients()` explicitly instead
of using this function.
Args:
loss: A `Tensor` containing the value to minimize.
global_step: Optional `Variable` to increment by one after the
variables have been updated.
var_list: Optional list or tuple of `Variable` objects to update to
minimize `loss`. Defaults to the list of variables collected in
the graph under the key `GraphKeys.TRAINABLE_VARIABLES`.
gate_gradients: How to gate the computation of gradients. Can be
`GATE_NONE`, `GATE_OP`, or `GATE_GRAPH`.
aggregation_method: Specifies the method used to combine gradient terms.
Valid values are defined in the class `AggregationMethod`.
colocate_gradients_with_ops: If True, try colocating gradients with
the corresponding op.
name: Optional name for the returned operation.
grad_loss: Optional. A `Tensor` holding the gradient computed for `loss`.
Returns:
An Operation that updates the variables in `var_list`. If `global_step`
was not `None`, that operation also increments `global_step`.
Raises:
ValueError: If some of the variables are not `Variable` objects.
@compatibility(eager)
When eager execution is enabled, `loss` should be a Python function that
takes elements of `var_list` as arguments and computes the value to be
minimized. If `var_list` is None, `loss` should take no arguments.
Minimization (and gradient computation) is done with respect to the
elements of `var_list` if not None, else with respect to any trainable
variables created during the execution of the `loss` function.
`gate_gradients`, `aggregation_method`, `colocate_gradients_with_ops` and
`grad_loss` are ignored when eager execution is enabled.
@end_compatibility
"""
grads_and_vars
=
self
.
compute_gradients
(
loss
,
var_list
=
var_list
,
gate_gradients
=
gate_gradients
,
aggregation_method
=
aggregation_method
,
colocate_gradients_with_ops
=
colocate_gradients_with_ops
,
grad_loss
=
grad_loss
)
vars_with_grad
=
[
v
for
g
,
v
in
grads_and_vars
if
g
is
not
None
]
if
not
vars_with_grad
:
raise
ValueError
(
"No gradients provided for any variable, check your graph for ops"
" that do not support gradients, between variables %s and loss %s."
%
([
str
(
v
)
for
_
,
v
in
grads_and_vars
],
loss
))
return
self
.
apply_gradients
(
grads_and_vars
,
global_step
=
global_step
,
name
=
name
)
def
compute_gradients
(
self
,
loss
,
var_list
=
None
,
gate_gradients
=
GATE_OP
,
aggregation_method
=
None
,
colocate_gradients_with_ops
=
False
,
grad_loss
=
None
):
"""Compute gradients of `loss` for the variables in `var_list`.
This is the first part of `minimize()`. It returns a list
of (gradient, variable) pairs where "gradient" is the gradient
for "variable". Note that "gradient" can be a `Tensor`, an
`IndexedSlices`, or `None` if there is no gradient for the
given variable.
Args:
loss: A Tensor containing the value to minimize or a callable taking
no arguments which returns the value to minimize. When eager execution
is enabled it must be a callable.
var_list: Optional list or tuple of `tf.Variable` to update to minimize
`loss`. Defaults to the list of variables collected in the graph
under the key `GraphKeys.TRAINABLE_VARIABLES`.
gate_gradients: How to gate the computation of gradients. Can be
`GATE_NONE`, `GATE_OP`, or `GATE_GRAPH`.
aggregation_method: Specifies the method used to combine gradient terms.
Valid values are defined in the class `AggregationMethod`.
colocate_gradients_with_ops: If True, try colocating gradients with
the corresponding op.
grad_loss: Optional. A `Tensor` holding the gradient computed for `loss`.
Returns:
A list of (gradient, variable) pairs. Variable is always present, but
gradient can be `None`.
Raises:
TypeError: If `var_list` contains anything else than `Variable` objects.
ValueError: If some arguments are invalid.
RuntimeError: If called with eager execution enabled and `loss` is
not callable.
@compatibility(eager)
When eager execution is enabled, `gate_gradients`, `aggregation_method`,
and `colocate_gradients_with_ops` are ignored.
@end_compatibility
"""
if
callable
(
loss
):
with
backprop
.
GradientTape
()
as
tape
:
if
var_list
is
not
None
:
tape
.
watch
(
var_list
)
loss_value
=
loss
()
# Scale loss if using a "mean" loss reduction and multiple towers.
# Have to be careful to call distribute_lib.get_loss_reduction()
# *after* loss() is evaluated, so we know what loss reduction it uses.
# TODO(josh11b): Test that we handle weight decay in a reasonable way.
if
distribute_lib
.
get_loss_reduction
()
==
"mean"
:
num_towers
=
distribute_lib
.
get_distribution_strategy
().
num_towers
if
num_towers
>
1
:
loss_value
*=
(
1.
/
num_towers
)
if
var_list
is
None
:
var_list
=
tape
.
watched_variables
()
grads
=
tape
.
gradient
(
loss_value
,
var_list
,
grad_loss
)
return
list
(
zip
(
grads
,
var_list
))
# Non-callable/Tensor loss case
if
context
.
executing_eagerly
():
raise
RuntimeError
(
"`loss` passed to Optimizer.compute_gradients should "
"be a function when eager execution is enabled."
)
# Scale loss if using a "mean" loss reduction and multiple towers.
if
distribute_lib
.
get_loss_reduction
()
==
"mean"
:
num_towers
=
distribute_lib
.
get_distribution_strategy
().
num_towers
if
num_towers
>
1
:
loss
*=
(
1.
/
num_towers
)
if
gate_gradients
not
in
[
Optimizer
.
GATE_NONE
,
Optimizer
.
GATE_OP
,
Optimizer
.
GATE_GRAPH
]:
raise
ValueError
(
"gate_gradients must be one of: Optimizer.GATE_NONE, "
"Optimizer.GATE_OP, Optimizer.GATE_GRAPH. Not %s"
%
gate_gradients
)
self
.
_assert_valid_dtypes
([
loss
])
if
grad_loss
is
not
None
:
self
.
_assert_valid_dtypes
([
grad_loss
])
if
var_list
is
None
:
var_list
=
(
variables
.
trainable_variables
()
+
ops
.
get_collection
(
ops
.
GraphKeys
.
TRAINABLE_RESOURCE_VARIABLES
))
else
:
var_list
=
nest
.
flatten
(
var_list
)
# pylint: disable=protected-access
var_list
+=
ops
.
get_collection
(
ops
.
GraphKeys
.
_STREAMING_MODEL_PORTS
)
# pylint: enable=protected-access
processors
=
[
_get_processor
(
v
)
for
v
in
var_list
]
if
not
var_list
:
raise
ValueError
(
"No variables to optimize."
)
var_refs
=
[
p
.
target
()
for
p
in
processors
]
grads
=
gradients
.
gradients
(
loss
,
var_refs
,
grad_ys
=
grad_loss
,
gate_gradients
=
(
gate_gradients
==
Optimizer
.
GATE_OP
),
aggregation_method
=
aggregation_method
,
colocate_gradients_with_ops
=
colocate_gradients_with_ops
)
if
gate_gradients
==
Optimizer
.
GATE_GRAPH
:
grads
=
control_flow_ops
.
tuple
(
grads
)
grads_and_vars
=
list
(
zip
(
grads
,
var_list
))
self
.
_assert_valid_dtypes
(
[
v
for
g
,
v
in
grads_and_vars
if
g
is
not
None
and
v
.
dtype
!=
dtypes
.
resource
])
return
grads_and_vars
def
apply_gradients
(
self
,
grads_and_vars
,
global_step
=
None
,
name
=
None
):
"""Apply gradients to variables.
This is the second part of `minimize()`. It returns an `Operation` that
applies gradients.
Args:
grads_and_vars: List of (gradient, variable) pairs as returned by
`compute_gradients()`.
global_step: Optional `Variable` to increment by one after the
variables have been updated.
name: Optional name for the returned operation. Default to the
name passed to the `Optimizer` constructor.
Returns:
An `Operation` that applies the specified gradients. If `global_step`
was not None, that operation also increments `global_step`.
Raises:
TypeError: If `grads_and_vars` is malformed.
ValueError: If none of the variables have gradients.
RuntimeError: If you should use `_distributed_apply()` instead.
"""
# This is a default implementation of apply_gradients() that can be shared
# by most optimizers. It relies on the subclass implementing the following
# methods: _create_slots(), _prepare(), _apply_dense(), and _apply_sparse().
# Handle DistributionStrategy case.
if
distribute_lib
.
get_cross_tower_context
():
raise
RuntimeError
(
"Use `_distributed_apply()` instead of "
"`apply_gradients()` in a cross-tower context."
)
# TODO(isaprykin): Get rid of `has_distribution_strategy()` check by
# always calling _distributed_apply(), using the default distribution
# as needed.
if
distribute_lib
.
has_distribution_strategy
():
grads_and_vars
=
get_filtered_grad_fn
(
lambda
_
:
grads_and_vars
)()
return
distribute_lib
.
get_tower_context
().
merge_call
(
self
.
_distributed_apply
,
grads_and_vars
,
global_step
,
name
)
# No DistributionStrategy case.
grads_and_vars
=
tuple
(
grads_and_vars
)
# Make sure repeat iteration works.
if
not
grads_and_vars
:
raise
ValueError
(
"No variables provided."
)
converted_grads_and_vars
=
[]
for
g
,
v
in
grads_and_vars
:
if
g
is
not
None
:
try
:
# Convert the grad to Tensor or IndexedSlices if necessary.
g
=
ops
.
convert_to_tensor_or_indexed_slices
(
g
)
except
TypeError
:
raise
TypeError
(
"Gradient must be convertible to a Tensor"
" or IndexedSlices, or None: %s"
%
g
)
if
not
isinstance
(
g
, (
ops
.
Tensor
,
ops
.
IndexedSlices
)):
raise
TypeError
(
"Gradient must be a Tensor, IndexedSlices, or None: %s"
%
g
)
p
=
_get_processor
(
v
)
converted_grads_and_vars
.
append
((
g
,
v
,
p
))
converted_grads_and_vars
=
tuple
(
converted_grads_and_vars
)
var_list
=
[
v
for
g
,
v
,
_
in
converted_grads_and_vars
if
g
is
not
None
]
if
not
var_list
:
raise
ValueError
(
"No gradients provided for any variable: %s."
%
([
str
(
v
)
for
_
,
_
,
v
in
converted_grads_and_vars
],))
with
ops
.
init_scope
():
self
.
_create_slots
(
var_list
)
update_ops
=
[]
with
ops
.
name_scope
(
name
,
self
.
_name
)
as
name
:
self
.
_prepare
()
for
grad
,
var
,
processor
in
converted_grads_and_vars
:
if
grad
is
None
:
continue
# We colocate all ops created in _apply_dense or _apply_sparse
# on the same device as the variable.
# TODO(apassos): figure out how to get the variable name here.
if
context
.
executing_eagerly
()
or
isinstance
(
var
,
resource_variable_ops
.
ResourceVariable
)
and
not
var
.
_in_graph_mode
:
# pylint: disable=protected-access
scope_name
=
""
else
:
scope_name
=
var
.
op
.
name
with
ops
.
name_scope
(
"update_"
+
scope_name
),
ops
.
colocate_with
(
var
):
update_ops
.
append
(
processor
.
update_op
(
self
,
grad
))
if
global_step
is
None
:
apply_updates
=
self
.
_finish
(
update_ops
,
name
)
else
:
with
ops
.
control_dependencies
([
self
.
_finish
(
update_ops
,
"update"
)]):
with
ops
.
colocate_with
(
global_step
):
if
isinstance
(
global_step
,
resource_variable_ops
.
ResourceVariable
):
# TODO(apassos): the implicit read in assign_add is slow; consider
# making it less so.
apply_updates
=
resource_variable_ops
.
assign_add_variable_op
(
global_step
.
handle
,
ops
.
convert_to_tensor
(
1
,
dtype
=
global_step
.
dtype
),
name
=
name
)
else
:
apply_updates
=
state_ops
.
assign_add
(
global_step
,
1
,
name
=
name
)
if
not
context
.
executing_eagerly
():
if
isinstance
(
apply_updates
,
ops
.
Tensor
):
apply_updates
=
apply_updates
.
op
train_op
=
ops
.
get_collection_ref
(
ops
.
GraphKeys
.
TRAIN_OP
)
if
apply_updates
not
in
train_op
:
train_op
.
append
(
apply_updates
)
return
apply_updates
def
_distributed_apply
(
self
,
distribution
,
grads_and_vars
,
global_step
=
None
,
name
=
None
):
"""A version of `apply_gradients` for cross-tower context.
This is a version of `apply_gradients()` for when you are using a
`DistributionStrategy` and are in a cross-tower context. If in a
tower context, use `apply_gradients()` as normal.
Args:
distribution: A `DistributionStrategy` object.
grads_and_vars: List of (gradient, variable) pairs as returned by
`compute_gradients()`, and then aggregated across towers.
global_step: Optional (mirrored) `Variable` to increment by one
after the variables have been updated.
name: Optional name for the returned operation. Default to the
name passed to the `Optimizer` constructor.
Returns:
An `Operation` that applies the specified gradients across all
towers. If `global_step` was not None, that operation also
increments `global_step`.
"""
reduced_grads
=
distribution
.
batch_reduce
(
"sum"
,
grads_and_vars
)
var_list
=
[
v
for
_
,
v
in
grads_and_vars
]
grads_and_vars
=
zip
(
reduced_grads
,
var_list
)
# Note that this is called in a cross-tower context.
self
.
_create_slots
(
var_list
)
def
update
(
v
,
g
):
"""Apply gradients to a replica variable."""
assert
v
is
not
None
try
:
# Convert the grad to Tensor or IndexedSlices if necessary.
g
=
ops
.
convert_to_tensor_or_indexed_slices
(
g
)
except
TypeError
:
raise
TypeError
(
"Gradient must be convertible to a Tensor"
" or IndexedSlices, or None: %s"
%
g
)
if
not
isinstance
(
g
, (
ops
.
Tensor
,
ops
.
IndexedSlices
)):
raise
TypeError
(
"Gradient must be a Tensor, IndexedSlices, or None: %s"
%
g
)
p
=
_get_processor
(
v
)
scope_name
=
""
if
context
.
executing_eagerly
()
else
v
.
op
.
name
# device_policy is set because non-mirrored tensors will be read in
# `update_op`. `_resource_apply_dense`, `lr_t`, `beta1_t` and `beta2_t`
# is an example.
with
ops
.
name_scope
(
"update_"
+
scope_name
):
return
p
.
update_op
(
self
,
g
)
with
ops
.
name_scope
(
name
,
self
.
_name
)
as
name
:
self
.
_prepare
()
update_ops
=
[
op
for
grad
,
var
in
grads_and_vars
for
op
in
distribution
.
unwrap
(
distribution
.
update
(
var
,
update
,
grad
))
]
def
finish
(
self
,
update_ops
):
return
self
.
_finish
(
update_ops
,
"update"
)
non_slot_devices
=
distribution
.
non_slot_devices
(
var_list
)
finish_updates
=
distribution
.
update_non_slot
(
non_slot_devices
,
finish
,
self
,
update_ops
)
if
global_step
is
None
:
apply_updates
=
distribution
.
group
(
finish_updates
,
name
=
name
)
else
:
with
ops
.
control_dependencies
(
distribution
.
unwrap
(
finish_updates
)):
apply_updates
=
distribution
.
group
(
distribution
.
update
(
global_step
,
state_ops
.
assign_add
,
1
,
name
=
name
))
if
not
context
.
executing_eagerly
():
if
isinstance
(
apply_updates
,
ops
.
Tensor
):
apply_updates
=
apply_updates
.
op
train_op
=
ops
.
get_collection_ref
(
ops
.
GraphKeys
.
TRAIN_OP
)
if
apply_updates
not
in
train_op
:
train_op
.
append
(
apply_updates
)
return
apply_updates
def
get_slot
(
self
,
var
,
name
):
"""Return a slot named `name` created for `var` by the Optimizer.
Some `Optimizer` subclasses use additional variables. For example
`Momentum` and `Adagrad` use variables to accumulate updates. This method
gives access to these `Variable` objects if for some reason you need them.
Use `get_slot_names()` to get the list of slot names created by the
`Optimizer`.
Args:
var: A variable passed to `minimize()` or `apply_gradients()`.
name: A string.
Returns:
The `Variable` for the slot if it was created, `None` otherwise.
"""
# pylint: disable=protected-access
named_slots
=
self
.
_slots
.
get
(
name
,
None
)
if
not
named_slots
:
return
None
if
hasattr
(
var
,
"_mirrored_container"
):
# NOTE: If this isn't patched, then there is no `handle` in
# `_resource_apply_dense`.
mirrored_container
=
var
.
_mirrored_container
()
assert
mirrored_container
is
not
None
if
context
.
executing_eagerly
():
key
=
mirrored_container
.
_unique_id
else
:
key
=
(
mirrored_container
.
graph
,
mirrored_container
.
_shared_name
)
# pylint: enable=protected-access
mirrored_slot
=
named_slots
.
get
(
key
,
None
)
if
mirrored_slot
is
None
:
return
None
return
mirrored_slot
.
get
(
device
=
var
.
device
)
return
named_slots
.
get
(
_var_key
(
var
),
None
)
def
get_slot_names
(
self
):
"""Return a list of the names of slots created by the `Optimizer`.
See `get_slot()`.
Returns:
A list of strings.
"""
return
sorted
(
self
.
_slots
.
keys
())
def
variables
(
self
):
"""A list of variables which encode the current state of `Optimizer`.
Includes slot variables and additional global variables created by the
optimizer in the current default graph.
Returns:
A list of variables.
"""
executing_eagerly
=
context
.
executing_eagerly
()
current_graph
=
ops
.
get_default_graph
()
def
_from_current_graph
(
variable
):
if
executing_eagerly
:
# No variable.op in eager mode. We don't expect lots of eager graphs,
# but behavior should be consistent with graph mode.
return
variable
.
_graph_key
==
current_graph
.
_graph_key
# pylint: disable=protected-access
else
:
return
variable
.
op
.
graph
is
current_graph
optimizer_variables
=
[
v
for
v
in
self
.
_non_slot_variables
()
if
_from_current_graph
(
v
)]
for
_
,
variable_dict
in
self
.
_slots
.
items
():
for
_
,
slot_for_variable
in
variable_dict
.
items
():
if
_from_current_graph
(
slot_for_variable
):
optimizer_variables
.
append
(
slot_for_variable
)
# Sort variables by name so that the return is deterministic.
return
sorted
(
optimizer_variables
,
key
=
lambda
v
:
v
.
name
)
def
_create_non_slot_variable
(
self
,
initial_value
,
name
,
colocate_with
):
"""Add an extra variable, not associated with a slot."""
# Recommendation: Use OptimizerV2 if your optimizer uses non-slot variables.
eager
=
context
.
executing_eagerly
()
graph
=
None
if
eager
else
colocate_with
.
graph
key
=
(
name
,
graph
)
v
=
self
.
_non_slot_dict
.
get
(
key
,
None
)
if
v
is
None
:
self
.
_maybe_initialize_checkpointable
()
distribution_strategy
=
distribute_lib
.
get_distribution_strategy
()
with
distribution_strategy
.
colocate_vars_with
(
colocate_with
):
if
eager
:
restored_initial_value
=
self
.
_preload_simple_restoration
(
name
=
name
,
shape
=
None
)
if
restored_initial_value
is
not
None
:
initial_value
=
restored_initial_value
v
=
variable_scope
.
variable
(
initial_value
,
name
=
name
,
trainable
=
False
)
# Restore this variable by name if necessary, but don't add a
# Checkpointable dependency. Optimizers return the current graph's
# non-slot variables from _checkpoint_dependencies explicitly rather
# than unconditionally adding dependencies (since there may be multiple
# non-slot variables with the same name in different graphs, trying to
# save all of them would result in errors).
self
.
_handle_deferred_dependencies
(
name
=
name
,
checkpointable
=
v
)
self
.
_non_slot_dict
[
key
]
=
v
return
v
@
property
def
_checkpoint_dependencies
(
self
):
"""From Checkpointable. Gather graph-specific non-slot variables to save."""
current_graph_non_slot_variables
=
[]
current_graph_key
=
ops
.
get_default_graph
().
_graph_key
# pylint: disable=protected-access
for
(
name
,
_
),
variable_object
in
sorted
(
self
.
_non_slot_dict
.
items
(),
# Avoid comparing graphs
key
=
lambda
item
:
item
[
0
][
0
]):
if
variable_object
.
_graph_key
==
current_graph_key
:
# pylint: disable=protected-access
current_graph_non_slot_variables
.
append
(
checkpointable
.
CheckpointableReference
(
name
=
name
,
ref
=
variable_object
))
return
(
super
(
Optimizer
,
self
).
_checkpoint_dependencies
+
current_graph_non_slot_variables
)
def
_lookup_dependency
(
self
,
name
):
"""From Checkpointable. Find a non-slot variable in the current graph."""
unconditional
=
super
(
Optimizer
,
self
).
_lookup_dependency
(
name
)
if
unconditional
is
not
None
:
return
unconditional
graph
=
None
if
context
.
executing_eagerly
()
else
ops
.
get_default_graph
()
return
self
.
_get_non_slot_variable
(
name
,
graph
=
graph
)
def
_get_non_slot_variable
(
self
,
name
,
graph
=
None
):
non_slot
=
self
.
_non_slot_dict
.
get
((
name
,
graph
),
None
)
if
hasattr
(
non_slot
,
"_mirrored_container"
):
# This is a mirrored non-slot. In order to enable code like `_finish`
# to assign to a non-slot, return the current context replica.
return
non_slot
.
get
()
else
:
return
non_slot
def
_non_slot_variables
(
self
):
"""Additional variables created by the `Optimizer`.
Returns:
A list or tuple of variables.
"""
return
self
.
_non_slot_dict
.
values
()
def
_assert_valid_dtypes
(
self
,
tensors
):
"""Asserts tensors are all valid types (see `_valid_dtypes`).
Args:
tensors: Tensors to check.
Raises:
ValueError: If any tensor is not a valid type.
"""
valid_dtypes
=
self
.
_valid_dtypes
()
for
t
in
tensors
:
dtype
=
t
.
dtype
.
base_dtype
if
dtype
not
in
valid_dtypes
:
raise
ValueError
(
"Invalid type %r for %s, expected: %s."
%
(
dtype
,
t
.
name
, [
v
for
v
in
valid_dtypes
]))
# --------------
# Methods to be implemented by subclasses if they want to use the
# inherited implementation of apply_gradients() or compute_gradients().
# --------------
def
_valid_dtypes
(
self
):
"""Valid types for loss, variables and gradients.
Subclasses should override to allow other float types.
Returns:
Valid types for loss, variables and gradients.
"""
return
set
(
[
dtypes
.
float16
,
dtypes
.
bfloat16
,
dtypes
.
float32
,
dtypes
.
float64
])
def
_create_slots
(
self
,
var_list
):
"""Create all slots needed by the variables.
Args:
var_list: A list of `Variable` objects.
"""
# No slots needed by default
pass
def
_prepare
(
self
):
"""Create all needed tensors before applying gradients.
This is called with the name_scope using the "name" that
users have chosen for the application of gradients.
"""
pass
def
_apply_dense
(
self
,
grad
,
var
):
"""Add ops to apply dense gradients to `var`.
Args:
grad: A `Tensor`.
var: A `Variable` object.
Returns:
An `Operation`.
"""
raise
NotImplementedError
()
def
_resource_apply_dense
(
self
,
grad
,
handle
):
"""Add ops to apply dense gradients to the variable `handle`.
Args:
grad: a `Tensor` representing the gradient.
handle: a `Tensor` of dtype `resource` which points to the variable
to be updated.
Returns:
An `Operation` which updates the value of the variable.
"""
raise
NotImplementedError
()
def
_resource_apply_sparse_duplicate_indices
(
self
,
grad
,
handle
,
indices
):
"""Add ops to apply sparse gradients to `handle`, with repeated indices.
Optimizers which override this method must deal with repeated indices. See
the docstring of `_apply_sparse_duplicate_indices` for details. By default
the correct behavior, to sum non-unique indices and their associated
gradients, is enforced by first pre-processing `grad` and `indices` and
passing them on to `_resource_apply_sparse`. Optimizers which deal correctly
with duplicate indices may instead override this method to avoid the
overhead of summing.
Args:
grad: a `Tensor` representing the gradient for the affected indices.
handle: a `Tensor` of dtype `resource` which points to the variable
to be updated.
indices: a `Tensor` of integral type representing the indices for
which the gradient is nonzero. Indices may be repeated.
Returns:
An `Operation` which updates the value of the variable.
"""
summed_grad
,
unique_indices
=
_deduplicate_indexed_slices
(
values
=
grad
,
indices
=
indices
)
return
self
.
_resource_apply_sparse
(
summed_grad
,
handle
,
unique_indices
)
def
_resource_apply_sparse
(
self
,
grad
,
handle
,
indices
):
"""Add ops to apply sparse gradients to the variable `handle`.
Similar to `_apply_sparse`, the `indices` argument to this method has been
de-duplicated. Optimizers which deal correctly with non-unique indices may
instead override `_resource_apply_sparse_duplicate_indices` to avoid this
overhead.
Args:
grad: a `Tensor` representing the gradient for the affected indices.
handle: a `Tensor` of dtype `resource` which points to the variable
to be updated.
indices: a `Tensor` of integral type representing the indices for
which the gradient is nonzero. Indices are unique.
Returns:
An `Operation` which updates the value of the variable.
"""
raise
NotImplementedError
()
def
_apply_sparse_duplicate_indices
(
self
,
grad
,
var
):
"""Add ops to apply sparse gradients to `var`, with repeated sparse indices.
Optimizers which override this method must deal with IndexedSlices objects
such as the following:
IndexedSlicesValue(values=[1, 1], indices=[0, 0], dense_shape=[1])
The correct interpretation is:
IndexedSlicesValue(values=[2], indices=[0], dense_shape=[1])
Many optimizers deal incorrectly with repeated indices when updating based
on sparse gradients (e.g. summing squares rather than squaring the sum, or
applying momentum terms multiple times). Adding first is always the correct
behavior, so this is enforced here by reconstructing the IndexedSlices to
have only unique indices, then calling _apply_sparse.
Optimizers which deal correctly with repeated indices may instead override
this method to avoid the overhead of summing indices.
Args:
grad: `IndexedSlices`.
var: A `Variable` object.
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL