Example #1
0
    def __init__(self):
        # Import data
        error = None
        for _ in range(10):
            try:
                self.mnist = input_data.read_data_sets(
                    "/tmp/tensorflow/mnist/input_data", one_hot=True)
                error = None
                break
            except Exception as e:
                error = e
                time.sleep(5)
        if error:
            raise ValueError("Failed to import data", error)

        # Set seed and build layers
        tf.set_random_seed(0)

        self.x = tf.placeholder(tf.float32, [None, 784], name="x")
        self.y_ = tf.placeholder(tf.float32, [None, 10], name="y_")
        y_conv, self.keep_prob = deepnn(self.x)

        # Need to define loss and optimizer attributes
        self.loss = tf.reduce_mean(
            tf.nn.softmax_cross_entropy_with_logits(labels=self.y_,
                                                    logits=y_conv))
        self.optimizer = tf.train.AdamOptimizer(1e-4)
        self.variables = TensorFlowVariables(self.loss,
                                             tf.get_default_session())

        # For evaluating test accuracy
        correct_prediction = tf.equal(tf.argmax(y_conv, 1),
                                      tf.argmax(self.y_, 1))
        self.accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
Example #2
0
    def __init__(self, batch=64, use_cpus=False):

        image_shape = [batch, 224, 224, 3]
        labels_shape = [batch]

        # Synthetic image should be within [0, 255].
        images = tf.truncated_normal(
            image_shape,
            dtype=tf.float32,
            mean=127,
            stddev=60,
            name='synthetic_images')

        # Minor hack to avoid H2D copy when using synthetic data
        inputs = tf.contrib.framework.local_variable(
            images, name='gpu_cached_images')
        labels = tf.random_uniform(
            labels_shape,
            minval=0,
            maxval=999,
            dtype=tf.int32,
            name='synthetic_labels')

        model = model_config.get_model_config("resnet101", MockDataset())
        logits, aux = model.build_network(
            inputs, data_format=use_cpus and "NHWC" or "NCHW")
        loss = tf.nn.sparse_softmax_cross_entropy_with_logits(
            logits=logits, labels=labels)

        # Implement model interface
        self.loss = tf.reduce_mean(loss, name='xentropy-loss')
        self.optimizer = tf.train.GradientDescentOptimizer(1e-6)

        self.variables = TensorFlowVariables(self.loss,
                                             tf.get_default_session())
Example #3
0
class TFBenchModel(Model):
    def __init__(self, batch=64, use_cpus=False):

        image_shape = [batch, 224, 224, 3]
        labels_shape = [batch]

        # Synthetic image should be within [0, 255].
        images = tf.truncated_normal(
            image_shape,
            dtype=tf.float32,
            mean=127,
            stddev=60,
            name='synthetic_images')

        # Minor hack to avoid H2D copy when using synthetic data
        inputs = tf.contrib.framework.local_variable(
            images, name='gpu_cached_images')
        labels = tf.random_uniform(
            labels_shape,
            minval=0,
            maxval=999,
            dtype=tf.int32,
            name='synthetic_labels')

        model = model_config.get_model_config("resnet101", MockDataset())
        logits, aux = model.build_network(
            inputs, data_format=use_cpus and "NHWC" or "NCHW")
        loss = tf.nn.sparse_softmax_cross_entropy_with_logits(
            logits=logits, labels=labels)

        # Implement model interface
        self.loss = tf.reduce_mean(loss, name='xentropy-loss')
        self.optimizer = tf.train.GradientDescentOptimizer(1e-6)

        self.variables = TensorFlowVariables(self.loss,
                                             tf.get_default_session())

    def get_loss(self):
        return self.loss

    def get_optimizer(self):
        return self.optimizer

    def get_feed_dict(self):
        return {}

    def get_weights(self):
        return self.variables.get_flat()

    def set_weights(self, weights):
        self.variables.set_flat(weights)
Example #4
0
    def __init__(self, registry, env, config, sess):
        self.config = config
        self.sess = sess

        obs_space = env.observation_space
        ac_space = env.action_space

        self.obs_size = int(np.prod(obs_space.shape))
        self.obs = tf.placeholder(tf.float32, [None, self.obs_size])
        self.ac_size = int(np.prod(ac_space.shape))
        self.act = tf.placeholder(tf.float32, [None, self.ac_size])
        self.action_bound = env.action_space.high
        # TODO: change action_bound to make more general

        self._setup_actor_network(obs_space, ac_space)
        self._setup_critic_network(obs_space, ac_space)
        self._setup_critic_loss(ac_space)

        with tf.variable_scope("critic"):
            self.critic_var_list = tf.get_collection(
                                     tf.GraphKeys.TRAINABLE_VARIABLES,
                                     tf.get_variable_scope().name
                                   )
            self.critic_vars = TensorFlowVariables(self.critic_loss,
                                                   self.sess)

        with tf.variable_scope("actor"):
            self.actor_var_list = tf.get_collection(
                                    tf.GraphKeys.TRAINABLE_VARIABLES,
                                    tf.get_variable_scope().name
                                  )
            self.actor_vars = TensorFlowVariables(self.output_action,
                                                  self.sess)

        if (self.config["noise_add"]):
            params = self.config["noise_parameters"]
            self.rand_process = OrnsteinUhlenbeckProcess(size=self.ac_size,
                                                         theta=params["theta"],
                                                         mu=params["mu"],
                                                         sigma=params["sigma"])
            self.epsilon = 1.0
Example #5
0
    def __init__(self, sess: tf.Session, config):
        """Neural network policy that compute action given observation.
        
        Args:
            config: Useful configuration, almost use as const.
        """
        self.sess = sess

        self.config = config
        self._build_model()
        self._set_loss()
        optimizer = tf.train.AdamOptimizer(0.01)
        self.train_op = optimizer.minimize(self.loss)

        self.variables = TensorFlowVariables(self.loss, self.sess)
Example #6
0
    def __init__(self, sess: tf.Session, config):
        """Neural network policy that compute action given observation.
        
        Args:
            config: Useful configuration, almost use as const.
        """
        self.sess = sess

        self.config = config
        self._build_model()
        self._set_loss()
        optimizer = tf.train.GradientDescentOptimizer(config.learning_rate)
        grads_and_vars = optimizer.compute_gradients(self.loss)
        self.train_op = optimizer.apply_gradients(grads_and_vars)

        self.variables = TensorFlowVariables(self.loss, self.sess)
        self.sess.run(tf.global_variables_initializer())
Example #7
0
class DDPGActorCritic():
    other_output = []
    is_recurrent = False

    def __init__(self, registry, env, config, sess):
        self.config = config
        self.sess = sess

        obs_space = env.observation_space
        ac_space = env.action_space

        self.obs_size = int(np.prod(obs_space.shape))
        self.obs = tf.placeholder(tf.float32, [None, self.obs_size])
        self.ac_size = int(np.prod(ac_space.shape))
        self.act = tf.placeholder(tf.float32, [None, self.ac_size])
        self.action_bound = env.action_space.high
        # TODO: change action_bound to make more general

        self._setup_actor_network(obs_space, ac_space)
        self._setup_critic_network(obs_space, ac_space)
        self._setup_critic_loss(ac_space)

        with tf.variable_scope("critic"):
            self.critic_var_list = tf.get_collection(
                tf.GraphKeys.TRAINABLE_VARIABLES,
                tf.get_variable_scope().name)
            self.critic_vars = TensorFlowVariables(self.critic_loss, self.sess)

        with tf.variable_scope("actor"):
            self.actor_var_list = tf.get_collection(
                tf.GraphKeys.TRAINABLE_VARIABLES,
                tf.get_variable_scope().name)
            self.actor_vars = TensorFlowVariables(self.output_action,
                                                  self.sess)

        if (self.config["noise_add"]):
            params = self.config["noise_parameters"]
            self.rand_process = OrnsteinUhlenbeckProcess(size=self.ac_size,
                                                         theta=params["theta"],
                                                         mu=params["mu"],
                                                         sigma=params["sigma"])
            self.epsilon = 1.0

    def _setup_critic_loss(self, action_space):
        """Sets up critic loss."""
        self.target_Q = tf.placeholder(tf.float32, [None, 1], name="target_q")

        # compare critic eval to critic_target (squared loss)
        self.reward = tf.placeholder(tf.float32, [None], name="reward")
        self.critic_target = tf.expand_dims(self.reward, 1) + \
            self.config['gamma'] * self.target_Q
        self.critic_loss = tf.reduce_mean(
            tf.square(self.critic_target - self.critic_eval))

    def _setup_critic_network(self, obs_space, ac_space):
        """Sets up Q network."""
        with tf.variable_scope("critic", reuse=tf.AUTO_REUSE):
            self.critic_network = DDPGCritic((self.obs, self.act), 1, {})
            self.critic_eval = self.critic_network.outputs

        with tf.variable_scope("critic", reuse=True):
            self.cn_for_loss = DDPGCritic((self.obs, self.output_action), 1,
                                          {}).outputs

    def _setup_actor_network(self, obs_space, ac_space):
        """Sets up actor network."""
        with tf.variable_scope("actor", reuse=tf.AUTO_REUSE):
            self.actor_network = DDPGActor(
                self.obs,
                self.ac_size,
                options={"action_bound": self.action_bound})
            self.output_action = self.actor_network.outputs

    def get_weights(self):
        """Returns critic weights, actor weights."""
        return self.critic_vars.get_weights(), self.actor_vars.get_weights()

    def set_weights(self, weights):
        """Sets critic and actor weights."""
        critic_weights, actor_weights = weights
        self.critic_vars.set_weights(critic_weights)
        self.actor_vars.set_weights(actor_weights)

    def compute(self, ob):
        """Returns action, given state."""
        flattened_ob = np.reshape(ob, [-1, np.prod(ob.shape)])
        action = self.sess.run(self.output_action, {self.obs: flattened_ob})
        if (self.config["noise_add"]):
            action += self.epsilon * self.rand_process.sample()
            if (self.epsilon > 0):
                self.epsilon -= self.config["noise_epsilon"]
        return action[0], {}

    def value(self, *args):
        return 0
Example #8
0
class MNISTModel(Model):
    def __init__(self):
        # Import data
        error = None
        for _ in range(10):
            try:
                self.mnist = input_data.read_data_sets(
                    "/tmp/tensorflow/mnist/input_data", one_hot=True)
                error = None
                break
            except Exception as e:
                error = e
                time.sleep(5)
        if error:
            raise ValueError("Failed to import data", error)

        # Set seed and build layers
        tf.set_random_seed(0)

        self.x = tf.placeholder(tf.float32, [None, 784], name="x")
        self.y_ = tf.placeholder(tf.float32, [None, 10], name="y_")
        y_conv, self.keep_prob = deepnn(self.x)

        # Need to define loss and optimizer attributes
        self.loss = tf.reduce_mean(
            tf.nn.softmax_cross_entropy_with_logits(labels=self.y_,
                                                    logits=y_conv))
        self.optimizer = tf.train.AdamOptimizer(1e-4)
        self.variables = TensorFlowVariables(self.loss,
                                             tf.get_default_session())

        # For evaluating test accuracy
        correct_prediction = tf.equal(tf.argmax(y_conv, 1),
                                      tf.argmax(self.y_, 1))
        self.accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

    def get_loss(self):
        return self.loss

    def get_optimizer(self):
        return self.optimizer

    def get_variables(self):
        return self.variables

    def get_feed_dict(self):
        batch = self.mnist.train.next_batch(50)
        return {
            self.x: batch[0],
            self.y_: batch[1],
            self.keep_prob: 0.5,
        }

    def get_metrics(self):
        accuracy = self.accuracy.eval(
            feed_dict={
                self.x: self.mnist.test.images,
                self.y_: self.mnist.test.labels,
                self.keep_prob: 1.0,
            })
        return {"accuracy": accuracy}

    def get_weights(self):
        return self.variables.get_flat()

    def set_weights(self, weights):
        self.variables.set_flat(weights)