class_docstr: Class for converting a gym environment, a controller style, and others into a black-box optimization benchmark.
class_docstr_tok: ["Class","for","converting","a","gym","environment",",","a","controller","style",",","and","others","into","a","black-box","optimization","benchmark","."]
docstr: Applies a neural net parametrized by x to an observation o. Returns an action or logits of actions.
docstr_tok: ["Applies","a","neural","net","parametrized","by","x","to","an","observation","o",".","Returns","an","action","or","logits","of","actions","."]
code: def neural(self, x: np.ndarray, o: np.ndarray):
      """Applies a neural net parametrized by x to an observation o. Returns an action or logits of actions."""
      if self.greedy_bias:
          assert "multi" not in self.control
          assert "structured" not in self.control
          self.greedy_coefficient = x[
              -1:
          ]  # We have decided that we can not have two runs in parallel.
          x = x[:-1]
      o = o.ravel()
      my_scale = 2**self.optimization_scale
      if "structured" not in self.name and self.optimization_scale != 0:
          x = np.asarray(my_scale * x, dtype=np.float32)
      if self.control == "linear":
          # The linear case is simplle.
          output = np.matmul(o, x[1:, :])
          output += x[0]
          return output.reshape(self.output_shape), np.zeros(0)
      if "structured" not in self.control:
          # If not structured then we split into two matrices.
          first_matrix = x[: self.first_size].reshape(
              self.first_layer_shape
          ) / np.sqrt(len(o))
          second_matrix = x[
              self.first_size : (self.first_size + self.second_size)
          ].reshape(self.second_layer_shape) / np.sqrt(self.num_neurons)
      else:
          # In the structured case we should have two entries with the right shapes.
          assert len(x) == 2
          first_matrix = np.asarray(x[0][0])
          second_matrix = np.asarray(x[0][1])
          assert (
              first_matrix.shape == self.first_layer_shape
          ), f"{first_matrix} does not match {self.first_layer_shape}"
          assert (
              second_matrix.shape == self.second_layer_shape
          ), f"{second_matrix} does not match {self.second_layer_shape}"
      if "resid" in self.control:
          first_matrix += my_scale * np.eye(*first_matrix.shape)
          second_matrix += my_scale * np.eye(*second_matrix.shape)
      assert len(o) == len(
          first_matrix[1:]
      ), f"{o.shape} coming in matrix of shape {first_matrix.shape}"
      output = np.matmul(o, first_matrix[1:])
      if "deep" in self.control:
          # The deep case must be split into several layers.
          current_index = self.first_size + self.second_size
          internal_layer_size = self.num_neurons**2
          s = (self.num_neurons, self.num_neurons)
          for _ in range(self.num_internal_layers):
              output = np.tanh(output)
              layer = x[
                  current_index : current_index + internal_layer_size
              ].reshape(s)
              if "resid" in self.control:
                  layer += my_scale * np.eye(*layer.shape)
              output = np.matmul(output, layer) / np.sqrt(
                  self.num_neurons
              )
              current_index += internal_layer_size
          assert current_index == len(x)
      output = np.matmul(
          np.tanh(output + first_matrix[0]), second_matrix
      )
      return (
          output[self.memory_len :].reshape(self.output_shape),
          output[: self.memory_len],
      )
  
imports: import os
  import copy
  import scipy.stats
  import typing
  import numpy
  import gym
  import nevergrad
  from nevergrad.parametrization import parameter
  from ..base import ExperimentFunction
label_desc: Use image node_id 8 for calling the GymMulti obj's underlying member method code with example usage: obj.neural(x, o) and returns: output,  output, output,  np