/
variable.py
1469 lines (1176 loc) · 51 KB
/
variable.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
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
import collections
import copy
import heapq
import traceback
import warnings
import weakref
import numpy
import chainer
from chainer import _backprop_utils
from chainer.backends import cuda
from chainer.backends import intel64
from chainer import initializers
from chainer.initializers import constant
from chainer.utils import argument
def _check_grad_type(func, x, gx):
if x.data is None or gx is None:
# ``x.data is None`` implies that the data array is not retained
return
if not chainer.is_arrays_compatible((gx, x.data)):
msg = ('Type of data and grad mismatch\ngrad: %s != data: %s' %
(type(gx), type(x.data)))
typ = TypeError
elif gx.dtype != x.data.dtype:
msg = ('Dtype of data and grad mismatch\ngrad: %s != data: %s' %
(gx.dtype, x.data.dtype))
typ = TypeError
elif gx.shape != x.data.shape:
msg = ('Shape of data and grad mismatch\ngrad: %s != data: %s' %
(gx.shape, x.data.shape))
typ = ValueError
else:
return
detail = ''
if func:
detail = 'Function `{0}` ({1}) has a bug.\n'.format(
type(func)._impl_name, func.label)
stack = func.stack
if stack:
detail += 'Stacktrace of the function is below:\n'
for line in traceback.format_list(func.stack):
detail += line
detail += '''
Please report this error to the issue tracker with the stack trace,
the information of your environment, and your script:
https://github.com/chainer/chainer/issues/new.
'''.format(type(func).__name__, func.label)
raise typ(detail + msg)
def variable_repr(var):
"""Return the string representation of a variable.
Args:
var (~chainer.Variable): Input Variable.
.. seealso:: numpy.array_repr
"""
xp = cuda.get_array_module(var)
if xp is numpy:
arr = var.data
else:
arr = var.data.get()
if var.name:
prefix = 'variable ' + var.name
else:
prefix = 'variable'
if arr is None:
lst = 'None'
elif arr.size > 0 or arr.shape == (0,):
lst = numpy.array2string(arr, None, None, None, ', ', prefix + '(')
else: # show zero-length shape unless it is (0,)
lst = '[], shape=%s' % (repr(arr.shape),)
return '%s(%s)' % (prefix, lst)
def variable_str(var):
"""Return the string representation of a variable.
Args:
var (~chainer.Variable): Input Variable.
.. seealso:: numpy.array_str
"""
xp = cuda.get_array_module(var)
if xp is numpy:
arr = var.data
else:
arr = var.data.get()
if var.name:
prefix = 'variable ' + var.name
else:
prefix = 'variable'
if arr is None:
lst = 'None'
else:
lst = numpy.array2string(arr, None, None, None, ' ', prefix + '(')
return '%s(%s)' % (prefix, lst)
class VariableNode(object):
"""Node in the backward computational graph representing a variable.
This object represents a variable node in a computational graph. The node
is used in error backpropagation (a.k.a. backprop) to determine which
gradient to be passed to each function.
A variable node is held by the corresponding :class:`~chainer.Variable`
object, which is managed by users. :class:`~chainer.FunctionNode` objects
that take the variable as an input also hold references to the variable
node.
Note that the node does not hold a reference to the corresponding data
array in general. The data array is actually accessible by the node in the
following cases.
1. If there exists a :class:`~chainer.Variable` object that holds a
reference to the variable node, the variable node holds a weak reference
to the variable object, and thus the data array is accessible via the
weak reference.
2. If :meth:`retain_data` is called, the node holds a reference to the data
array. It is mainly called by a function that needs the input or output
data array in its backprop procedure.
See :meth:`FunctionNode.retain_inputs()
<chainer.FunctionNode.retain_inputs>`
and :meth:`FunctionNode.retain_outputs()
<chainer.FunctionNode.retain_outputs>` for more details.
Users usually do not need to touch this variable node object. The
computational graph is automatically managed by Chainer, and any interface
that is beneficial for users is also provided by
:class:`~chainer.Variable`.
Args:
variable (Variable): The corresponding variable object.
name (str): Name of the variable node.
Attributes:
dtype: Data type of the data array.
shape: Shape of the data array.
name (str): Name of the variable node.
"""
_creator_node = None
_data = None
_rank = 0
# Name of the Function is assigned if this variable is a gradient generated
# by an old-style Function
_old_style_grad_generator = None
def __init__(self, variable, name, **kwargs):
argument.check_unexpected_kwargs(
kwargs,
grad='unexpected keyword argument "grad": '
'pass the gradient to Variable instead'
)
self._variable = weakref.ref(variable)
self.name = name
self._requires_grad = variable.requires_grad
vdata = variable.data
self._update_data_info(vdata)
@property
def creator(self):
"""Function object that created this variable node.
When the function is implemented with the old-style API (i.e., it uses
:class:`~chainer.Function` class),
this property returns the :class:`~chainer.Function` object.
The object is extracted from the :class:`~chainer.FunctionAdapter`
object, so the returned object is not the function node, but instead
the actual implementation of forward and backward procedures.
When the function is implemented with the new-style API (i.e., it uses
:class:`~chainer.FunctionNode` class),
this property returns the function node
object. In this case, the returned object is same as
:attr:`creator_node`.
.. warning::
As of v3.0.0, when the creator is an old-style function, the
following code is invalid:
.. code-block:: python
creator = v.creator
v.creator = None
...
v.creator = creator
The point is that :class:`~chainer.FunctionNode` objects are used
as nodes in the computational graph instead of
:class:`~chainer.Function`, and each :class:`~chainer.Function`
object only holds a *weak reference* to the corresponding
:class:`~chainer.FunctionNode`.
Since ``creator`` returns the :class:`~chainer.Function` object,
the :class:`~chainer.FunctionNode` object is not kept by preserving
``creator``.
The above code should be fixed as follows.
.. code-block:: python
creator_node = v.creator_node
v.creator_node = None
...
v.creator_node = creator_node
"""
node = self._creator_node
if node is None:
return None
if isinstance(node, chainer.function.FunctionAdapter):
return node.function
return node
@creator.setter
def creator(self, func):
self.creator_node = func
@property
def creator_node(self):
"""Function node that has this variable as an output.
See :class:`~chainer.FunctionNode` for the definition of a function
node.
"""
return self._creator_node
@creator_node.setter
def creator_node(self, func):
if isinstance(func, chainer.Function):
func = func.node
self._creator_node = func
if func is not None:
self._rank = func.rank + 1
@property
def data(self):
"""Data array of the corresponding variable.
If the data is not available, it returns ``None``.
"""
return self._data
@data.setter
def data(self, d):
self._data = d
self._update_data_info(d)
@property
def grad(self):
"""Gradient array of the corresponding variable.
If the variable is not available, it returns ``None``.
"""
var = self._variable()
return None if var is None else var.grad
@property
def grad_var(self):
"""Gradient variable of the corresponding variable.
If the corresponding variable is not available, it return ``None``.
"""
var = self._variable()
return None if var is None else var._grad_var
@property
def label(self):
"""Short text that represents the variable node."""
if self.shape == ():
return str(self.dtype)
return '(%s), %s' % (', '.join(map(str, self.shape)),
str(self.dtype))
@property
def rank(self):
return self._rank
@property
def requires_grad(self):
"""It indicates that ``grad`` will be set in backward calculation."""
return self._requires_grad
def get_variable(self):
"""Returns the corresponding :class:`~chainer.Variable` object.
VariableNode object holds a weak reference of the variable object. If
the reference is alive, it is returned by this property. Otherwise,
this property creates a new :class:`~chainer.Variable` object from
this node object and returns it.
Returns:
Variable: The variable object that refers this node.
"""
var = self._variable()
if var is not None:
return var
var = Variable(self.data, name=self.name,
requires_grad=self._requires_grad)
var._node = self
return var
def get_variable_or_none(self):
"""Returns the holding :class:`~chainer.Variable` object or ``None``.
VariableNode object holds a weak reference of the variable object.If
the reference is alive, it is returned by this property. Otherwise,
returns ``None``.
Returns:
Variable: The variable object that refers this node.
"""
return self._variable()
def set_creator(self, creator):
"""Sets a :class:`~chainer.Function` object that created this node.
This method is equivalent to ``self.creator = creator``. A
:class:`~chainer.FunctionNode` object can also be passed.
Args:
creator (Function or FunctionNode): Function that has created this
variable.
"""
self.creator = creator
def set_creator_node(self, creator_node):
"""Sets a :class:`~chainer.FunctionNode` object that created this node.
This method is equivalent to ``self.creator_node = creator_node``. A
:class:`~chainer.Function` object can also be passed, in which case the
:attr:`Function.node <chainer.Function.node>` attribute is used.
Args:
creator_node (FunctionNode or Function): Function node that has
this variable as an output.
"""
self.creator_node = creator_node
def unchain(self):
"""Deletes the reference to the creator of this variable node.
This method is equivalent to ``self.creator_node = None``.
"""
self.creator_node = None
def retain_data(self):
"""Lets the node hold a reference to the underlying data array.
This method gets the data array of the corresponding variable and keeps
it. If the weak reference to the corresponding variable is dead, it
raises an error.
"""
variable = self._variable()
if variable is not None:
self.data = variable.data
else:
raise RuntimeError('cannot retain variable data: the variable has '
'been already released')
def _update_data_info(self, d):
if d is None:
self.dtype = None
self.shape = None
else:
self.dtype = d.dtype
self.shape = d.shape
# If the node has a reference to data, update it as well.
if self._data is not None:
self._data = d
def _check_old_style_gradient(self):
if self._old_style_grad_generator is not None:
raise RuntimeError(
'cannot twice-differentiate an old style Function "%s"' %
self._old_style_grad_generator)
def _create_variable(data, name, grad, requires_grad):
return Variable(
data, name=name, grad=grad, requires_grad=requires_grad)
class Variable(object):
"""__init__(data=None, *, name=None, grad=None, requires_grad=True)
Array with a structure to keep track of computation.
Every variable holds a data array of type either :class:`numpy.ndarray` or
:class:`cupy.ndarray`.
A variable object holds a data array and a
:class:`~chainer.variable.VariableNode` object of
a computational graph. If the variable is constructed by the user, the node
is *root* and does not hold any parent. If the variable is constructed by a
:class:`~chainer.FunctionNode` object (i.e., by calling functions under
``chainer.functions`` or user-defined functions), or by using operators
(see the list below), the node holds a reference to its parent called
:attr:`creator_node`.
This reference is used in backpropagation to backtrack the graph.
Users can disable (resp. enable) this chaining behavior by calling
:func:`~chainer.no_backprop_mode` (resp.
:func:`~chainer.force_backprop_mode`).
In the former context, a variable never creates a computational graph,
whereas in the latter context, it is forced to create.
.. note::
The following operators are defined for variable(s).
* Indexing: ``a[slices]`` (:meth:`__getitem__`)
* Addition: ``a + b`` (:meth:`__add__`, :meth:`__radd__`)
* Subtraction: ``a - b`` (:meth:`__sub__`, :meth:`__rsub__`)
* Multiplication: ``a * b`` (:meth:`__mul__`, :meth:`__rmul__`)
* Division: ``a / b`` (:meth:`__div__`, :meth:`__rdiv__`, \
:meth:`__truediv__`, :meth:`__rtruediv__`)
* Floor Division: ``a // b`` (:meth:`__floordiv__`, \
:meth:`__rfloordiv__`)
* Exponentiation: ``a ** b`` (:meth:`__pow__`, :meth:`__rpow__`)
* Matrix Multiplication: ``a @ b`` (:meth:`__matmul__`, \
:meth:`__rmatmul__`)
* Negation (Arithmetic): ``- a`` (:meth:`__neg__`)
* Absolute value: ``abs(a)`` (:meth:`__abs__`)
.. warning::
``volatile`` argument is not supported anymore since v2.
Instead, use :func:`chainer.no_backprop_mode`.
Args:
data (numpy.ndarray or cupy.ndarray): Initial data array.
name (str): Name of the variable.
grad (numpy.ndarray or cupy.ndarray): Initial gradient array.
requires_grad (bool): Boolean indicating whether ``grad`` will be set
in backward calculation.
""" # NOQA
def __init__(self, data=None, **kwargs):
argument.check_unexpected_kwargs(
kwargs, volatile='volatile argument is not supported anymore. '
'Use chainer.using_config')
name, grad, requires_grad \
= argument.parse_kwargs(
kwargs, ('name', None), ('grad', None),
('requires_grad', True))
if (data is not None and
not isinstance(data, chainer.get_array_types())):
msg = '''numpy.ndarray or cuda.ndarray are expected.
Actual: {0}'''.format(type(data))
raise TypeError(msg)
# Use a list as a data structure to hold the data array indirectly to
# abstract its initialized/uninitialized state.
self._data = [data]
self._requires_grad = requires_grad
self._node = VariableNode(self, name)
self._grad_var = None if grad is None else Variable(grad)
self._loss_scale = None
def __copy__(self):
return self._copy_to(Variable())
def _copy_to(self, target):
target.__dict__ = copy.copy(self.__dict__)
target._node = VariableNode(target, self.name)
return target
def __reduce__(self):
return _create_variable, (self.data, self.name, self.grad,
self._requires_grad)
def __repr__(self):
return variable_repr(self)
def __str__(self):
return variable_str(self)
@property
def xp(self):
"""Array module for this variable.
Depending on which of CPU/GPU this variable is on, this property
returns :mod:`numpy` or :mod:`cupy`.
"""
return cuda.get_array_module(self)
@property
def name(self):
return self._node.name
@name.setter
def name(self, n):
self._node.name = n
def summary(self):
if self.name:
return '<variable %s>' % self.name
else:
return '<variable at 0x%x>' % id(self)
def debug_print(self):
"""Display a summary of the stored data and location of the Variable"""
msg = """{summary}
- device: {device}
- backend: {backend}
- shape: {shape}
- dtype: {dtype}
- statistics: {stats}
- grad: {grad}"""
stats_msg = 'mean={0:.8f}, std={1:.8f}'
data = self.data
with cuda.get_device_from_array(data) as dev:
xp = numpy if int(dev) == -1 else cuda.cupy
if data is None:
# `data` can be `None` if constructed without any arguments
device = None
backend = None
stats = None
else:
device = getattr(data, 'device', 'CPU')
backend = type(data)
stats = stats_msg.format(float(xp.mean(data)),
float(xp.std(data)))
shape = getattr(data, 'shape', None)
dtype = getattr(data, 'dtype', None)
if self.grad is None:
grad = None
elif xp.all(self.grad == 0):
grad = 0
else:
grad = stats_msg.format(float(xp.mean(self.grad)),
float(xp.std(self.grad)))
return msg.format(summary=self.summary(), device=device,
backend=backend, shape=shape, dtype=dtype,
stats=stats, grad=grad)
def __pos__(self):
return self
def __len__(self):
"""Returns the first dimension of the data array.
Returns:
int: Number of the first dimension of the data array.
"""
return len(self.data)
@property
def label(self):
"""Short text that represents the variable."""
return self._node.label
@property
def creator(self):
"""Function implementation that created this variable.
When this variable has been created by an old-style function (i.e., it
is implemented as a subclass of :class:`Function`), this property
returns that :class:`Function` object.
When this variable has been created by a new-style function (i.e., it
is implemented as a subclass of :class:`FunctionNode` class), this
property returns that node object.
"""
return self._node.creator
@creator.setter
def creator(self, func):
self._node.creator = func
@property
def creator_node(self):
""":class:`FunctionNode` object that created this variable.
This property has a setter to which ``None`` can be set. Setting
``None`` to this property is equivalent to call :meth:`unchain`;
it purges the variable from the function that created this variable.
The setter also accepts the original :class:`FunctionNode` object that
created this variable. For example, you can once set ``None`` to this
property and then set the original value again.
.. note::
Setting an irrelevant :meth:`FunctionNode` object does not emit any
error immediately, whereas the behavior is undefined. Do not set
a :meth:`FunctionNode` object that did not create this variable
object.
"""
return self._node._creator_node
@creator_node.setter
def creator_node(self, func):
self._node.creator_node = func
@property
def array(self):
"""The underlying data array.
It is either :class:`numpy.ndarray` or :class:`cupy.ndarray` object,
or ``None`` if the variable in in an uninitialized state.
"""
return self._data[0]
@array.setter
def array(self, d):
self._data[0] = d
self._node._update_data_info(d)
@property
def data(self):
"""The underlying data array (equivalent to :attr:`array`).
Note that using this attribute directly is discouraged; use
:attr:`array` instead. Using :attr:`array`, you can find an error
earlier when your code mixes up Variable and ndarray because
ndarray does not have an attribute ``.array`` while it has
``.data``.
"""
return self._data[0]
@data.setter
def data(self, d):
self._data[0] = d
self._node._update_data_info(d)
@property
def grad(self):
"""Gradient array of this variable.
Note that this property returns the underlying array of the gradient
variable instead of the gradient variable itself; to get/set
gradient variable, use :attr:`grad_var` instead.
"""
gv = self._grad_var
return None if gv is None else gv.data
@grad.setter
def grad(self, g):
self.grad_var = None if g is None else Variable(g)
@property
def grad_var(self):
"""Gradient variable."""
return self._grad_var
@grad_var.setter
def grad_var(self, g):
if g is not None:
_check_grad_type(None, self, g.data)
self._grad_var = g
@property
def shape(self):
return self.data.shape
@property
def ndim(self):
return self.data.ndim
@property
def size(self):
return self.data.size
@property
def dtype(self):
return self.data.dtype
@property
def rank(self):
return self._node.rank
@property
def node(self):
return self._node
@property
def requires_grad(self):
"""It indicates that ``grad`` will be set in backward calculation."""
return self._requires_grad
@property
def T(self):
"""Transposition of this variable."""
return chainer.functions.transpose(self)
def to_cpu(self):
"""Copies the data and gradient arrays to CPU."""
data = self.data
if data is None:
return
if isinstance(data, cuda.ndarray):
# cupy.ndarray to numpy.ndarray
self._data = [cuda.to_cpu(data)]
elif isinstance(data, intel64.mdarray):
# ideep.mdarray to numpy.ndarray
self._data = [numpy.array(data)]
if self._grad_var is not None:
self._grad_var.to_cpu()
# ensure that the node tracks the device migration
node = self._node
if node._data is not None:
node.retain_data()
def to_gpu(self, device=None):
"""Copies the data and gradient arrays to specified GPU.
Args:
device: Target device specifier. If omitted, the current device is
used.
"""
if self.data is None:
self._data = [None] # Renew placeholder to break sharing
else:
self._data = [cuda.to_gpu(self.data, device)]
if self._grad_var is not None:
self._grad_var.to_gpu(device)
# ensure that the node tracks the device migration
node = self._node
if node._data is not None:
node.retain_data()
def to_intel64(self):
"""Copies the data and gradient arrays to intel64 specific mdarray.
If the array is not suited for intel64, it will be converted to
:class:`numpy.ndarray`.
"""
intel64.check_ideep_available()
data = self.data
if data is not None:
if isinstance(data, cuda.ndarray):
# cupy.ndarray to numpy.ndarray
data = data.get()
if (isinstance(data, numpy.ndarray) and data.ndim in (1, 2, 4)):
# TODO(kmaehashi): Remove ndim validation once iDeep has fixed.
# Currently iDeep only supports (1, 2, 4)-dim arrays.
# Note that array returned from `ideep.array` may not be an
# iDeep mdarray, e.g., when the dtype is not float32.
data = intel64.ideep.array(
data, itype=intel64.ideep.wgt_array)
self._data = [data]
if self._grad_var is not None:
self._grad_var.to_intel64()
# ensure that the node tracks the device migration
node = self._node
if node._data is not None:
node.retain_data()
def cleargrad(self):
"""Clears the gradient array."""
self._grad_var = None
def zerograd(self):
"""Initializes the gradient array by zeros.
Note that the gradient variable is unchained from the computational
graph by this method because this operation breaks the backprop
validity.
.. deprecated:: v1.15
Use :meth:`cleargrad` instead.
"""
warnings.warn(
'Variable.zerograd is deprecated. Use Variable.cleargrad instead.',
DeprecationWarning)
if self.data is None:
return
with cuda.get_device_from_array(self.data) as dev:
gv = self._grad_var
if gv is None:
xp = numpy if dev.id == -1 else cuda.cupy
self.grad = xp.zeros_like(self.data)
else:
gv.unchain()
gv.data.fill(0)
def copydata(self, var):
"""Copies the data array from given source variable.
This method copies the data array from given variable to this variable.
The copy is done even if the arrays reside on different devices,
including across the host and a GPU device. If this variable has an
uninitialized data array, this method initializes it by the data array
of the given variable. Similarly, if the given variable has an
uninitialized data array, this method initializes it by the data array
of this variable (``self``). If both are uninitialized, this method
does nothing.
Args:
var (Variable): Source variable.
"""
src = var.data
dst = self.data
if src is None:
if dst is None:
return
var.initialize(self.shape)
src = var.data
elif dst is None:
self.initialize(src.shape)
dst = self.data
src_xp = cuda.get_array_module(src)
dst_xp = cuda.get_array_module(dst)
if dst_xp is src_xp:
dst_xp.copyto(dst, src)
elif dst_xp is numpy:
dst_xp.copyto(dst, src.get())
else:
dst.set(src)
def addgrad(self, var):
"""Accumulates the gradient array from given source variable.
This method adds the gradient of a given variable to the gradient of
this variable. The accumulation is even done across the host and
different devices. If this variable has uninitialized data/grad arrays,
this method initializes it with the shape of the given variable and
then accumulates the gradient.
Args:
var (Variable): Source variable.
"""
src = var._grad_var
if src is None:
return
if self.data is None:
self.initialize(var.shape)
dst = self._grad_var
src_dev = cuda.get_device_from_array(src.data)
dst_dev = cuda.get_device_from_array(self.data)
if src_dev.id != dst_dev.id:
src = chainer.functions.copy(src, dst_dev.id)
self._grad_var = src if dst is None else src + dst
def set_creator(self, gen_func):
"""Notifies the variable that the given function is its creator.
Args:
gen_func (Function): Function object that creates this variable as
one of its outputs.
"""
self._node.set_creator(gen_func)
def set_creator_node(self, fnode):
"""Notifies the variable that the given node is its creator.
Args:
fnode (FunctionNode): Function node that has this variable as an
output.
"""
self._node.set_creator_node(fnode)
def backward(self, retain_grad=False, enable_double_backprop=False,
loss_scale=None):
"""Runs error backpropagation (a.k.a.\\ backprop) from this variable.
On backprop,
:meth:`FunctionNode.backward() <chainer.FunctionNode.backward>`
is called on each :class:`~chainer.FunctionNode` object appearing in
the backward graph starting from this variable.
The backward graph is represented by backward
references from variable nodes to their creators, and from function
nodes to their input variable nodes. The backprop stops at all root
nodes. Some function nodes set ``None`` as gradients of some inputs,
where further backprop does not take place at such inputs.
This method uses :data:`grad` as the initial error array. User can
manually set a gradient array before calling this method.
If the shape of :data:`data` is ``()`` (i.e., it is scalar) and
:data:`grad` is ``None``, then this method automatically complements
1.0 as the initial error. This is useful on starting backprop from
some scalar loss value.
From v3, this method supports *differentiable backprop* (a.k.a. double
backprop, grad of grads). To enable it, pass
``enable_double_backprop=True``.
Args:
retain_grad (bool): If ``True``, the gradient arrays of all
intermediate variables are kept.
Otherwise, :data:`~chainer.Variable.grad` of the
intermediate variables are set to ``None`` on appropriate
timing, which may reduce the maximum memory consumption.
In most cases of training some models, the purpose of backprop
is to compute gradients of parameters, not of all variables,
and therefore it is recommended to set this flag ``False``.
enable_double_backprop (bool): *(Added in v3.0)* If ``True``,
computational trace of the whole backpropagation procedure is
recorded to the computational graph so that one can further do
backpropagation from the resulting gradients. Note that
enabling it results in larger memory consumption needed to
store the gradients w.r.t intermediate variables that are
required for the second gradient computation.
loss_scale (float): Loss scaling factor. Loss scaling is a usefull
technique to mitigate vanishing gradient issue that tends to
happen when low precision data type like float16 is used during
training. If you set loss scaling factor, gradients of loss
values are to be multiplied by the factor before backprop
starts. The factor is propagated to whole gradients in a
computational graph along the backprop. The gradients of
parameters are divided by the factor just before the parameters
are to be updated.
"""
with chainer.using_config('enable_backprop', enable_double_backprop):
self._backward_main(retain_grad, loss_scale)
def _backward_main(self, retain_grad, loss_scale):
self._node._check_old_style_gradient()
if self.creator_node is None:
return
initial_device = None
if cuda.available and isinstance(self.data, cuda.ndarray):
try:
initial_device = cuda.Device()
except cuda.cupy.cuda.runtime.CUDARuntimeError as e:
if e.status != 38: # cudaErrorNoDevice
raise
is_debug = chainer.is_debug()
cand_funcs = []
seen_set = set()
grads = {}
# Initialize error by 1, if this is a loss variable
if self.data.size == 1 and self._grad_var is None:
if self.data.ndim != 0:
warnings.warn(
'Treating a scalar as a variable with only one element'
' in Variable.backward is deprecated. A scalar variable'
' must be a 0-dimensional array. Apply'
' chainer.functions.squeeze to obtain a scalar variable.'
' If the size of this variable accidentally becomes one,'
' set zero to grad.',
DeprecationWarning)
with cuda.get_device_from_array(self.data) as device:
if device is cuda.DummyDevice:
self.grad = numpy.ones_like(self.data)
else: