Python tensorflow.float64() Examples

The following are 30 code examples of tensorflow.float64(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module tensorflow , or try the search function .
Example #1
Source File: model.py    From PathCon with MIT License 7 votes vote down vote up
def _get_neighbors_and_masks(self, relations, entity_pairs, train_edges):
        edges_list = [relations]
        masks = []
        train_edges = tf.expand_dims(train_edges, -1)  # [batch_size, 1]

        for i in range(self.context_hops):
            if i == 0:
                neighbor_entities = entity_pairs
            else:
                neighbor_entities = tf.reshape(tf.gather(self.edge2entities, edges_list[-1]), [self.batch_size, -1])
            neighbor_edges = tf.reshape(tf.gather(self.entity2edges, neighbor_entities), [self.batch_size, -1])
            edges_list.append(neighbor_edges)

            mask = neighbor_edges - train_edges  # [batch_size, -1]
            mask = tf.cast(tf.cast(mask, tf.bool), tf.float64)  # [batch_size, -1]
            masks.append(mask)
        return edges_list, masks 
Example #2
Source File: parametric_GP.py    From ParametricGP with MIT License 6 votes vote down vote up
def train(self):
        print("Total number of parameters: %d" % (self.hyp.shape[0]))
        
        X_tf = tf.placeholder(tf.float64)
        y_tf = tf.placeholder(tf.float64)
        hyp_tf = tf.Variable(self.hyp, dtype=tf.float64)
        
        train = self.likelihood(hyp_tf, X_tf, y_tf)
        
        init = tf.global_variables_initializer()
        self.sess.run(init)
        
        start_time = timeit.default_timer()
        for i in range(1,self.max_iter+1):
            # Fetch minibatch
            X_batch, y_batch = fetch_minibatch(self.X,self.y,self.N_batch)
            self.sess.run(train, {X_tf:X_batch, y_tf:y_batch})
            
            if i % self.monitor_likelihood == 0:
                elapsed = timeit.default_timer() - start_time
                nlml = self.sess.run(self.nlml)
                print('Iteration: %d, NLML: %.2f, Time: %.2f' % (i, nlml, elapsed))
                start_time = timeit.default_timer()

        self.hyp = self.sess.run(hyp_tf) 
Example #3
Source File: model.py    From PathCon with MIT License 6 votes vote down vote up
def _rnn(self, path_ids):
        path_ids = tf.reshape(path_ids, [self.batch_size * self.path_samples])  # [batch_size * path_samples]
        paths = tf.nn.embedding_lookup(self.id2path, path_ids)  # [batch_size * path_samples, max_path_len]
        # [batch_size * path_samples, max_path_len, relation_dim]
        path_features = tf.nn.embedding_lookup(self.relation_features, paths)
        lengths = tf.nn.embedding_lookup(self.id2length, path_ids)  # [batch_size * path_samples]

        cell = tf.nn.rnn_cell.LSTMCell(num_units=self.hidden_dim, name='basic_lstm_cell')
        initial_state = cell.zero_state(self.batch_size * self.path_samples, tf.float64)

        # [batch_size * path_samples, hidden_dim]
        _, last_state = tf.nn.dynamic_rnn(cell, path_features, sequence_length=lengths, initial_state=initial_state)

        self.W, self.b = self._get_weight_and_bias(self.hidden_dim, self.n_relations)
        output = tf.matmul(last_state.h, self.W) + self.b  # [batch_size * path_samples, n_relations]
        output = tf.reshape(output, [self.batch_size, self.path_samples, self.n_relations])

        return output 
Example #4
Source File: model.py    From PathCon with MIT License 6 votes vote down vote up
def _build_relation_feature(self):
        if self.feature_type == 'id':
            self.relation_dim = self.n_relations
            self.relation_features = tf.eye(self.n_relations, dtype=tf.float64)
        elif self.feature_type == 'bow':
            bow = np.load('../data/' + self.dataset + '/bow.npy')
            self.relation_dim = bow.shape[1]
            self.relation_features = tf.constant(bow, tf.float64)
        elif self.feature_type == 'bert':
            bert = np.load('../data/' + self.dataset + '/bert.npy')
            self.relation_dim = bert.shape[1]
            self.relation_features = tf.constant(bert, tf.float64)

        # the feature of the last relation (the null relation) is a zero vector
        self.relation_features = tf.concat([self.relation_features, tf.zeros([1, self.relation_dim], tf.float64)],
                                           axis=0, name='relation_features') 
Example #5
Source File: gaussian_distribution.py    From tf-example-models with Apache License 2.0 6 votes vote down vote up
def initialize(self, dtype=tf.float64):
        if self.tf_mean is None:
            if self.mean is not None:
                self.tf_mean = tf.Variable(self.mean, dtype=dtype)
            else:
                self.tf_mean = tf.Variable(tf.cast(tf.fill([self.dims], 0.0), dtype))

        if self.tf_covariance is None:
            if self.covariance is not None:
                self.tf_covariance = self.covariance
            else:
                self.tf_covariance = FullCovariance(self.dims)

            self.tf_covariance.initialize(dtype)

        if self.tf_ln2piD is None:
            self.tf_ln2piD = tf.constant(np.log(2 * np.pi) * self.dims, dtype=dtype) 
Example #6
Source File: running_mean_std.py    From HardRLWithYoutube with MIT License 6 votes vote down vote up
def __init__(self, epsilon=1e-4, shape=(), scope=''):
        sess = get_session()

        self._new_mean = tf.placeholder(shape=shape, dtype=tf.float64)
        self._new_var = tf.placeholder(shape=shape, dtype=tf.float64)
        self._new_count = tf.placeholder(shape=(), dtype=tf.float64)

        
        with tf.variable_scope(scope, reuse=tf.AUTO_REUSE):
            self._mean  = tf.get_variable('mean',  initializer=np.zeros(shape, 'float64'),      dtype=tf.float64)
            self._var   = tf.get_variable('std',   initializer=np.ones(shape, 'float64'),       dtype=tf.float64)    
            self._count = tf.get_variable('count', initializer=np.full((), epsilon, 'float64'), dtype=tf.float64)

        self.update_ops = tf.group([
            self._var.assign(self._new_var),
            self._mean.assign(self._new_mean),
            self._count.assign(self._new_count)
        ])

        sess.run(tf.variables_initializer([self._mean, self._var, self._count]))
        self.sess = sess
        self._set_mean_var_count() 
Example #7
Source File: tf_transformer_test.py    From spark-deep-learning with Apache License 2.0 6 votes vote down vote up
def test_graph_array_types(self):
        test_dtypes = [(tf.int32, ArrayType(IntegerType()), np.int32),
                       (tf.int64, ArrayType(LongType()), np.int64),
                       (tf.float32, ArrayType(FloatType()), np.float32),
                       (tf.float64, ArrayType(DoubleType()), np.float64)]
        for tf_dtype, spark_dtype, np_type in test_dtypes:
            transformer = _build_transformer(lambda session:
                                             TFInputGraph.fromGraph(session.graph, session,
                                                                    [_tensor_input_name],
                                                                    [_tensor_output_name]),
                                             tf_dtype)
            gin = transformer.getTFInputGraph()
            local_features = _build_local_features(np_type)
            expected = _get_expected_result(gin, local_features)
            schema = StructType([StructField('inputCol', spark_dtype)])
            dataset = self.session.createDataFrame(local_features, schema)
            _check_transformer_output(transformer, dataset, expected)

# The name of the input tensor 
Example #8
Source File: running_mean_std.py    From Reinforcement_Learning_for_Traffic_Light_Control with Apache License 2.0 6 votes vote down vote up
def __init__(self, epsilon=1e-4, shape=(), scope=''):
        sess = get_session()

        self._new_mean = tf.placeholder(shape=shape, dtype=tf.float64)
        self._new_var = tf.placeholder(shape=shape, dtype=tf.float64)
        self._new_count = tf.placeholder(shape=(), dtype=tf.float64)


        with tf.variable_scope(scope, reuse=tf.AUTO_REUSE):
            self._mean  = tf.get_variable('mean',  initializer=np.zeros(shape, 'float64'),      dtype=tf.float64)
            self._var   = tf.get_variable('std',   initializer=np.ones(shape, 'float64'),       dtype=tf.float64)
            self._count = tf.get_variable('count', initializer=np.full((), epsilon, 'float64'), dtype=tf.float64)

        self.update_ops = tf.group([
            self._var.assign(self._new_var),
            self._mean.assign(self._new_mean),
            self._count.assign(self._new_count)
        ])

        sess.run(tf.variables_initializer([self._mean, self._var, self._count]))
        self.sess = sess
        self._set_mean_var_count() 
Example #9
Source File: model.py    From tensorflow-wavenet with MIT License 6 votes vote down vote up
def predict_proba(self, waveform, global_condition=None, name='wavenet'):
        '''Computes the probability distribution of the next sample based on
        all samples in the input waveform.
        If you want to generate audio by feeding the output of the network back
        as an input, see predict_proba_incremental for a faster alternative.'''
        with tf.name_scope(name):
            if self.scalar_input:
                encoded = tf.cast(waveform, tf.float32)
                encoded = tf.reshape(encoded, [-1, 1])
            else:
                encoded = self._one_hot(waveform)

            gc_embedding = self._embed_gc(global_condition)
            raw_output = self._create_network(encoded, gc_embedding)
            out = tf.reshape(raw_output, [-1, self.quantization_channels])
            # Cast to float64 to avoid bug in TensorFlow
            proba = tf.cast(
                tf.nn.softmax(tf.cast(out, tf.float64)), tf.float32)
            last = tf.slice(
                proba,
                [tf.shape(proba)[0] - 1, 0],
                [1, self.quantization_channels])
            return tf.reshape(last, [-1]) 
Example #10
Source File: model.py    From tensorflow-wavenet with MIT License 6 votes vote down vote up
def predict_proba_incremental(self, waveform, global_condition=None,
                                  name='wavenet'):
        '''Computes the probability distribution of the next sample
        incrementally, based on a single sample and all previously passed
        samples.'''
        if self.filter_width > 2:
            raise NotImplementedError("Incremental generation does not "
                                      "support filter_width > 2.")
        if self.scalar_input:
            raise NotImplementedError("Incremental generation does not "
                                      "support scalar input yet.")
        with tf.name_scope(name):
            encoded = tf.one_hot(waveform, self.quantization_channels)
            encoded = tf.reshape(encoded, [-1, self.quantization_channels])
            gc_embedding = self._embed_gc(global_condition)
            raw_output = self._create_generator(encoded, gc_embedding)
            out = tf.reshape(raw_output, [-1, self.quantization_channels])
            proba = tf.cast(
                tf.nn.softmax(tf.cast(out, tf.float64)), tf.float32)
            last = tf.slice(
                proba,
                [tf.shape(proba)[0] - 1, 0],
                [1, self.quantization_channels])
            return tf.reshape(last, [-1]) 
Example #11
Source File: mixture_model.py    From tf-example-models with Apache License 2.0 6 votes vote down vote up
def __init__(self, data, components, cluster=None, dtype=tf.float64):
        if isinstance(data, np.ndarray):
            data = [data]

        self.data = data
        self.dims = sum(d.shape[1] for d in data)
        self.num_points = data[0].shape[0]
        self.components = components

        self.tf_graph = tf.Graph()

        self._initialize_workers(cluster)
        self._initialize_component_mapping()
        self._initialize_data_sources()
        self._initialize_variables(dtype)
        self._initialize_graph(dtype) 
Example #12
Source File: py_func_batch_env.py    From fine-lm with MIT License 6 votes vote down vote up
def simulate(self, action):
    """Step the batch of environments.

    The results of the step can be accessed from the variables defined below.

    Args:
      action: Tensor holding the batch of actions to apply.

    Returns:
      Operation.
    """
    with tf.name_scope('environment/simulate'):
      if action.dtype in (tf.float16, tf.float32, tf.float64):
        action = tf.check_numerics(action, 'action')
      observ_dtype = utils.parse_dtype(self._batch_env.observation_space)
      observ, reward, done = tf.py_func(
          lambda a: self._batch_env.step(a)[:3], [action],
          [observ_dtype, tf.float32, tf.bool], name='step')
      observ = tf.check_numerics(observ, 'observ')
      reward = tf.check_numerics(reward, 'reward')
      reward.set_shape((len(self),))
      done.set_shape((len(self),))
      with tf.control_dependencies([self._observ.assign(observ)]):
        return tf.identity(reward), tf.identity(done) 
Example #13
Source File: in_graph_batch_env.py    From soccer-matlab with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def simulate(self, action):
    """Step the batch of environments.

    The results of the step can be accessed from the variables defined below.

    Args:
      action: Tensor holding the batch of actions to apply.

    Returns:
      Operation.
    """
    with tf.name_scope('environment/simulate'):
      if action.dtype in (tf.float16, tf.float32, tf.float64):
        action = tf.check_numerics(action, 'action')
      observ_dtype = self._parse_dtype(self._batch_env.observation_space)
      observ, reward, done = tf.py_func(
          lambda a: self._batch_env.step(a)[:3], [action],
          [observ_dtype, tf.float32, tf.bool], name='step')
      observ = tf.check_numerics(observ, 'observ')
      reward = tf.check_numerics(reward, 'reward')
      return tf.group(
          self._observ.assign(observ),
          self._action.assign(action),
          self._reward.assign(reward),
          self._done.assign(done)) 
Example #14
Source File: tensorFactorisation.py    From decompose with MIT License 6 votes vote down vote up
def llhIndividual(self, X: Tensor) -> Tensor:
        """Log likelihood of the parameters given data `X`."""

        # log likelihood of the noise
        llhRes = self.likelihood.llh(self.U, X)
        llh = llhRes

        # log likelihood of the factors
        llhU = []
        llhUfk = []
        U = list(self.U)
        for f, postUf in enumerate(self.postU):
            U = self.rescale(U=U, fNonUnit=f)
            UfT = tf.transpose(U[f])
            llhUfk.append(tf.reduce_sum(postUf.prior.llh(UfT), axis=0))
            llhUf = tf.reduce_sum(postUf.prior.llh(UfT))
            llh = llh + llhUf
            llhU.append(llhUf)
        llh = tf.cast(llh, tf.float64)
        return(llh, llhRes, llhU, llhUfk) 
Example #15
Source File: in_graph_batch_env.py    From soccer-matlab with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def simulate(self, action):
    """Step the batch of environments.

    The results of the step can be accessed from the variables defined below.

    Args:
      action: Tensor holding the batch of actions to apply.

    Returns:
      Operation.
    """
    with tf.name_scope('environment/simulate'):
      if action.dtype in (tf.float16, tf.float32, tf.float64):
        action = tf.check_numerics(action, 'action')
      observ_dtype = self._parse_dtype(self._batch_env.observation_space)
      observ, reward, done = tf.py_func(
          lambda a: self._batch_env.step(a)[:3], [action],
          [observ_dtype, tf.float32, tf.bool], name='step')
      observ = tf.check_numerics(observ, 'observ')
      reward = tf.check_numerics(reward, 'reward')
      return tf.group(
          self._observ.assign(observ),
          self._action.assign(action),
          self._reward.assign(reward),
          self._done.assign(done)) 
Example #16
Source File: running_mean_std.py    From Reinforcement_Learning_for_Traffic_Light_Control with Apache License 2.0 6 votes vote down vote up
def __init__(self, epsilon=1e-4, shape=(), scope=''):
        sess = get_session()

        self._new_mean = tf.placeholder(shape=shape, dtype=tf.float64)
        self._new_var = tf.placeholder(shape=shape, dtype=tf.float64)
        self._new_count = tf.placeholder(shape=(), dtype=tf.float64)


        with tf.variable_scope(scope, reuse=tf.AUTO_REUSE):
            self._mean  = tf.get_variable('mean',  initializer=np.zeros(shape, 'float64'),      dtype=tf.float64)
            self._var   = tf.get_variable('std',   initializer=np.ones(shape, 'float64'),       dtype=tf.float64)
            self._count = tf.get_variable('count', initializer=np.full((), epsilon, 'float64'), dtype=tf.float64)

        self.update_ops = tf.group([
            self._var.assign(self._new_var),
            self._mean.assign(self._new_mean),
            self._count.assign(self._new_count)
        ])

        sess.run(tf.variables_initializer([self._mean, self._var, self._count]))
        self.sess = sess
        self._set_mean_var_count() 
Example #17
Source File: accountant.py    From DOTA_models with Apache License 2.0 6 votes vote down vote up
def __init__(self, total_examples, moment_orders=32):
    """Initialize a MomentsAccountant.

    Args:
      total_examples: total number of examples.
      moment_orders: the order of moments to keep.
    """

    assert total_examples > 0
    self._total_examples = total_examples
    self._moment_orders = (moment_orders
                           if isinstance(moment_orders, (list, tuple))
                           else range(1, moment_orders + 1))
    self._max_moment_order = max(self._moment_orders)
    assert self._max_moment_order < 100, "The moment order is too large."
    self._log_moments = [tf.Variable(numpy.float64(0.0),
                                     trainable=False,
                                     name=("log_moments-%d" % moment_order))
                         for moment_order in self._moment_orders] 
Example #18
Source File: accountant.py    From DOTA_models with Apache License 2.0 6 votes vote down vote up
def get_privacy_spent(self, sess, target_eps=None):
    """Report the spending so far.

    Args:
      sess: the session to run the tensor.
      target_eps: the target epsilon. Unused.
    Returns:
      the list containing a single EpsDelta, with values as Python floats (as
      opposed to numpy.float64). This is to be consistent with
      MomentAccountant which can return a list of (eps, delta) pair.
    """

    # pylint: disable=unused-argument
    unused_target_eps = target_eps
    eps_squared_sum, delta_sum = sess.run([self._eps_squared_sum,
                                           self._delta_sum])
    return [EpsDelta(math.sqrt(eps_squared_sum), float(delta_sum))] 
Example #19
Source File: univariate.py    From zhusuan with MIT License 6 votes vote down vote up
def __init__(self, logits, dtype=tf.int32, group_ndims=0, **kwargs):
        self._logits = tf.convert_to_tensor(logits)
        param_dtype = assert_same_dtype_in(
            [(self._logits, 'Categorical.logits')],
            [tf.float32, tf.float64])

        allowed_dtypes = [tf.float32, tf.float64, tf.int32, tf.int64]
        assert_dtype_in_dtypes(dtype, allowed_dtypes)

        self._logits = assert_rank_at_least_one(
                self._logits, 'Categorical.logits')
        self._n_categories = get_shape_at(self._logits, -1)

        super(Categorical, self).__init__(
            dtype=dtype,
            param_dtype=param_dtype,
            is_continuous=False,
            is_reparameterized=False,
            group_ndims=group_ndims,
            **kwargs) 
Example #20
Source File: neural_programmer.py    From DOTA_models with Apache License 2.0 6 votes vote down vote up
def __init__(self):
    global FLAGS
    self.FLAGS = FLAGS
    self.unk_token = "UNK"
    self.entry_match_token = "entry_match"
    self.column_match_token = "column_match"
    self.dummy_token = "dummy_token"
    self.tf_data_type = {}
    self.tf_data_type["double"] = tf.float64
    self.tf_data_type["float"] = tf.float32
    self.np_data_type = {}
    self.np_data_type["double"] = np.float64
    self.np_data_type["float"] = np.float32
    self.operations_set = ["count"] + [
        "prev", "next", "first_rs", "last_rs", "group_by_max", "greater",
        "lesser", "geq", "leq", "max", "min", "word-match"
    ] + ["reset_select"] + ["print"]
    self.word_ids = {}
    self.reverse_word_ids = {}
    self.word_count = {}
    self.random = Random(FLAGS.python_seed) 
Example #21
Source File: isotropic_covariance.py    From tf-example-models with Apache License 2.0 6 votes vote down vote up
def initialize(self, dtype=tf.float64):
        if self.tf_variance_scalar is None:
            if self.scalar is not None:
                self.tf_variance_scalar = tf.Variable(self.scalar, dtype=dtype)
            else:
                self.tf_variance_scalar = tf.Variable(1.0, dtype=dtype)

        if self.has_prior is None:
            if self.prior is not None:
                self.has_prior = True
                self.tf_alpha = tf.constant(self.prior["alpha"], dtype=dtype)
                self.tf_beta = tf.constant(self.prior["beta"], dtype=dtype)
            else:
                self.has_prior = False

        self.tf_dims = tf.constant(self.dims, dtype=dtype) 
Example #22
Source File: mpi_running_mean_std.py    From Reinforcement_Learning_for_Traffic_Light_Control with Apache License 2.0 5 votes vote down vote up
def update(self, x):
        x = x.astype('float64')
        n = int(np.prod(self.shape))
        totalvec = np.zeros(n*2+1, 'float64')
        addvec = np.concatenate([x.sum(axis=0).ravel(), np.square(x).sum(axis=0).ravel(), np.array([len(x)],dtype='float64')])
        MPI.COMM_WORLD.Allreduce(addvec, totalvec, op=MPI.SUM)
        self.incfiltparams(totalvec[0:n].reshape(self.shape), totalvec[n:2*n].reshape(self.shape), totalvec[2*n]) 
Example #23
Source File: running_mean_std.py    From Reinforcement_Learning_for_Traffic_Light_Control with Apache License 2.0 5 votes vote down vote up
def __init__(self, epsilon=1e-4, shape=()):
        self.mean = np.zeros(shape, 'float64')
        self.var = np.ones(shape, 'float64')
        self.count = epsilon 
Example #24
Source File: running_mean_std.py    From Reinforcement_Learning_for_Traffic_Light_Control with Apache License 2.0 5 votes vote down vote up
def __init__(self, epsilon=1e-4, shape=()):
        self.mean = np.zeros(shape, 'float64')
        self.var = np.ones(shape, 'float64')
        self.count = epsilon 
Example #25
Source File: mpi_running_mean_std.py    From Reinforcement_Learning_for_Traffic_Light_Control with Apache License 2.0 5 votes vote down vote up
def update(self, x):
        x = x.astype('float64')
        n = int(np.prod(self.shape))
        totalvec = np.zeros(n*2+1, 'float64')
        addvec = np.concatenate([x.sum(axis=0).ravel(), np.square(x).sum(axis=0).ravel(), np.array([len(x)],dtype='float64')])
        MPI.COMM_WORLD.Allreduce(addvec, totalvec, op=MPI.SUM)
        self.incfiltparams(totalvec[0:n].reshape(self.shape), totalvec[n:2*n].reshape(self.shape), totalvec[2*n]) 
Example #26
Source File: operations_test.py    From tensorflow_constrained_optimization with Apache License 2.0 5 votes vote down vote up
def test_upper_bound_raises_on_tensor(self):
    """Make sure that upper_bound() raises when given a non-Expression."""
    value1 = 3.1
    value2 = 2.7
    tensor1 = tf.constant(value1, dtype=tf.float32)
    expression2 = operations.wrap_rate(tf.constant(value2, dtype=tf.float64))

    # List element is a Tensor, instead of an Expression.
    with self.assertRaises(TypeError):
      operations.upper_bound([tensor1, expression2]) 
Example #27
Source File: ops.py    From basenji with Apache License 2.0 5 votes vote down vote up
def _per_target_mean(values, weights, name='per-target-mean'):
  """Compute weighted mean across all but final dimension.

  Args:
    values: [..., num_targets] Tensor
    weights: Tensor. Either the same shape as values or broadcastable to it.
    name: string
  Returns:
    tuple containing tf.metrics-compatible value op and update_op.
    The value_op has shape [num_targets].
  """

  # First, reduce over all but the final dimension

  values = tf.convert_to_tensor(values)
  weights = tf.convert_to_tensor(weights)

  weights_dtype = tf.float64 if values.dtype == tf.float64 else tf.float32
  weights = tf.cast(weights, weights_dtype)

  reduction_axes = list(range(values.shape.ndims - 1))

  reduced_weights = tf.reduce_mean(weights, axis=reduction_axes)
  reduced_weighted_values = tf.reduce_mean(
      values * weights, axis=reduction_axes)

  return tf.metrics.mean_tensor(reduced_weighted_values *
                                (1. / reduced_weights), reduced_weights) 
Example #28
Source File: mpi_running_mean_std.py    From Reinforcement_Learning_for_Traffic_Light_Control with Apache License 2.0 5 votes vote down vote up
def __init__(self, epsilon=1e-2, shape=()):

        self._sum = tf.get_variable(
            dtype=tf.float64,
            shape=shape,
            initializer=tf.constant_initializer(0.0),
            name="runningsum", trainable=False)
        self._sumsq = tf.get_variable(
            dtype=tf.float64,
            shape=shape,
            initializer=tf.constant_initializer(epsilon),
            name="runningsumsq", trainable=False)
        self._count = tf.get_variable(
            dtype=tf.float64,
            shape=(),
            initializer=tf.constant_initializer(epsilon),
            name="count", trainable=False)
        self.shape = shape

        self.mean = tf.to_float(self._sum / self._count)
        self.std = tf.sqrt( tf.maximum( tf.to_float(self._sumsq / self._count) - tf.square(self.mean) , 1e-2 ))

        newsum = tf.placeholder(shape=self.shape, dtype=tf.float64, name='sum')
        newsumsq = tf.placeholder(shape=self.shape, dtype=tf.float64, name='var')
        newcount = tf.placeholder(shape=[], dtype=tf.float64, name='count')
        self.incfiltparams = U.function([newsum, newsumsq, newcount], [],
            updates=[tf.assign_add(self._sum, newsum),
                     tf.assign_add(self._sumsq, newsumsq),
                     tf.assign_add(self._count, newcount)]) 
Example #29
Source File: running_mean_std.py    From Reinforcement_Learning_for_Traffic_Light_Control with Apache License 2.0 5 votes vote down vote up
def __init__(self, epsilon=1e-4, shape=()):
        self.mean = np.zeros(shape, 'float64')
        self.var = np.ones(shape, 'float64')
        self.count = epsilon 
Example #30
Source File: mpi_running_mean_std.py    From Reinforcement_Learning_for_Traffic_Light_Control with Apache License 2.0 5 votes vote down vote up
def __init__(self, epsilon=1e-2, shape=()):

        self._sum = tf.get_variable(
            dtype=tf.float64,
            shape=shape,
            initializer=tf.constant_initializer(0.0),
            name="runningsum", trainable=False)
        self._sumsq = tf.get_variable(
            dtype=tf.float64,
            shape=shape,
            initializer=tf.constant_initializer(epsilon),
            name="runningsumsq", trainable=False)
        self._count = tf.get_variable(
            dtype=tf.float64,
            shape=(),
            initializer=tf.constant_initializer(epsilon),
            name="count", trainable=False)
        self.shape = shape

        self.mean = tf.to_float(self._sum / self._count)
        self.std = tf.sqrt( tf.maximum( tf.to_float(self._sumsq / self._count) - tf.square(self.mean) , 1e-2 ))

        newsum = tf.placeholder(shape=self.shape, dtype=tf.float64, name='sum')
        newsumsq = tf.placeholder(shape=self.shape, dtype=tf.float64, name='var')
        newcount = tf.placeholder(shape=[], dtype=tf.float64, name='count')
        self.incfiltparams = U.function([newsum, newsumsq, newcount], [],
            updates=[tf.assign_add(self._sum, newsum),
                     tf.assign_add(self._sumsq, newsumsq),
                     tf.assign_add(self._count, newcount)])