Exemple #1
0
 def initialize_objective(self):
     self.C, self.c = (
         T.variable(T.random_normal([self.ds, self.ds])),
         T.variable(T.random_normal([self.ds])),
     )
     if self.learn_stdev:
         self.stdev = T.variable(T.to_float(self.cost_stdev))
     else:
         self.stdev = T.to_float(self.cost_stdev)
Exemple #2
0
 def initialize_objective(self):
     H, ds, da = self.horizon, self.ds, self.da
     if self.time_varying:
         A = T.concatenate([T.eye(ds), T.zeros([ds, da])], -1)
         self.A = T.variable(A[None] + 1e-2 * T.random_normal([H - 1, ds, ds + da]))
         self.Q_log_diag = T.variable(T.random_normal([H - 1, ds]) + 1)
         self.Q = T.matrix_diag(T.exp(self.Q_log_diag))
     else:
         A = T.concatenate([T.eye(ds), T.zeros([ds, da])], -1)
         self.A = T.variable(A + 1e-2 * T.random_normal([ds, ds + da]))
         self.Q_log_diag = T.variable(T.random_normal([ds]) + 1)
         self.Q = T.matrix_diag(T.exp(self.Q_log_diag))
Exemple #3
0
 def create_parameter(self, name, shape, initial_value=None):
     if name not in self.parameters:
         if initial_value is None:
             parameter = T.variable(
                 initialize_weights(self.initialization, shape),
                 name=name,
             )
         else:
             parameter = T.variable(
                 np.array(initial_value, dtype=T.floatx()),
                 name=name,
             )
         self.parameters[name] = parameter
Exemple #4
0
def orthogonal(shape, scale=1.1):
    flat_shape = (shape[0], np.prod(shape[1:]))
    a = np.random.normal(0.0, 1.0, flat_shape)
    u, _, v = np.linalg.svd(a, full_matrices=False)
    # Pick the one with the correct shape.
    q = u if u.shape == flat_shape else v
    q = q.reshape(shape)
    return T.variable(scale * q[:shape[0], :shape[1]])
Exemple #5
0
    a_idx = segments[:, 0]
    b_idx = segments[:, 1]
    leaf_segment = segments[:, 2]
    m = segments[:, 3]
    log_fac = segments[:, 4]

    x = T.matrix(name='x')
    e = T.matrix(name='e')
    q_network = Vector(X.shape[1], placeholder=x, is_input=False) >> Repeat(Tanh(200), 2)
    q_mu_network = q_network >> Linear(D)
    q_mu = q_mu_network.get_outputs()[0].get_placeholder()
    q_sigma_network = q_network >> Linear(D)
    q_sigma = tf.sqrt(tf.exp(q_sigma_network.get_outputs()[0].get_placeholder()))
    z = q_mu + e * q_sigma

    values, times = T.variable(values), T.variable(times)
    values = tf.concat(0, [z, values])
    harmonic = T.variable(create_harmonic(M))

    a_batch_values = T.gather(values, a_idx)
    a_batch_times = T.gather(times, a_idx)
    b_batch_values = T.gather(values, b_idx)
    b_batch_times = T.gather(times, b_idx)
    harmonic_m = T.gather(harmonic, m - 1)

    time_delta = b_batch_times - a_batch_times

    normal_log_prob = log_normal(b_batch_times, a_batch_times, time_delta * 1.0 / 1)

    log_pt = (log_a(a_batch_times) + (A(a_batch_times) - A(b_batch_times)) * harmonic_m)
    tree_log_prob = tf.select(tf.cast(leaf_segment, 'bool'), T.zeros_like(a_batch_times), log_pt + tf.to_float(log_fac))
Exemple #6
0
def make_variable(dist):
    return dist.__class__(T.variable(dist.get_parameters('natural')),
                          parameter_type='natural')
Exemple #7
0
def identity(shape, scale=1):
    if len(shape) != 2 or shape[0] != shape[1]:
        raise ValueError('Identity matrix initialization can only be used '
                         'for 2D square matrices.')
    else:
        return T.variable(scale * np.identity(shape[0]))
Exemple #8
0
 def as_variable(self):
     params = self.get_parameters('natural')
     return self.__class__(
         {stat: T.variable(params[stat])
          for stat in self.statistics()}, 'natural')