A tensor-like object whose value can be updated only up until execution. After creating the freezable variable, you can update its value by calling `var.update_value(new_value)` (similar to a regular variable). Unlike an actual variable, the value used during execution is the current value
(value, shape=None, name=None)
| 1072 | |
| 1073 | |
| 1074 | def freezable_variable(value, shape=None, name=None): |
| 1075 | """A tensor-like object whose value can be updated only up until execution. |
| 1076 | |
| 1077 | After creating the freezable variable, you can update its value by calling |
| 1078 | `var.update_value(new_value)` (similar to a regular variable). |
| 1079 | Unlike an actual variable, the value used during execution is the current |
| 1080 | value at the time the execution function (`backend.function()`) was created. |
| 1081 | |
| 1082 | This is an internal API, expected to be temporary. It is used to implement a |
| 1083 | mutable `trainable` property for `BatchNormalization` layers, with a frozen |
| 1084 | value after model compilation. |
| 1085 | |
| 1086 | We don't use a plain variable in this case because we need the value used |
| 1087 | in a specific model to be frozen after `compile` has been called |
| 1088 | (e.g. GAN use case). |
| 1089 | |
| 1090 | Arguments: |
| 1091 | value: The initial value for the tensor-like object. |
| 1092 | shape: The shape for the tensor-like object (cannot be changed). |
| 1093 | name: The name for the tensor-like object. |
| 1094 | |
| 1095 | Returns: |
| 1096 | A tensor-like object with a static value that can be updated via |
| 1097 | `x.update_value(new_value)`, up until creating an execution function |
| 1098 | (afterwards the value is fixed). |
| 1099 | """ |
| 1100 | graph = get_graph() |
| 1101 | with graph.as_default(): |
| 1102 | x = array_ops.placeholder_with_default( |
| 1103 | value, shape=shape, name=name) |
| 1104 | x._initial_value = value |
| 1105 | x._current_value = value |
| 1106 | |
| 1107 | def update_value(new_value): |
| 1108 | x._current_value = new_value |
| 1109 | |
| 1110 | def get_value(): |
| 1111 | return x._current_value |
| 1112 | |
| 1113 | x.update_value = update_value |
| 1114 | x.get_value = get_value |
| 1115 | |
| 1116 | global _FREEZABLE_VARS |
| 1117 | if graph not in _FREEZABLE_VARS: |
| 1118 | _FREEZABLE_VARS[graph] = object_identity.ObjectIdentityWeakSet() |
| 1119 | _FREEZABLE_VARS[graph].add(x) |
| 1120 | return x |
| 1121 | |
| 1122 | |
| 1123 | @keras_export('keras.backend.shape') |
nothing calls this directly
no test coverage detected