/
base_layer.py
1357 lines (1181 loc) · 52.7 KB
/
base_layer.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
"""Contains the base Layer class, from which all layers inherit.
"""
from __future__ import print_function
from __future__ import absolute_import
from __future__ import division
import copy
import re
from six.moves import zip
from .. import backend as K
from .. import initializers
from ..utils.layer_utils import count_params
from ..utils.generic_utils import has_arg
from ..utils.generic_utils import object_list_uid
from ..utils.generic_utils import to_list
from ..utils.generic_utils import is_all_none
from ..legacy import interfaces
class Layer(object):
"""Abstract base layer class.
# Properties
name: String, must be unique within a model.
input_spec: List of InputSpec class instances
each entry describes one required input:
- ndim
- dtype
A layer with `n` input tensors must have
an `input_spec` of length `n`.
trainable: Boolean, whether the layer weights
will be updated during training.
uses_learning_phase: Whether any operation
of the layer uses `K.in_training_phase()`
or `K.in_test_phase()`.
input_shape: Shape tuple. Provided for convenience,
but note that there may be cases in which this
attribute is ill-defined (e.g. a shared layer
with multiple input shapes), in which case
requesting `input_shape` will raise an Exception.
Prefer using `layer.get_input_shape_for(input_shape)`,
or `layer.get_input_shape_at(node_index)`.
output_shape: Shape tuple. See above.
inbound_nodes: List of nodes.
outbound_nodes: List of nodes.
input, output: Input/output tensor(s). Note that if the layer is used
more than once (shared layer), this is ill-defined
and will raise an exception. In such cases, use
`layer.get_input_at(node_index)`.
input_mask, output_mask: Same as above, for masks.
trainable_weights: List of variables.
non_trainable_weights: List of variables.
weights: The concatenation of the lists trainable_weights and
non_trainable_weights (in this order).
# Methods
call(x, mask=None): Where the layer's logic lives.
__call__(x, mask=None): Wrapper around the layer logic (`call`).
If x is a Keras tensor:
- Connect current layer with last layer from tensor:
`self._add_inbound_node(last_layer)`
- Add layer to tensor history
If layer is not built:
- Build from x._keras_shape
get_weights()
set_weights(weights)
get_config()
count_params()
compute_output_shape(input_shape)
compute_mask(x, mask)
get_input_at(node_index)
get_output_at(node_index)
get_input_shape_at(node_index)
get_output_shape_at(node_index)
get_input_mask_at(node_index)
get_output_mask_at(node_index)
# Class Methods
from_config(config)
# Internal methods:
build(input_shape)
_add_inbound_node(layer, index=0)
assert_input_compatibility()
"""
def __init__(self, **kwargs):
self.input_spec = None
self.supports_masking = False
self.stateful = False
# These properties will be set upon call of self.build()
self._trainable_weights = []
self._non_trainable_weights = []
self._losses = []
self._updates = []
self._per_input_losses = {}
self._per_input_updates = {}
self._built = False
# These lists will be filled via successive calls
# to self._add_inbound_node().
self._inbound_nodes = []
self._outbound_nodes = []
# These properties should be set by the user via keyword arguments.
# note that 'dtype', 'input_shape' and 'batch_input_shape'
# are only applicable to input layers: do not pass these keywords
# to non-input layers.
allowed_kwargs = {'input_shape',
'batch_input_shape',
'batch_size',
'dtype',
'name',
'trainable',
'weights',
'input_dtype', # legacy
}
for kwarg in kwargs:
if kwarg not in allowed_kwargs:
raise TypeError('Keyword argument not understood:', kwarg)
name = kwargs.get('name')
if not name:
prefix = self.__class__.__name__
name = _to_snake_case(prefix) + '_' + str(K.get_uid(prefix))
self.name = name
self.trainable = kwargs.get('trainable', True)
if 'input_shape' in kwargs or 'batch_input_shape' in kwargs:
# In this case we will later create an input layer
# to insert before the current layer
if 'batch_input_shape' in kwargs:
batch_input_shape = tuple(kwargs['batch_input_shape'])
elif 'input_shape' in kwargs:
if 'batch_size' in kwargs:
batch_size = kwargs['batch_size']
else:
batch_size = None
batch_input_shape = (
batch_size,) + tuple(kwargs['input_shape'])
self.batch_input_shape = batch_input_shape
# Set dtype.
dtype = kwargs.get('dtype')
if dtype is None:
dtype = kwargs.get('input_dtype')
if dtype is None:
dtype = K.floatx()
self.dtype = dtype
if 'weights' in kwargs:
self._initial_weights = kwargs['weights']
else:
self._initial_weights = None
@staticmethod
def _node_key(layer, node_index):
"""Converts a layer and its index to a unique (immutable type) name.
This function is used internally with `self._network_nodes`.
# Arguments
layer: The layer.
node_index: The layer's position (e.g. via enumerate) in a list of
nodes.
# Returns
The unique name.
"""
return layer.name + '_ib-' + str(node_index)
@property
def losses(self):
return self._losses
@property
def updates(self):
if not self.trainable and not self.stateful:
return []
return self._updates
@property
def built(self):
return self._built
@built.setter
def built(self, value):
self._built = value
@property
def trainable_weights(self):
trainable = getattr(self, 'trainable', True)
if trainable:
return self._trainable_weights
else:
return []
@trainable_weights.setter
def trainable_weights(self, weights):
self._trainable_weights = weights
@property
def non_trainable_weights(self):
trainable = getattr(self, 'trainable', True)
if not trainable:
return self._trainable_weights + self._non_trainable_weights
else:
return self._non_trainable_weights
@non_trainable_weights.setter
def non_trainable_weights(self, weights):
self._non_trainable_weights = weights
@interfaces.legacy_add_weight_support
def add_weight(self,
name,
shape,
dtype=None,
initializer=None,
regularizer=None,
trainable=True,
constraint=None):
"""Adds a weight variable to the layer.
# Arguments
name: String, the name for the weight variable.
shape: The shape tuple of the weight.
dtype: The dtype of the weight.
initializer: An Initializer instance (callable).
regularizer: An optional Regularizer instance.
trainable: A boolean, whether the weight should
be trained via backprop or not (assuming
that the layer itself is also trainable).
constraint: An optional Constraint instance.
# Returns
The created weight variable.
"""
initializer = initializers.get(initializer)
if dtype is None:
dtype = K.floatx()
weight = K.variable(initializer(shape),
dtype=dtype,
name=name,
constraint=constraint)
if regularizer is not None:
with K.name_scope('weight_regularizer'):
self.add_loss(regularizer(weight))
if trainable:
self._trainable_weights.append(weight)
else:
self._non_trainable_weights.append(weight)
return weight
def assert_input_compatibility(self, inputs):
"""Checks compatibility between the layer and provided inputs.
This checks that the tensor(s) `input`
verify the input assumptions of the layer
(if any). If not, exceptions are raised.
# Arguments
inputs: input tensor or list of input tensors.
# Raises
ValueError: in case of mismatch between
the provided inputs and the expectations of the layer.
"""
inputs = to_list(inputs)
for x in inputs:
try:
K.is_keras_tensor(x)
except ValueError:
raise ValueError('Layer ' + self.name + ' was called with '
'an input that isn\'t a symbolic tensor. '
'Received type: ' +
str(type(x)) + '. Full input: ' +
str(inputs) + '. All inputs to the layer '
'should be tensors.')
if not self.input_spec:
return
if not isinstance(self.input_spec, (list, tuple)):
input_spec = to_list(self.input_spec)
else:
input_spec = self.input_spec
if len(inputs) != len(input_spec):
raise ValueError('Layer ' + self.name + ' expects ' +
str(len(input_spec)) + ' inputs, '
'but it received ' + str(len(inputs)) +
' input tensors. Input received: ' +
str(inputs))
for input_index, (x, spec) in enumerate(zip(inputs, input_spec)):
if spec is None:
continue
# Check ndim.
if spec.ndim is not None:
if K.ndim(x) != spec.ndim:
raise ValueError('Input ' + str(input_index) +
' is incompatible with layer ' +
self.name + ': expected ndim=' +
str(spec.ndim) + ', found ndim=' +
str(K.ndim(x)))
if spec.max_ndim is not None:
ndim = K.ndim(x)
if ndim is not None and ndim > spec.max_ndim:
raise ValueError('Input ' + str(input_index) +
' is incompatible with layer ' +
self.name + ': expected max_ndim=' +
str(spec.max_ndim) + ', found ndim=' +
str(K.ndim(x)))
if spec.min_ndim is not None:
ndim = K.ndim(x)
if ndim is not None and ndim < spec.min_ndim:
raise ValueError('Input ' + str(input_index) +
' is incompatible with layer ' +
self.name + ': expected min_ndim=' +
str(spec.min_ndim) + ', found ndim=' +
str(K.ndim(x)))
# Check dtype.
if spec.dtype is not None:
if K.dtype(x) != spec.dtype:
raise ValueError('Input ' + str(input_index) +
' is incompatible with layer ' +
self.name + ': expected dtype=' +
str(spec.dtype) + ', found dtype=' +
str(K.dtype(x)))
# Check specific shape axes.
if spec.axes:
try:
x_shape = K.int_shape(x)
except TypeError:
x_shape = None
if x_shape is not None:
for axis, value in spec.axes.items():
if (value is not None and
x_shape[int(axis)] not in {value, None}):
raise ValueError(
'Input ' + str(input_index) +
' is incompatible with layer ' +
self.name + ': expected axis ' +
str(axis) + ' of input shape to have '
'value ' + str(value) +
' but got shape ' + str(x_shape))
# Check shape.
if spec.shape is not None:
try:
x_shape = K.int_shape(x)
except TypeError:
x_shape = None
if x_shape is not None:
for spec_dim, dim in zip(spec.shape, x_shape):
if spec_dim is not None and dim is not None:
if spec_dim != dim:
raise ValueError(
'Input ' + str(input_index) +
' is incompatible with layer ' +
self.name + ': expected shape=' +
str(spec.shape) + ', found shape=' +
str(x_shape))
def call(self, inputs, **kwargs):
"""This is where the layer's logic lives.
# Arguments
inputs: Input tensor, or list/tuple of input tensors.
**kwargs: Additional keyword arguments.
# Returns
A tensor or list/tuple of tensors.
"""
return inputs
def __call__(self, inputs, **kwargs):
"""Wrapper around self.call(), for handling internal references.
If a Keras tensor is passed:
- We call self._add_inbound_node().
- If necessary, we `build` the layer to match
the _keras_shape of the input(s).
- We update the _keras_shape of every input tensor with
its new shape (obtained via self.compute_output_shape).
This is done as part of _add_inbound_node().
- We update the _keras_history of the output tensor(s)
with the current layer.
This is done as part of _add_inbound_node().
# Arguments
inputs: Can be a tensor or list/tuple of tensors.
**kwargs: Additional keyword arguments to be passed to `call()`.
# Returns
Output of the layer's `call` method.
# Raises
ValueError: in case the layer is missing shape information
for its `build` call.
"""
if isinstance(inputs, list):
inputs = inputs[:]
with K.name_scope(self.name):
# Handle laying building (weight creating, input spec locking).
if not self.built:
# Raise exceptions in case the input is not compatible
# with the input_spec specified in the layer constructor.
self.assert_input_compatibility(inputs)
# Collect input shapes to build layer.
input_shapes = []
for x_elem in to_list(inputs):
if hasattr(x_elem, '_keras_shape'):
input_shapes.append(x_elem._keras_shape)
elif hasattr(K, 'int_shape'):
input_shapes.append(K.int_shape(x_elem))
else:
raise ValueError('You tried to call layer "' +
self.name +
'". This layer has no information'
' about its expected input shape, '
'and thus cannot be built. '
'You can build it manually via: '
'`layer.build(batch_input_shape)`')
if len(input_shapes) == 1:
self.build(input_shapes[0])
else:
self.build(input_shapes)
self.built = True
# Load weights that were specified at layer instantiation.
if self._initial_weights is not None:
self.set_weights(self._initial_weights)
# Raise exceptions in case the input is not compatible
# with the input_spec set at build time.
self.assert_input_compatibility(inputs)
# Handle mask propagation.
previous_mask = _collect_previous_mask(inputs)
user_kwargs = copy.copy(kwargs)
if not is_all_none(previous_mask):
# The previous layer generated a mask.
if has_arg(self.call, 'mask'):
if 'mask' not in kwargs:
# If mask is explicitly passed to __call__,
# we should override the default mask.
kwargs['mask'] = previous_mask
# Handle automatic shape inference (only useful for Theano).
input_shape = _collect_input_shape(inputs)
# Actually call the layer,
# collecting output(s), mask(s), and shape(s).
output = self.call(inputs, **kwargs)
output_mask = self.compute_mask(inputs, previous_mask)
# If the layer returns tensors from its inputs, unmodified,
# we copy them to avoid loss of tensor metadata.
output_ls = to_list(output)
inputs_ls = to_list(inputs)
output_ls_copy = []
for x in output_ls:
if x in inputs_ls:
x = K.identity(x)
output_ls_copy.append(x)
if len(output_ls_copy) == 1:
output = output_ls_copy[0]
else:
output = output_ls_copy
# Inferring the output shape is only relevant for Theano.
if all([s is not None
for s in to_list(input_shape)]):
output_shape = self.compute_output_shape(input_shape)
else:
if isinstance(input_shape, list):
output_shape = [None for _ in input_shape]
else:
output_shape = None
if (not isinstance(output_mask, (list, tuple)) and
len(output_ls) > 1):
# Augment the mask to match the length of the output.
output_mask = [output_mask] * len(output_ls)
# Add an inbound node to the layer, so that it keeps track
# of the call and of all new variables created during the call.
# This also updates the layer history of the output tensor(s).
# If the input tensor(s) had not previous Keras history,
# this does nothing.
self._add_inbound_node(input_tensors=inputs,
output_tensors=output,
input_masks=previous_mask,
output_masks=output_mask,
input_shapes=input_shape,
output_shapes=output_shape,
arguments=user_kwargs)
# Apply activity regularizer if any:
if (hasattr(self, 'activity_regularizer') and
self.activity_regularizer is not None):
with K.name_scope('activity_regularizer'):
regularization_losses = [
self.activity_regularizer(x)
for x in to_list(output)]
self.add_loss(regularization_losses,
inputs=to_list(inputs))
return output
def _add_inbound_node(self, input_tensors, output_tensors,
input_masks, output_masks,
input_shapes, output_shapes, arguments=None):
"""Internal method to create an inbound node for the layer.
# Arguments
input_tensors: list of input tensors.
output_tensors: list of output tensors.
input_masks: list of input masks (a mask can be a tensor, or None).
output_masks: list of output masks
(a mask can be a tensor, or None).
input_shapes: list of input shape tuples.
output_shapes: list of output shape tuples.
arguments: dictionary of keyword arguments that were passed to the
`call` method of the layer at the call that created the node.
"""
input_tensors = to_list(input_tensors)
output_tensors = to_list(output_tensors)
input_masks = to_list(input_masks)
output_masks = to_list(output_masks)
input_shapes = to_list(input_shapes)
output_shapes = to_list(output_shapes)
# Collect input tensor(s) coordinates.
inbound_layers = []
node_indices = []
tensor_indices = []
for x in input_tensors:
if hasattr(x, '_keras_history'):
inbound_layer, node_index, tensor_index = x._keras_history
inbound_layers.append(inbound_layer)
node_indices.append(node_index)
tensor_indices.append(tensor_index)
else:
inbound_layers.append(None)
node_indices.append(None)
tensor_indices.append(None)
# Create node, add it to inbound nodes.
Node(
self,
inbound_layers=inbound_layers,
node_indices=node_indices,
tensor_indices=tensor_indices,
input_tensors=input_tensors,
output_tensors=output_tensors,
input_masks=input_masks,
output_masks=output_masks,
input_shapes=input_shapes,
output_shapes=output_shapes,
arguments=arguments
)
# Update tensor history, _keras_shape and _uses_learning_phase.
for i in range(len(output_tensors)):
output_tensors[i]._keras_shape = output_shapes[i]
uses_lp = any(
[getattr(x, '_uses_learning_phase', False)
for x in input_tensors])
uses_lp = getattr(self, 'uses_learning_phase', False) or uses_lp
output_tensors[i]._uses_learning_phase = getattr(
output_tensors[i], '_uses_learning_phase', False) or uses_lp
output_tensors[i]._keras_history = (self,
len(self._inbound_nodes) - 1,
i)
def compute_output_shape(self, input_shape):
"""Computes the output shape of the layer.
Assumes that the layer will be built
to match that input shape provided.
# Arguments
input_shape: Shape tuple (tuple of integers)
or list of shape tuples (one per output tensor of the layer).
Shape tuples can include None for free dimensions,
instead of an integer.
# Returns
An input shape tuple.
"""
return input_shape
def compute_mask(self, inputs, mask=None):
"""Computes an output mask tensor.
# Arguments
inputs: Tensor or list of tensors.
mask: Tensor or list of tensors.
# Returns
None or a tensor (or list of tensors,
one per output tensor of the layer).
"""
if not self.supports_masking:
if mask is not None:
if isinstance(mask, list):
if any(m is not None for m in mask):
raise TypeError('Layer ' + self.name +
' does not support masking, '
'but was passed an input_mask: ' +
str(mask))
else:
raise TypeError('Layer ' + self.name +
' does not support masking, '
'but was passed an input_mask: ' +
str(mask))
# masking not explicitly supported: return None as mask
return None
# if masking is explicitly supported, by default
# carry over the input mask
return mask
def build(self, input_shape):
"""Creates the layer weights.
Must be implemented on all layers that have weights.
# Arguments
input_shape: Keras tensor (future input to layer)
or list/tuple of Keras tensors to reference
for weight shape computations.
"""
self.built = True
def _get_node_attribute_at_index(self, node_index, attr, attr_name):
"""Retrieves an attribute (e.g. input_tensors) from a node.
This is used to implement the methods:
- get_input_shape_at
- get_output_shape_at
- get_input_at
etc...
# Arguments
node_index: Integer index of the node from which
to retrieve the attribute.
attr: Exact node attribute name.
attr_name: Human-readable attribute name, for error messages.
# Returns
The layer's attribute `attr` at the node of index `node_index`.
# Raises
RuntimeError: If the layer has no inbound nodes.
ValueError: If the index is does not match any node.
"""
if not self._inbound_nodes:
raise RuntimeError('The layer has never been called '
'and thus has no defined ' + attr_name + '.')
if not len(self._inbound_nodes) > node_index:
raise ValueError('Asked to get ' + attr_name +
' at node ' + str(node_index) +
', but the layer has only ' +
str(len(self._inbound_nodes)) + ' inbound nodes.')
values = getattr(self._inbound_nodes[node_index], attr)
if len(values) == 1:
return values[0]
else:
return values
def get_input_shape_at(self, node_index):
"""Retrieves the input shape(s) of a layer at a given node.
# Arguments
node_index: Integer, index of the node
from which to retrieve the attribute.
E.g. `node_index=0` will correspond to the
first time the layer was called.
# Returns
A shape tuple
(or list of shape tuples if the layer has multiple inputs).
"""
return self._get_node_attribute_at_index(node_index,
'input_shapes',
'input shape')
def get_output_shape_at(self, node_index):
"""Retrieves the output shape(s) of a layer at a given node.
# Arguments
node_index: Integer, index of the node
from which to retrieve the attribute.
E.g. `node_index=0` will correspond to the
first time the layer was called.
# Returns
A shape tuple
(or list of shape tuples if the layer has multiple outputs).
"""
return self._get_node_attribute_at_index(node_index,
'output_shapes',
'output shape')
def get_input_at(self, node_index):
"""Retrieves the input tensor(s) of a layer at a given node.
# Arguments
node_index: Integer, index of the node
from which to retrieve the attribute.
E.g. `node_index=0` will correspond to the
first time the layer was called.
# Returns
A tensor (or list of tensors if the layer has multiple inputs).
"""
return self._get_node_attribute_at_index(node_index,
'input_tensors',
'input')
def get_output_at(self, node_index):
"""Retrieves the output tensor(s) of a layer at a given node.
# Arguments
node_index: Integer, index of the node
from which to retrieve the attribute.
E.g. `node_index=0` will correspond to the
first time the layer was called.
# Returns
A tensor (or list of tensors if the layer has multiple outputs).
"""
return self._get_node_attribute_at_index(node_index,
'output_tensors',
'output')
def get_input_mask_at(self, node_index):
"""Retrieves the input mask tensor(s) of a layer at a given node.
# Arguments
node_index: Integer, index of the node
from which to retrieve the attribute.
E.g. `node_index=0` will correspond to the
first time the layer was called.
# Returns
A mask tensor
(or list of tensors if the layer has multiple inputs).
"""
return self._get_node_attribute_at_index(node_index,
'input_masks',
'input mask')
def get_output_mask_at(self, node_index):
"""Retrieves the output mask tensor(s) of a layer at a given node.
# Arguments
node_index: Integer, index of the node
from which to retrieve the attribute.
E.g. `node_index=0` will correspond to the
first time the layer was called.
# Returns
A mask tensor
(or list of tensors if the layer has multiple outputs).
"""
return self._get_node_attribute_at_index(node_index,
'output_masks',
'output mask')
@property
def input(self):
"""Retrieves the input tensor(s) of a layer.
Only applicable if the layer has exactly one inbound node,
i.e. if it is connected to one incoming layer.
# Returns
Input tensor or list of input tensors.
# Raises
AttributeError: if the layer is connected to
more than one incoming layers.
"""
if len(self._inbound_nodes) > 1:
raise AttributeError('Layer ' + self.name +
' has multiple inbound nodes, '
'hence the notion of "layer input" '
'is ill-defined. '
'Use `get_input_at(node_index)` instead.')
elif not self._inbound_nodes:
raise AttributeError('Layer ' + self.name +
' is not connected, no input to return.')
return self._get_node_attribute_at_index(0, 'input_tensors',
'input')
@property
def output(self):
"""Retrieves the output tensor(s) of a layer.
Only applicable if the layer has exactly one inbound node,
i.e. if it is connected to one incoming layer.
# Returns
Output tensor or list of output tensors.
# Raises
AttributeError: if the layer is connected to
more than one incoming layers.
"""
if not self._inbound_nodes:
raise AttributeError('Layer ' + self.name +
' has no inbound nodes.')
if len(self._inbound_nodes) > 1:
raise AttributeError('Layer ' + self.name +
' has multiple inbound nodes, '
'hence the notion of "layer output" '
'is ill-defined. '
'Use `get_output_at(node_index)` instead.')
return self._get_node_attribute_at_index(0, 'output_tensors',
'output')
@property
def input_mask(self):
"""Retrieves the input mask tensor(s) of a layer.
Only applicable if the layer has exactly one inbound node,
i.e. if it is connected to one incoming layer.
# Returns
Input mask tensor (potentially None) or list of input
mask tensors.
# Raises
AttributeError: if the layer is connected to
more than one incoming layers.
"""
if len(self._inbound_nodes) != 1:
raise AttributeError('Layer ' + self.name +
' has multiple inbound nodes, ' +
'hence the notion of "layer input mask" '
'is ill-defined. '
'Use `get_input_mask_at(node_index)` '
'instead.')
return self._get_node_attribute_at_index(0, 'input_masks',
'input mask')
@property
def output_mask(self):
"""Retrieves the output mask tensor(s) of a layer.
Only applicable if the layer has exactly one inbound node,
i.e. if it is connected to one incoming layer.
# Returns
Output mask tensor (potentially None) or list of output
mask tensors.
# Raises
AttributeError: if the layer is connected to
more than one incoming layers.
"""
if len(self._inbound_nodes) != 1:
raise AttributeError('Layer ' + self.name +
' has multiple inbound nodes, '
'hence the notion of "layer output mask" '
'is ill-defined. '
'Use `get_output_mask_at(node_index)` '
'instead.')
return self._get_node_attribute_at_index(0, 'output_masks',
'output mask')
@property
def input_shape(self):
"""Retrieves the input shape tuple(s) of a layer.
Only applicable if the layer has exactly one inbound node,
i.e. if it is connected to one incoming layer.
# Returns
Input shape tuple
(or list of input shape tuples, one tuple per input tensor).
# Raises
AttributeError: if the layer is connected to
more than one incoming layers.
"""
if not self._inbound_nodes:
raise AttributeError('The layer has never been called '
'and thus has no defined input shape.')
all_input_shapes = set(
[str(node.input_shapes) for node in self._inbound_nodes])
if len(all_input_shapes) == 1:
input_shapes = self._inbound_nodes[0].input_shapes
if len(input_shapes) == 1:
return input_shapes[0]
else:
return input_shapes
else:
raise AttributeError('The layer "' + str(self.name) +
' has multiple inbound nodes, '
'with different input shapes. Hence '
'the notion of "input shape" is '
'ill-defined for the layer. '
'Use `get_input_shape_at(node_index)` '
'instead.')
@property
def output_shape(self):
"""Retrieves the output shape tuple(s) of a layer.
Only applicable if the layer has one inbound node,
or if all inbound nodes have the same output shape.
# Returns
Output shape tuple
(or list of input shape tuples, one tuple per output tensor).
# Raises
AttributeError: if the layer is connected to
more than one incoming layers.
"""
if not self._inbound_nodes:
raise AttributeError('The layer has never been called '
'and thus has no defined output shape.')
all_output_shapes = set(
[str(node.output_shapes) for node in self._inbound_nodes])
if len(all_output_shapes) == 1:
output_shapes = self._inbound_nodes[0].output_shapes
if len(output_shapes) == 1:
return output_shapes[0]
else:
return output_shapes
else:
raise AttributeError('The layer "' + str(self.name) +
' has multiple inbound nodes, '
'with different output shapes. Hence '
'the notion of "output shape" is '
'ill-defined for the layer. '
'Use `get_output_shape_at(node_index)` '
'instead.')
def add_loss(self, losses, inputs=None):
"""Adds losses to the layer.
The loss may potentially be conditional on some inputs tensors,
for instance activity losses are conditional on the layer's inputs.
# Arguments
losses: loss tensor or list of loss tensors
to add to the layer.
inputs: input tensor or list of inputs tensors to mark
the losses as conditional on these inputs.
If None is passed, the loss is assumed unconditional
(e.g. L2 weight regularization, which only depends
on the layer's weights variables, not on any inputs tensors).
"""
if losses is None or losses == []:
return
# Update self.losses
losses = to_list(losses)
if hasattr(self, '_losses'):
self._losses += losses
# Update self._per_input_updates
if isinstance(inputs, list) and inputs == []:
inputs = None
if inputs is not None:
inputs_hash = object_list_uid(inputs)
else:
# Updates indexed by None are unconditional
# rather than input-dependent
inputs_hash = None
if inputs_hash not in self._per_input_losses:
self._per_input_losses[inputs_hash] = []
self._per_input_losses[inputs_hash] += losses
def add_update(self, updates, inputs=None):
"""Adds updates to the layer.
The updates may potentially be conditional on some inputs tensors,
for instance batch norm updates are conditional on the layer's inputs.
# Arguments
updates: update op or list of update ops
to add to the layer.
inputs: input tensor or list of inputs tensors to mark
the updates as conditional on these inputs.
If None is passed, the updates are assumed unconditional.
"""
if updates is None or updates == []:
return
# Update self.updates
updates = to_list(updates)
if hasattr(self, '_updates'):
self._updates += updates
# Update self._per_input_updates
if isinstance(inputs, list) and inputs == []:
inputs = None
if inputs is not None:
inputs_hash = object_list_uid(inputs)