Esempio n. 1
0
    def __init__(self,
                 linear_size,
                 num_layers,
                 residual,
                 batch_norm,
                 max_norm,
                 batch_size,
                 learning_rate,
                 summaries_dir,
                 dtype=tf.float32):
        """Creates the linear + relu model

    Args
      linear_size: integer. number of units in each layer of the model
      num_layers: integer. number of bilinear blocks in the model
      residual: boolean. Whether to add residual connections
      batch_norm: boolean. Whether to use batch normalization
      max_norm: boolean. Whether to clip weights to a norm of 1
      batch_size: integer. The size of the batches used during training
      learning_rate: float. Learning rate to start with
      summaries_dir: String. Directory where to log progress
      predict_14: boolean. Whether to predict 14 instead of 17 joints
      dtype: the to_3D_ressources type to use to store internal variables
    """

        # There are in total 17 joints in H3.6M and 16 in MPII (and therefore in stacked
        # hourglass detections). We settled with 16 joints in 2d just to make models
        # compatible (e.g. you can train on ground truth 2d and test on SH detections).
        # This does not seem to have an effect on prediction performance.

        self.HUMAN_2D_SIZE = 13 * 2

        # In 3d all the predictions are zero-centered around the root (hip) joint, so
        # we actually predict only 16 joints. The error is still computed over 17 joints,
        # because if one uses, e.g. Procrustes alignment, there is still error in the
        # hip to account for!
        # There is also an option to predict only 14 joints, which makes our results
        # directly comparable to those in https://arxiv.org/pdf/1611.09010.pdf

        self.HUMAN_3D_SIZE = 13
        """ set input/ouput size of the network"""
        self.input_size = self.HUMAN_2D_SIZE
        self.output_size = self.HUMAN_3D_SIZE

        self.isTraining = tf.placeholder(tf.bool, name="isTrainingflag")
        """ neurons dropout percentage by layer"""
        self.dropout_keep_prob = tf.placeholder(tf.float32,
                                                name="dropout_keep_prob")

        # Summary writers for train and test runs
        self.train_writer = tf.summary.FileWriter(
            os.path.join(summaries_dir, 'train'))
        self.test_writer = tf.summary.FileWriter(
            os.path.join(summaries_dir, 'test'))
        """ width of the hidden neurons """
        self.linear_size = linear_size
        """ batch size """
        self.batch_size = batch_size
        """ instanciate an exponential decay learning rate = init_learning_rate * decay_rate ^ (global_step / decay_step )
        with global_step incremented each time (each batch?).
    """
        self.learning_rate = tf.Variable(float(learning_rate),
                                         trainable=False,
                                         dtype=dtype,
                                         name="learning_rate")
        self.global_step = tf.Variable(0, trainable=False, name="global_step")
        decay_steps = 1000000  # empirical
        decay_rate = 0.995  # empirical
        self.learning_rate = tf.train.exponential_decay(
            self.learning_rate, self.global_step, decay_steps, decay_rate)
        """
    input/output vector feeding the network
    """
        # === Transform the serverInputs ===
        with vs.variable_scope("serverInputs"):

            # in=2d poses, out=3d poses
            enc_in = tf.placeholder(dtype,
                                    shape=[None, self.input_size],
                                    name="enc_in")
            dec_out = tf.placeholder(dtype,
                                     shape=[None, self.output_size],
                                     name="dec_out")

            self.encoder_inputs = enc_in
            self.decoder_outputs = dec_out
        """
    each layer : input are row vectors :
    input : [1, HUMAN_2D]

    y3 = [1, HUMAN_2D]*[self.HUMAN_2D_SIZE, linear_size]
    """
        # === Create the linear + relu combos ===
        with vs.variable_scope("linear_model"):
            """ 2D_size => hidden_neuron_width  :   w1(edges weights), b1(offset)   """
            # === First layer, brings dimensionality up to linear_size ===
            w1 = tf.get_variable(name="w1",
                                 initializer=kaiming,
                                 shape=[self.HUMAN_2D_SIZE, linear_size],
                                 dtype=dtype)
            b1 = tf.get_variable(name="b1",
                                 initializer=kaiming,
                                 shape=[linear_size],
                                 dtype=dtype)
            """ if maxnorm selected : l2 normalization by batch (seems to be by edge-neuron normalization l2| w[k,:] |  ) """
            w1 = tf.clip_by_norm(w1, 1) if max_norm else w1
            y3 = tf.matmul(enc_in, w1) + b1
            """ y3 = results before activation function"""
            """ by batch normalization of the features """
            if batch_norm:
                y3 = tf.layers.batch_normalization(y3,
                                                   training=self.isTraining,
                                                   name="batch_normalization")
            """ add the activation function + dropout in y3 """
            y3 = tf.nn.relu(y3)
            y3 = tf.nn.dropout(y3, self.dropout_keep_prob)

            # === Create multiple bi-linear layers ===
            """
      from 0 until num_layers :
        add 2 layers to the model based with the same properties of the layer above.
        at the end of the second layer : add residual (y_input + y_after_the_2_layer)
      """
            for idx in range(num_layers):
                y3 = self.two_linear(y3, linear_size, residual,
                                     self.dropout_keep_prob, max_norm,
                                     batch_norm, dtype, idx)
            """
      output layer
      """
            # === Last linear layer has HUMAN_3D_SIZE in output ===
            w4 = tf.get_variable(name="w4",
                                 initializer=kaiming,
                                 shape=[linear_size, self.HUMAN_3D_SIZE],
                                 dtype=dtype)
            b4 = tf.get_variable(name="b4",
                                 initializer=kaiming,
                                 shape=[self.HUMAN_3D_SIZE],
                                 dtype=dtype)
            w4 = tf.clip_by_norm(w4, 1) if max_norm else w4
            y = tf.matmul(y3, w4) + b4
            # === End linear model ===

        # Store the outputs here
        self.outputs = y
        """  MSE :  SUM{ (pred - real)^2 }  """
        self.loss = tf.reduce_mean(tf.square(y - dec_out))

        self.loss_summary = tf.summary.scalar('loss/loss', self.loss)

        # To keep track of the loss in mm
        """ need to know more about : err_mm_summary{err_mm} """
        self.err_mm = tf.placeholder(tf.float32, name="error_mm")
        self.err_mm_summary = tf.summary.scalar("loss/error_mm", self.err_mm)

        # Gradients and update operation for training the model.
        """ optimizer for the GD (adam)  """
        opt = tf.train.AdamOptimizer(self.learning_rate)
        """ todo : review """
        update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
        """ todo : review, seems to be the backprop """
        with tf.control_dependencies(update_ops):

            # Update all the trainable parameters
            gradients = opt.compute_gradients(self.loss)
            self.gradients = [[] if i == None else i for i in gradients]
            self.updates = opt.apply_gradients(gradients,
                                               global_step=self.global_step)

        # Keep track of the learning rate
        self.learning_rate_summary = tf.summary.scalar(
            'learning_rate/learning_rate', self.learning_rate)

        # To save the model
        """ variable of the model can be saved """
        self.saver = tf.train.Saver(tf.global_variables(), max_to_keep=10)
    def _forward(self, input_indxs, outpt_indxs, scores, weights):
        """Build the graph for the forward pass.

    Args:
      input_indxs: int32 or int64 tensor for input labels
      outpt_indxs: int32 or int64 tensor for outpt labels
      scores: float32 tensor for co-occurrence score
      weights: float32 tensor for loss weights

    Returns:
      loss: a univariate tensor giving the loss from the batch
    """
        # Initialize input/outpt word (node) parameters
        self._default_scope = tf.get_variable_scope()
        init_width = 0.5 / (self._vector_size + self._covariate_size)
        self._word['input'] = self._weight_initializer('word_input',
                                                       init_width,
                                                       self._vocab_size,
                                                       self._vector_size)
        self._word['outpt'] = self._weight_initializer('word_outpt',
                                                       init_width,
                                                       self._vocab_size,
                                                       self._vector_size)

        # Initialize input/outpt bias parameters
        self._bias['input'] = self._weight_initializer('bias_input',
                                                       init_width,
                                                       self._vocab_size, 1)
        self._bias['outpt'] = self._weight_initializer('bias_outpt',
                                                       init_width,
                                                       self._vocab_size, 1)

        if self._covariate_size > 0:
            # Initialize input/outpt cvrt transformation parameters
            self._cvrt_transformation['input'] = self._weight_initializer(
                'cvrt_input', init_width, self._covariate_data.shape[1],
                self._covariate_size)
            self._cvrt_transformation['outpt'] = self._weight_initializer(
                'cvrt_outpt', init_width, self._covariate_data.shape[1],
                self._covariate_size)

            # Project the covariate data with the transformation parameters
            self._cvrt['input'] = tf.matmul(self._covariate_data_tensor,
                                            self._cvrt_transformation['input'])
            self._cvrt['outpt'] = tf.matmul(self._covariate_data_tensor,
                                            self._cvrt_transformation['outpt'])

            if self._use_monet:
                # Compute covariate svd
                _, self._u, _ = tf.linalg.svd(self._cvrt['input'] +
                                              self._cvrt['outpt'])

                # Project base word vecs and get word vecs
                self._projected_word_input = tf.stop_gradient(
                    self._word['input'] - self._db_level * tf.matmul(
                        self._u,
                        tf.matmul(tf.transpose(self._u), self._word['input'])))
                self._projected_word_outpt = tf.stop_gradient(
                    self._word['outpt'] - self._db_level * tf.matmul(
                        self._u,
                        tf.matmul(tf.transpose(self._u), self._word['outpt'])))

        # Get loss input word vectors
        if self._use_monet:
            self._input_word_vecs = tf.nn.embedding_lookup(
                self._projected_word_input, input_indxs)
            self._outpt_word_vecs = tf.nn.embedding_lookup(
                self._projected_word_outpt, outpt_indxs)
        else:
            self._input_word_vecs = tf.nn.embedding_lookup(
                self._word['input'], input_indxs)
            self._outpt_word_vecs = tf.nn.embedding_lookup(
                self._word['outpt'], outpt_indxs)

        # Get loss input bias vectors
        self._input_bias_vecs = tf.nn.embedding_lookup(self._bias['input'],
                                                       input_indxs)
        self._outpt_bias_vecs = tf.nn.embedding_lookup(self._bias['outpt'],
                                                       outpt_indxs)
        self._word_pred = tf.reduce_sum(tf.multiply(self._input_word_vecs,
                                                    self._outpt_word_vecs),
                                        axis=1)
        self._bias_pred = tf.reduce_sum(self._input_bias_vecs +
                                        self._outpt_bias_vecs,
                                        axis=1)
        estimated_score = self._bias_pred
        self._word_pred = tf.reduce_sum(tf.multiply(self._input_word_vecs,
                                                    self._outpt_word_vecs),
                                        axis=1)
        estimated_score += self._word_pred

        # Add covariate terms
        if self._covariate_size > 0:
            self._input_cvrt_vecs = tf.nn.embedding_lookup(
                self._cvrt['input'], input_indxs)
            self._outpt_cvrt_vecs = tf.nn.embedding_lookup(
                self._cvrt['outpt'], outpt_indxs)
            self._cvrt_pred = tf.reduce_sum(tf.multiply(
                self._input_cvrt_vecs, self._outpt_cvrt_vecs),
                                            axis=1)
            estimated_score += self._cvrt_pred
        else:
            self._cvrt_pred = tf.constant(0.0)

        self._scores = scores
        self._est_score = estimated_score
        if self._use_w2v:
            loss = self._compute_w2v_loss(input_indxs)
        else:
            diff = estimated_score - scores
            self._diff = diff
            loss = tf.reduce_sum(tf.multiply(weights, tf.square(diff))) / 2
        return loss
Esempio n. 3
0
import os
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
from tensorflow.core.protobuf import saver_pb2
import driving_data
import model

LOGDIR = (r"C:\Users\DM\Desktop\Autopilot-TensorFlow-master\save copy")

sess = tf.InteractiveSession()

L2NormConst = 0.001

train_vars = tf.trainable_variables()

loss = tf.reduce_mean(tf.square(tf.subtract(
    model.y_, model.y))) + tf.add_n([tf.nn.l2_loss(v)
                                     for v in train_vars]) * L2NormConst
train_step = tf.train.AdamOptimizer(1e-4).minimize(loss)
sess.run(tf.initialize_all_variables())

# create a summary to monitor cost tensor
tf.summary.scalar("loss", loss)
# merge all summaries into a single op
merged_summary_op = tf.summary.merge_all()

saver = tf.train.Saver(write_version=saver_pb2.SaverDef.V1)

# op to write logs to Tensorboard
logs_path = r'C:\Users\DM\Desktop\Autopilot-TensorFlow-master\logs'
summary_writer = tf.summary.FileWriter(logs_path, graph=tf.get_default_graph())
Esempio n. 4
0
import tensorflow.compat.v1 as tf

tf.disable_v2_behavior()

tf.set_random_seed(777)

xTrain = [1, 2, 3]
yTrain = [1, 2, 3]

W = tf.Variable(tf.random_normal([1]), name="weight")
b = tf.Variable(tf.random_normal([1]), name="bias")

hypothesis = xTrain * W + b

cost = tf.reduce_mean(tf.square(hypothesis - yTrain))
train = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(cost)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    for step in range(2001):
        _, cost_val, W_val, b_val = sess.run([train, cost, W, b])

        if step % 20 == 0:
            print(step, '\t', cost_val, W_val, b_val)
Esempio n. 5
0
y_proba_argmax = tf.argmax(y_proba, axis=2, name="y_proba")

y_pred = tf.squeeze(y_proba_argmax, axis=[1, 2], name="y_pred")

"""# Labels"""

y = tf.placeholder(shape=[None], dtype=tf.int64, name="y")

"""# Margin loss"""

T = tf.one_hot(y, depth=caps2_n_caps, name="T")

caps2_output_norm = safe_norm(caps2_output, axis=-2, keep_dims=True,
                              name="caps2_output_norm")

present_error_raw = tf.square(tf.maximum(0., m_plus - caps2_output_norm),
                              name="present_error_raw")
present_error = tf.reshape(present_error_raw, shape=(-1, num_class),
                           name="present_error")

absent_error_raw = tf.square(tf.maximum(0., caps2_output_norm - m_minus),
                             name="absent_error_raw")
absent_error = tf.reshape(absent_error_raw, shape=(-1, num_class),
                          name="absent_error")

L = tf.add(T * present_error, lambda_ * (1.0 - T) * absent_error,
           name="L")

margin_loss = tf.reduce_mean(tf.reduce_sum(L, axis=1), name="margin_loss")

"""## Final Loss"""
Esempio n. 6
0
def mse(pred, label):
  pred = tf.reshape(pred, [-1])
  label = tf.reshape(label, [-1])
  return tf.reduce_mean(tf.square(pred - label))
Esempio n. 7
0
import tensorflow.compat.v1 as tf
# 2버전으로 찾아서 수정해보려 하였는데 1버전에 대한 예제 코드만 많이 나와서 1버전 먼저 익힐 것 같다 ㅜ
tf.disable_v2_behavior()

X = tf.placeholder(tf.float32, shape=[None])
Y = tf.placeholder(tf.float32, shape=[None])

W = tf.Variable(tf.random.normal([1]), name='weight')
b = tf.Variable(tf.random.normal([1]), name='bias')

hypothesis = X * W + b

cost = tf.reduce_mean(tf.square(hypothesis - Y))

optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)
train = optimizer.minimize(cost)

sess = tf.Session()
# initializes global variables in the graph
sess.run(tf.global_variables_initializer())

#Fit the line
for step in range(2001):
    cost_val, W_val, b_val, _ = sess.run([cost, W, b, train],
                                         feed_dict={
                                             X: [1, 2, 3, 4, 5],
                                             Y: [2.1, 3.1, 4.1, 5.1, 6.1]
                                         })
    if step % 20 == 0:
        print(step, cost_val, W_val, b_val)
  def _build_single_q_network(self, observations, head, state_t, state_tp1,
                              done_mask, reward_t, error_weight):
    """Builds the computational graph for a single Q network.

    Briefly, this part is calculating the following two quantities:
    1. q_value = q_fn(observations)
    2. td_error = q_fn(state_t) - reward_t - gamma * q_fn(state_tp1)
    The optimization target is to minimize the td_error.

    Args:
      observations: shape = [batch_size, hparams.fingerprint_length]. The input
        of the Q function.
      head: shape = [1]. The index of the head chosen for decision in bootstrap
        DQN.
      state_t: shape = [batch_size, hparams.fingerprint_length]. The state at
        time step t.
      state_tp1: a list of tensors, with total number of batch_size, each has
        shape = [num_actions, hparams.fingerprint_length]. Note that the
        num_actions can be different for each tensor. The state at time step
        t+1, tp1 is short for t plus 1.
      done_mask: shape = [batch_size, 1] Whether state_tp1 is the terminal
        state.
      reward_t: shape = [batch_size, 1] the reward at time step t.
      error_weight: shape = [batch_size, 1] weight for the loss.

    Returns:
      q_values: Tensor of [batch_size, 1]. The q values for the observations.
      td_error: Tensor of [batch_size, 1]. The TD error.
      weighted_error: Tensor of [batch_size, 1]. The TD error weighted by
        error_weight.
      q_fn_vars: List of tf.Variables. The variables of q_fn when computing
        the q_values of state_t
      q_fn_vars: List of tf.Variables. The variables of q_fn when computing
        the q_values of state_tp1

    """
    with tf.variable_scope('q_fn'):
      # q_value have shape [batch_size, 1].
      q_values = tf.gather(self.q_fn(observations), head, axis=-1)

    # calculating q_fn(state_t)
    # The Q network shares parameters with the action graph.
    with tf.variable_scope('q_fn', reuse=True):
      q_t = self.q_fn(state_t, reuse=True)
    q_fn_vars = tf.trainable_variables(
        scope=tf.get_variable_scope().name + '/q_fn')

    # calculating q_fn(state_tp1)
    with tf.variable_scope('q_tp1', reuse=tf.AUTO_REUSE):
      q_tp1 = [self.q_fn(s_tp1, reuse=tf.AUTO_REUSE) for s_tp1 in state_tp1]
    q_tp1_vars = tf.trainable_variables(
        scope=tf.get_variable_scope().name + '/q_tp1')

    if self.double_q:
      with tf.variable_scope('q_fn', reuse=True):
        q_tp1_online = [self.q_fn(s_tp1, reuse=True) for s_tp1 in state_tp1]
      if self.num_bootstrap_heads:
        num_heads = self.num_bootstrap_heads
      else:
        num_heads = 1
      # determine the action to choose based on online Q estimator.
      q_tp1_online_idx = [
          tf.stack([tf.argmax(q, axis=0),
                    tf.range(num_heads, dtype=tf.int64)],
                   axis=1) for q in q_tp1_online
      ]
      # use the index from max online q_values to compute the value
      # function
      v_tp1 = tf.stack(
          [tf.gather_nd(q, idx) for q, idx in zip(q_tp1, q_tp1_online_idx)],
          axis=0)
    else:
      v_tp1 = tf.stack([tf.reduce_max(q) for q in q_tp1], axis=0)

    # if s_{t+1} is the terminal state, we do not evaluate the Q value of
    # the state.
    q_tp1_masked = (1.0 - done_mask) * v_tp1

    q_t_target = reward_t + self.gamma * q_tp1_masked

    # stop gradient from flowing to the computating graph which computes
    # the Q value of s_{t+1}.
    # td_error has shape [batch_size, 1]
    td_error = q_t - tf.stop_gradient(q_t_target)

    # If use bootstrap, each head is trained with a different subset of the
    # training sample. Like the idea of dropout.
    if self.num_bootstrap_heads:
      head_mask = tf.keras.backend.random_binomial(
          shape=(1, self.num_bootstrap_heads), p=0.6)
      td_error = tf.reduce_mean(td_error * head_mask, axis=1)
    # The loss comes from a traditional trick in convex optimization:
    # http://web.stanford.edu/~boyd/cvxbook/.
    # See Chapter 6 pp. 298
    # It will makes the optimization robust.
    # Specifically, the loss will use l1 instead of l2 loss when the td error
    # gets larger than 1.0. The l2 loss has the disadvantage that it has
    # the tendency to be dominated by outliers. In terms of estimation theory,
    # the asymptotic relative efficiency of the l1 loss estimator is better
    # for heavy-tailed distributions.
    errors = tf.where(
        tf.abs(td_error) < 1.0, tf.square(td_error),
        1.0 * (tf.abs(td_error) - 0.5))
    weighted_error = tf.reduce_mean(error_weight * errors)
    return q_values, td_error, weighted_error, q_fn_vars, q_tp1_vars
Esempio n. 9
0
 def _get_kg_score(h_e, r_e, t_e):
     kg_score = tf.reduce_sum(tf.square((h_e + r_e - t_e)),
                              1,
                              keepdims=True)
     return kg_score
Esempio n. 10
0
def build_genie_model(feat_dict,
                      cfg,
                      batch_size,
                      seq_len,
                      is_training=True,
                      seq_varlens=None,
                      dtype=tf.float32):
    """Builds a Piano Genie model.

  Args:
    feat_dict: Dictionary containing input tensors.
    cfg: Configuration object.
    batch_size: Number of items in batch.
    seq_len: Length of each batch item.
    is_training: Set to False for evaluation.
    seq_varlens: If not None, a tensor with the batch sequence lengths.
    dtype: Model weight type.

  Returns:
    A dict containing tensors for relevant model config.
  """
    out_dict = {}

    # Parse features
    pitches = util.demidify(feat_dict["midi_pitches"])
    velocities = feat_dict["velocities"]
    pitches_scalar = ((tf.cast(pitches, tf.float32) / 87.) * 2.) - 1.

    # Create sequence lens
    if is_training and cfg.train_randomize_seq_len:
        seq_lens = tf.random_uniform([batch_size],
                                     minval=cfg.train_seq_len_min,
                                     maxval=seq_len + 1,
                                     dtype=tf.int32)
        stp_varlen_mask = tf.sequence_mask(seq_lens,
                                           maxlen=seq_len,
                                           dtype=tf.float32)
    elif seq_varlens is not None:
        seq_lens = seq_varlens
        stp_varlen_mask = tf.sequence_mask(seq_varlens,
                                           maxlen=seq_len,
                                           dtype=tf.float32)
    else:
        seq_lens = tf.ones([batch_size], dtype=tf.int32) * seq_len
        stp_varlen_mask = None

    # Encode
    if (cfg.stp_emb_unconstrained or cfg.stp_emb_vq or cfg.stp_emb_iq
            or cfg.seq_emb_unconstrained or cfg.seq_emb_vae
            or cfg.lor_emb_unconstrained):
        # Build encoder features
        enc_feats = []
        if cfg.enc_pitch_scalar:
            enc_feats.append(tf.expand_dims(pitches_scalar, axis=-1))
        else:
            enc_feats.append(tf.one_hot(pitches, 88))
        if "delta_times_int" in cfg.enc_aux_feats:
            enc_feats.append(
                tf.one_hot(feat_dict["delta_times_int"],
                           cfg.data_max_discrete_times + 1))
        if "velocities" in cfg.enc_aux_feats:
            enc_feats.append(
                tf.one_hot(velocities, cfg.data_max_discrete_velocities + 1))
        enc_feats = tf.concat(enc_feats, axis=2)

        with tf.variable_scope("encoder"):
            enc_stp, enc_seq = simple_lstm_encoder(
                enc_feats,
                seq_lens,
                rnn_celltype=cfg.rnn_celltype,
                rnn_nlayers=cfg.rnn_nlayers,
                rnn_nunits=cfg.rnn_nunits,
                rnn_bidirectional=cfg.enc_rnn_bidirectional,
                dtype=dtype)

    latents = []

    # Step embeddings (single vector per timestep)
    if cfg.stp_emb_unconstrained:
        with tf.variable_scope("stp_emb_unconstrained"):
            stp_emb_unconstrained = tf.layers.dense(
                enc_stp, cfg.stp_emb_unconstrained_embedding_dim)

        out_dict["stp_emb_unconstrained"] = stp_emb_unconstrained
        latents.append(stp_emb_unconstrained)

    # Quantized step embeddings with VQ-VAE
    if cfg.stp_emb_vq:
        import sonnet as snt  # pylint:disable=g-import-not-at-top,import-outside-toplevel
        with tf.variable_scope("stp_emb_vq"):
            with tf.variable_scope("pre_vq"):
                # pre_vq_encoding is tf.float32 of [batch_size, seq_len, embedding_dim]
                pre_vq_encoding = tf.layers.dense(enc_stp,
                                                  cfg.stp_emb_vq_embedding_dim)

            with tf.variable_scope("quantizer"):
                assert stp_varlen_mask is None
                vq_vae = snt.nets.VectorQuantizer(
                    embedding_dim=cfg.stp_emb_vq_embedding_dim,
                    num_embeddings=cfg.stp_emb_vq_codebook_size,
                    commitment_cost=cfg.stp_emb_vq_commitment_cost)
                vq_vae_output = vq_vae(pre_vq_encoding,
                                       is_training=is_training)

                stp_emb_vq_quantized = vq_vae_output["quantize"]
                stp_emb_vq_discrete = tf.reshape(
                    tf.argmax(vq_vae_output["encodings"],
                              axis=1,
                              output_type=tf.int32), [batch_size, seq_len])
                stp_emb_vq_codebook = tf.transpose(vq_vae.embeddings)

        out_dict["stp_emb_vq_quantized"] = stp_emb_vq_quantized
        out_dict["stp_emb_vq_discrete"] = stp_emb_vq_discrete
        out_dict["stp_emb_vq_loss"] = vq_vae_output["loss"]
        out_dict["stp_emb_vq_codebook"] = stp_emb_vq_codebook
        out_dict["stp_emb_vq_codebook_ppl"] = vq_vae_output["perplexity"]
        latents.append(stp_emb_vq_quantized)

        # This tensor retrieves continuous embeddings from codebook. It should
        # *never* be used during training.
        out_dict["stp_emb_vq_quantized_lookup"] = tf.nn.embedding_lookup(
            stp_emb_vq_codebook, stp_emb_vq_discrete)

    # Integer-quantized step embeddings with straight-through
    if cfg.stp_emb_iq:
        with tf.variable_scope("stp_emb_iq"):
            with tf.variable_scope("pre_iq"):
                # pre_iq_encoding is tf.float32 of [batch_size, seq_len]
                pre_iq_encoding = tf.layers.dense(enc_stp, 1)[:, :, 0]

            def iqst(x, n):
                """Integer quantization with straight-through estimator."""
                eps = 1e-7
                s = float(n - 1)
                xp = tf.clip_by_value((x + 1) / 2.0, -eps, 1 + eps)
                xpp = tf.round(s * xp)
                xppp = 2 * (xpp / s) - 1
                return xpp, x + tf.stop_gradient(xppp - x)

            with tf.variable_scope("quantizer"):
                # Pass rounded vals to decoder w/ straight-through estimator
                stp_emb_iq_discrete_f, stp_emb_iq_discrete_rescaled = iqst(
                    pre_iq_encoding, cfg.stp_emb_iq_nbins)
                stp_emb_iq_discrete = tf.cast(stp_emb_iq_discrete_f + 1e-4,
                                              tf.int32)
                stp_emb_iq_discrete_f = tf.cast(stp_emb_iq_discrete,
                                                tf.float32)
                stp_emb_iq_quantized = tf.expand_dims(
                    stp_emb_iq_discrete_rescaled, axis=2)

                # Determine which elements round to valid indices
                stp_emb_iq_inrange = tf.logical_and(
                    tf.greater_equal(pre_iq_encoding, -1),
                    tf.less_equal(pre_iq_encoding, 1))
                stp_emb_iq_inrange_mask = tf.cast(stp_emb_iq_inrange,
                                                  tf.float32)
                stp_emb_iq_valid_p = weighted_avg(stp_emb_iq_inrange_mask,
                                                  stp_varlen_mask)

                # Regularize to encourage encoder to output in range
                stp_emb_iq_range_penalty = weighted_avg(
                    tf.square(tf.maximum(tf.abs(pre_iq_encoding) - 1, 0)),
                    stp_varlen_mask)

                # Regularize to correlate latent finite differences to input
                stp_emb_iq_dlatents = pre_iq_encoding[:,
                                                      1:] - pre_iq_encoding[:, :
                                                                            -1]
                if cfg.stp_emb_iq_contour_dy_scalar:
                    stp_emb_iq_dnotes = pitches_scalar[:,
                                                       1:] - pitches_scalar[:, :
                                                                            -1]
                else:
                    stp_emb_iq_dnotes = tf.cast(
                        pitches[:, 1:] - pitches[:, :-1], tf.float32)
                if cfg.stp_emb_iq_contour_exp == 1:
                    power_func = tf.identity
                elif cfg.stp_emb_iq_contour_exp == 2:
                    power_func = tf.square
                else:
                    raise NotImplementedError()
                if cfg.stp_emb_iq_contour_comp == "product":
                    comp_func = tf.multiply
                elif cfg.stp_emb_iq_contour_comp == "quotient":
                    comp_func = lambda x, y: tf.divide(x, y + 1e-6)
                else:
                    raise NotImplementedError()

                stp_emb_iq_contour_penalty = weighted_avg(
                    power_func(
                        tf.maximum(
                            cfg.stp_emb_iq_contour_margin -
                            comp_func(stp_emb_iq_dnotes, stp_emb_iq_dlatents),
                            0)),
                    None if stp_varlen_mask is None else stp_varlen_mask[:,
                                                                         1:])

                # Regularize to maintain note consistency
                stp_emb_iq_note_held = tf.cast(
                    tf.equal(pitches[:, 1:] - pitches[:, :-1], 0), tf.float32)
                if cfg.stp_emb_iq_deviate_exp == 1:
                    power_func = tf.abs
                elif cfg.stp_emb_iq_deviate_exp == 2:
                    power_func = tf.square

                if stp_varlen_mask is None:
                    mask = stp_emb_iq_note_held
                else:
                    mask = stp_varlen_mask[:, 1:] * stp_emb_iq_note_held
                stp_emb_iq_deviate_penalty = weighted_avg(
                    power_func(stp_emb_iq_dlatents), mask)

                # Calculate perplexity of discrete encoder posterior
                if stp_varlen_mask is None:
                    mask = stp_emb_iq_inrange_mask
                else:
                    mask = stp_varlen_mask * stp_emb_iq_inrange_mask
                stp_emb_iq_discrete_oh = tf.one_hot(stp_emb_iq_discrete,
                                                    cfg.stp_emb_iq_nbins)
                stp_emb_iq_avg_probs = weighted_avg(stp_emb_iq_discrete_oh,
                                                    mask,
                                                    axis=[0, 1],
                                                    expand_mask=True)
                stp_emb_iq_discrete_ppl = tf.exp(
                    -tf.reduce_sum(stp_emb_iq_avg_probs *
                                   tf.log(stp_emb_iq_avg_probs + 1e-10)))

        out_dict["stp_emb_iq_quantized"] = stp_emb_iq_quantized
        out_dict["stp_emb_iq_discrete"] = stp_emb_iq_discrete
        out_dict["stp_emb_iq_valid_p"] = stp_emb_iq_valid_p
        out_dict["stp_emb_iq_range_penalty"] = stp_emb_iq_range_penalty
        out_dict["stp_emb_iq_contour_penalty"] = stp_emb_iq_contour_penalty
        out_dict["stp_emb_iq_deviate_penalty"] = stp_emb_iq_deviate_penalty
        out_dict["stp_emb_iq_discrete_ppl"] = stp_emb_iq_discrete_ppl
        latents.append(stp_emb_iq_quantized)

        # This tensor converts discrete values to continuous.
        # It should *never* be used during training.
        out_dict["stp_emb_iq_quantized_lookup"] = tf.expand_dims(
            2. * (stp_emb_iq_discrete_f / (cfg.stp_emb_iq_nbins - 1.)) - 1.,
            axis=2)

    # Sequence embedding (single vector per sequence)
    if cfg.seq_emb_unconstrained:
        with tf.variable_scope("seq_emb_unconstrained"):
            seq_emb_unconstrained = tf.layers.dense(
                enc_seq, cfg.seq_emb_unconstrained_embedding_dim)

        out_dict["seq_emb_unconstrained"] = seq_emb_unconstrained

        seq_emb_unconstrained = tf.stack([seq_emb_unconstrained] * seq_len,
                                         axis=1)
        latents.append(seq_emb_unconstrained)

    # Sequence embeddings (variational w/ reparameterization trick)
    if cfg.seq_emb_vae:
        with tf.variable_scope("seq_emb_vae"):
            seq_emb_vae = tf.layers.dense(enc_seq,
                                          cfg.seq_emb_vae_embedding_dim * 2)

            mean = seq_emb_vae[:, :cfg.seq_emb_vae_embedding_dim]
            stddev = 1e-6 + tf.nn.softplus(
                seq_emb_vae[:, cfg.seq_emb_vae_embedding_dim:])
            seq_emb_vae = mean + stddev * tf.random_normal(
                tf.shape(mean), 0, 1, dtype=dtype)

            kl = tf.reduce_mean(
                0.5 * tf.reduce_sum(tf.square(mean) + tf.square(stddev) -
                                    tf.log(1e-8 + tf.square(stddev)) - 1,
                                    axis=1))

        out_dict["seq_emb_vae"] = seq_emb_vae
        out_dict["seq_emb_vae_kl"] = kl

        seq_emb_vae = tf.stack([seq_emb_vae] * seq_len, axis=1)
        latents.append(seq_emb_vae)

    # Low-rate embeddings
    if cfg.lor_emb_unconstrained:
        assert seq_len % cfg.lor_emb_n == 0

        with tf.variable_scope("lor_emb_unconstrained"):
            # Downsample step embeddings
            rnn_embedding_dim = int(enc_stp.get_shape()[-1])
            enc_lor = tf.reshape(enc_stp, [
                batch_size, seq_len // cfg.lor_emb_n,
                cfg.lor_emb_n * rnn_embedding_dim
            ])
            lor_emb_unconstrained = tf.layers.dense(
                enc_lor, cfg.lor_emb_unconstrained_embedding_dim)

            out_dict["lor_emb_unconstrained"] = lor_emb_unconstrained

            # Upsample lo-rate embeddings for decoding
            lor_emb_unconstrained = tf.expand_dims(lor_emb_unconstrained,
                                                   axis=2)
            lor_emb_unconstrained = tf.tile(lor_emb_unconstrained,
                                            [1, 1, cfg.lor_emb_n, 1])
            lor_emb_unconstrained = tf.reshape(
                lor_emb_unconstrained,
                [batch_size, seq_len, cfg.lor_emb_unconstrained_embedding_dim])

            latents.append(lor_emb_unconstrained)

    # Build decoder features
    dec_feats = latents

    if cfg.dec_autoregressive:
        # Retrieve pitch numbers
        curr_pitches = pitches
        last_pitches = curr_pitches[:, :-1]
        last_pitches = tf.pad(last_pitches, [[0, 0], [1, 0]],
                              constant_values=-1)  # Prepend <SOS> token
        out_dict["dec_last_pitches"] = last_pitches
        dec_feats.append(tf.one_hot(last_pitches + 1, 89))

        if cfg.dec_pred_velocity:
            curr_velocities = velocities
            last_velocities = curr_velocities[:, :-1]
            last_velocities = tf.pad(last_velocities, [[0, 0], [1, 0]])
            dec_feats.append(
                tf.one_hot(last_velocities,
                           cfg.data_max_discrete_velocities + 1))

    if "delta_times_int" in cfg.dec_aux_feats:
        dec_feats.append(
            tf.one_hot(feat_dict["delta_times_int"],
                       cfg.data_max_discrete_times + 1))
    if "velocities" in cfg.dec_aux_feats:
        assert not cfg.dec_pred_velocity
        dec_feats.append(
            tf.one_hot(feat_dict["velocities"],
                       cfg.data_max_discrete_velocities + 1))

    assert dec_feats
    dec_feats = tf.concat(dec_feats, axis=2)

    # Decode
    with tf.variable_scope("decoder"):
        dec_stp, dec_initial_state, dec_final_state = simple_lstm_decoder(
            dec_feats,
            seq_lens,
            batch_size,
            rnn_celltype=cfg.rnn_celltype,
            rnn_nlayers=cfg.rnn_nlayers,
            rnn_nunits=cfg.rnn_nunits)

        with tf.variable_scope("pitches"):
            dec_recons_logits = tf.layers.dense(dec_stp, 88)

        dec_recons_loss = weighted_avg(
            tf.nn.sparse_softmax_cross_entropy_with_logits(
                logits=dec_recons_logits, labels=pitches), stp_varlen_mask)

        out_dict["dec_initial_state"] = dec_initial_state
        out_dict["dec_final_state"] = dec_final_state
        out_dict["dec_recons_logits"] = dec_recons_logits
        out_dict["dec_recons_scores"] = tf.nn.softmax(dec_recons_logits,
                                                      axis=-1)
        out_dict["dec_recons_preds"] = tf.argmax(dec_recons_logits,
                                                 output_type=tf.int32,
                                                 axis=-1)
        out_dict["dec_recons_midi_preds"] = util.remidify(
            out_dict["dec_recons_preds"])
        out_dict["dec_recons_loss"] = dec_recons_loss

        if cfg.dec_pred_velocity:
            with tf.variable_scope("velocities"):
                dec_recons_velocity_logits = tf.layers.dense(
                    dec_stp, cfg.data_max_discrete_velocities + 1)

            dec_recons_velocity_loss = weighted_avg(
                tf.nn.sparse_softmax_cross_entropy_with_logits(
                    logits=dec_recons_velocity_logits, labels=velocities),
                stp_varlen_mask)

            out_dict["dec_recons_velocity_logits"] = dec_recons_velocity_logits
            out_dict["dec_recons_velocity_loss"] = dec_recons_velocity_loss

    # Stats
    if cfg.stp_emb_vq or cfg.stp_emb_iq:
        discrete = out_dict["stp_emb_vq_discrete" if cfg.
                            stp_emb_vq else "stp_emb_iq_discrete"]
        dx = pitches[:, 1:] - pitches[:, :-1]
        dy = discrete[:, 1:] - discrete[:, :-1]
        contour_violation = tf.reduce_mean(
            tf.cast(tf.less(dx * dy, 0), tf.float32))

        dx_hold = tf.equal(dx, 0)
        deviate_violation = weighted_avg(
            tf.cast(tf.not_equal(dy, 0), tf.float32),
            tf.cast(dx_hold, tf.float32))

        out_dict["contour_violation"] = contour_violation
        out_dict["deviate_violation"] = deviate_violation

    return out_dict
Esempio n. 11
0
weights_2 = tf.Variable(
    tf.truncated_normal([20, env.action_space.n], stddev=0.01))
bias_2 = tf.Variable(tf.constant(0.0, shape=[env.action_space.n]))

hidden_layer = tf.nn.tanh(tf.matmul(input_placeholder, weights_1) + bias_1)
output_layer = tf.matmul(hidden_layer, weights_2) + bias_2

action_placeholder = tf.placeholder("float", [None, 2])
target_placeholder = tf.placeholder("float", [None])

# network estimation
q_estimation = tf.reduce_sum(tf.multiply(output_layer, action_placeholder),
                             reduction_indices=1)

# loss function
loss = tf.reduce_mean(tf.square(target_placeholder - q_estimation))

# Use Adam
train_operation = tf.train.AdamOptimizer().minimize(loss)

# initialize TF variables
session = tf.Session()
session.run(tf.global_variables_initializer())


def choose_next_action(state, rand_action_prob):
    """
    Simplified e-greedy policy
    :param state: current state
    :param rand_action_prob: probability to select random action
    """
Esempio n. 12
0
def rms(variables):
    return tf.sqrt(
        sum([tf.reduce_sum(tf.square(v)) for v in variables]) /
        sum(int(np.prod(v.shape.as_list())) for v in variables))
Esempio n. 13
0
def backpropagationBasedFiltering(
    lines0_values,  # initial (logarithm of) bones lenghts
    rootsx0_values,  # head position
    rootsy0_values,
    rootsz0_values,
    anglesx0_values,  # angles of limbs
    anglesy0_values,
    anglesz0_values,
    tarx_values,  # target   
    tary_values,
    w_values,  # weights of estimated points (likelihood of estimation)   
    structure,
    dtype,
    learningRate=0.1,
    nCycles=1000,
    regulatorRates=[0.001, 0.1],
):

    T = rootsx0_values.shape[0]
    nBones, nPoints = skeletalModel.structureStats(structure)
    nLimbs = len(structure)

    # vector of (logarithm of) bones length
    #   shape: (nLines,)
    lines = tf.Variable(lines0_values, dtype=dtype)
    # x cooordinates of head
    #   shape: (T, 1)
    rootsx = tf.Variable(rootsx0_values, dtype=dtype)
    # y cooordinates of head
    rootsy = tf.Variable(rootsy0_values, dtype=dtype)
    # z cooordinates of head
    rootsz = tf.Variable(rootsz0_values, dtype=dtype)
    # x coordinate of angles
    #   shape: (T, nLimbs)
    anglesx = tf.Variable(anglesx0_values, dtype=dtype)
    # y coordinate of angles
    anglesy = tf.Variable(anglesy0_values, dtype=dtype)
    # z coordinate of angles
    anglesz = tf.Variable(anglesz0_values, dtype=dtype)

    # target
    #   shape: (T, nPoints)
    tarx = tf.placeholder(dtype=dtype)
    tary = tf.placeholder(dtype=dtype)
    # likelihood from previous pose estimator
    #   shape: (T, nPoints)
    w = tf.placeholder(dtype=dtype)

    # resultant coordinates. It's a list for now. It will be concatenate into a matrix later
    #   shape: (T, nPoints)
    x = [None for i in range(nPoints)]
    y = [None for i in range(nPoints)]
    z = [None for i in range(nPoints)]

    # head first
    x[0] = rootsx
    y[0] = rootsy
    z[0] = rootsz

    # now other limbs
    i = 0
    # for numerical stability of angles normalization
    epsilon = 1e-10
    for a, b, l in structure:
        # limb length
        L = tf.exp(lines[l])
        # angle
        Ax = anglesx[0:T, i:(i + 1)]
        Ay = anglesy[0:T, i:(i + 1)]
        Az = anglesz[0:T, i:(i + 1)]
        # angle norm
        normA = tf.sqrt(tf.square(Ax) + tf.square(Ay) +
                        tf.square(Az)) + epsilon
        # new joint position
        x[b] = x[a] + L * Ax / normA
        y[b] = y[a] + L * Ay / normA
        z[b] = z[a] + L * Az / normA
        i = i + 1

    # making a matrix from the list
    x = tf.concat(x, axis=1)
    y = tf.concat(y, axis=1)
    z = tf.concat(z, axis=1)

    # weighted MSE
    loss = tf.reduce_sum(w * tf.square(x - tarx) +
                         w * tf.square(y - tary)) / (T * nPoints)

    # regularozators
    # reg1 is a sum of bones length
    reg1 = tf.reduce_sum(tf.exp(lines))
    # reg2 is a square of trajectory length
    dx = x[0:(T - 1), 0:nPoints] - x[1:T, 0:nPoints]
    dy = y[0:(T - 1), 0:nPoints] - y[1:T, 0:nPoints]
    dz = z[0:(T - 1), 0:nPoints] - z[1:T, 0:nPoints]
    reg2 = tf.reduce_sum(tf.square(dx) + tf.square(dy) + tf.square(dz)) / (
        (T - 1) * nPoints)

    optimizeThis = loss + regulatorRates[0] * reg1 + regulatorRates[1] * reg2

    # the backpropagation
    optimizer = tf.train.GradientDescentOptimizer(learningRate)
    train = optimizer.minimize(optimizeThis)
    init = tf.variables_initializer(tf.global_variables())
    sess = tf.Session()
    sess.run(init)
    for iCycle in range(nCycles):
        sess.run(train, {tarx: tarx_values, tary: tary_values, w: w_values})
        print("iCycle = %3d, loss = %e" %
              (iCycle,
               sess.run([loss], {
                   tarx: tarx_values,
                   tary: tary_values,
                   w: w_values
               })[0]))

    # returning final coordinates
    return sess.run([x, y, z], {})
Esempio n. 14
0
    def _init_graph(self):
        self.graph = tf.Graph()
        with self.graph.as_default():

            tf.set_random_seed(self.random_seed)

            self.feat_index = tf.placeholder(tf.int32,
                                             shape=[None, None],
                                             name="feat_index")  # None * F
            self.feat_value = tf.placeholder(tf.float32,
                                             shape=[None, None],
                                             name="feat_value")  # None * F
            self.label = tf.placeholder(tf.float32,
                                        shape=[None, 1],
                                        name="label")  # None * 1
            self.dropout_keep_fm = tf.placeholder(tf.float32,
                                                  shape=[None],
                                                  name="dropout_keep_fm")
            self.dropout_keep_deep = tf.placeholder(tf.float32,
                                                    shape=[None],
                                                    name="dropout_keep_deep")
            self.train_phase = tf.placeholder(tf.bool, name="train_phase")

            self.weights = self._initialize_weights()

            # model
            self.embeddings = tf.nn.embedding_lookup(
                self.weights["feature_embeddings"],
                self.feat_index)  # None * F * K
            feat_value = tf.reshape(self.feat_value,
                                    shape=[-1, self.field_size, 1])
            self.embeddings = tf.multiply(self.embeddings, feat_value)

            # ---------- first order term ----------
            self.y_first_order = tf.nn.embedding_lookup(
                self.weights["feature_bias"], self.feat_index)  # None * F * 1
            self.y_first_order = tf.reduce_sum(
                tf.multiply(self.y_first_order, feat_value), 2)  # None * F
            self.y_first_order = tf.nn.dropout(
                self.y_first_order, self.dropout_keep_fm[0])  # None * F

            # ---------- second order term ---------------
            # sum_square part
            self.summed_features_emb = tf.reduce_sum(self.embeddings,
                                                     1)  # None * K
            self.summed_features_emb_square = tf.square(
                self.summed_features_emb)  # None * K

            # square_sum part
            self.squared_features_emb = tf.square(self.embeddings)
            self.squared_sum_features_emb = tf.reduce_sum(
                self.squared_features_emb, 1)  # None * K

            # second order
            self.y_second_order = 0.5 * tf.subtract(
                self.summed_features_emb_square,
                self.squared_sum_features_emb)  # None * K
            self.y_second_order = tf.nn.dropout(
                self.y_second_order, self.dropout_keep_fm[1])  # None * K

            # ---------- Deep component ----------
            self.y_deep = tf.reshape(
                self.embeddings,
                shape=[-1,
                       self.field_size * self.embedding_size])  # None * (F*K)
            self.y_deep = tf.nn.dropout(self.y_deep, self.dropout_keep_deep[0])
            for i in range(0, len(self.deep_layers)):
                self.y_deep = tf.add(
                    tf.matmul(self.y_deep, self.weights["layer_%d" % i]),
                    self.weights["bias_%d" % i])  # None * layer[i] * 1
                if self.batch_norm:
                    self.y_deep = self.batch_norm_layer(
                        self.y_deep,
                        train_phase=self.train_phase,
                        scope_bn="bn_%d" % i)  # None * layer[i] * 1
                self.y_deep = self.deep_layers_activation(self.y_deep)
                self.y_deep = tf.nn.dropout(
                    self.y_deep,
                    self.dropout_keep_deep[1 +
                                           i])  # dropout at each Deep layer

            # ---------- DeepFM ----------
            if self.use_fm and self.use_deep:
                concat_input = tf.concat(
                    [self.y_first_order, self.y_second_order, self.y_deep],
                    axis=1)
            elif self.use_fm:
                concat_input = tf.concat(
                    [self.y_first_order, self.y_second_order], axis=1)
            elif self.use_deep:
                concat_input = self.y_deep
            self.out = tf.add(
                tf.matmul(concat_input, self.weights["concat_projection"]),
                self.weights["concat_bias"])

            # loss
            if self.loss_type == "logloss":
                self.out = tf.nn.sigmoid(self.out)
                self.loss = tf.losses.log_loss(self.label, self.out)
            elif self.loss_type == "mse":
                self.loss = tf.nn.l2_loss(tf.subtract(self.label, self.out))
            elif self.loss_type == "rmse":
                self.loss = tf.sqrt(tf.reduce_mean((self.label - self.out)**2))
            # l2 regularization on weights
            if self.l2_reg > 0:
                self.loss += l2_regularizer(self.l2_reg)(
                    self.weights["concat_projection"])
                if self.use_deep:
                    for i in range(len(self.deep_layers)):
                        self.loss += l2_regularizer(self.l2_reg)(
                            self.weights["layer_%d" % i])

            # optimizer
            if self.optimizer_type == "adam":
                self.optimizer = tf.train.AdamOptimizer(
                    learning_rate=self.learning_rate,
                    beta1=0.9,
                    beta2=0.999,
                    epsilon=1e-8).minimize(self.loss)
            elif self.optimizer_type == "adagrad":
                self.optimizer = tf.train.AdagradOptimizer(
                    learning_rate=self.learning_rate,
                    initial_accumulator_value=1e-8).minimize(self.loss)
            elif self.optimizer_type == "gd":
                self.optimizer = tf.train.GradientDescentOptimizer(
                    learning_rate=self.learning_rate).minimize(self.loss)
            elif self.optimizer_type == "momentum":
                self.optimizer = tf.train.MomentumOptimizer(
                    learning_rate=self.learning_rate,
                    momentum=0.95).minimize(self.loss)
            elif self.optimizer_type == "yellowfin":
                self.optimizer = YFOptimizer(learning_rate=self.learning_rate,
                                             momentum=0.0).minimize(self.loss)

            # init
            self.saver = tf.train.Saver()
            init = tf.global_variables_initializer()
            self.sess = self._init_session()
            self.sess.run(init)

            # number of params
            total_parameters = 0
            for variable in self.weights.values():
                shape = variable.get_shape()
                variable_parameters = 1
                for dim in shape:
                    variable_parameters *= dim.value
                total_parameters += variable_parameters
            if self.verbose > 0:
                print("#params: %d" % total_parameters)
Esempio n. 15
0
def huber_loss(x, delta=1.0):
    """Reference: https://en.wikipedia.org/wiki/Huber_loss"""
    return tf1.where(
        tf1.abs(x) < delta,
        tf1.square(x) * 0.5, delta * (tf1.abs(x) - 0.5 * delta))
Esempio n. 16
0
def run_regression(tensor_data, movie_init_df):
    def feature_normalize(dataset):
        mu = np.mean(dataset, axis=0)
        sigma = np.std(dataset, axis=0)
        return (dataset - mu) / sigma

    def append_bias_reshape(features, labels):
        n_training_samples = features.shape[0]
        n_dim = features.shape[1]
        f = np.reshape(np.c_[np.ones(n_training_samples), features],
                       [n_training_samples, n_dim + 1])
        l = np.reshape(labels, [n_training_samples, 1])
        return f, l

    features = np.array(tensor_data)
    labels = np.array(movie_init_df['target'])
    normalized_features = feature_normalize(features)
    f, l = append_bias_reshape(normalized_features, labels)
    n_dim = f.shape[1]

    rnd_indices = np.random.rand(len(f)) < 0.80

    train_x = f[rnd_indices]
    train_y = l[rnd_indices]
    test_x = f[~rnd_indices]
    test_y = l[~rnd_indices]

    learning_rate = 0.01
    training_epochs = 1000
    cost_history = np.empty(shape=[1], dtype=float)

    X = tf.placeholder(tf.float32, [None, n_dim])
    Y = tf.placeholder(tf.float32, [None, 1])
    W = tf.Variable(tf.ones([n_dim, 1]))

    init = tf.initialize_all_variables()

    y_ = tf.matmul(X, W)
    cost = tf.reduce_mean(tf.square(y_ - Y))
    training_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(
        cost)

    sess = tf.Session()
    sess.run(init)

    for epoch in range(training_epochs):
        sess.run(training_step, feed_dict={X: train_x, Y: train_y})
        cost_history = np.append(
            cost_history, sess.run(cost, feed_dict={
                X: train_x,
                Y: train_y
            }))

    plt.plot(range(len(cost_history)), cost_history)
    plt.axis([0, training_epochs, 0, np.max(cost_history)])
    plt.xlabel('Epochs')
    plt.ylabel('Error')
    plt.show()

    pred_y = sess.run(y_, feed_dict={X: test_x})
    mse = tf.reduce_mean(tf.square(pred_y - test_y))
    print("MSE: %.4f" % sess.run(mse))

    fig, ax = plt.subplots()
    ax.scatter(test_y, pred_y)
    ax.plot([test_y.min(), test_y.max()],
            [test_y.min(), test_y.max()],
            'k--',
            lw=3)
    ax.set_xlabel('Measured')
    ax.set_ylabel('Predicted')
    plt.show()

    fig, ax = plt.subplots()
    ax.scatter(test_y, pred_y)
    ax.plot([test_y.min(), test_y.max()],
            [test_y.min(), test_y.max()],
            'k--',
            lw=3)
    ax.set_xlabel('Measured')
    ax.set_ylabel('Predicted')
    plt.show()
Esempio n. 17
0
# In[586]:

yph = tf.placeholder(tf.float32, [batch_size])

# ** Our graph **

# In[609]:

y_model = m * xph + b  # y for trainging the model

# ** Loss Function **

# In[588]:

error = tf.reduce_mean(tf.square(yph - y_model))

# ** Optimizer **

# In[601]:

optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.0001)
train = optimizer.minimize(error)

# ** Initial Variables **
#

# In[590]:

init = tf.global_variables_initializer()
Esempio n. 18
0
def KLD_loss(mu, logvar):
    return -0.5 * tf.reduce_sum(1 + logvar - tf.square(mu) -
                                tf.math.exp(logvar))
Esempio n. 19
0
def p_train(make_obs_ph_n,
            act_space_n,
            p_index,
            p_func,
            q_func,
            optimizer,
            mut_inf_coef=0,
            grad_norm_clipping=None,
            local_q_func=False,
            num_units=64,
            scope="trainer",
            reuse=None):
    with tf.variable_scope(scope, reuse=reuse):
        # create distribtuions
        act_pdtype_n = [make_pdtype(act_space) for act_space in act_space_n]

        # set up placeholders
        obs_ph_n = make_obs_ph_n
        act_ph_n = [
            act_pdtype_n[i].sample_placeholder([None], name="action" + str(i))
            for i in range(len(act_space_n))
        ]

        p_input = obs_ph_n[p_index]

        p = p_func(p_input,
                   int(act_pdtype_n[p_index].param_shape()[0]),
                   scope="p_func",
                   num_units=num_units)
        p_func_vars = U.scope_vars(U.absolute_scope_name("p_func"))

        # wrap parameters in distribution
        act_pd = act_pdtype_n[p_index].pdfromflat(p)

        act_sample = act_pd.sample()
        p_reg = tf.reduce_mean(tf.square(act_pd.flatparam()))

        act_input_n = act_ph_n + []
        act_input_n[p_index] = act_pd.sample()
        q_input = tf.concat(obs_ph_n + act_input_n, 1)
        if local_q_func:
            q_input = tf.concat([obs_ph_n[p_index], act_input_n[p_index]], 1)
        q = q_func(q_input, 1, scope="q_func", reuse=True,
                   num_units=num_units)[:, 0]
        pg_loss = -tf.reduce_mean(q)

        loss = pg_loss + p_reg * 1e-3

        optimize_expr = U.minimize_and_clip(optimizer, loss, p_func_vars,
                                            grad_norm_clipping)

        # Create callable functions
        train = U.function(inputs=obs_ph_n + act_ph_n,
                           outputs=loss,
                           updates=[optimize_expr])
        act = U.function(inputs=[obs_ph_n[p_index]], outputs=act_sample)
        p_values = U.function([obs_ph_n[p_index]], p)

        # target network
        target_p = p_func(p_input,
                          int(act_pdtype_n[p_index].param_shape()[0]),
                          scope="target_p_func",
                          num_units=num_units)
        target_p_func_vars = U.scope_vars(
            U.absolute_scope_name("target_p_func"))
        update_target_p = make_update_exp(p_func_vars, target_p_func_vars)

        target_act_sample = act_pdtype_n[p_index].pdfromflat(target_p).sample()
        target_act = U.function(inputs=[obs_ph_n[p_index]],
                                outputs=target_act_sample)

        return act, train, update_target_p, {
            'p_values': p_values,
            'target_act': target_act
        }
Esempio n. 20
0
    y_pred = w * x + b

#开启交互式会话
sess = tf.InteractiveSession()
#初始化
sess.run(tf.global_variables_initializer())  #tf.global_variables_initializer()

#将tensor的内容fetch出来
y_pred_numpy = y_pred.eval(session=sess)
plt.plot(x_train, y_train, 'bo', label='real')
plt.plot(x_train, y_pred_numpy, 'ro', label='estimated')
plt.legend()  #函数主要的作用就是给图加上图例

#优化模型
##定义误差函数
loss = tf.reduce_mean(tf.square(y - y_pred))
print(loss.eval(session=sess))

#梯度下降法
##求导
w_grad, b_grad = tf.gradients(loss, [w, b])
print('w_grad: %.4f' % w_grad.eval(session=sess))
print('b_grad: %.4f' % b_grad.eval(session=sess))

##定义学习率
lr = 1e-2
w_update = w.assign_sub(lr * w_grad)
b_update = b.assign_sub(lr * b_grad)

sess.run([w_update, b_update])
Esempio n. 21
0
# Define formula
# define data points
data_points = tf.Variable(dataset)
cluster_labels = tf.Variable(tf.zeros([num_samples], dtype=tf.int64))
# Define initial centroids
initial_centroids = np.array(
    [dataset[np.random.choice(len(dataset))] for _ in range(num_clusters)])
centroids = tf.Variable(initial_centroids)
# Do some reshape to apply subtraction
expanded_centroids = tf.reshape(tf.tile(centroids, [num_samples, 1]),
                                [num_samples, num_clusters, dimensions])
expanded_points = tf.reshape(tf.tile(data_points, [1, num_clusters]),
                             [num_samples, num_clusters, dimensions])

# Calculate distance
distances = tf.reduce_sum(tf.square(expanded_points - expanded_centroids),
                          axis=2)
# Assign a cluster to each point
assignments = tf.argmin(distances, 1)


# Update new cluster
def data_group_avg(assignments, data):
    sum_total = tf.unsorted_segment_sum(data, assignments, 3)
    num_total = tf.unsorted_segment_sum(tf.ones_like(data), assignments, 3)
    avg_by_group = sum_total / num_total
    return avg_by_group


means = data_group_avg(assignments, data_points)
update = tf.group(centroids.assign(means), cluster_labels.assign(assignments))
def loss(model,
         n,
         A_stencil,
         A_matrices,
         S_matrices,
         index=None,
         pos=-1.,
         phase="Training",
         epoch=-1,
         grid_size=8,
         remove=True):
    with tf.device(DEVICE):
        A_matrices = tf.conj(A_matrices)
        S_matrices = tf.conj(S_matrices)
        pi = tf.constant(np.pi)
        theta_x = np.array(([
            i * 2 * pi / n
            for i in range(-n // (grid_size * 2) + 1, n // (grid_size * 2) + 1)
        ]))
    with tf.device(DEVICE):
        if phase == "Test" and epoch == 0:
            P_stencil = model(A_stencil, True)
            P_matrix = utils.compute_p2LFA(P_stencil, n, grid_size)
            P_matrix = tf.transpose(P_matrix, [2, 0, 1, 3, 4])
            P_matrix_t = tf.transpose(P_matrix, [0, 1, 2, 4, 3],
                                      conjugate=True)
            A_c = tf.matmul(tf.matmul(P_matrix_t, A_matrices), P_matrix)

            index_to_remove = len(theta_x) * (
                -1 + n // (2 * grid_size)) + n // (2 * grid_size) - 1
            A_c = tf.reshape(A_c, (-1, int(theta_x.shape[0])**2,
                                   (grid_size // 2)**2, (grid_size // 2)**2))
            A_c_removed = tf.concat(
                [A_c[:, :index_to_remove], A_c[:, index_to_remove + 1:]], 1)
            P_matrix_t_reshape = tf.reshape(
                P_matrix_t, (-1, int(theta_x.shape[0])**2,
                             (grid_size // 2)**2, grid_size**2))
            P_matrix_reshape = tf.reshape(
                P_matrix, (-1, int(theta_x.shape[0])**2, grid_size**2,
                           (grid_size // 2)**2))
            A_matrices_reshaped = tf.reshape(
                A_matrices,
                (-1, int(theta_x.shape[0])**2, grid_size**2, grid_size**2))
            A_matrices_removed = tf.concat([
                A_matrices_reshaped[:, :index_to_remove],
                A_matrices_reshaped[:, index_to_remove + 1:]
            ], 1)

            P_matrix_removed = tf.concat([
                P_matrix_reshape[:, :index_to_remove],
                P_matrix_reshape[:, index_to_remove + 1:]
            ], 1)
            P_matrix_t_removed = tf.concat([
                P_matrix_t_reshape[:, :index_to_remove],
                P_matrix_t_reshape[:, index_to_remove + 1:]
            ], 1)

            A_coarse_inv_removed = tf.matrix_solve(A_c_removed,
                                                   P_matrix_t_removed)

            CGC_removed = tf.eye(grid_size ** 2, dtype=tf.complex128) \
                          - tf.matmul(tf.matmul(P_matrix_removed, A_coarse_inv_removed), A_matrices_removed)
            S_matrices_reshaped = tf.reshape(
                S_matrices,
                (-1, int(theta_x.shape[0])**2, grid_size**2, grid_size**2))
            S_removed = tf.concat([
                S_matrices_reshaped[:, :index_to_remove],
                S_matrices_reshaped[:, index_to_remove + 1:]
            ], 1)
            iteration_matrix = tf.matmul(tf.matmul(CGC_removed, S_removed),
                                         S_removed)
            loss_test = tf.reduce_mean(
                tf.reduce_mean(
                    tf.reduce_sum(tf.square(tf.abs(iteration_matrix)), [2, 3]),
                    1))
            return tf.constant([0.]), loss_test.numpy()
        if index is not None:
            P_stencil = model(A_stencil, index=index, pos=pos, phase=phase)
        else:
            P_stencil = model(A_stencil, phase=phase)

        if not (phase == "Test" and epoch == 0):
            P_matrix = utils.compute_p2LFA(P_stencil, n, grid_size)

            P_matrix = tf.transpose(P_matrix, [2, 0, 1, 3, 4])
            P_matrix_t = tf.transpose(P_matrix, [0, 1, 2, 4, 3],
                                      conjugate=True)

            A_c = tf.matmul(tf.matmul(P_matrix_t, A_matrices), P_matrix)
            index_to_remove = len(theta_x) * (
                -1 + n // (2 * grid_size)) + n // (2 * grid_size) - 1
            A_c = tf.reshape(A_c, (-1, int(theta_x.shape[0])**2,
                                   (grid_size // 2)**2, (grid_size // 2)**2))
            A_c_removed = tf.concat(
                [A_c[:, :index_to_remove], A_c[:, index_to_remove + 1:]], 1)
            P_matrix_t_reshape = tf.reshape(
                P_matrix_t, (-1, int(theta_x.shape[0])**2,
                             (grid_size // 2)**2, grid_size**2))
            P_matrix_reshape = tf.reshape(
                P_matrix, (-1, int(theta_x.shape[0])**2, grid_size**2,
                           (grid_size // 2)**2))
            A_matrices_reshaped = tf.reshape(
                A_matrices,
                (-1, int(theta_x.shape[0])**2, grid_size**2, grid_size**2))
            A_matrices_removed = tf.concat([
                A_matrices_reshaped[:, :index_to_remove],
                A_matrices_reshaped[:, index_to_remove + 1:]
            ], 1)

            P_matrix_removed = tf.concat([
                P_matrix_reshape[:, :index_to_remove],
                P_matrix_reshape[:, index_to_remove + 1:]
            ], 1)
            P_matrix_t_removed = tf.concat([
                P_matrix_t_reshape[:, :index_to_remove],
                P_matrix_t_reshape[:, index_to_remove + 1:]
            ], 1)
            A_coarse_inv_removed = tf.matrix_solve(A_c_removed,
                                                   P_matrix_t_removed)

            CGC_removed = tf.eye(grid_size ** 2, dtype=tf.complex128) \
                          - tf.matmul(tf.matmul(P_matrix_removed, A_coarse_inv_removed), A_matrices_removed)
            S_matrices_reshaped = tf.reshape(
                S_matrices,
                (-1, int(theta_x.shape[0])**2, grid_size**2, grid_size**2))
            S_removed = tf.concat([
                S_matrices_reshaped[:, :index_to_remove],
                S_matrices_reshaped[:, index_to_remove + 1:]
            ], 1)
            iteration_matrix_all = tf.matmul(tf.matmul(CGC_removed, S_removed),
                                             S_removed)

            if remove:
                if phase != 'Test':
                    iteration_matrix = iteration_matrix_all
                    for _ in range(0):
                        iteration_matrix = tf.matmul(iteration_matrix_all,
                                                     iteration_matrix_all)
                else:
                    iteration_matrix = iteration_matrix_all
                loss = tf.reduce_mean(
                    tf.reduce_max(
                        tf.pow(
                            tf.reduce_sum(tf.square(tf.abs(iteration_matrix)),
                                          [2, 3]), 1), 1))
            else:
                loss = tf.reduce_mean(
                    tf.reduce_mean(
                        tf.reduce_sum(tf.square(tf.abs(iteration_matrix_all)),
                                      [2, 3]), 1))

                print("Real loss: ", loss.numpy())
            real_loss = loss.numpy()
            return loss, real_loss
Esempio n. 23
0
    def build_dqn(self):
        # ------- Building the DQN -------
        self.w = {}
        self.t_w = {}

        initializer = tf.truncated_normal_initializer(0, 0.02)
        activation_fn = tf.nn.relu
        n_hidden_1 = 500
        n_hidden_2 = 250
        n_hidden_3 = 120
        n_input = 82
        n_output = 60

        def encoder(x):
            weights = {
                'encoder_h1':
                tf.Variable(
                    tf.truncated_normal([n_input, n_hidden_1], stddev=0.1)),
                'encoder_h2':
                tf.Variable(
                    tf.truncated_normal([n_hidden_1, n_hidden_2], stddev=0.1)),
                'encoder_h3':
                tf.Variable(
                    tf.truncated_normal([n_hidden_2, n_hidden_3], stddev=0.1)),
                'encoder_h4':
                tf.Variable(
                    tf.truncated_normal([n_hidden_3, n_output], stddev=0.1)),
                'encoder_b1':
                tf.Variable(tf.truncated_normal([n_hidden_1], stddev=0.1)),
                'encoder_b2':
                tf.Variable(tf.truncated_normal([n_hidden_2], stddev=0.1)),
                'encoder_b3':
                tf.Variable(tf.truncated_normal([n_hidden_3], stddev=0.1)),
                'encoder_b4':
                tf.Variable(tf.truncated_normal([n_output], stddev=0.1)),
            }
            layer_1 = tf.nn.relu(
                tf.add(tf.matmul(x, weights['encoder_h1']),
                       weights['encoder_b1']))
            layer_2 = tf.nn.relu(
                tf.add(tf.matmul(layer_1, weights['encoder_h2']),
                       weights['encoder_b2']))
            layer_3 = tf.nn.relu(
                tf.add(tf.matmul(layer_2, weights['encoder_h3']),
                       weights['encoder_b3']))
            layer_4 = tf.nn.relu(
                tf.add(tf.matmul(layer_3, weights['encoder_h4']),
                       weights['encoder_b4']))
            return layer_4, weights

        with tf.variable_scope('prediction'):
            self.s_t = tf.placeholder('float32', [None, n_input])
            self.q, self.w = encoder(self.s_t)
            self.q_action = tf.argmax(self.q, dimension=1)

        with tf.variable_scope('target'):
            self.target_s_t = tf.placeholder('float32', [None, n_input])
            self.target_q, self.target_w = encoder(self.target_s_t)
            self.target_q_idx = tf.placeholder('int32', [None, None],
                                               'output_idx')
            self.target_q_with_idx = tf.gather_nd(self.target_q,
                                                  self.target_q_idx)

        with tf.variable_scope('pred_to_target'):
            self.t_w_input = {}
            self.t_w_assign_op = {}
            for name in self.w.keys():
                print('name in self w keys', name)
                self.t_w_input[name] = tf.placeholder(
                    'float32',
                    self.target_w[name].get_shape().as_list(),
                    name=name)
                self.t_w_assign_op[name] = self.target_w[name].assign(
                    self.t_w_input[name])

        def clipped_error(x):
            try:
                return tf.select(
                    tf.abs(x) < 1.0, 0.5 * tf.square(x),
                    tf.abs(x) - 0.5)
            except:
                return tf.where(
                    tf.abs(x) < 1.0, 0.5 * tf.square(x),
                    tf.abs(x) - 0.5)

        with tf.variable_scope('optimizer'):
            self.target_q_t = tf.placeholder('float32',
                                             None,
                                             name='target_q_t')
            self.action = tf.placeholder('int32', None, name='action')
            action_one_hot = tf.one_hot(self.action,
                                        n_output,
                                        1.0,
                                        0.0,
                                        name='action_one_hot')
            q_acted = tf.reduce_sum(self.q * action_one_hot,
                                    reduction_indices=1,
                                    name='q_acted')
            self.delta = self.target_q_t - q_acted
            self.global_step = tf.Variable(0, trainable=False)
            self.loss = tf.reduce_mean(tf.square(self.delta), name='loss')
            self.learning_rate_step = tf.placeholder('int64',
                                                     None,
                                                     name='learning_rate_step')
            self.learning_rate_op = tf.maximum(
                self.learning_rate_minimum,
                tf.train.exponential_decay(self.learning_rate,
                                           self.learning_rate_step,
                                           self.learning_rate_decay_step,
                                           self.learning_rate_decay,
                                           staircase=True))
            self.optim = tf.train.RMSPropOptimizer(self.learning_rate_op,
                                                   momentum=0.95,
                                                   epsilon=0.01).minimize(
                                                       self.loss)

        tf.initialize_all_variables().run()
        self.update_target_q_network()
#Model paramenter

w = tf.compat.v1.Variable([0.3], tf.float32)
b = tf.compat.v1.Variable([-0.3], tf.float32)

# w = -1, b = 1

# input and output
x = tf.compat.v1.placeholder(tf.float32)

linear_model = w * x + b

y = tf.compat.v1.placeholder(tf.float32)

# Loss function

squared_delta = tf.square(linear_model - y)
loss = tf.reduce_sum(squared_delta)

init = tf.global_variables_initializer()

# Run computational graph
sobj = tf.compat.v1.Session()

sobj.run(init)

print(sobj.run(loss, {x: [1, 2, 3, 4], y: [0, -1, -2, -3]}))

sobj.close()
Esempio n. 25
0
def safe_norm(s, axis=-1, epsilon=1e-7, keep_dims=False, name=None):
    with tf.name_scope(name, default_name="safe_norm"):
        squared_norm = tf.reduce_sum(tf.square(s), axis=axis,
                                     keep_dims=keep_dims)
        return tf.sqrt(squared_norm + epsilon)
Esempio n. 26
0
numero_iteraciones_entrenamiento = 5000
tamanio_lote = 1

# placeholders
x = tf.placeholder(tf.float32, [None, numero_pasos, numero_entradas])
y = tf.placeholder(tf.float32, [None, numero_pasos, numero_salidas])

# creating neural layer
capa = tf.contrib.rnn.OutputProjectionWrapper(tf.contrib.rnn.BasicLSTMCe11(
    num_units=numero_neuronas, activation=tf.nn.relu),
                                              output_size=numero_salidas)

salidas, estados = tf.nn.dynamic_rnn(capa, x, dtype=tf.float32)

# error function and optimizer function
funcion_error = tf.reduce_mean(tf.square(salidas - y))
optimizador = tf.train.AdamOptimizer(learnin_rate=tasa_aprendizaje)
entrenamiento = optimizador.minimize(funcion_error)

init = tf.global_variables_initializer()
saver = tf.train.Saver()

# x and y lotes creation using tensorflow session
with tf.Session() as sesion:
    sesion.run(init)
    for iteracion in range(numero_iteraciones_entrenamiento):
        lote_x, lote_y = lotes(entrenamiento_normalizado, tamanio_lote,
                               numero_pasos)
        sesion.run(entrenamiento, feed_dict={x: lote_x, y: lote_y})
        if iteracion % 100 == 0:
            error = funcion_error.eval(feed_dict={x: lote_x, y: lote_y})
    def train(self, X):
        _w = tf.placeholder("float", [self._input_size, self._output_size])
        _hb = tf.placeholder("float", [self._output_size])
        _vb = tf.placeholder("float", [self._input_size])

        prv_w = np.zeros([self._input_size, self._output_size], "float")
        prv_hb = np.zeros([self._output_size], "float")
        prv_vb = np.zeros([self._input_size], "float")

        cur_w = np.zeros([self._input_size, self._output_size], "float")
        cur_hb = np.zeros([self._output_size], "float")
        cur_vb = np.zeros([self._input_size], "float")

        v0 = tf.placeholder("float", [None, self._original_input_size])

        forwardOne = tf.nn.relu(tf.sign(
            tf.nn.sigmoid(tf.matmul(v0, self.rbmOne.w) + self.rbmOne.hb) - tf.random_uniform(
                tf.shape(tf.nn.sigmoid(tf.matmul(v0, self.rbmOne.w) + self.rbmOne.hb)))))
        forwardTwo = tf.nn.relu(tf.sign(
            tf.nn.sigmoid(tf.matmul(forwardOne, self.rbmTwo.w) + self.rbmTwo.hb) - tf.random_uniform(
                tf.shape(tf.nn.sigmoid(tf.matmul(forwardOne, self.rbmTwo.w) + self.rbmTwo.hb)))))
        forward = tf.nn.relu(tf.sign(
            tf.nn.sigmoid(tf.matmul(forwardTwo, self.rbmThree.w) + self.rbmThree.hb) - tf.random_uniform(
                tf.shape(tf.nn.sigmoid(tf.matmul(forwardTwo, self.rbmThree.w) + self.rbmThree.hb)))))
        h0 = self.sample_prob(self.prob_h_given_v(forward, _w, _hb))
        v1 = self.sample_prob(self.prob_v_given_h(h0, _w, _vb))
        h1 = self.prob_h_given_v(v1, _w, _hb)

        positive_grad = tf.matmul(tf.transpose(forward), h0)
        negative_grad = tf.matmul(tf.transpose(v1), h1)

        update_w = _w + self.learning_rate * (positive_grad - negative_grad) / tf.to_float(tf.shape(forward)[0])
        update_vb = _vb + self.learning_rate * tf.reduce_mean(forward - v1, 0)
        update_hb = _hb + self.learning_rate * tf.reduce_mean(h0 - h1, 0)

        backwardOne = tf.nn.relu(tf.sign(
            tf.nn.sigmoid(tf.matmul(v1, self.rbmThree.w.T) + self.rbmThree.vb) - tf.random_uniform(
                tf.shape(tf.nn.sigmoid(tf.matmul(v1, self.rbmThree.w.T) + self.rbmThree.vb)))))
        backwardTwo = tf.nn.relu(tf.sign(
            tf.nn.sigmoid(tf.matmul(backwardOne, self.rbmTwo.w.T) + self.rbmTwo.vb) - tf.random_uniform(
                tf.shape(tf.nn.sigmoid(tf.matmul(backwardOne, self.rbmTwo.w.T) + self.rbmTwo.vb)))))
        backward = tf.nn.relu(tf.sign(
            tf.nn.sigmoid(tf.matmul(backwardTwo, self.rbmOne.w.T) + self.rbmOne.vb) - tf.random_uniform(
                tf.shape(tf.nn.sigmoid(tf.matmul(backwardTwo, self.rbmOne.w.T) + self.rbmOne.vb)))))

        err = tf.reduce_mean(tf.square(v0 - backward))
        error_list = []

        with tf.Session() as sess:
            sess.run(tf.global_variables_initializer())
            for epoch in range(self.epochs):
                for start, end in zip(range(0, len(X), self.batchsize), range(self.batchsize, len(X), self.batchsize)):
                    batch = X[start:end]
                    cur_w = sess.run(update_w, feed_dict={v0: batch, _w: prv_w, _hb: prv_hb, _vb: prv_vb})
                    cur_hb = sess.run(update_hb, feed_dict={v0: batch, _w: prv_w, _hb: prv_hb, _vb: prv_vb})
                    cur_vb = sess.run(update_vb, feed_dict={v0: batch, _w: prv_w, _hb: prv_hb, _vb: prv_vb})
                    prv_w = cur_w
                    prv_hb = cur_hb
                    prv_vb = cur_vb
                error = sess.run(err, feed_dict={v0: X, _w: cur_w, _vb: cur_vb, _hb: cur_hb})
                print('Epoch: %d' % (epoch + 1), 'reconstruction error: %f' % error)
                error_list.append(error)
            self.w = prv_w
            self.hb = prv_hb
            self.vb = prv_vb
            return error_list
Esempio n. 28
0
def compute_feature_ranking(input_image,
                            saliency_map,
                            threshold,
                            global_mean,
                            rescale_heatmap=True,
                            keep_information=False,
                            use_squared_value=False):
    """Returns the feature importance ranking estimate.

  If the saliency map ranking is higher than the ranking method threshold, the
  feature ranking estimate is the product of the rescaled [0,1] saliency method
  and the raw input image. If not, the feature estimate is set to
  the mean of the raw input image.

  Args:
    input_image: A 3D float tensor containing the model inputs.
    saliency_map: A 3D float tensor containing the saliency feature ranking.
    threshold: Percentile of information in the saliency method ranking to
      retain.
    global_mean: The global mean for each color channel.
    rescale_heatmap: Boolean, true if saliency heatmap is rescaled.
    keep_information: Boolean, whether the important information should be
      removed or kept.
    use_squared_value: Boolean, whether to take the squared value of the
      heatmap.

  Returns:
    feature_ranking: feature ranking input
  Raises:
    ValueError: The ranking method passed is not known.
  """

    if use_squared_value:
        saliency_map = tf.square(saliency_map)
    else:
        tf.logging.info('not using squared value')

    if rescale_heatmap:
        # re-scale the range of saliency map pixels between [0-1]
        saliency_map = rescale_input(saliency_map)
    else:
        tf.logging.info('not rescaling heatmap')

    total_pixels = IMAGE_DIMS[0] * IMAGE_DIMS[1] * IMAGE_DIMS[2]
    saliency_map = tf.reshape(saliency_map, [total_pixels])
    input_image = tf.reshape(input_image, [total_pixels])

    num_pixels = np.int((threshold / 100.) * total_pixels)

    # we add small epsilon to saliency_method for percentage method
    # epsilon allows us to distinguish between 0 value pixel in saliency heatmap
    # and pixels set to 0 by the tf.scatter_nd gather.
    epsilon = tf.fill([total_pixels], 0.00001)
    saliency_map = tf.add(saliency_map, epsilon)

    global_mean_constant = tf.constant(global_mean, shape=[1, 1, 3])
    substitute_information = tf.reshape(tf.multiply(global_mean_constant, 1.),
                                        [total_pixels])

    feature_ranking = percentage_ranking(
        saliency_map=saliency_map,
        input_image=input_image,
        substitute_information=substitute_information,
        keep_information=keep_information,
        number_pixels=num_pixels)

    feature_ranking = tf.reshape(feature_ranking, IMAGE_DIMS)

    return feature_ranking
    def backward(self):

        assert ('X' in self.phs)
        assert ('Y' in self.phs)
        assert ('fX' in self.vars)
        fX = self.vars['fX']
        Y = self.phs['Y']

        Y_one_hot = tf.one_hot(Y, depth=self.layer_sizes[-1], dtype=tf.float32)
        loss = tf.nn.softmax_cross_entropy_with_logits_v2(logits=fX,
                                                          labels=Y_one_hot)
        loss = tf.reduce_mean(loss)
        self.vars['loss'] = loss
        self.vars['losses'] = {}
        self.vars['losses'][0] = loss

        var_list = self.get_trainable_vars()
        self.vars['orig_var_list'] = var_list

        # Fisher stuff
        print('Creating fisher ops')
        fisher_current = [
            utils.get_var("fisher_diag_%sc" %
                          var.name.split(":")[0].replace("/", "_"))
            for var in var_list
        ]
        grads = [
            tf.gradients(self.vars['batch_log_likelihood'], var_list)
            for var_list in self.objs['fisher_var_lists']
        ]
        fisher_delta = []
        for i in range(len(self.objs['fisher_ws'])):
            fisher_delta += [tf.add_n([tf.square(g[i]) for g in grads])]

        fisher_sum_up_ops = [
            tf.assign_add(fc, fd)
            for fc, fd in zip(fisher_current, fisher_delta)
        ]
        self.objs['fisher_sum_up_ops'] = fisher_sum_up_ops

        opt = tf.train.AdamOptimizer(self.lr)
        self.objs['opt'] = opt

        # update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
        # with tf.control_dependencies(update_ops):
        # print("Trainable vars: %s" % str(var_list))
        print("Trainable vars:")
        self.print_vars(var_list)
        op = self.objs['opt'].minimize(loss, var_list=var_list)
        self.vars['train_op'] = op
        self.vars['train_ops'] = {}
        self.vars['train_ops'][0] = op

        predictions = tf.argmax(tf.nn.softmax(fX), axis=1)
        predictions = tf.cast(predictions, tf.uint8)  # cast to uint8, like Y
        self.vars['predictions'] = predictions

        acc = tf.equal(Y, predictions)
        acc = tf.cast(acc,
                      tf.float32)  # For averaging, first cast bool to float32
        acc = tf.reduce_mean(acc)
        self.vars['acc'] = acc
Esempio n. 30
0
x1 = [row[0] for row in data]
x2 = [row[1] for row in data]  # 새로 추가되는 값
y_data = [row[2] for row in data]

# 기울기 a와 y절편 b의 값을 임의로 정함. 단 기울기의 범위는 0-10 사이, y 절편은 0-100사이에서 변하게 함
a1 = tf.Variable(tf.random_uniform([1], 0, 10, dtype=tf.float64, seed=0))
a2 = tf.Variable(tf.random_uniform([1], 0, 10, dtype=tf.float64,
                                   seed=0))  # 새로 추가되는 값
b = tf.Variable(tf.random_uniform([1], 0, 100, dtype=tf.float64, seed=0))

# 새로운 방정식
y = a1 * x1 + a2 * x2 + b

# 텐서플로 RMSE 함수
rmse = tf.sqrt(tf.reduce_mean(tf.square(y - y_data)))

# 학습률 값
learning_rate = 0.1

# RMSE 값을 최소로 하는 값 찾기
gradient_descent = tf.train.GradientDescentOptimizer(learning_rate).minimize(
    rmse)

# 학습이 진행되는 부분
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for step in range(2001):
        sess.run(gradient_descent)
        if step % 100 == 0:
            print(