Adds a new variable to the layer. Arguments: name: Variable name. shape: Variable shape. Defaults to scalar if unspecified. dtype: The type of the variable. Defaults to `self.dtype` or `float32`. initializer: Initializer instance (callable). regularizer: Regularize
(self,
name=None,
shape=None,
dtype=None,
initializer=None,
regularizer=None,
trainable=None,
constraint=None,
partitioner=None,
use_resource=None,
synchronization=tf_variables.VariableSynchronization.AUTO,
aggregation=tf_variables.VariableAggregation.NONE,
**kwargs)
| 400 | |
| 401 | @doc_controls.for_subclass_implementers |
| 402 | def add_weight(self, |
| 403 | name=None, |
| 404 | shape=None, |
| 405 | dtype=None, |
| 406 | initializer=None, |
| 407 | regularizer=None, |
| 408 | trainable=None, |
| 409 | constraint=None, |
| 410 | partitioner=None, |
| 411 | use_resource=None, |
| 412 | synchronization=tf_variables.VariableSynchronization.AUTO, |
| 413 | aggregation=tf_variables.VariableAggregation.NONE, |
| 414 | **kwargs): |
| 415 | """Adds a new variable to the layer. |
| 416 | |
| 417 | Arguments: |
| 418 | name: Variable name. |
| 419 | shape: Variable shape. Defaults to scalar if unspecified. |
| 420 | dtype: The type of the variable. Defaults to `self.dtype` or `float32`. |
| 421 | initializer: Initializer instance (callable). |
| 422 | regularizer: Regularizer instance (callable). |
| 423 | trainable: Boolean, whether the variable should be part of the layer's |
| 424 | "trainable_variables" (e.g. variables, biases) |
| 425 | or "non_trainable_variables" (e.g. BatchNorm mean and variance). |
| 426 | Note that `trainable` cannot be `True` if `synchronization` |
| 427 | is set to `ON_READ`. |
| 428 | constraint: Constraint instance (callable). |
| 429 | partitioner: Partitioner to be passed to the `Trackable` API. |
| 430 | use_resource: Whether to use `ResourceVariable`. |
| 431 | synchronization: Indicates when a distributed a variable will be |
| 432 | aggregated. Accepted values are constants defined in the class |
| 433 | `tf.VariableSynchronization`. By default the synchronization is set to |
| 434 | `AUTO` and the current `DistributionStrategy` chooses |
| 435 | when to synchronize. If `synchronization` is set to `ON_READ`, |
| 436 | `trainable` must not be set to `True`. |
| 437 | aggregation: Indicates how a distributed variable will be aggregated. |
| 438 | Accepted values are constants defined in the class |
| 439 | `tf.VariableAggregation`. |
| 440 | **kwargs: Additional keyword arguments. Accepted values are `getter` and |
| 441 | `collections`. |
| 442 | |
| 443 | Returns: |
| 444 | The created variable. Usually either a `Variable` or `ResourceVariable` |
| 445 | instance. If `partitioner` is not `None`, a `PartitionedVariable` |
| 446 | instance is returned. |
| 447 | |
| 448 | Raises: |
| 449 | RuntimeError: If called with partitioned variable regularization and |
| 450 | eager execution is enabled. |
| 451 | ValueError: When giving unsupported dtype and no initializer or when |
| 452 | trainable has been set to True with synchronization set as `ON_READ`. |
| 453 | """ |
| 454 | if shape is None: |
| 455 | shape = () |
| 456 | # Validate optional keyword arguments. |
| 457 | for kwarg in kwargs: |
| 458 | if kwarg not in ['getter', 'collections', 'experimental_autocast', 'ev_option']: |
| 459 | raise TypeError('Unknown keyword argument:', kwarg) |