Showing posts with label session. Show all posts
Showing posts with label session. Show all posts

What is and how to use tf.keras.backend.clear_session()



tf.keras.backend.clear_session resets all state generated by Keras.

Keras manages a global state, which it uses to implement the Functional model-building API and to uniquify autogenerated layer names.

If you are creating many models in a loop, this global state will consume an increasing amount of memory over time, and you may want to clear it. Calling clear_session() releases the global state: this helps avoid clutter from old models and layers, especially when memory is limited.

Example 1: calling clear_session() when creating models in a loop

for _ in range(100):
  # Without `clear_session()`, each iteration of this loop will
  # slightly increase the size of the global state managed by Keras
  model = tf.keras.Sequential([tf.keras.layers.Dense(10) for _ in range(10)])

for _ in range(100):
  # With `clear_session()` called at the beginning,
  # Keras starts with a blank state at each iteration
  # and memory consumption is constant over time.
  tf.keras.backend.clear_session()
  model = tf.keras.Sequential([tf.keras.layers.Dense(10) for _ in range(10)])
Example 2: resetting the layer name generation counter

import tensorflow as tf
layers = [tf.keras.layers.Dense(10) for _ in range(10)]
new_layer = tf.keras.layers.Dense(10)
print(new_layer.name)

#Output
dense_10

tf.keras.backend.set_learning_phase(1)
print(tf.keras.backend.learning_phase())

tf.keras.backend.clear_session()
new_layer = tf.keras.layers.Dense(10)
print(new_layer.name)

# Output
dense

Putty SSH remote server with password in Windows

putty.exe -ssh [username]@[host] -pw [yourpassword]

You can also run some commands immediately after logging into the server.

putty.exe -ssh [username]@[host] -pw [yourpassword] -m [yourfiilename]

You can edit the file with [yourfiilename] with commands you need to execute, e.g., a file contains commands for ending a screen 

screen -S [screename] -X quit

Keep process running after ending the ssh session using "screen"

Simple scenario from askubuntu:
  • ssh into your remote box. Type screen Then start the process you want.
  • Press Ctrl-A then Ctrl-D. This will "detach" your screen session but leave your processes running. You can now log out of the remote box.
  • If you want to come back later, log on again and type screen -r This will "resume" your screen session, and you can see the output of your process. However, if you have multiple screens have been detached before, you might get this below:





  • In this case, just specify the screen you want to re-attach
    • screen -r 174044.pts-3.srvgal107
  • Kill screen session : Press Ctrl-A then K.
  • Access attatched screen : screen -D -r '1234.somescreensession'
  • Scroll inside screen
    • Maybe there's a better way, but I'm used to scrolling using the "copy mode" (which you can use to copy text using screen itself, although that requires the paste command too):
      • Hit your screen prefix combination (C-a / control+A by default), then hit Escape.
      • Move up/down with the arrow keys (↑ and ↓).
      • When you're done, hit Return twice to get back to the end of the scroll buffer.
      (If you move the cursor after hitting Return once, you'll be selecting text to copy, and hitting Return the second time will copy it. Then you can paste with C-a followed by] )
  • Quite a session: screen -X -S [session # you want to kill] quit