Repository: tensorlayer/TensorLayer

Issue:
memory leaks when assign params again and again
```python
# this line may run may times
tl.files.assign_params(sess, load_params, r_network_eval.network)
```

assign_params may create new op again and again, is this the cause the leaking of memory?

Could we add an option that we could return the assign ops, and `session.run` could be call by user manually?


Discussion:
yes, we could set a default `assign_ops=None`
Or something like: sess=None, when user want run the ops manually? Is this a good idea?
i see, when `sess=None` what will the code look like?
Always return ops, auto run when session is given:)

```python
def assign_params(sess, params, network):
    """Assign the given parameters to the TensorLayer network.

    Parameters
    ----------
    sess : TensorFlow Session
    params : a list
        A list of parameters in order.
    network : a :class:`Layer` class
        The network to be assigned

    Examples
    --------
    >>> Save your network as follow:
    >>> tl.files.save_npz(network.all_params, name='model_test.npz')
    >>> network.print_params()
    ...
    ... Next time, load and assign your network as follow:
    >>> sess.run(tf.initialize_all_variables()) # re-initialize, then save and assign
    >>> load_params = tl.files.load_npz(name='model_test.npz')
    >>> tl.files.assign_params(sess, load_params, network)
    >>> network.print_params()

    References
    ----------
    - `Assign value to a TensorFlow variable <http://stackoverflow.com/questions/34220532/how-to-assign-value-to-a-tensorflow-variable>`_
    """
    ops = []
    for idx, param in enumerate(params):
        ops.append(network.all_params[idx].assign(param))
    if sess is not None:
        sess.run(ops)
    return ops
```
we may need to set default sess to None, e.g. `def assign_params(sess=None, params=[], network=None)` ~?

but one problem is that if we re-run this ops, it will assign the same params right?
It is confused when network is set to be None and params is empty. It may be good when sess is the last param, but it will break old code in this case.

Yes, in my test case:)  There is 5 param case in my test, they works well when assigned many times.

Resolving patch:
diff --git a/tensorlayer/files.py b/tensorlayer/files.py
--- a/tensorlayer/files.py
+++ b/tensorlayer/files.py
@@ -607,12 +607,17 @@ def assign_params(sess, params, network):
 
     Parameters
     ----------
-    sess : TensorFlow Session
+    sess : TensorFlow Session. Automatically run when sess is not None.
     params : a list
         A list of parameters in order.
     network : a :class:`Layer` class
         The network to be assigned
 
+    Returns
+    --------
+    ops : list
+        A list of tf ops in order that assign params. Support sess.run(ops) manually.
+
     Examples
     --------
     >>> Save your network as follow:
@@ -632,7 +637,9 @@ def assign_params(sess, params, network):
     ops = []
     for idx, param in enumerate(params):
         ops.append(network.all_params[idx].assign(param))
-    sess.run(ops)
+    if sess is not None:
+        sess.run(ops)
+    return ops
 
 def load_and_assign_npz(sess=None, name=None, network=None):
     """Load model from npz and assign to a network.


Test patch: