形式
Python
投稿日時
2018-03-06 20:50
公開期間
無期限
  1. # Copyright 2015 The TensorFlow Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # =============================================================================
  15. # pylint: disable=unused-import,g-bad-import-order
  16. """Contains the base Layer class, from which all layers inherit."""
  17. from __future__ import absolute_import
  18. from __future__ import division
  19. from __future__ import print_function
  20. import collections
  21. import copy
  22. import re
  23. import weakref
  24. import numpy as np
  25. from tensorflow.python.eager import context
  26. from tensorflow.python.estimator import util as estimator_util
  27. from tensorflow.python.framework import dtypes
  28. from tensorflow.python.framework import ops
  29. from tensorflow.python.framework import tensor_shape
  30. from tensorflow.python.layers import utils as layers_util
  31. from tensorflow.python.framework import tensor_util
  32. from tensorflow.python.ops import array_ops
  33. from tensorflow.python.ops import variable_scope as vs
  34. from tensorflow.python.ops import variables as tf_variables
  35. from tensorflow.python.platform import tf_logging as logging
  36. from tensorflow.python.training import checkpointable
  37. from tensorflow.python.util import nest
  38. from tensorflow.python.util.tf_export import tf_export
  39. @tf_export('layers.Layer')
  40. class Layer(checkpointable.CheckpointableBase):
  41. """Base layer class.
  42. This is the class from which all layers inherit, implementing common
  43. infrastructure functionality.
  44. A layer is a class implementing common neural networks operations, such
  45. as convolution, batch norm, etc. These operations require managing variables,
  46. losses, and updates, as well as applying TensorFlow ops to input tensors.
  47. Users will just instantiate it and then treat it as a callable.
  48. We recommend that descendants of Layer implement the following methods:
  49. * `__init__()`: Save configuration in member variables
  50. * `build()`: Called once from `__call__`, when we know the shapes of inputs
  51. and `dtype`. Should have the calls to `add_variable()`, and then
  52. call the super's `build()` (which sets `self.built = True`, which is
  53. nice in case the user wants to call `build()` manually before the
  54. first `__call__`).
  55. * `call()`: Called in `__call__` after making sure `build()` has been called
  56. once. Should actually perform the logic of applying the layer to the
  57. input tensors (which should be passed in as the first argument).
  58. Read-only properties:
  59. `name`: The name of the layer (string).
  60. `dtype`: Default dtype of the layer (default of `None` means use the
  61. type of the first input).
  62. `trainable_variables`: List of trainable variables.
  63. `non_trainable_variables`: List of non-trainable variables.
  64. `variables`: List of all variables of this layer, trainable and
  65. non-trainable.
  66. `updates`: List of update ops of this layer.
  67. `losses`: List of losses added by this layer.
  68. Mutable properties:
  69. `trainable`: Whether the layer should be trained (boolean).
  70. `input_spec`: Optional (list of) `InputSpec` object(s) specifying the
  71. constraints on inputs that can be accepted by the layer.
  72. """
  73. def __init__(self, trainable=True, name=None, dtype=None,
  74. activity_regularizer=None, **kwargs):
  75. # We use a kwargs dict here because these kwargs only exist
  76. # for compatibility reasons.
  77. # The list of kwargs is subject to changes in the future.
  78. # We do not want to commit to it or to expose the list to users at all.
  79. # Note this is exactly as safe as defining kwargs in the function signature,
  80. # the only difference being that the list of valid kwargs is defined
  81. # below rather rather in the signature, and default values are defined
  82. # in calls to kwargs.get().
  83. allowed_kwargs = {
  84. '_scope',
  85. '_reuse',
  86. 'input_shape', # For compatibility with Keras `Sequential` model.
  87. 'batch_size', # For compatibility with Keras `Sequential` model.
  88. }
  89. for kwarg in kwargs:
  90. if kwarg not in allowed_kwargs:
  91. raise TypeError('Keyword argument not understood:', kwarg)
  92. # Mutable properties
  93. # Indicates whether the layer's weights are updated during training
  94. # and whether the layer's updates are run during training
  95. self.trainable = trainable
  96. # A stateful layer is a layer whose updates are run during inference too,
  97. # for instance stateful RNNs.
  98. self.stateful = False
  99. # Indicates whether `build` needs to be called upon layer call, to create
  100. # the layer's weights.
  101. self.built = False
  102. # Provides information about which inputs are compatible with the layer.
  103. self.input_spec = None
  104. if activity_regularizer and context.in_eager_mode():
  105. raise ValueError(
  106. ('Activity regularization is not supported when executing eagerly. '
  107. 'Got activity_regularizer=%s') % (activity_regularizer,))
  108. self._activity_regularizer = activity_regularizer
  109. self._trainable_weights = []
  110. self._non_trainable_weights = []
  111. self._updates = []
  112. # When executing eagerly, _losses is a list of zero-argument lambdas which
  113. # return tensors. When using graph execution, _losses is a list of ops.
  114. self._losses = []
  115. self._reuse = kwargs.get('_reuse')
  116. self._graph = ops.get_default_graph()
  117. self._dtype = None if dtype is None else dtypes.as_dtype(dtype).name
  118. call_fn_args = estimator_util.fn_args(self.call)
  119. self._compute_previous_mask = ('mask' in call_fn_args or
  120. hasattr(self, 'compute_mask'))
  121. self._call_has_scope_arg = 'scope' in call_fn_args
  122. # These lists will be filled via successive calls
  123. # to self._add_inbound_node().
  124. self._inbound_nodes = []
  125. self._outbound_nodes = []
  126. self._init_set_name(name)
  127. # Determine variable scope.
  128. scope = kwargs.get('_scope')
  129. if scope:
  130. with vs.variable_scope(scope) as captured_scope:
  131. self._scope = captured_scope
  132. else:
  133. self._scope = None
  134. # Set `_batch_input_shape` attribute
  135. # for compatibility with Keras `Sequential` model.
  136. if 'input_shape' in kwargs:
  137. batch_size = kwargs.get('batch_size')
  138. self._batch_input_shape = (batch_size,) + tuple(kwargs['input_shape'])
  139. def _init_set_name(self, name):
  140. # Determine layer name (non-unique).
  141. if isinstance(name, vs.VariableScope):
  142. base_name = name.name
  143. else:
  144. base_name = name
  145. self._name = name
  146. if not name:
  147. self._name, base_name = self._make_unique_name()
  148. self._base_name = base_name
  149. @property
  150. def dtype(self):
  151. return self._dtype
  152. @property
  153. def name(self):
  154. return self._name
  155. @property
  156. def activity_regularizer(self):
  157. """Optional regularizer function for the output of this layer."""
  158. return self._activity_regularizer
  159. @property
  160. def scope_name(self):
  161. if not self._scope:
  162. raise ValueError('No name available for layer scope because the layer "' +
  163. self._name + '" has not been used yet. The scope name ' +
  164. ' is determined the first time the layer instance is ' +
  165. 'called. You must therefore call the layer before ' +
  166. 'querying `scope_name`.')
  167. return self._scope.name
  168. @property
  169. def trainable_weights(self):
  170. return self._trainable_weights if self.trainable else []
  171. @property
  172. def non_trainable_weights(self):
  173. if self.trainable:
  174. return self._non_trainable_weights
  175. else:
  176. return self._trainable_weights + self._non_trainable_weights
  177. @property
  178. def trainable_variables(self):
  179. return self.trainable_weights
  180. @property
  181. def non_trainable_variables(self):
  182. return self.non_trainable_weights
  183. @property
  184. def weights(self):
  185. """Returns the list of all layer variables/weights.
  186. Returns:
  187. A list of variables.
  188. """
  189. return self.trainable_weights + self.non_trainable_weights
  190. @property
  191. def variables(self):
  192. """Returns the list of all layer variables/weights.
  193. Returns:
  194. A list of variables.
  195. """
  196. return self.weights
  197. @property
  198. def updates(self):
  199. if context.in_eager_mode():
  200. raise RuntimeError('Layer.updates not supported in Eager mode.')
  201. if not self.trainable and not self.stateful:
  202. return []
  203. return self._updates
  204. def add_update(self, updates, inputs=None):
  205. """Add update op(s), potentially dependent on layer inputs.
  206. Weight updates (for instance, the updates of the moving mean and variance
  207. in a BatchNormalization layer) may be dependent on the inputs passed
  208. when calling a layer. Hence, when reusing the same layer on
  209. different inputs `a` and `b`, some entries in `layer.updates` may be
  210. dependent on `a` and some on `b`. This method automatically keeps track
  211. of dependencies.
  212. The `get_updates_for` method allows to retrieve the updates relevant to a
  213. specific set of inputs.
  214. This call is ignored in Eager mode.
  215. Arguments:
  216. updates: Update op, or list/tuple of update ops.
  217. inputs: If anything other than None is passed, it signals the updates
  218. are conditional on some of the layer's inputs,
  219. and thus they should only be run where these inputs are available.
  220. This is the case for BatchNormalization updates, for instance.
  221. If None, the updates will be taken into account unconditionally,
  222. and you are responsible for making sure that any dependency they might
  223. have is available at runtime.
  224. A step counter might fall into this category.
  225. """
  226. if context.in_eager_mode():
  227. return # Updates already applied when in eager mode.
  228. updates = _to_list(updates)
  229. updates = [x if isinstance(x, ops.Operation)
  230. else ops.convert_to_tensor(x) for x in updates]
  231. self._updates += updates
  232. if inputs is None:
  233. for u in updates:
  234. u._unconditional_update = True # pylint: disable=protected-access
  235. else:
  236. for u in updates:
  237. u._unconditional_update = False # pylint: disable=protected-access
  238. def get_updates_for(self, inputs):
  239. """Retrieves updates relevant to a specific set of inputs.
  240. Arguments:
  241. inputs: Input tensor or list/tuple of input tensors.
  242. Returns:
  243. List of update ops of the layer that depend on `inputs`.
  244. Raises:
  245. RuntimeError: If called in Eager mode.
  246. """
  247. if context.in_eager_mode():
  248. raise RuntimeError('`get_updates_for()` not supported in Eager mode.')
  249. # Updates disabled if layer is not trainable and not explicitly stateful.
  250. if not self.trainable and not self.stateful:
  251. return []
  252. if inputs is None:
  253. # Requesting unconditional updates.
  254. return [x for x in self.updates if x._unconditional_update] # pylint: disable=protected-access
  255. # Requesting input-conditional updates.
  256. inputs = nest.flatten(inputs)
  257. reachable = layers_util.get_reachable_from_inputs(inputs, self.updates)
  258. updates = []
  259. for update in self.updates:
  260. if update in reachable:
  261. updates.append(update)
  262. return updates
  263. @property
  264. def losses(self):
  265. """Losses which are associated with this `Layer`.
  266. Note that when executing eagerly, getting this property evaluates
  267. regularizers. When using graph execution, variable regularization ops have
  268. already been created and are simply returned here.
  269. Returns:
  270. A list of tensors.
  271. """
  272. if context.in_eager_mode():
  273. # _losses may only contain variable regularization losses when executing
  274. # eagerly, and they have been saved as lambdas to be executed when
  275. # requested.
  276. return [regularizer() for regularizer in self._losses]
  277. else:
  278. return self._losses
  279. def add_loss(self, losses, inputs=None):
  280. """Add loss tensor(s), potentially dependent on layer inputs.
  281. Some losses (for instance, activity regularization losses) may be dependent
  282. on the inputs passed when calling a layer. Hence, when reusing the same
  283. layer on different inputs `a` and `b`, some entries in `layer.losses` may
  284. be dependent on `a` and some on `b`. This method automatically keeps track
  285. of dependencies.
  286. The `get_losses_for` method allows to retrieve the losses relevant to a
  287. specific set of inputs.
  288. Note that `add_loss` is not supported when executing eagerly. Instead,
  289. variable regularizers may be added through `add_variable`. Activity
  290. regularization is not supported directly (but such losses may be returned
  291. from `Layer.call()`).
  292. Arguments:
  293. losses: Loss tensor, or list/tuple of tensors.
  294. inputs: If anything other than None is passed, it signals the losses
  295. are conditional on some of the layer's inputs,
  296. and thus they should only be run where these inputs are available.
  297. This is the case for activity regularization losses, for instance.
  298. If `None` is passed, the losses are assumed
  299. to be unconditional, and will apply across all dataflows of the layer
  300. (e.g. weight regularization losses).
  301. Raises:
  302. RuntimeError: If called in Eager mode.
  303. """
  304. if context.in_eager_mode():
  305. # TODO(fchollet): it should be possible (and highly desirable) to support
  306. # `add_loss` in eager mode. This allows great convenience and flexibility
  307. # in defining custom losses on the fly (e.g. in VAEs).
  308. # Simply appending the loss value to `self._losses`
  309. # is the correct behavior.
  310. # The only caveat is that we need to force the user to only call
  311. # `add_loss` from inside a model or Layer's `call` method
  312. # (otherwise the loss computation cannot be backproped through).
  313. raise RuntimeError('Layer.add_loss not supported in Eager mode.')
  314. losses = _to_list(losses)
  315. self._losses += losses
  316. if inputs is None:
  317. for loss in losses:
  318. loss._unconditional_loss = True # pylint: disable=protected-access
  319. else:
  320. for loss in losses:
  321. loss._unconditional_loss = False # pylint: disable=protected-access
  322. # TODO(fchollet): deprecate collection below.
  323. _add_elements_to_collection(losses, ops.GraphKeys.REGULARIZATION_LOSSES)
  324. def get_losses_for(self, inputs):
  325. """Retrieves losses relevant to a specific set of inputs.
  326. Arguments:
  327. inputs: Input tensor or list/tuple of input tensors.
  328. Returns:
  329. List of loss tensors of the layer that depend on `inputs`.
  330. Raises:
  331. RuntimeError: If called in Eager mode.
  332. """
  333. if context.in_eager_mode():
  334. raise RuntimeError('Layer.get_losses_for not supported in Eager mode.')
  335. if inputs is None:
  336. # Requesting unconditional losses.
  337. return [x for x in self.losses if x._unconditional_loss] # pylint: disable=protected-access
  338. # Requesting input-conditional losses.
  339. inputs = nest.flatten(inputs)
  340. # Retrieve the set of tensors in the TF graph that depend on `inputs`.
  341. # The losses we want to return will be part of this set.
  342. # To avoid unnecessary work, we stop the search in case all of
  343. # `self.losses` have been retrieved.
  344. reachable = layers_util.get_reachable_from_inputs(inputs, self.losses)
  345. losses = []
  346. for loss in self.losses:
  347. if loss in reachable:
  348. losses.append(loss)
  349. return losses
  350. def build(self, _):
  351. """Creates the variables of the layer."""
  352. self.built = True
  353. def call(self, inputs, **kwargs): # pylint: disable=unused-argument
  354. """The logic of the layer lives here.
  355. Arguments:
  356. inputs: input tensor(s).
  357. **kwargs: additional keyword arguments.
  358. Returns:
  359. Output tensor(s).
  360. """
  361. return inputs
  362. def _name_scope_name(self, current_variable_scope):
  363. """Determines op naming for the Layer."""
  364. return current_variable_scope.original_name_scope
  365. def compute_output_shape(self, input_shape):
  366. """Computes the output shape of the layer given the input shape.
  367. Args:
  368. input_shape: A (possibly nested tuple of) `TensorShape`. It need not
  369. be fully defined (e.g. the batch size may be unknown).
  370. Returns:
  371. A (possibly nested tuple of) `TensorShape`.
  372. Raises:
  373. TypeError: if `input_shape` is not a (possibly nested tuple of)
  374. `TensorShape`.
  375. ValueError: if `input_shape` is incomplete or is incompatible with the
  376. the layer.
  377. """
  378. raise NotImplementedError
  379. def _make_unique_name(self, name_uid_map=None, avoid_names=None,
  380. namespace='', zero_based=False):
  381. base_name = _to_snake_case(self.__class__.__name__)
  382. name = _unique_layer_name(base_name, name_uid_map=name_uid_map,
  383. avoid_names=avoid_names, namespace=namespace,
  384. zero_based=zero_based)
  385. return (name, base_name)
  386. def _set_scope(self, scope=None):
  387. if self._scope is None:
  388. # If constructed with _scope=None, lazy setting of scope.
  389. if self._reuse:
  390. with vs.variable_scope(
  391. scope if scope is not None else self._base_name) as captured_scope:
  392. self._scope = captured_scope
  393. else:
  394. with vs.variable_scope(
  395. scope, default_name=self._base_name) as captured_scope:
  396. self._scope = captured_scope
  397. def add_variable(self, name, shape, dtype=None,
  398. initializer=None, regularizer=None,
  399. trainable=True, constraint=None,
  400. partitioner=None):
  401. """Adds a new variable to the layer, or gets an existing one; returns it.
  402. Arguments:
  403. name: variable name.
  404. shape: variable shape.
  405. dtype: The type of the variable. Defaults to `self.dtype` or `float32`.
  406. initializer: initializer instance (callable).
  407. regularizer: regularizer instance (callable).
  408. trainable: whether the variable should be part of the layer's
  409. "trainable_variables" (e.g. variables, biases)
  410. or "non_trainable_variables" (e.g. BatchNorm mean, stddev).
  411. Note, if the current variable scope is marked as non-trainable
  412. then this parameter is ignored and any added variables are also
  413. marked as non-trainable.
  414. constraint: constraint instance (callable).
  415. partitioner: (optional) partitioner instance (callable). If
  416. provided, when the requested variable is created it will be split
  417. into multiple partitions according to `partitioner`. In this case,
  418. an instance of `PartitionedVariable` is returned. Available
  419. partitioners include `tf.fixed_size_partitioner` and
  420. `tf.variable_axis_size_partitioner`. For more details, see the
  421. documentation of `tf.get_variable` and the "Variable Partitioners
  422. and Sharding" section of the API guide.
  423. Returns:
  424. The created variable. Usually either a `Variable` or `ResourceVariable`
  425. instance. If `partitioner` is not `None`, a `PartitionedVariable`
  426. instance is returned.
  427. Raises:
  428. RuntimeError: If called with partioned variable regularization and
  429. eager execution is enabled.
  430. """
  431. # `init_graph` should point to the graph in which variable initialization
  432. # will occur; it should be None if and only if initialization will take
  433. # place in the eager context.
  434. init_graph = None
  435. if context.in_graph_mode():
  436. default_graph = ops.get_default_graph()
  437. if default_graph.building_function:
  438. with ops.init_scope():
  439. # Retrieve the variables from the graph into which variables
  440. # will be lifted; if initialization ops will be lifted into
  441. # the eager context, then there is nothing to retrieve, since variable
  442. # collections are not supported when eager execution is enabled.
  443. if context.in_graph_mode():
  444. init_graph = ops.get_default_graph()
  445. existing_variables = set(tf_variables.global_variables())
  446. else:
  447. # Initialization ops will not be lifted out of the default graph.
  448. init_graph = default_graph
  449. existing_variables = set(tf_variables.global_variables())
  450. if dtype is None:
  451. dtype = self.dtype or dtypes.float32
  452. self._set_scope(None)
  453. reuse = self.built or self._reuse
  454. with vs.variable_scope(
  455. self._scope, reuse=reuse, auxiliary_name_scope=False) as scope:
  456. with ops.name_scope(self._name_scope_name(scope)):
  457. variable = self._add_variable_with_custom_getter(
  458. name=name,
  459. shape=shape,
  460. getter=vs.get_variable,
  461. # Manage errors in Layer rather than Checkpointable.
  462. overwrite=True,
  463. initializer=initializer,
  464. dtype=dtypes.as_dtype(dtype),
  465. constraint=constraint,
  466. trainable=trainable and self.trainable,
  467. partitioner=partitioner)
  468. if init_graph is not None: # pylint: disable=protected-access
  469. # The variable was created and initialized in a graph.
  470. if variable in existing_variables:
  471. # To match the behavior of tf.get_variable(), we only apply
  472. # regularization if the variable is newly created.
  473. return variable
  474. with init_graph.as_default():
  475. trainable_variables = tf_variables.trainable_variables()
  476. if (trainable and self.trainable and
  477. variable not in trainable_variables):
  478. # A custom getter / variable scope overrode the trainable flag.
  479. trainable = False
  480. if regularizer:
  481. if isinstance(variable, tf_variables.PartitionedVariable):
  482. for v in variable:
  483. with ops.colocate_with(v.op):
  484. with ops.name_scope(name + '/Regularizer'):
  485. regularization = regularizer(v)
  486. if regularization is not None:
  487. self.add_loss(regularization)
  488. else:
  489. with ops.colocate_with(variable.op):
  490. with ops.name_scope(name + '/Regularizer'):
  491. regularization = regularizer(variable)
  492. if regularization is not None:
  493. self.add_loss(regularization)
  494. elif regularizer: # and initialization took place in an eager context
  495. if isinstance(variable, tf_variables.PartitionedVariable):
  496. raise RuntimeError(
  497. 'Partitioned variable regularization is not yet '
  498. 'supported when executing eagerly. File a feature request'
  499. 'if this is important to you.')
  500. # Save a zero-argument lambda which runs the regularizer on the
  501. # variable, to be executed when `Layer.losses` is requested.
  502. # This makes losses responsive to variable updates when executing
  503. # eagerly.
  504. #
  505. # TODO(akshayka): Do the same for graphs as well, so that losses
  506. # collected in a while_loop can be run outside its control flow
  507. # context and so that losses won't be swallowed up by graph functions
  508. # (i.e., `.losses()` should always create regularizers).
  509. self._losses.append(lambda: regularizer(variable))
  510. if trainable:
  511. self._trainable_weights.append(variable)
  512. else:
  513. self._non_trainable_weights.append(variable)
  514. return variable
  515. def __call__(self, inputs, *args, **kwargs):
  516. """Wraps `call`, applying pre- and post-processing steps.
  517. Arguments:
  518. inputs: input tensor(s).
  519. *args: additional positional arguments to be passed to `self.call`.
  520. **kwargs: additional keyword arguments to be passed to `self.call`.
  521. **Note**: kwarg `scope` is reserved for use by the layer.
  522. Returns:
  523. Output tensor(s).
  524. Note:
  525. - If the layer's `call` method takes a `scope` keyword argument,
  526. this argument will be automatically set to the current variable scope.
  527. - If the layer's `call` method takes a `mask` argument (as some Keras
  528. layers do), its default value will be set to the mask generated
  529. for `inputs` by the previous layer (if `input` did come from
  530. a layer that generated a corresponding mask, i.e. if it came from
  531. a Keras layer with masking support.
  532. Raises:
  533. ValueError: if the layer's `call` method returns None (an invalid value).
  534. """
  535. self._set_scope(kwargs.pop('scope', None))
  536. input_list = nest.flatten(inputs)
  537. in_graph_mode = context.in_graph_mode()
  538. in_deferred_mode = isinstance(input_list[0], _DeferredTensor)
  539. # Ensure the Layer, if being reused, is working with inputs from
  540. # the same graph as where it was created.
  541. if in_graph_mode:
  542. try:
  543. ops._get_graph_from_inputs(input_list, graph=self.graph) # pylint: disable=protected-access
  544. except ValueError as e:
  545. raise ValueError('Input graph and Layer graph are not the same: %s' % e)
  546. if in_graph_mode or in_deferred_mode:
  547. user_kwargs = copy.copy(kwargs)
  548. # Handle Keras mask propagation from previous layer to current layer.
  549. previous_mask = None
  550. if (not hasattr(self, '_compute_previous_mask') or
  551. self._compute_previous_mask):
  552. previous_mask = _collect_previous_mask(inputs)
  553. if ('mask' in estimator_util.fn_args(self.call) and
  554. 'mask' not in kwargs and
  555. not _is_all_none(previous_mask)):
  556. # The previous layer generated a mask, and mask was not explicitly pass
  557. # to __call__, hence we set previous_mask as the default value.
  558. kwargs['mask'] = previous_mask
  559. if self.built:
  560. try:
  561. # Some classes which inherit from Layer do not use its constructor, so
  562. # rather than initializing to None we check for an AttributeError.
  563. scope_context_manager = self._always_reuse_variable_scope
  564. except AttributeError:
  565. # From this point we will always set reuse=True, so create a "final"
  566. # variable scope with this setting. We avoid re-creating variable scopes
  567. # after this point as an optimization.
  568. self._always_reuse_variable_scope = vs.variable_scope(
  569. self._scope, reuse=True, auxiliary_name_scope=False)
  570. scope_context_manager = self._always_reuse_variable_scope
  571. else:
  572. scope_context_manager = vs.variable_scope(
  573. self._scope, reuse=self._reuse, auxiliary_name_scope=False)
  574. input_shapes = None
  575. with scope_context_manager as scope:
  576. with ops.name_scope(self._name_scope_name(scope)):
  577. if not self.built:
  578. if not in_graph_mode:
  579. # Activity regularization is currently unsupported in Eager mode.
  580. if self._activity_regularizer:
  581. raise ValueError('activity_regularizer currently unsupported in '
  582. 'Eager mode. Found an activity_regularizer in '
  583. '%s(%s).' % (self.__class__.__name__, self))
  584. if not in_graph_mode and not in_deferred_mode:
  585. # TODO(agarwal): support _keras_history in Eager mode.
  586. for x in input_list:
  587. if hasattr(x, '_keras_history'):
  588. raise ValueError('_keras_history currently unsupported in '
  589. 'Eager mode. Found _keras_history in %s while '
  590. 'executing __call__ for %s(%s)' %
  591. (x, self.__class_.__name__, self))
  592. # Check input assumptions set before layer building, e.g. input rank.
  593. self._assert_input_compatibility(inputs)
  594. if input_list and self._dtype is None:
  595. try:
  596. self._dtype = input_list[0].dtype.base_dtype.name
  597. except AttributeError:
  598. pass
  599. input_shapes = nest.map_structure(lambda x: x.get_shape(), inputs)
  600. self.build(input_shapes)
  601. try:
  602. # Note: not all sub-classes of Layer call Layer.__init__ (especially
  603. # the ones under tensorflow/python/keras). Hence we recompute this
  604. # attribute here if it is not set.
  605. # TODO(agarwal): Fix the sub-classes and avoid this complexity.
  606. call_has_scope_arg = self._call_has_scope_arg
  607. except AttributeError:
  608. call_has_scope_arg = 'scope' in estimator_util.fn_args(self.call)
  609. if call_has_scope_arg:
  610. kwargs['scope'] = scope
  611. # Check input assumptions set after layer building, e.g. input shape.
  612. if in_graph_mode or in_deferred_mode:
  613. self._assert_input_compatibility(inputs)
  614. if not in_deferred_mode:
  615. outputs = self.call(inputs, *args, **kwargs)
  616. if outputs is None:
  617. raise ValueError('A layer\'s `call` method should return a Tensor '
  618. 'or a list of Tensors, not None.')
  619. else:
  620. # Deferred mode behavior: use `compute_output_shape` to
  621. # infer the number of outputs of the layer and their shapes.
  622. if input_shapes is None:
  623. input_shapes = nest.map_structure(lambda x: x.get_shape(), inputs)
  624. output_shapes = self.compute_output_shape(input_shapes)
  625. output_shapes = nest.flatten(output_shapes)
  626. outputs = [
  627. # TODO(fchollet): name the deferred tensors?
  628. _DeferredTensor(shape=shape, dtype=self._dtype)
  629. for shape in output_shapes
  630. ]
  631. if len(outputs) == 1:
  632. outputs = outputs[0]
  633. if in_graph_mode:
  634. # Apply activity regularization.
  635. # Note that it should be applied every time the layer creates a new
  636. # output, since it is output-specific.
  637. if self._activity_regularizer:
  638. output_list = nest.flatten(outputs)
  639. for output in output_list:
  640. with ops.name_scope('ActivityRegularizer'):
  641. activity_regularization = self._activity_regularizer(output)
  642. self.add_loss(activity_regularization, inputs=inputs)
  643. # TODO(fchollet): consider enabling masking for Eager mode.
  644. if hasattr(self, 'compute_mask'):
  645. output_mask = self.compute_mask(inputs, previous_mask)
  646. if isinstance(outputs, (list, tuple)):
  647. if output_mask is None:
  648. output_mask = [None for _ in range(len(outputs))]
  649. for x, m in zip(outputs, output_mask):
  650. x._keras_mask = m # pylint: disable=protected-access
  651. else:
  652. outputs._keras_mask = output_mask # pylint: disable=protected-access
  653. if in_graph_mode:
  654. # If all input tensors have history metadata,
  655. # we update the output tensors
  656. # with corresponding history metadata, thus eventually allowing to use
  657. # these tensors to instantiate a Network.
  658. if _have_all_keras_metadata(inputs):
  659. # If the layer returns tensors from its inputs, unmodified,
  660. # we copy them to avoid loss of tensor metadata.
  661. output_ls = nest.flatten(outputs)
  662. output_ls_copy = []
  663. for x in output_ls:
  664. if x in input_list:
  665. with ops.name_scope(scope.original_name_scope):
  666. x = array_ops.identity(x)
  667. output_ls_copy.append(x)
  668. if len(output_ls_copy) == 1:
  669. outputs = output_ls_copy[0]
  670. else:
  671. outputs = output_ls_copy
  672. # Update global default collections.
  673. _add_elements_to_collection(self.updates, ops.GraphKeys.UPDATE_OPS)
  674. if in_deferred_mode or in_graph_mode:
  675. if _have_all_keras_metadata(inputs):
  676. # Add an inbound node to the layer, so it can keep track of this call.
  677. # This updates the layer history of the output tensor(s).
  678. self._add_inbound_node(
  679. input_tensors=inputs, output_tensors=outputs, arguments=user_kwargs)
  680. self.built = True
  681. return outputs
  682. @property
  683. def graph(self):
  684. if context.in_eager_mode():
  685. raise RuntimeError('Layer.graph not supported in Eager mode.')
  686. return self._graph
  687. def __deepcopy__(self, memo):
  688. no_copy = set(['_graph'])
  689. shallow_copy = set(['_scope', '_always_reuse_variable_scope'])
  690. cls = self.__class__
  691. result = cls.__new__(cls)
  692. memo[id(self)] = result
  693. for k, v in self.__dict__.items():
  694. if k in no_copy:
  695. setattr(result, k, v)
  696. elif k in shallow_copy:
  697. setattr(result, k, copy.copy(v))
  698. elif _is_tensor_or_tensor_list(v):
  699. setattr(result, k, v)
  700. else:
  701. setattr(result, k, copy.deepcopy(v, memo))
  702. return result
  703. def apply(self, inputs, *args, **kwargs):
  704. """Apply the layer on a input.
  705. This simply wraps `self.__call__`.
  706. Arguments:
  707. inputs: Input tensor(s).
  708. *args: additional positional arguments to be passed to `self.call`.
  709. **kwargs: additional keyword arguments to be passed to `self.call`.
  710. Returns:
  711. Output tensor(s).
  712. """
  713. return self.__call__(inputs, *args, **kwargs)
  714. def _add_inbound_node(self,
  715. input_tensors,
  716. output_tensors,
  717. arguments=None):
  718. """Internal method to create an inbound node for the layer.
  719. Arguments:
  720. input_tensors: list of input tensors.
  721. output_tensors: list of output tensors.
  722. arguments: dictionary of keyword arguments that were passed to the
  723. `call` method of the layer at the call that created the node.
  724. """
  725. input_tensors = nest.flatten(input_tensors)
  726. output_tensors = nest.flatten(output_tensors)
  727. # Collect input tensor(s) coordinates.
  728. inbound_layers = []
  729. node_indices = []
  730. tensor_indices = []
  731. for x in input_tensors:
  732. assert hasattr(x, '_keras_history')
  733. inbound_layer, node_index, tensor_index = x._keras_history # pylint: disable=protected-access
  734. inbound_layers.append(inbound_layer)
  735. node_indices.append(node_index)
  736. tensor_indices.append(tensor_index)
  737. # Create node, add it to inbound nodes.
  738. Node(
  739. self,
  740. inbound_layers=inbound_layers,
  741. node_indices=node_indices,
  742. tensor_indices=tensor_indices,
  743. input_tensors=input_tensors,
  744. output_tensors=output_tensors,
  745. arguments=arguments)
  746. # Update tensor history metadata.
  747. for i in range(len(output_tensors)):
  748. # The metadata attribute consists of 1) a layer instance
  749. # 2) a node index for the layer, 3) a tensor index for the node.
  750. # The allows layer reuse (multiple nodes per layer) and multi-output
  751. # or multi-input layers (e.g. a layer can return multiple tensors,
  752. # and each can be sent to a different layer).
  753. output_tensors[i]._keras_history = (self, len(self._inbound_nodes) - 1, i) # pylint: disable=protected-access
  754. def _get_node_attribute_at_index(self, node_index, attr, attr_name):
  755. """Private utility to retrieves an attribute (e.g. inputs) from a node.
  756. This is used to implement the methods:
  757. - get_input_shape_at
  758. - get_output_shape_at
  759. - get_input_at
  760. etc...
  761. Arguments:
  762. node_index: Integer index of the node from which
  763. to retrieve the attribute.
  764. attr: Exact node attribute name.
  765. attr_name: Human-readable attribute name, for error messages.
  766. Returns:
  767. The layer's attribute `attr` at the node of index `node_index`.
  768. Raises:
  769. RuntimeError: If the layer has no inbound nodes, or if called in Eager
  770. mode.
  771. ValueError: If the index provided does not match any node.
  772. """
  773. assert context.in_graph_mode()
  774. if not self._inbound_nodes:
  775. raise RuntimeError('The layer has never been called '
  776. 'and thus has no defined ' + attr_name + '.')
  777. if not len(self._inbound_nodes) > node_index:
  778. raise ValueError('Asked to get ' + attr_name + ' at node ' +
  779. str(node_index) + ', but the layer has only ' +
  780. str(len(self._inbound_nodes)) + ' inbound nodes.')
  781. values = getattr(self._inbound_nodes[node_index], attr)
  782. if len(values) == 1:
  783. return values[0]
  784. else:
  785. return values
  786. def get_input_shape_at(self, node_index):
  787. """Retrieves the input shape(s) of a layer at a given node.
  788. Arguments:
  789. node_index: Integer, index of the node
  790. from which to retrieve the attribute.
  791. E.g. `node_index=0` will correspond to the
  792. first time the layer was called.
  793. Returns:
  794. A shape tuple
  795. (or list of shape tuples if the layer has multiple inputs).
  796. Raises:
  797. RuntimeError: If called in Eager mode.
  798. """
  799. if context.in_eager_mode():
  800. raise RuntimeError(
  801. 'Layer.get_input_shape_at not supported in Eager mode.')
  802. return self._get_node_attribute_at_index(node_index, 'input_shapes',
  803. 'input shape')
  804. def get_output_shape_at(self, node_index):
  805. """Retrieves the output shape(s) of a layer at a given node.
  806. Arguments:
  807. node_index: Integer, index of the node
  808. from which to retrieve the attribute.
  809. E.g. `node_index=0` will correspond to the
  810. first time the layer was called.
  811. Returns:
  812. A shape tuple
  813. (or list of shape tuples if the layer has multiple outputs).
  814. Raises:
  815. RuntimeError: If called in Eager mode.
  816. """
  817. if context.in_eager_mode():
  818. raise RuntimeError(
  819. 'Layer.get_output_shape_at not supported in Eager mode.')
  820. return self._get_node_attribute_at_index(node_index, 'output_shapes',
  821. 'output shape')
  822. def get_input_at(self, node_index):
  823. """Retrieves the input tensor(s) of a layer at a given node.
  824. Arguments:
  825. node_index: Integer, index of the node
  826. from which to retrieve the attribute.
  827. E.g. `node_index=0` will correspond to the
  828. first time the layer was called.
  829. Returns:
  830. A tensor (or list of tensors if the layer has multiple inputs).
  831. Raises:
  832. RuntimeError: If called in Eager mode.
  833. """
  834. if context.in_eager_mode():
  835. raise RuntimeError('Layer.get_input_at not supported in Eager mode.')
  836. return self._get_node_attribute_at_index(node_index, 'input_tensors',
  837. 'input')
  838. def get_output_at(self, node_index):
  839. """Retrieves the output tensor(s) of a layer at a given node.
  840. Arguments:
  841. node_index: Integer, index of the node
  842. from which to retrieve the attribute.
  843. E.g. `node_index=0` will correspond to the
  844. first time the layer was called.
  845. Returns:
  846. A tensor (or list of tensors if the layer has multiple outputs).
  847. Raises:
  848. RuntimeError: If called in Eager mode.
  849. """
  850. if context.in_eager_mode():
  851. raise RuntimeError('Layer.get_output_at not supported in Eager mode.')
  852. return self._get_node_attribute_at_index(node_index, 'output_tensors',
  853. 'output')
  854. @property
  855. def input(self):
  856. """Retrieves the input tensor(s) of a layer.
  857. Only applicable if the layer has exactly one input,
  858. i.e. if it is connected to one incoming layer.
  859. Returns:
  860. Input tensor or list of input tensors.
  861. Raises:
  862. AttributeError: if the layer is connected to
  863. more than one incoming layers.
  864. Raises:
  865. RuntimeError: If called in Eager mode.
  866. AttributeError: If no inbound nodes are found.
  867. """
  868. if context.in_eager_mode():
  869. raise RuntimeError('Layer.input not supported in Eager mode.')
  870. if not self._inbound_nodes:
  871. raise AttributeError('Layer ' + self.name +
  872. ' is not connected, no input to return.')
  873. return self._get_node_attribute_at_index(0, 'input_tensors', 'input')
  874. @property
  875. def output(self):
  876. """Retrieves the output tensor(s) of a layer.
  877. Only applicable if the layer has exactly one output,
  878. i.e. if it is connected to one incoming layer.
  879. Returns:
  880. Output tensor or list of output tensors.
  881. Raises:
  882. AttributeError: if the layer is connected to more than one incoming
  883. layers.
  884. RuntimeError: if called in Eager mode.
  885. """
  886. if context.in_eager_mode():
  887. raise RuntimeError('Layer.output not supported in Eager mode.')
  888. if not self._inbound_nodes:
  889. raise AttributeError('Layer ' + self.name + ' has no inbound nodes.')
  890. return self._get_node_attribute_at_index(0, 'output_tensors', 'output')
  891. @property
  892. def input_shape(self):
  893. """Retrieves the input shape(s) of a layer.
  894. Only applicable if the layer has exactly one input,
  895. i.e. if it is connected to one incoming layer, or if all inputs
  896. have the same shape.
  897. Returns:
  898. Input shape, as an integer shape tuple
  899. (or list of shape tuples, one tuple per input tensor).
  900. Raises:
  901. AttributeError: if the layer has no defined input_shape.
  902. RuntimeError: if called in Eager mode.
  903. """
  904. if context.in_eager_mode():
  905. raise RuntimeError('Layer.input_shape not supported in Eager mode.')
  906. if not self._inbound_nodes:
  907. raise AttributeError('The layer has never been called '
  908. 'and thus has no defined input shape.')
  909. all_input_shapes = set(
  910. [str(node.input_shapes) for node in self._inbound_nodes])
  911. if len(all_input_shapes) == 1:
  912. input_shapes = self._inbound_nodes[0].input_shapes
  913. if len(input_shapes) == 1:
  914. return tuple(tensor_shape.TensorShape(input_shapes[0]).as_list())
  915. else:
  916. return [
  917. tuple(tensor_shape.TensorShape(shape).as_list())
  918. for shape in input_shapes
  919. ]
  920. else:
  921. raise AttributeError('The layer "' + str(self.name) +
  922. ' has multiple inbound nodes, '
  923. 'with different input shapes. Hence '
  924. 'the notion of "input shape" is '
  925. 'ill-defined for the layer. '
  926. 'Use `get_input_shape_at(node_index)` '
  927. 'instead.')
  928. def count_params(self):
  929. """Count the total number of scalars composing the weights.
  930. Returns:
  931. An integer count.
  932. Raises:
  933. ValueError: if the layer isn't yet built
  934. (in which case its weights aren't yet defined).
  935. """
  936. if not self.built:
  937. if self.__class__.__name__ == 'Sequential':
  938. self.build() # pylint: disable=no-value-for-parameter
  939. else:
  940. raise ValueError('You tried to call `count_params` on ' + self.name +
  941. ', but the layer isn\'t built. '
  942. 'You can build it manually via: `' + self.name +
  943. '.build(batch_input_shape)`.')
  944. weight_shapes = [w.get_shape().as_list() for w in self.weights]
  945. return int(sum([np.prod(w) for w in weight_shapes]))
  946. @property
  947. def output_shape(self):
  948. """Retrieves the output shape(s) of a layer.
  949. Only applicable if the layer has one output,
  950. or if all outputs have the same shape.
  951. Returns:
  952. Output shape, as an integer shape tuple
  953. (or list of shape tuples, one tuple per output tensor).
  954. Raises:
  955. AttributeError: if the layer has no defined output shape.
  956. RuntimeError: if called in Eager mode.
  957. """
  958. if context.in_eager_mode():
  959. raise RuntimeError('Layer.output_shape not supported in Eager mode.')
  960. if not self._inbound_nodes:
  961. raise AttributeError('The layer has never been called '
  962. 'and thus has no defined output shape.')
  963. all_output_shapes = set(
  964. [str(node.output_shapes) for node in self._inbound_nodes])
  965. if len(all_output_shapes) == 1:
  966. output_shapes = self._inbound_nodes[0].output_shapes
  967. if len(output_shapes) == 1:
  968. return tuple(tensor_shape.TensorShape(output_shapes[0]).as_list())
  969. else:
  970. return [
  971. tuple(tensor_shape.TensorShape(shape).as_list())
  972. for shape in output_shapes
  973. ]
  974. else:
  975. raise AttributeError('The layer "%s"'
  976. ' has multiple inbound nodes, '
  977. 'with different output shapes. Hence '
  978. 'the notion of "output shape" is '
  979. 'ill-defined for the layer. '
  980. 'Use `get_output_shape_at(node_index)` '
  981. 'instead.' % self.name)
  982. @property
  983. def inbound_nodes(self):
  984. """Deprecated, do NOT use! Only for compatibility with external Keras."""
  985. return self._inbound_nodes
  986. @property
  987. def outbound_nodes(self):
  988. """Deprecated, do NOT use! Only for compatibility with external Keras."""
  989. return self._outbound_nodes
  990. def _assert_input_compatibility(self, inputs):
  991. """Checks compatibility between the layer and provided inputs.
  992. This checks that the tensor(s) `inputs` verify the input assumptions
  993. of the layer (if any). If not, a clear and actional exception gets raised.
  994. Arguments:
  995. inputs: input tensor or list of input tensors.
  996. Raises:
  997. ValueError: in case of mismatch between
  998. the provided inputs and the expectations of the layer.
  999. """
  1000. if not self.input_spec:
  1001. return
  1002. if not isinstance(self.input_spec, (list, tuple)):
  1003. input_spec = nest.flatten(self.input_spec)
  1004. else:
  1005. input_spec = self.input_spec
  1006. inputs = nest.flatten(inputs)
  1007. if len(inputs) != len(input_spec):
  1008. raise ValueError('Layer ' + self.name + ' expects ' +
  1009. str(len(input_spec)) + ' inputs, '
  1010. 'but it received ' + str(len(inputs)) +
  1011. ' input tensors. Inputs received: ' + str(inputs))
  1012. for input_index, (x, spec) in enumerate(zip(inputs, input_spec)):
  1013. if spec is None:
  1014. continue
  1015. if (spec.ndim is not None or
  1016. spec.min_ndim is not None or
  1017. spec.max_ndim is not None):
  1018. if x.get_shape().ndims is None:
  1019. raise ValueError('Input ' + str(input_index) + ' of layer ' +
  1020. self.name + ' is incompatible with the layer: '
  1021. 'its rank is undefined, but the layer requires a '
  1022. 'defined rank.')
  1023. # Check ndim.
  1024. if spec.ndim is not None:
  1025. ndim = x.get_shape().ndims
  1026. if ndim != spec.ndim:
  1027. raise ValueError('Input ' + str(input_index) + ' of layer ' +
  1028. self.name + ' is incompatible with the layer: '
  1029. 'expected ndim=' + str(spec.ndim) + ', found ndim=' +
  1030. str(ndim) + '. Full shape received: ' +
  1031. str(x.get_shape().as_list()))
  1032. if spec.max_ndim is not None:
  1033. ndim = x.get_shape().ndims
  1034. if ndim is not None and ndim > spec.max_ndim:
  1035. raise ValueError('Input ' + str(input_index) + ' of layer ' +
  1036. self.name + ' is incompatible with the layer: '
  1037. 'expected max_ndim=' + str(spec.max_ndim) +
  1038. ', found ndim=' + str(ndim))
  1039. if spec.min_ndim is not None:
  1040. ndim = x.get_shape().ndims
  1041. if ndim is not None and ndim < spec.min_ndim:
  1042. raise ValueError('Input ' + str(input_index) + ' of layer ' +
  1043. self.name + ' is incompatible with the layer: '
  1044. ': expected min_ndim=' + str(spec.min_ndim) +
  1045. ', found ndim=' + str(ndim) +
  1046. '. Full shape received: ' +
  1047. str(x.get_shape().as_list()))
  1048. # Check dtype.
  1049. if spec.dtype is not None:
  1050. if x.dtype != spec.dtype:
  1051. raise ValueError('Input ' + str(input_index) + ' of layer ' +
  1052. self.name + ' is incompatible with the layer: '
  1053. 'expected dtype=' + str(spec.dtype) +
  1054. ', found dtype=' + str(x.dtype))
  1055. # Check specific shape axes.
  1056. if spec.axes:
  1057. shape = x.get_shape().as_list()
  1058. if shape is not None:
  1059. for axis, value in spec.axes.items():
  1060. if hasattr(value, 'value'):
  1061. value = value.value
  1062. if value is not None and shape[int(axis)] not in {value, None}:
  1063. raise ValueError(
  1064. 'Input ' + str(input_index) + ' of layer ' + self.name + ' is'
  1065. ' incompatible with the layer: expected axis ' + str(axis) +
  1066. ' of input shape to have value ' + str(value) +
  1067. ' but received input with shape ' + str(shape))
  1068. # Check shape.
  1069. if spec.shape is not None:
  1070. shape = x.get_shape().as_list()
  1071. if shape is not None:
  1072. for spec_dim, dim in zip(spec.shape, shape):
  1073. if spec_dim is not None and dim is not None:
  1074. if spec_dim != dim:
  1075. raise ValueError('Input ' + str(input_index) +
  1076. ' is incompatible with layer ' + self.name +
  1077. ': expected shape=' + str(spec.shape) +
  1078. ', found shape=' + str(shape))
  1079. @tf_export('keras.layers.InputSpec', 'layers.InputSpec')
  1080. class InputSpec(object):
  1081. """Specifies the ndim, dtype and shape of every input to a layer.
  1082. Every layer should expose (if appropriate) an `input_spec` attribute:
  1083. a list of instances of InputSpec (one per input tensor).
  1084. A None entry in a shape is compatible with any dimension,
  1085. a None shape is compatible with any shape.
  1086. Arguments:
  1087. dtype: Expected DataType of the input.
  1088. shape: Shape tuple, expected shape of the input
  1089. (may include None for unchecked axes).
  1090. ndim: Integer, expected rank of the input.
  1091. max_ndim: Integer, maximum rank of the input.
  1092. min_ndim: Integer, minimum rank of the input.
  1093. axes: Dictionary mapping integer axes to
  1094. a specific dimension value.
  1095. """
  1096. def __init__(self,
  1097. dtype=None,
  1098. shape=None,
  1099. ndim=None,
  1100. max_ndim=None,
  1101. min_ndim=None,
  1102. axes=None):
  1103. self.dtype = dtype
  1104. self.shape = shape
  1105. if shape is not None:
  1106. self.ndim = len(shape)
  1107. else:
  1108. self.ndim = ndim
  1109. self.max_ndim = max_ndim
  1110. self.min_ndim = min_ndim
  1111. self.axes = axes or {}
  1112. def __repr__(self):
  1113. spec = [('dtype=' + str(self.dtype)) if self.dtype else '',
  1114. ('shape=' + str(self.shape)) if self.shape else '',
  1115. ('ndim=' + str(self.ndim)) if self.ndim else '',
  1116. ('max_ndim=' + str(self.max_ndim)) if self.max_ndim else '',
  1117. ('min_ndim=' + str(self.min_ndim)) if self.min_ndim else '',
  1118. ('axes=' + str(self.axes)) if self.axes else '']
  1119. return 'InputSpec(%s)' % ', '.join(x for x in spec if x)
  1120. class Node(object):
  1121. """A `Node` describes the connectivity between two layers.
  1122. Each time a layer is connected to some new input,
  1123. a node is added to `layer._inbound_nodes`.
  1124. Each time the output of a layer is used by another layer,
  1125. a node is added to `layer._outbound_nodes`.
  1126. Arguments:
  1127. outbound_layer: the layer that takes
  1128. `input_tensors` and turns them into `output_tensors`
  1129. (the node gets created when the `call`
  1130. method of the layer was called).
  1131. inbound_layers: a list of layers, the same length as `input_tensors`,
  1132. the layers from where `input_tensors` originate.
  1133. node_indices: a list of integers, the same length as `inbound_layers`.
  1134. `node_indices[i]` is the origin node of `input_tensors[i]`
  1135. (necessary since each inbound layer might have several nodes,
  1136. e.g. if the layer is being shared with a different data stream).
  1137. tensor_indices: a list of integers,
  1138. the same length as `inbound_layers`.
  1139. `tensor_indices[i]` is the index of `input_tensors[i]` within the
  1140. output of the inbound layer
  1141. (necessary since each inbound layer might
  1142. have multiple tensor outputs, with each one being
  1143. independently manipulable).
  1144. input_tensors: list of input tensors.
  1145. output_tensors: list of output tensors.
  1146. arguments: dictionary of keyword arguments that were passed to the
  1147. `call` method of the layer at the call that created the node.
  1148. `node_indices` and `tensor_indices` are basically fine-grained coordinates
  1149. describing the origin of the `input_tensors`.
  1150. A node from layer A to layer B is added to:
  1151. - A._outbound_nodes
  1152. - B._inbound_nodes
  1153. """
  1154. def __init__(self,
  1155. outbound_layer,
  1156. inbound_layers,
  1157. node_indices,
  1158. tensor_indices,
  1159. input_tensors,
  1160. output_tensors,
  1161. arguments=None):
  1162. # Layer instance (NOT a list).
  1163. if isinstance(outbound_layer, list):
  1164. raise ValueError(
  1165. '`outbound_layer` should be a layer instance, not a list.')
  1166. # this is the layer that takes a list of input tensors
  1167. # and turns them into a list of output tensors.
  1168. # the current node will be added to
  1169. # the inbound_nodes of outbound_layer.
  1170. self.outbound_layer = outbound_layer
  1171. # The following 3 properties describe where
  1172. # the input tensors come from: which layers,
  1173. # and for each layer, which node and which
  1174. # tensor output of each node.
  1175. # List of layer instances.
  1176. self.inbound_layers = inbound_layers
  1177. # List of integers, 1:1 mapping with inbound_layers.
  1178. self.node_indices = node_indices
  1179. # List of integers, 1:1 mapping with inbound_layers.
  1180. self.tensor_indices = tensor_indices
  1181. # Following 2 properties:
  1182. # tensor inputs and outputs of outbound_layer.
  1183. # List of tensors. 1:1 mapping with inbound_layers.
  1184. self.input_tensors = input_tensors
  1185. # List of tensors, created by outbound_layer.call().
  1186. self.output_tensors = output_tensors
  1187. # Following 2 properties: input and output shapes.
  1188. # List of shape tuples, shapes of input_tensors.
  1189. self.input_shapes = [layers_util.static_shape(x) for x in input_tensors]
  1190. # List of shape tuples, shapes of output_tensors.
  1191. self.output_shapes = [layers_util.static_shape(x) for x in output_tensors]
  1192. # Optional keyword arguments to layer's `call`.
  1193. self.arguments = arguments
  1194. # Add nodes to all layers involved.
  1195. for layer in inbound_layers:
  1196. if layer is not None:
  1197. # For compatibility with external Keras, we use the deprecated
  1198. # accessor here.
  1199. layer.outbound_nodes.append(self)
  1200. # For compatibility with external Keras, we use the deprecated
  1201. # accessor here.
  1202. outbound_layer.inbound_nodes.append(self)
  1203. def get_config(self):
  1204. inbound_names = []
  1205. for layer in self.inbound_layers:
  1206. if layer:
  1207. inbound_names.append(layer.name)
  1208. else:
  1209. inbound_names.append(None)
  1210. return {
  1211. 'outbound_layer': self.outbound_layer.name,
  1212. 'inbound_layers': inbound_names,
  1213. 'node_indices': self.node_indices,
  1214. 'tensor_indices': self.tensor_indices
  1215. }
  1216. class _DeferredTensor(object):
  1217. """Tensor-like object used to build graphs of layers in Eager mode.
  1218. When calling a layer on a DeferredTensor, the layer will not perform any
  1219. computation and will simply perfom shape inference to return new
  1220. DeferredTensors with appropriate shape information. Thus DeferredTensor
  1221. behaves like a graph-mode Tensor when manipulated by layers.
  1222. """
  1223. def __init__(self, shape, dtype, name=None):
  1224. self.shape = tensor_shape.TensorShape(shape)
  1225. if dtype is None:
  1226. self.dtype = dtypes.as_dtype(np.float32)
  1227. else:
  1228. self.dtype = dtypes.as_dtype(dtype)
  1229. self.name = name
  1230. def get_shape(self):
  1231. return self.shape
  1232. def __str__(self):
  1233. return "DeferredTensor('%s', shape=%s, dtype=%s)" % (self.name,
  1234. self.get_shape(),
  1235. self.dtype.name)
  1236. def __repr__(self):
  1237. return "<_DeferredTensor '%s' shape=%s dtype=%s>" % (self.name,
  1238. self.get_shape(),
  1239. self.dtype.name)
  1240. def _is_tensor_or_tensor_list(v):
  1241. v = nest.flatten(v)
  1242. if v and isinstance(v[0], ops.Tensor):
  1243. return True
  1244. else:
  1245. return False
  1246. def _to_snake_case(name):
  1247. intermediate = re.sub('(.)([A-Z][a-z0-9]+)', r'\1_\2', name)
  1248. insecure = re.sub('([a-z])([A-Z])', r'\1_\2', intermediate).lower()
  1249. # If the class is private the name starts with "_" which is not secure
  1250. # for creating scopes. We prefix the name with "private" in this case.
  1251. if insecure[0] != '_':
  1252. return insecure
  1253. return 'private' + insecure
  1254. def _to_list(x):
  1255. """This normalizes a list/tuple or single element into a list.
  1256. If a single element is passed, we return
  1257. a list of size 1 containing the element.
  1258. Arguments:
  1259. x: list or tuple or single element.
  1260. Returns:
  1261. A list.
  1262. """
  1263. if isinstance(x, (list, tuple)):
  1264. return list(x)
  1265. return [x]
  1266. def _add_elements_to_collection(elements, collection_list):
  1267. if context.in_eager_mode():
  1268. raise RuntimeError('Using collections from Layers not supported in Eager '
  1269. 'mode. Tried to add %s to %s' % (elements,
  1270. collection_list))
  1271. elements = nest.flatten(elements)
  1272. collection_list = nest.flatten(collection_list)
  1273. for name in collection_list:
  1274. collection = ops.get_collection_ref(name)
  1275. collection_set = set(collection)
  1276. for element in elements:
  1277. if element not in collection_set:
  1278. collection.append(element)
  1279. def _is_all_none(iterable_or_element):
  1280. if not isinstance(iterable_or_element, (list, tuple)):
  1281. iterable = [iterable_or_element]
  1282. else:
  1283. iterable = iterable_or_element
  1284. # We cannot use Python's `any` because the iterable may return Tensors.
  1285. for element in iterable:
  1286. if element is not None:
  1287. return False
  1288. return True
  1289. def _have_all_keras_metadata(iterable_or_element):
  1290. if not isinstance(iterable_or_element, (list, tuple)):
  1291. iterable = [iterable_or_element]
  1292. else:
  1293. iterable = iterable_or_element
  1294. return all([hasattr(x, '_keras_history') for x in iterable])
  1295. def _collect_previous_mask(input_tensors):
  1296. """Retrieves the output mask(s) of the previous node.
  1297. Arguments:
  1298. input_tensors: A tensor or list of tensors.
  1299. Returns:
  1300. A mask tensor or list of mask tensors.
  1301. """
  1302. input_tensors = nest.flatten(input_tensors)
  1303. masks = []
  1304. for x in input_tensors:
  1305. if hasattr(x, '_keras_mask'):
  1306. mask = x._keras_mask # pylint: disable=protected-access
  1307. masks.append(mask)
  1308. else:
  1309. masks.append(None)
  1310. if len(masks) == 1:
  1311. return masks[0]
  1312. return masks
  1313. # A global dictionary mapping graph objects to an index of counters used
  1314. # for various layer names in each graph.
  1315. # Allows to give unique autogenerated names to layers, in a graph-specific way.
  1316. PER_GRAPH_LAYER_NAME_UIDS = weakref.WeakKeyDictionary()
  1317. def _get_default_graph_uid_map():
  1318. graph = ops.get_default_graph()
  1319. name_uid_map = PER_GRAPH_LAYER_NAME_UIDS.get(graph, None)
  1320. if name_uid_map is None:
  1321. name_uid_map = collections.defaultdict(int)
  1322. PER_GRAPH_LAYER_NAME_UIDS[graph] = name_uid_map
  1323. return name_uid_map
  1324. def _unique_layer_name(name, name_uid_map=None, avoid_names=None, namespace='',
  1325. zero_based=False):
  1326. """Makes a layer name (or arbitrary string) unique within a TensorFlow graph.
  1327. Arguments:
  1328. name: String name to make unique.
  1329. name_uid_map: An optional defaultdict(int) to use when creating unique
  1330. names. If None (default), uses a per-Graph dictionary.
  1331. avoid_names: An optional set or dict with names which should not be used. If
  1332. None (default) does not avoid any names.
  1333. namespace: Gets a name which is unique within the (graph, namespace). Layers
  1334. which are not Networks use a blank namespace and so get graph-global
  1335. names.
  1336. zero_based: If True, name sequences start with no suffix (e.g. "dense",
  1337. "dense_1"). If False, naming is one-based ("dense_1", "dense_2").
  1338. Returns:
  1339. Unique string name.
  1340. Example:
  1341. ```python
  1342. _unique_layer_name('dense') # dense_1
  1343. _unique_layer_name('dense') # dense_2
  1344. ```
  1345. """
  1346. if name_uid_map is None:
  1347. name_uid_map = _get_default_graph_uid_map()
  1348. if avoid_names is None:
  1349. avoid_names = set()
  1350. proposed_name = None
  1351. while proposed_name is None or proposed_name in avoid_names:
  1352. name_key = (namespace, name)
  1353. if zero_based:
  1354. number = name_uid_map[name_key]
  1355. if number:
  1356. proposed_name = name + '_' + str(number)
  1357. else:
  1358. proposed_name = name
  1359. name_uid_map[name_key] += 1
  1360. else:
  1361. name_uid_map[name_key] += 1
  1362. proposed_name = name + '_' + str(name_uid_map[name_key])
  1363. return proposed_name
ダウンロード 印刷用表示

このコピペの URL

JavaScript での埋め込み

iframe での埋め込み

元のテキスト