Exemple #1
0
def gbm_euler_step(states,
                   drift,
                   vol,
                   t,
                   dt,
                   key = 0,
                   random_normal_op = None
                  ):
  """Euler discretization step for the Geometric Brownian Motion.

  Whenever random_normal_op is not specified, Threefry is used as
  a back-end with a counter initialized to 0 and [key, int(t / dt)]
  as a block-cipher key.
  Will only use vector operations on TPU, tf.float32 is default precision.

  Args:
    states: a [num_samples] tensor of scalars, the simulated processes' states.
    drift: a scalar, the Geometric Brownian Motion drift.
    vol: a scalar, the Geometric Brownian Motion volatility.
    t: a scalar, the current time value.
    dt: a scalar, the temporal discretization resolution.
    key: an int32, the key representing the id for the random number stream.
      2^32 different keys can be used here, keyed streams have a period of 2^32.
    random_normal_op: function taking no arguments and returning a [num_samples]
      tensor of normal pseudo-random-numbers to override the default normal
      pseudo-random-number generator (for testing and debugging).

  Returns:
    a [num_samples] tensor of scalars, the simulated states at
      the next time step.

  Raises:
    ValueError if shape of states is not fully known.
  """

  states = _convert_to_tensor_and_check_fully_known_shape(states)

  if random_normal_op:
    normals = random_normal_op()
  else:
    num_samples = states.get_shape().as_list()[0]
    normals = random_normal([num_samples], t / dt, key)

  dw_t = normals * tf.sqrt(dt)

  return states * (
      tf.constant(1.0, dtype=states.dtype) + drift * dt + vol * dw_t)
    def apply_gradients(self, grads_and_vars, global_step=None, name=None):
        """See base class."""
        assignments = []
        for (grad, param) in grads_and_vars:
            if grad is None or param is None:
                continue

            param_name = self._get_variable_name(param.name)

            m = tf.get_variable(name=param_name + "/adam_m",
                                shape=param.shape.as_list(),
                                dtype=tf.float32,
                                trainable=False,
                                initializer=tf.zeros_initializer())
            v = tf.get_variable(name=param_name + "/adam_v",
                                shape=param.shape.as_list(),
                                dtype=tf.float32,
                                trainable=False,
                                initializer=tf.zeros_initializer())

            # Standard Adam update.
            next_m = (tf.multiply(self.beta_1, m) +
                      tf.multiply(1.0 - self.beta_1, grad))
            next_v = (tf.multiply(self.beta_2, v) +
                      tf.multiply(1.0 - self.beta_2, tf.square(grad)))

            update = next_m / (tf.sqrt(next_v) + self.epsilon)

            # Just adding the square of the weights to the loss function is *not*
            # the correct way of using L2 regularization/weight decay with Adam,
            # since that will interact with the m and v parameters in strange ways.
            #
            # Instead we want ot decay the weights in a manner that doesn't interact
            # with the m/v parameters. This is equivalent to adding the square
            # of the weights to the loss with plain (non-momentum) SGD.
            if self._do_use_weight_decay(param_name):
                update += self.weight_decay_rate * param

            update_with_lr = self.learning_rate * update

            next_param = param - update_with_lr

            assignments.extend(
                [param.assign(next_param),
                 m.assign(next_m),
                 v.assign(next_v)])
        return tf.group(*assignments, name=name)
Exemple #3
0
    def _perform_update_steps(self, observ, action, old_mean, old_logstd,
                              reward, length):
        """Perform multiple update steps of value function and policy.

    The advantage is computed once at the beginning and shared across
    iterations. We need to decide for the summary of one iteration, and thus
    choose the one after half of the iterations.

    Args:
      observ: Sequences of observations.
      action: Sequences of actions.
      old_mean: Sequences of action means of the behavioral policy.
      old_logstd: Sequences of action log stddevs of the behavioral policy.
      reward: Sequences of rewards.
      length: Batch of sequence lengths.

    Returns:
      Summary tensor.
    """
        return_ = utility.discounted_return(reward, length,
                                            self._config.discount)
        value = self._network(observ, length).value
        if self._config.gae_lambda:
            advantage = utility.lambda_return(reward, value, length,
                                              self._config.discount,
                                              self._config.gae_lambda)
        else:
            advantage = return_ - value
        mean, variance = tf.nn.moments(advantage, axes=[0, 1], keep_dims=True)
        advantage = (advantage - mean) / (tf.sqrt(variance) + 1e-8)
        advantage = tf.Print(advantage,
                             [tf.reduce_mean(return_),
                              tf.reduce_mean(value)], 'return and value: ')
        advantage = tf.Print(advantage, [tf.reduce_mean(advantage)],
                             'normalized advantage: ')
        # pylint: disable=g-long-lambda
        value_loss, policy_loss, summary = tf.scan(
            lambda _1, _2: self._update_step(
                observ, action, old_mean, old_logstd, reward, advantage, length
            ),
            tf.range(self._config.update_epochs), [0., 0., ''],
            parallel_iterations=1)
        print_losses = tf.group(
            tf.Print(0, [tf.reduce_mean(value_loss)], 'value loss: '),
            tf.Print(0, [tf.reduce_mean(policy_loss)], 'policy loss: '))
        with tf.control_dependencies([value_loss, policy_loss, print_losses]):
            return summary[self._config.update_epochs // 2]
Exemple #4
0
def _weights_for_num_sources(source_waveforms,
                             num_sources,
                             consider_as_zero=None):
    """Return shape (batch, source) weights for examples with num_sources."""
    source_norms = tf.sqrt(tf.reduce_mean(tf.square(source_waveforms),
                                          axis=-1))
    max_sources = signal_util.static_or_dynamic_dim_size(source_waveforms, 1)
    consider_nonzero = tf.greater(source_norms, 1e-8)
    if consider_as_zero is not None:
        consider_nonzero = tf.logical_and(consider_nonzero,
                                          tf.logical_not(consider_as_zero))
    num_sources_per_example = tf.reduce_sum(tf.cast(consider_nonzero,
                                                    tf.float32),
                                            axis=1,
                                            keepdims=True)
    has_num_sources = tf.equal(num_sources_per_example, num_sources)
    return tf.tile(has_num_sources, (1, max_sources))
Exemple #5
0
 def _net(self, inputs):
     self.layers = []
     hs = inputs
     input_shape = tf.data.get_output_shapes(
         list(self.datasets.values())[0])[0].as_list()
     for i, layer_specs in enumerate(self._config['layers']):
         layer_specs = copy.deepcopy(layer_specs)
         layer_type = layer_specs["type"]
         LayerClass = get_layer_class(layer_specs)
         layer_specs = prepare_layer_specs(layer_specs, input_shape,
                                           self._init_parameter)
         layer = LayerClass(**layer_specs)
         self.layers.append(layer)
         hs = layer(hs)
         if layer_type not in ['flatten', 'batch_norm']:
             if layer_type in ['conv_2d', 'max_pooling_2d']:
                 reduced_axes = [0, 1, 2]
                 cor_axis = 3
             elif layer_type == 'dense':
                 reduced_axes = [0]
                 cor_axis = 1
             else:
                 raise ValueError(
                     "Unsupported layer type {}\n"
                     "Supported layer types for "
                     "correlation computation are: {}".format(
                         layer_type,
                         ['dense', 'conv_2d', 'max_pooling_2d']))
             tensor_name = 'hs{}_corr'.format(i)
             self.fetches['accumulators'][tensor_name] = \
                 tensor_ops.corcov_loss(
                     hs,
                     reduced_axes=reduced_axes,
                     cor_axis=cor_axis,
                     punish='correlation',
                     reduction='mean',
                     norm='sqr'
                 )
             tensor_name = 'hs{}_rms'.format(i)
             self.fetches['accumulators'][tensor_name] = tf.sqrt(
                 tf.reduce_mean(tf.square(hs)))
             if hasattr(layer, 'kernel'):
                 tensor_name = 'kernel{}'.format(i)
                 self.fetches['tensors'][tensor_name] = layer.kernel
         input_shape = hs.get_shape().as_list()
     return hs
Exemple #6
0
    def _resource_apply_dense(self, grad, handle):
        var = handle
        m = self.get_slot(var, "m")

        if len(var.shape) < self._mindim:
            return tf.group(*[var, m])
        lr_t = tf1.cast(self._lr_t, var.dtype.base_dtype)
        momentum_t = tf1.cast(self._momentum_t, var.dtype.base_dtype)

        scale = tf1.sqrt(tf1.reduce_sum(var**2))
        dscale = tf1.sign(tf1.reduce_sum(var * grad) / (scale + 1e-12))

        m_t = m.assign(momentum_t * m - lr_t * dscale)

        new_scale = scale + m_t
        var_update = tf1.assign(var, var * new_scale / (scale + 1e-12))
        return tf1.group(*[var_update, m_t])
Exemple #7
0
def linear_relu_diagonal(x, A, b):
    """
    compute y = relu(x)^T A + b
    """
    x_var_diag = x.var
    sqrt_x_var_diag = tf.sqrt(x_var_diag)
    mu = x.mean / (sqrt_x_var_diag + EPSILON)

    pdf = bu.standard_gaussian(mu)
    cdf = bu.gaussian_cdf(mu)
    softrelu = pdf + mu * cdf

    z_mean = sqrt_x_var_diag * softrelu
    y_mean = tf.matmul(z_mean, A.mean) + b.mean
    z_var = x_var_diag * (cdf + mu * softrelu - tf.square(softrelu))
    y_cov = linear_covariance_diagonal(z_mean, z_var, A, b)
    return gv.GaussianVar(y_mean, y_cov)
def KL(p, q, hypers=None, global_step=1.0E99):
    if isinstance(p, DiagonalGaussianVar):
        if isinstance(q, DiagonalGaussianVar):
            safe_qvar = q.var + bu.EPSILON
            entropy_term = 0.5 * (1 + bu.log2pi + tf.log(p.var))
            cross_entropy_term = 0.5 * (bu.log2pi + tf.log(safe_qvar) + (p.var + (p.mean - q.mean)**2) / safe_qvar)           
            return tf.reduce_sum(cross_entropy_term - entropy_term)
        elif isinstance(q, DiagonalLaplaceVar):
            sigma = tf.sqrt(p.var)
            mu_ovr_sigma = p.mean / sigma
            tmp = 2 * bu.standard_gaussian(mu_ovr_sigma) + mu_ovr_sigma * tf.erf(mu_ovr_sigma * bu.one_ovr_sqrt2)
            tmp *= sigma / q.b
            tmp += 0.5 * tf.log(2 * q.b * q.b / (pi * p.var)) - 0.5
            return tf.reduce_sum(tmp)
        elif isinstance(q, InverseGammaVar):
            return EBKL(p, q, hypers, global_step)
    print('unsupported KL')
Exemple #9
0
def _ln(input_tensor, gain, bias, epsilon=1e-5, axes=None):
    """
    Apply layer normalisation.

    :param input_tensor: (TensorFlow Tensor) The input tensor for the Layer normalization
    :param gain: (TensorFlow Tensor) The scale tensor for the Layer normalization
    :param bias: (TensorFlow Tensor) The bias tensor for the Layer normalization
    :param epsilon: (float) The epsilon value for floating point calculations
    :param axes: (tuple, list or int) The axes to apply the mean and variance calculation
    :return: (TensorFlow Tensor) a normalizing layer
    """
    if axes is None:
        axes = [1]
    mean, variance = tf.nn.moments(input_tensor, axes=axes, keep_dims=True)
    input_tensor = (input_tensor - mean) / tf.sqrt(variance + epsilon)
    input_tensor = input_tensor * gain + bias
    return input_tensor
Exemple #10
0
    def _std(self):
        """Computes the current estimate of the standard deviation.

    Note that the standard deviation is not defined until at least two samples
    were seen.

    Returns:
      Tensor of current variance.
    """
        variance = tf.cond(
            self._count > 1,
            lambda: self._var_sum / tf.cast(self._count - 1, tf.float32),
            lambda: tf.ones_like(self._var_sum) * float('nan'))
        # The epsilon corrects for small negative variance values caused by
        # the algorithm. It was empirically chosen to work with all environments
        # tested.
        return tf.sqrt(variance + 1e-4)
def layerNorm(inputs, dim, name):
    with tf.variable_scope(name, reuse=tf.AUTO_REUSE, dtype=tf.float32):
        scale = tf.get_variable("scale", shape=[1, 1, dim],
                                dtype=tf.float32,
                                initializer=tf.ones_initializer())

        shift = tf.get_variable("shift", shape=[1, 1, dim],
                                dtype=tf.float32,
                                initializer=tf.zeros_initializer())

    mean, var = tf.nn.moments(inputs, [-1], keep_dims=True)

    epsilon = 1e-9

    LN = tf.multiply((scale / tf.sqrt(var + epsilon)), (inputs - mean)) + shift

    return LN
 def get_reward(self, state):
     '''Calculate the reward given a state
     Size of tensors' first dimension is the batch size for parallel computation
     Params
     ======
         state: rank-2 tensor, state in the form [position,orientation,vel,ang_vel]
     Returns
     ======
         rank-1 tensor, reward
     '''
     position = state[:, 0:2]
     # orientation = state[:, 2]
     # Distance between train target and current position
     reward = -tf.sqrt(
         tf.reduce_sum(tf.square(self.train_target - position), axis=1))
     # reward -= tf.norm(orientation, axis=1)
     return reward
Exemple #13
0
 def _cwise_fn(kfield, pk, kx, ky, kz):
     kx = tf.reshape(kx, [-1, 1, 1])
     ky = tf.reshape(ky, [1, -1, 1])
     kz = tf.reshape(kz, [1, 1, -1])
     kk = tf.sqrt((kx / boxsize * nc)**2 + (ky / boxsize * nc)**2 +
                  (kz / boxsize * nc)**2)
     shape = kk.shape
     kk = tf.reshape(kk, [-1])
     pkmesh = tfp.math.interp_regular_1d_grid(
         x=kk,
         x_ref_min=1e-05,
         x_ref_max=1000.0,
         y_ref=pk,
         grid_regularizing_transform=tf.log)
     pkmesh = tf.reshape(pkmesh, shape)
     kfield = kfield * tf.cast((pkmesh / boxsize**3)**0.5, tf.complex64)
     return kfield
Exemple #14
0
def gelu(x):
    """Gaussian Error Linear Units (GELUs)
    
    GLUEs are nonconvex, nonmonotonic.
    
    Arguments:
      x: Input tensor.
    
    References:
      Gaussian Error Linear Units (GELUs), Hendrycks et. al, 2018.
      
    Links: 
        [https://arxiv.org/pdf/1606.08415.pdf](https://arxiv.org/pdf/1606.08415.pdf)
    """

    return 0.5 * x * (
        1 + tf.tanh(tf.sqrt(2 / np.pi) * (x + 0.044715 * tf.pow(x, 3))))
Exemple #15
0
    def __init__(self, dataset):
        self.dataset = dataset
        self.LEARNING_RATE = 1e-4

        self.x = tf.placeholder(tf.float32, [None, None, None, 1])
        self.y_act = tf.placeholder(tf.float32, [None, None, None, 1])
        self.y_pre = self.inf(self.x)

        # Loss - Euclidean Distance
        self.loss = tf.sqrt(tf.reduce_mean(tf.square(self.y_act - self.y_pre)))
        self.act_sum = tf.reduce_sum(self.y_act)
        self.pre_sum = tf.reduce_sum(self.y_pre)

        # Mean Absolute Error
        self.MAE = tf.abs(self.act_sum - self.pre_sum)

        self.train_step = tf.train.AdamOptimizer(self.LEARNING_RATE).minimize(self.loss)
Exemple #16
0
    def __init__(self, flag_train, hps):
        self.flag_train = flag_train
        self.hps = hps
        self.activation = tf.nn.relu

        W, b = [], []
        hps.n_hs_ext = [hps.n_in] + hps.n_hs + [hps.n_out]
        for i in range(len(hps.n_hs_ext) - 1):
            w_init = tf.truncated_normal(
                [hps.n_hs_ext[i], hps.n_hs_ext[i + 1]],
                mean=0,
                stddev=tf.sqrt(2.0 / hps.n_hs_ext[i]),
                seed=hps.seed)
            W.append(tf.Variable(w_init, name='weight_' + str(i)))
            b_init = tf.zeros([1, hps.n_hs_ext[i + 1]])
            b.append(tf.Variable(b_init, name='bias_' + str(i)))
        self.W, self.b = W, b
Exemple #17
0
def add_summaries(scope_name, model_name, var, header_name="", save_stddev=True, save_mean=False, save_max=False,
                  save_min=False):
    with tf.name_scope(scope_name):
        mean_var = tf.reduce_mean(var)
        if save_mean:
            tf.summary.scalar(header_name + "mean/" + model_name, mean_var)

        if save_stddev:
            stddev_var = tf.sqrt(tf.reduce_mean(tf.square(var - mean_var)))
            tf.summary.scalar(header_name + "stddev/" + model_name, stddev_var)

        if save_max:
            tf.summary.scalar(header_name + "max/" + model_name, tf.reduce_max(var))

        if save_min:
            tf.summary.scalar(header_name + "min/" + model_name, tf.reduce_min(var))
        tf.summary.histogram(header_name + model_name, var)
Exemple #18
0
 def __init__(self, config, restore_dir=None, device="cpu"):
     self.__config = config
     self.__coin_number = config["input"]["coin_number"]
     self.__net = network.CNN(config["input"]["feature_number"],
                              self.__coin_number,
                              config["input"]["window_size"],
                              config["layers"],
                              device=device)
     self.__global_step = tf.Variable(0, trainable=False)
     self.__train_operation = None
     self.__y = tf.placeholder(tf.float32,
                               shape=[
                                   None,
                                   self.__config["input"]["feature_number"],
                                   self.__coin_number
                               ])
     self.__future_price = tf.concat(
         [tf.ones([self.__net.input_num, 1]), self.__y[:, 0, :]], 1)
     self.__future_omega = (self.__future_price * self.__net.output) /\
                           tf.reduce_sum(self.__future_price * self.__net.output, axis=1)[:, None]
     # tf.assert_equal(tf.reduce_sum(self.__future_omega, axis=1), tf.constant(1.0))
     self.__commission_ratio = self.__config["trading"][
         "trading_consumption"]
     self.__pv_vector = tf.reduce_sum(self.__net.output * self.__future_price, reduction_indices=[1]) *\
                        (tf.concat([tf.ones(1), self.__pure_pc()], axis=0))
     self.__log_mean_free = tf.reduce_mean(
         tf.log(
             tf.reduce_sum(self.__net.output * self.__future_price,
                           reduction_indices=[1])))
     self.__portfolio_value = tf.reduce_prod(self.__pv_vector)
     self.__mean = tf.reduce_mean(self.__pv_vector)
     self.__log_mean = tf.reduce_mean(tf.log(self.__pv_vector))
     self.__standard_deviation = tf.sqrt(
         tf.reduce_mean((self.__pv_vector - self.__mean)**2))
     self.__sharp_ratio = (self.__mean - 1) / self.__standard_deviation
     self.__loss = self.__set_loss_function()
     self.__train_operation = self.init_train(
         learning_rate=self.__config["training"]["learning_rate"],
         decay_steps=self.__config["training"]["decay_steps"],
         decay_rate=self.__config["training"]["decay_rate"],
         training_method=self.__config["training"]["training_method"])
     self.__saver = tf.train.Saver()
     if restore_dir:
         self.__saver.restore(self.__net.session, restore_dir)
     else:
         self.__net.session.run(tf.global_variables_initializer())
Exemple #19
0
    def _resource_apply_dense(self, grad, var):
        """Add ops to apply dense gradients to `var`.

        Args:
          grad: A `Tensor`.
          var: A `Variable` object.

        Returns:
          An `Operation`.
        """

        assignments = []

        param_name = self._get_variable_name(var.name)

        m = self.get_slot(var, "m")
        v = self.get_slot(var, "v")

        # Standard Adam update.
        next_m = (tf.multiply(self.beta_1, m) +
                  tf.multiply(1.0 - self.beta_1, grad))
        next_v = (tf.multiply(self.beta_2, v) +
                  tf.multiply(1.0 - self.beta_2, tf.square(grad)))

        update = next_m / (tf.sqrt(next_v) + self.epsilon)

        # Just adding the square of the weights to the loss function is *not*
        # the correct way of using L2 regularization/weight decay with Adam,
        # since that will interact with the m and v parameters in strange ways.
        #
        # Instead we want ot decay the weights in a manner that doesn't interact
        # with the m/v parameters. This is equivalent to adding the square
        # of the weights to the loss with plain (non-momentum) SGD.
        if self._do_use_weight_decay(param_name):
            update += self.weight_decay_rate * var

        update_with_lr = self.learning_rate * update

        next_param = var - update_with_lr

        assignments.extend([
            var.assign(next_param, use_locking=self._use_locking),
            m.assign(next_m),
            v.assign(next_v)
        ])
        return tf.group(*assignments)
Exemple #20
0
def run_embeddings(data, args):
    """Construct tensorflow computation graph and train the model."""
    graph = tf.Graph()
    with graph.as_default():
        train_inputs = tf.placeholder(tf.int32, shape=[args.batch_size])
        train_labels = tf.placeholder(tf.int32, shape=[args.batch_size, 1])
        with tf.device('/cpu:0'):
            embeddings = tf.Variable(
                tf.random_uniform([args.graph_size, args.emb_dim], -1.0, 1.0))
            embed = tf.nn.embedding_lookup(embeddings, train_inputs)
            nce_weights = tf.Variable(
                tf.truncated_normal([args.graph_size, args.emb_dim],
                                    stddev=1.0 / math.sqrt(args.emb_dim)))
            nce_biases = tf.Variable(tf.zeros([args.graph_size]))
        loss = tf.reduce_mean(
            tf.nn.nce_loss(weights=nce_weights,
                           biases=nce_biases,
                           labels=train_labels,
                           inputs=embed,
                           num_sampled=args.num_nsamp,
                           num_classes=args.graph_size))
        optimizer = tf.train.GradientDescentOptimizer(
            args.learning_rate).minimize(loss)
        norm = tf.sqrt(tf.reduce_sum(tf.square(embeddings), 1, keep_dims=True))
        normalized_embeddings = embeddings / norm
        init = tf.initialize_all_variables()
    with tf.Session(graph=graph) as session:
        init.run()
        print("Initialized")
        average_loss = 0
        for step in range(args.num_step):
            batch_inputs, batch_labels = generate_batch(data, args)
            feed_dict = {
                train_inputs: batch_inputs,
                train_labels: batch_labels
            }
            _, loss_val = session.run([optimizer, loss], feed_dict=feed_dict)
            average_loss += loss_val
            if step % 10000 == 0:
                if step > 0:
                    average_loss /= 10000
                print("Average loss at step ", step, ": ", average_loss)
                average_loss = 0
        final_embeddings = normalized_embeddings.eval()
    return final_embeddings
Exemple #21
0
    def __init__(self,
                 atom_types,
                 initial_params={},
                 offsets=None,
                 pair_trainable=True,
                 rho_trainable=True,
                 r0_trainable=False):
        with _tf.variable_scope("SMATB"):
            EAMpotential.__init__(self, atom_types)

            A = {}
            xi = {}
            p = {}
            q = {}
            r0 = {}
            pairPot = {}
            rho = {}
            for t1, t2 in combinations_with_replacement(atom_types, r=2):
                t12 = tuple(sorted([t1, t2]))
                A[t12], p[t12], r0[t12], pairPot[t12] = (
                    SMATBpotential.build_pairPot(t12, initial_params,
                                                 r0_trainable, pair_trainable))
                xi[t12], q[t12], _, rho[t12] = SMATBpotential.build_rho(
                    t12, initial_params, r0_trainable, rho_trainable)

            offsets = offsets or [0.0] * len(self.atom_types)

            for t1, offset in zip(self.atom_types, offsets):
                with _tf.name_scope("{}_ANN".format(t1)):
                    self.ANNs[t1] = EAMAtomicNN(atom_types, offset)
                    self.ANNs[t1].F = lambda rho: -_tf.sqrt(rho)

                    for t2 in self.atom_types:
                        t12 = tuple(sorted([t1, t2]))
                        with _tf.variable_scope("{}_{}_PairPot".format(*t12),
                                                reuse=_tf.AUTO_REUSE):
                            self.ANNs[t1].pairPot[t2] = pairPot[t12](
                                self.ANNs[t1].inputs[t2])
                        with _tf.variable_scope("{}_{}_rho".format(*t12),
                                                reuse=_tf.AUTO_REUSE):
                            self.ANNs[t1].rho[t2] = rho[t12](
                                self.ANNs[t1].inputs[t2])

                    self.ANNs[t1]._post_setup()
            EAMpotential._post_setup(self)
    def __init__(self,
                 X,
                 batch_size,
                 input_size,
                 output_size,
                 model='lstm',
                 rnn_size=128,
                 num_layers=2):
        self._model = model
        self._num_unit = rnn_size  # LSTM的单元个数
        self._num_layers = num_layers  # LSTM的层数
        self._input_size = input_size  # 最后全连接层输入维数
        self._output_size = output_size  # 最后全连接层输出维数
        self._model_layers = self._get_layer()  # 获得模型的LSTM隐含层

        self._initial_state = self._model_layers.zero_state(
            batch_size, tf.float32)  # 定义初始状态

        with tf.compat.v1.variable_scope('rnnlm'):
            n = (self._num_unit + self._output_size) * 0.5
            scale = tf.sqrt(3 / n)
            # 全连接层的参数定义
            softmax_w = tf.get_variable(
                "softmax_w", [self._num_unit, self._output_size],
                initializer=tf.random_uniform_initializer(-scale, scale))
            softmax_b = tf.get_variable(
                "softmax_b", [self._output_size],
                initializer=tf.random_uniform_initializer(-scale, scale))
            with tf.device("/cpu:0"):
                embedding = tf.get_variable("embedding",
                                            [self._input_size, self._num_unit])
                inputs = tf.nn.embedding_lookup(embedding, X)

        # 运行隐含层LSTM
        outputs, last_state = tf.nn.dynamic_rnn(
            self._model_layers,
            inputs,
            initial_state=self._initial_state,
            scope="rnnlm")
        self._outputs = tf.reshape(outputs, [-1, self._num_unit])
        self._last_state = last_state
        # 得到全连接层结果
        self._logists = tf.matmul(self._outputs, softmax_w) + softmax_b
        # 得到预测结果
        self._probs = tf.nn.softmax(self._logists)
Exemple #23
0
    def test_european_derivative_price_delta_mc_close_to_black_scholes(self):
        current_price = tf.constant(100.0, dtype=tf.float32)
        r = 0.05
        vol = tf.constant([[0.2]], dtype=tf.float32)
        strike = 100.0
        maturity = 0.1
        dt = 0.01

        bs_call_price = util.black_scholes_call_price(current_price, r, vol,
                                                      strike, maturity)
        bs_delta = monte_carlo_manager.sensitivity_autodiff(
            bs_call_price, current_price)

        num_samples = int(1e3)
        initial_states = tf.ones([num_samples, 1]) * current_price

        def _dynamics_op(log_s, t, dt):
            return dynamics.gbm_log_euler_step_nd(log_s, r, vol, t, dt, key=1)

        def _payoff_fn(log_s):
            return tf.exp(-r * maturity) * payoffs.call_payoff(
                tf.exp(log_s), strike)

        (_, _, outcomes) = monte_carlo_manager.non_callable_price_mc(
            tf.log(initial_states), _dynamics_op, _payoff_fn, maturity,
            num_samples, dt)

        mc_deltas = monte_carlo_manager.sensitivity_autodiff(
            outcomes, initial_states)

        mean_mc_deltas = tf.reduce_mean(mc_deltas)
        mc_deltas_std = tf.sqrt(
            tf.reduce_mean(mc_deltas**2) - (mean_mc_deltas**2))

        with self.test_session() as session:
            bs_delta_eval = session.run(bs_delta)
            mean_mc_deltas_eval, mc_deltas_std_eval = session.run(
                (mean_mc_deltas, mc_deltas_std))

        self.assertLessEqual(
            mean_mc_deltas_eval,
            bs_delta_eval + 3.0 * mc_deltas_std_eval / np.sqrt(num_samples))
        self.assertGreaterEqual(
            mean_mc_deltas_eval,
            bs_delta_eval - 3.0 * mc_deltas_std_eval / np.sqrt(num_samples))
Exemple #24
0
    def __init__(self, nvertices, mean, var, idx=None, post=None, nn=None, n2=None, **kwargs):
        Prior.__init__(self)
        self.name = kwargs.get("name", "MRF2SpatialPrior")
        self.nvertices = nvertices
        self.mean = tf.fill([nvertices], mean, name="%s_mean" % self.name)
        self.var = tf.fill([nvertices], var, name="%s_var" % self.name)
        self.std = tf.sqrt(self.var, name="%s_std" % self.name)

        # nn is a sparse tensor of shape [W, W]. If nn[A, B] = 1 then A is
        # a nearest neighbour of B
        self.nn = nn

        # We need the number of samples to implement the log PDF function
        self.sample_size = kwargs.get("sample_size", 5)

        # Set up spatial smoothing parameter calculation from posterior and neighbour lists
        self.logak = tf.Variable(-5.0, name="log_ak", dtype=tf.float32)
        self.ak = self.log_tf(tf.exp(self.logak, name="ak"))
Exemple #25
0
 def loss_with_spring(self):
     margin = 5.0
     labels_t = self.y_
     labels_f = tf.subtract(1.0, self.y_, name="1-yi")  # labels_ = !labels;
     eucd2 = tf.pow(tf.subtract(self.o1, self.o2), 2)
     eucd2 = tf.reduce_sum(eucd2, 1)
     eucd = tf.sqrt(eucd2 + 1e-6, name="eucd")
     C = tf.constant(margin, name="C")
     # yi*||CNN(p1i)-CNN(p2i)||^2 + (1-yi)*max(0, C-||CNN(p1i)-CNN(p2i)||^2)
     pos = tf.multiply(labels_t, eucd2, name="yi_x_eucd2")
     # neg = tf.multiply(labels_f, tf.subtract(0.0,eucd2), name="yi_x_eucd2")
     # neg = tf.multiply(labels_f, tf.maximum(0.0, tf.subtract(C,eucd2)), name="Nyi_x_C-eucd_xx_2")
     neg = tf.multiply(labels_f,
                       tf.pow(tf.maximum(tf.subtract(C, eucd), 0), 2),
                       name="Nyi_x_C-eucd_xx_2")
     losses = tf.add(pos, neg, name="losses")
     loss = tf.reduce_mean(losses, name="loss")
     return loss
Exemple #26
0
def _smoothness_helper(motion_map):
    """Calculates L1 (total variation) smoothness loss of a tensor.
    Args:
      motion_map: A tensor to be smoothed, of shape [B, H, W, C].
    Returns:
      A scalar tf.Tensor, The total variation loss.
    """
    # We roll in order to impose continuity across the boundary. The motivation is
    # that there is some ambiguity between rotation and spatial gradients of
    # translation maps. We would like to discourage spatial gradients of the
    # translation field, and to absorb sich gradients into the rotation as much as
    # possible. This is why we impose continuity across the spatial boundary.
    motion_map_dx = motion_map - tf.roll(motion_map, 1, 1)
    motion_map_dy = motion_map - tf.roll(motion_map, 1, 2)
    sm_loss = tf.sqrt(1e-24 + tf.square(motion_map_dx) +
                      tf.square(motion_map_dy))
    tf.summary.image("motion_sm", sm_loss)
    return tf.reduce_mean(sm_loss)
Exemple #27
0
def _group_std(inputs, data_format='channels_first', num_groups=32):
    """Grouped standard deviation along the channel dimension."""
    axis = 3 if data_format == 'channels_last' else 1
    while num_groups > 1:
        if inputs.shape[axis] % num_groups == 0:
            break
        num_groups -= 1
    if data_format == 'channels_last':
        n, h, w, c = inputs.shape
        x = tf.reshape(inputs, [n, h, w, num_groups, c // num_groups])
        _, variance = tf.nn.moments(x, [1, 2, 4], keep_dims=True)
    else:
        n, c, h, w = inputs.shape
        x = tf.reshape(inputs, [n, num_groups, c // num_groups, h, w])
        _, variance = tf.nn.moments(x, [2, 3, 4], keep_dims=True)
    std = tf.sqrt(variance + BATCH_NORM_EPSILON)
    std = tf.broadcast_to(std, x.shape)
    return tf.reshape(std, inputs.shape)
Exemple #28
0
def variable_summary(var):
    """ Wrapper that adds summary to Tensor variable to be visualized 
		  with TensorBoard. 
	"""
    with tf.name_scope('summaries'):
        mean = tf.reduce_mean(var)
        tf.summary.scalar('mean', mean)

        with tf.name_scope('stddev'):
            stddev = tf.sqrt(tf.reduce_mean(tf.square(var - mean)))

        # Visualizing gradient
        tf.summary.scalar('stddev', stddev)
        tf.summary.scalar('max', tf.reduce_max(var))
        tf.summary.scalar('min', tf.reduce_min(var))

        # Visualizing activation distribution
        tf.summary.histogram('histogram', var)
def StitchImages(images):
    # images is [batch, x, y, c]
    batch, width, _, channels = tf.unstack(tf.shape(images))
    num_per_side = tf.to_int32(tf.ceil(tf.sqrt(tf.to_float(batch))))
    new_width = num_per_side * width
    paddings = tf.concat([
        tf.zeros([4, 1], dtype=tf.int32),
        tf.stack([num_per_side * num_per_side - batch, 0, 0, 0])[Ellipsis,
                                                                 tf.newaxis]
    ], -1)
    images = tf.pad(images, paddings)

    images = tf.transpose(images, [1, 0, 2, 3])
    images = tf.reshape(images, [width, num_per_side, new_width, channels])
    images = tf.transpose(images, [1, 0, 2, 3])
    images = tf.reshape(images, [1, new_width, new_width, channels])

    return images
Exemple #30
0
def generate_gaussian(logits, sigma_nonlin, sigma_param):
    """Generate a Gaussian distribution given a selected parameterisation."""

    mu, sigma = tf.split(value=logits, num_or_size_splits=2, axis=1)

    if sigma_nonlin == 'exp':
        sigma = tf.exp(sigma)
    elif sigma_nonlin == 'softplus':
        sigma = tf.nn.softplus(sigma)
    else:
        raise ValueError('Unknown sigma_nonlin {}'.format(sigma_nonlin))

    if sigma_param == 'var':
        sigma = tf.sqrt(sigma)
    elif sigma_param != 'std':
        raise ValueError('Unknown sigma_param {}'.format(sigma_param))

    return tfp.distributions.Normal(loc=mu, scale=sigma)