Code Monkey home page Code Monkey logo

Comments (7)

oars avatar oars commented on July 17, 2024 1

Hi Eric,

Note that you are using a tf_environment. The way your code is structured you are generating Tensorflow Ops, but not evaluating them which is causing the increase in memory usage.

You'll want to change the later segment of your code to be:

tf_py_env = MyEnv()
tf_env = tf_py_environment.TFPyEnvironment(tf_py_env)
action = tf.constant([0.0])
reset_op = tf_env.reset()
step_op = tf_env.step(action)

i = 0

with tf.Session() as sess:
  while True:
      if i % 10000 == 0:
          print(i)
          time_step = sess.run(reset_op)
      time_step = sess.run(step_op)
      i += 1

from agents.

ChengshuLi avatar ChengshuLi commented on July 17, 2024

@oars

Thank you so much for your help! It completely makes sense.

I think I was just following agents/tf_agents/colabs/environments_tutorial.ipynb without thinking too much about it and also overlooked tf.enable_eager_execution().

Thanks again.

from agents.

ChengshuLi avatar ChengshuLi commented on July 17, 2024

@oars After running your code, I actually got an error:

RuntimeError: The Session graph is empty.  Add operations to the graph before calling run().

Do you know how I can fix this?

Also, as I mentioned above, when I ran the code tf_agents/agents/dqn/examples/train_eval_atari.py with no modification, the memory usage still fluctuated between 40% to 80+%, in which case my computer also becomes slower. Is that to be expected?

Thanks!

from agents.

oars avatar oars commented on July 17, 2024

Can you try updating tf-agents and trying again? I can't reproduce your error. For reference I just re-ran with this code:

import tensorflow as tf
from tf_agents.environments import tf_py_environment, py_environment
from tf_agents.specs import array_spec
from tf_agents.environments import time_step as ts
import numpy as np

img_size = 5000

class MyEnv(py_environment.Base):
    def __init__(self):
        self._action_spec = array_spec.BoundedArraySpec(shape=(), dtype=np.float32)
        self._observation_spec = array_spec.BoundedArraySpec(shape=(img_size, img_size, 3), dtype=np.float32)

    def action_spec(self):
        return self._action_spec

    def observation_spec(self):
        return self._observation_spec

    def _reset(self):
        return ts.restart(np.zeros(shape=(img_size, img_size, 3), dtype=np.float32))

    def _step(self, action):
        return ts.transition(np.zeros(shape=(img_size, img_size, 3), dtype=np.float32), reward=0.0, discount=1.0)

tf_py_env = MyEnv()
tf_env = tf_py_environment.TFPyEnvironment(tf_py_env)
action = tf.constant([0.0])
reset_op = tf_env.reset()
step_op = tf_env.step(action)

i = 0

with tf.Session() as sess:
  while True:
      if i % 100 == 0:
          print(i)
          time_step = sess.run(reset_op)
      time_step = sess.run(step_op)
      i += 1

Regarding the memory fluctuations I wouldn't expect that to happen either. Note that your image is fairly large ~280MB as raw float32 so if there are a couple of internal instances of it memory usage will be large.

from agents.

ChengshuLi avatar ChengshuLi commented on July 17, 2024

It works!

I used tf.enable_eager_execution() before and it didn't work.
Sorry I should have spent more time understanding tf eager mode and this error should be obvious. Thanks a lot.

from agents.

liujuncn avatar liujuncn commented on July 17, 2024

@oars How to deal with it in tf 2.0 without tf.Session ?

from agents.

oars avatar oars commented on July 17, 2024

How to deal with what? There are examples using environments in 2.0. Please look at the colabs.

from agents.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.