示例#1
0
def create_environment(level_name, seed, is_test=False):
    """Creates an environment wrapped in a `FlowEnvironment`."""
    if level_name.startswith('doom_'):
        config = None
        p = py_process.PyProcess(PyProcessDoom, level_name, config,
                                 FLAGS.num_action_repeats, seed)
        return environments.FlowEnvironment(p.proxy)
    else:
        if level_name in dmlab30.ALL_LEVELS:
            level_name = 'contributed/dmlab30/' + level_name

        # Note, you may want to use a level cache to speed of compilation of
        # environment maps. See the documentation for the Python interface of DeepMind
        # Lab.
        config = {
            'width': FLAGS.width,
            'height': FLAGS.height,
            'datasetPath': FLAGS.dataset_path,
            'logLevel': 'WARN',
            'gpuDeviceIndex': '0',
            'renderer': FLAGS.renderer,
            'benchmark_mode': FLAGS.benchmark_mode,
        }
        if is_test:
            config['allowHoldOutLevels'] = 'true'
            # Mixer seed for evalution, see
            # https://github.com/deepmind/lab/blob/master/docs/users/python_api.md
            config['mixerSeed'] = 0x600D5EED
        p = py_process.PyProcess(environments.PyProcessDmLab, level_name,
                                 config, FLAGS.num_action_repeats, seed)
        return environments.FlowEnvironment(p.proxy)
示例#2
0
    def test_close_on_error(self):
        with tempfile.NamedTemporaryFile() as tmp:

            class Example(object):
                def __init__(self, filename):
                    self._filename = filename

                def something(self):
                    raise ValueError('foo')

                def close(self):
                    with tf.gfile.Open(self._filename, 'w') as f:
                        f.write('was_closed')

                @staticmethod
                def _tensor_specs(method_name, unused_kwargs,
                                  unused_constructor_kwargs):
                    if method_name == 'something':
                        return ()

            with tf.Graph().as_default():
                p = py_process.PyProcess(Example, tmp.name)
                result = p.proxy.something()

                with tf.train.SingularMonitoredSession(
                        hooks=[py_process.PyProcessHook()]) as session:
                    with self.assertRaisesRegexp(Exception, 'foo'):
                        session.run(result)

            self.assertEqual('was_closed', tmp.read())
示例#3
0
  def test_args(self):

    class Example(object):

      def __init__(self, dim0):
        self._dim0 = dim0

      def compute(self, dim1):
        return np.zeros([self._dim0, dim1], dtype=np.int32)

      @staticmethod
      def _tensor_specs(method_name, kwargs, constructor_kwargs):
        dim0 = constructor_kwargs['dim0']
        dim1 = kwargs['dim1']
        if method_name == 'compute':
          return tf.contrib.framework.TensorSpec([dim0, dim1], tf.int32)

    with tf.Graph().as_default():
      p = py_process.PyProcess(Example, 1)
      result = p.proxy.compute(2)

      with tf.train.SingularMonitoredSession(
          hooks=[py_process.PyProcessHook()]) as session:
        self.assertEqual([1, 2], result.shape)
        self.assertAllEqual([[0, 0]], session.run(result))
示例#4
0
  def test_close(self):
    with tempfile.NamedTemporaryFile() as tmp:
      class Example(object):

        def __init__(self, filename):
          self._filename = filename

        def close(self):
          with tf.gfile.Open(self._filename, 'w') as f:
            f.write('was_closed')

        @staticmethod
        def _tensor_specs(method_name, unused_kwargs,
                          unused_constructor_kwargs):
          if method_name == 'something':
            return ()

      with tf.Graph().as_default():
        py_process.PyProcess(Example, tmp.name)

        with tf.train.SingularMonitoredSession(
            hooks=[py_process.PyProcessHook()]):
          pass

      self.assertEqual('was_closed', tmp.read())
示例#5
0
    def test_small(self):
        class Example(object):
            def __init__(self, a):
                self._a = a

            def inc(self):
                self._a += 1

            def compute(self, b):
                return np.array(self._a + b, dtype=np.int32)

            @staticmethod
            def _tensor_specs(method_name, unused_args,
                              unused_constructor_kwargs):
                if method_name == 'compute':
                    return tf.contrib.framework.TensorSpec([], tf.int32)
                elif method_name == 'inc':
                    return ()

        with tf.Graph().as_default():
            p = py_process.PyProcess(Example, 1)
            inc = p.proxy.inc()
            compute = p.proxy.compute(2)

            with tf.train.SingularMonitoredSession(
                    hooks=[py_process.PyProcessHook()]) as session:
                self.assertTrue(isinstance(inc, tf.Operation))
                session.run(inc)

                self.assertEqual([], compute.shape)
                self.assertEqual(4, session.run(compute))
示例#6
0
    def test_threading(self):
        class Example(object):
            def __init__(self):
                pass

            def wait(self):
                time.sleep(.2)
                return None

            @staticmethod
            def _tensor_specs(method_name, unused_args,
                              unused_constructor_kwargs):
                if method_name == 'wait':
                    return tf.contrib.framework.TensorSpec([], tf.int32)

        with tf.Graph().as_default():
            p = py_process.PyProcess(Example)
            wait = p.proxy.wait()

            hook = py_process.PyProcessHook()
            with tf.train.SingularMonitoredSession(hooks=[hook]) as session:

                def run():
                    with self.assertRaises(tf.errors.OutOfRangeError):
                        session.run(wait)

                t = self.checkedThread(target=run)
                t.start()
                time.sleep(.1)
            t.join()
def create_atari_environment(env_id, seed, is_test=False):
    #   print("Before env proxy")
    config = {'width': 84, 'height': 84, 'level': env_id, 'logLevel': 'warn'}
    env_proxy = py_process.PyProcess(environments.PyProcessAtari, env_id,
                                     config, FLAGS.num_action_repeats, seed)

    environment = environments.FlowEnvironment(env_proxy.proxy)
    return environment
示例#8
0
    def benchmark_one(self):
        with tf.Graph().as_default():
            p = py_process.PyProcess(PyProcessBenchmarks.Example)
            compute = p.proxy.compute(2)

            with tf.train.SingularMonitoredSession(
                    hooks=[py_process.PyProcessHook()]) as session:

                self.run_op_benchmark(name='process_one',
                                      sess=session,
                                      op_or_tensor=compute,
                                      burn_iters=10,
                                      min_iters=5000)
示例#9
0
def create_environment(level_name, seed, is_test=False):
    """Creates an environment wrapped in a `FlowEnvironment`."""

    # Note, you may want to use a level cache to speed of compilation of
    # environment maps. See the documentation for the Python interface of DeepMind
    # Lab.
    config = {'width': FLAGS.width, 'height': FLAGS.height}
    if is_test:
        config['allowHoldOutLevels'] = 'true'
        # Mixer seed for evalution, see
        # https://github.com/deepmind/lab/blob/master/docs/users/python_api.md
        config['mixerSeed'] = 0x600D5EED
    p = py_process.PyProcess(environments.PyProcessGym, level_name, config)
    return environments.FlowEnvironment(p.proxy)
示例#10
0
def create_atari_environment(env_id, seed, is_test=False):

    config = {'width': FLAGS.width, 'height': FLAGS.height}

    if is_test:
        config['allowHoldOutLevels'] = 'true'
        # Mixer seed for evalution, see
        # https://github.com/deepmind/lab/blob/master/docs/users/python_api.md
        config['mixerSeed'] = 0x600D5EED

    process = py_process.PyProcess(atari_environment.PyProcessAtari, env_id,
                                   config, seed)
    proxy_env = atari_environment.FlowEnvironment(process.proxy)
    return proxy_env
示例#11
0
    def benchmark_many(self):
        with tf.Graph().as_default():
            ps = [
                py_process.PyProcess(PyProcessBenchmarks.Example)
                for _ in range(200)
            ]
            compute_ops = [p.proxy.compute(2) for p in ps]
            compute = tf.group(*compute_ops)

            with tf.train.SingularMonitoredSession(
                    hooks=[py_process.PyProcessHook()]) as session:

                self.run_op_benchmark(name='process_many',
                                      sess=session,
                                      op_or_tensor=compute,
                                      burn_iters=10,
                                      min_iters=500)
示例#12
0
    def test_close(self):
        with tempfile.NamedTemporaryFile() as tmp:

            class Example(object):
                def __init__(self, filename):
                    self._filename = filename

                def close(self):
                    with tf.gfile.Open(self._filename, 'w') as f:
                        f.write('was_closed')

            with tf.Graph().as_default():
                py_process.PyProcess(Example, tmp.name)

                with tf.train.SingularMonitoredSession(
                        hooks=[py_process.PyProcessHook()]):
                    pass

            self.assertEqual('was_closed', tmp.read())
示例#13
0
    def test_error_handling_constructor(self):
        class Example(object):
            def __init__(self):
                raise ValueError('foo')

            def something(self):
                pass

            @staticmethod
            def _tensor_specs(method_name, unused_kwargs,
                              unused_constructor_kwargs):
                if method_name == 'something':
                    return ()

        with tf.Graph().as_default():
            py_process.PyProcess(Example, 1)

            with self.assertRaisesRegexp(Exception, 'foo'):
                with tf.train.SingularMonitoredSession(
                        hooks=[py_process.PyProcessHook()]):
                    pass
示例#14
0
def create_environment(env_sampler,
                       initial_task_name=None,
                       seed=0):
  """Creates an environment wrapped in a `FlowEnvironment`."""
  # Sample a task if not provided
  if initial_task_name is None:
    initial_task_name = np.random.choice(env_sampler.task_names)
  # config is empty dict for now
  config = {}
  p = py_process.PyProcess(environments.PyProcessCraftLab, env_sampler,
                           initial_task_name, config, FLAGS.num_action_repeats,
                           seed)

  flow_env = environments.FlowEnvironment(p.proxy)

  # TODO clean me up, useful for debugging
  # obs_reset = p.proxy.initial()
  # rew, done, obs_step = p.proxy.step(0)
  # output_initial, state_initial = flow_env.initial()
  # output_step, state_step = flow_env.step(0, state_initial)

  return flow_env
示例#15
0
def create_environment(config, is_test=False):
    """Creates an exchange environment wrapped in a `FlowEnvironment`."""

    p = py_process.PyProcess(wrappers.PyProcessExchEnv, config)

    return wrappers.FlowEnvironment(p.proxy)
示例#16
0
def create_environment(config, is_test=False):
    """Creates an environment wrapped in a `FlowEnvironment`."""
    p = py_process.PyProcess(environments.PyProcessPommerMan, config=config)
    return environments.FlowEnvironment(p.proxy)
示例#17
0
def create_environment(game_name, state_size):
    """Creates an environment wrapped in a `FlowEnvironment`."""
    config = {'observation_size': state_size}

    p = py_process.PyProcess(environments.PyProcessGym, game_name, config)
    return environments.FlowEnvironment(p.proxy)