Load new value into this variable. Writes new value to variable's memory. Doesn't add ops to the graph. This convenience method requires a session where the graph containing this variable has been launched. If no session is passed, the default session is used. See `tf.compat.v1.Se
(self, value, session=None)
| 1194 | @deprecated(None, |
| 1195 | "Prefer Variable.assign which has equivalent behavior in 2.X.") |
| 1196 | def load(self, value, session=None): |
| 1197 | """Load new value into this variable. |
| 1198 | |
| 1199 | Writes new value to variable's memory. Doesn't add ops to the graph. |
| 1200 | |
| 1201 | This convenience method requires a session where the graph |
| 1202 | containing this variable has been launched. If no session is |
| 1203 | passed, the default session is used. See `tf.compat.v1.Session` for more |
| 1204 | information on launching a graph and on sessions. |
| 1205 | |
| 1206 | ```python |
| 1207 | v = tf.Variable([1, 2]) |
| 1208 | init = tf.compat.v1.global_variables_initializer() |
| 1209 | |
| 1210 | with tf.compat.v1.Session() as sess: |
| 1211 | sess.run(init) |
| 1212 | # Usage passing the session explicitly. |
| 1213 | v.load([2, 3], sess) |
| 1214 | print(v.eval(sess)) # prints [2 3] |
| 1215 | # Usage with the default session. The 'with' block |
| 1216 | # above makes 'sess' the default session. |
| 1217 | v.load([3, 4], sess) |
| 1218 | print(v.eval()) # prints [3 4] |
| 1219 | ``` |
| 1220 | |
| 1221 | Args: |
| 1222 | value: New variable value |
| 1223 | session: The session to use to evaluate this variable. If none, the |
| 1224 | default session is used. |
| 1225 | |
| 1226 | Raises: |
| 1227 | ValueError: Session is not passed and no default session |
| 1228 | """ |
| 1229 | if context.executing_eagerly(): |
| 1230 | self.assign(value) |
| 1231 | else: |
| 1232 | session = session or ops.get_default_session() |
| 1233 | if session is None: |
| 1234 | raise ValueError( |
| 1235 | "Either session argument should be provided or default session " |
| 1236 | "should be established") |
| 1237 | session.run(self.initializer, {self.initializer.inputs[1]: value}) |
| 1238 | |
| 1239 | # Conversion to tensor. |
| 1240 | @staticmethod |