Sets the value of a variable, from a Numpy array. Arguments: x: Tensor to set to a new value. value: Value to set the tensor to, as a Numpy array (of the same shape).
(x, value)
| 3189 | |
| 3190 | @keras_export('keras.backend.set_value') |
| 3191 | def set_value(x, value): |
| 3192 | """Sets the value of a variable, from a Numpy array. |
| 3193 | |
| 3194 | Arguments: |
| 3195 | x: Tensor to set to a new value. |
| 3196 | value: Value to set the tensor to, as a Numpy array |
| 3197 | (of the same shape). |
| 3198 | """ |
| 3199 | value = np.asarray(value, dtype=dtype(x)) |
| 3200 | if ops.executing_eagerly_outside_functions(): |
| 3201 | with ops.init_scope(): |
| 3202 | x.assign(value) |
| 3203 | else: |
| 3204 | with get_graph().as_default(): |
| 3205 | tf_dtype = dtypes_module.as_dtype(x.dtype.name.split('_')[0]) |
| 3206 | if hasattr(x, '_assign_placeholder'): |
| 3207 | assign_placeholder = x._assign_placeholder |
| 3208 | assign_op = x._assign_op |
| 3209 | else: |
| 3210 | # In order to support assigning weights to resizable variables in |
| 3211 | # Keras, we make a placeholder with the correct number of dimensions |
| 3212 | # but with None in each dimension. This way, we can assign weights |
| 3213 | # of any size (as long as they have the correct dimensionality). |
| 3214 | placeholder_shape = tensor_shape.TensorShape([None] * value.ndim) |
| 3215 | assign_placeholder = array_ops.placeholder( |
| 3216 | tf_dtype, shape=placeholder_shape) |
| 3217 | assign_op = x.assign(assign_placeholder) |
| 3218 | x._assign_placeholder = assign_placeholder |
| 3219 | x._assign_op = assign_op |
| 3220 | get_session().run(assign_op, feed_dict={assign_placeholder: value}) |
| 3221 | |
| 3222 | |
| 3223 | @keras_export('keras.backend.batch_set_value') |
nothing calls this directly
no test coverage detected