Example #1
0
def mean_squared_error(output, target, is_mean=False):
    """Return the TensorFlow expression of mean-squre-error of two distributions.

    Parameters
    ----------
    output : 2D or 4D tensor.
    target : 2D or 4D tensor.
    is_mean : boolean, if True, use ``tf.reduce_mean`` to compute the loss of one data, otherwise, use ``tf.reduce_sum`` (default).

    References
    ------------
    - `Wiki Mean Squared Error <https://en.wikipedia.org/wiki/Mean_squared_error>`_
    """
    with tf.name_scope("mean_squared_error_loss"):
        if output.get_shape().ndims == 2:   # [batch_size, n_feature]
            if is_mean:
                mse = tf.reduce_mean(tf.reduce_mean(tf.squared_difference(output, target), 1))
            else:
                mse = tf.reduce_mean(tf.reduce_sum(tf.squared_difference(output, target), 1))
        elif output.get_shape().ndims == 4: # [batch_size, w, h, c]
            if is_mean:
                mse = tf.reduce_mean(tf.reduce_mean(tf.squared_difference(output, target), [1, 2, 3]))
            else:
                mse = tf.reduce_mean(tf.reduce_sum(tf.squared_difference(output, target), [1, 2, 3]))
        return mse
def create_cost_spacing(t, length, normalized = True):
  d = tf.sqrt(tf.reduce_sum(tf.square(t), reduction_indices = 1));
  if normalized:
    s = t.get_shape().as_list();
    return tf.reduce_mean(tf.squared_difference(d, tf.constant(length / s[0], "float32")));
  else:
    return tf.reduce_mean(tf.squared_difference(d, tf.constant(length, "float32")));
Example #3
0
def arg_closest_anchor(bboxes, anchors):
  """Find the closest anchor. Box Format [ymin, xmin, ymax, xmax]
  """
  num_anchors = anchors.get_shape().as_list()[0]
  num_bboxes = tf.shape(bboxes)[0]

  _indices = tf.reshape(tf.range(num_bboxes), shape=[-1, 1])
  _indices = tf.reshape(tf.stack([_indices] * num_anchors, axis=1), shape=[-1, 1])
  bboxes_m = tf.gather_nd(bboxes, _indices)
  # bboxes_m = tf.Print(bboxes_m, [bboxes_m], "bboxes_m", summarize=100)

  anchors_m = tf.tile(anchors, [num_bboxes, 1])
  # anchors_m = tf.Print(anchors_m, [anchors_m], "anchors_m", summarize=100)

  square_dist = tf.squared_difference(bboxes_m[:, 0], anchors_m[:, 0]) + \
                tf.squared_difference(bboxes_m[:, 1], anchors_m[:, 1]) + \
                tf.squared_difference(bboxes_m[:, 2], anchors_m[:, 2]) + \
                tf.squared_difference(bboxes_m[:, 3], anchors_m[:, 3])

  square_dist = tf.reshape(square_dist, shape=[num_bboxes, num_anchors])

  # square_dist = tf.Print(square_dist, [square_dist], "square_dist", summarize=100)

  indices = tf.arg_min(square_dist, dimension=1)

  return indices
Example #4
0
        def LSGAN_losses(real, fake):
            d_real = tf.reduce_mean(tf.squared_difference(real, 1), name='d_real')
            d_fake = tf.reduce_mean(tf.square(fake), name='d_fake')
            d_loss = tf.multiply(d_real + d_fake, 0.5, name='d_loss')

            g_loss = tf.reduce_mean(tf.squared_difference(fake, 1), name='g_loss')
            add_moving_summary(g_loss, d_loss)
            return g_loss, d_loss
Example #5
0
    def __init__(self, sess, state, action, learning_rate, tau):
        self.sess = sess
        self.state_dim = len(state)
        self.action_dim = len(action)
        self.rate = learning_rate
        self.tau = tau

        # create critic network
        train_network = self.create_network('critic_train')
        self.inputs = train_network[0]
        self.actions = train_network[1]
        self.q_outputs = train_network[2]
        self.state_outputs = train_network[3]
        self.train_net = tf.get_collection(
            tf.GraphKeys.TRAINABLE_VARIABLES, scope=train_network[4]
        )

        # create target critic network
        target_network = self.create_network('critic_target')
        self.target_inputs = target_network[0]
        self.target_actions = target_network[1]
        self.target_q_outputs = target_network[2]
        self.target_state_outputs = target_network[3]
        self.target_net = tf.get_collection(
            tf.GraphKeys.TRAINABLE_VARIABLES, scope=target_network[4]
        )

        # op for updating target network with train network weights
        self.update = [self.target_net[i].assign(
            tf.mul(self.train_net[i], self.tau) +
            tf.mul(self.target_net[i], 1. - self.tau)
        )
            for i in range(len(self.target_net))]

        # define loss and optimization op
        self.state_prime = tf.placeholder(fl32, [None, self.state_dim])
        self.batch_state_loss = \
            tf.squared_difference(self.s_prime, self.state_outputs)
        self.state_loss = tf.reduce_mean(self.batch_state_loss)

        self.y = tf.placeholder(fl32, [None, 1])
        self.batch_q_loss = tf.squared_difference(self.y, self.q_outputs)
        self.q_loss = tf.reduce_mean(self.batch_loss)

        self.train_q = \
            tf.train.AdamOptimizer(self.rate).minimize(self.q_loss)
        self.train_state = \
            tf.train.AdamOptimizer(self.rate).minimize(self.state_loss)

        # get the gradient of the train net with respect to actions
        self.policy_gradient = tf.gradients(self.q_outputs, self.actions)
        self.explore_gradient = tf.gradients(self.state_loss, self.actions)

        # print total number of parameters (neurons)
        vars = self.train_net
        print sum([sum([reduce(
                lambda x, y: x * y, l.get_shape().as_list()) for l in e])
                for e in [vars]])
Example #6
0
def TV_loss(img,tv_weight):
	shape = tf.shape(img)  # the shape of the img is (1,H,W,C)
	img_row_before = tf.slice(img,[0,0,0,0],[-1,-1,shape[2]-1,-1])
	img_row_after = tf.slice(img,[0,0,1,0],[-1,-1,shape[2]-1,-1])
	img_col_before = tf.slice(img,[0,0,0,0],[-1,shape[1]-1,-1,-1])
	img_col_after = tf.slice(img,[0,1,0,0],[-1,shape[1]-1,-1,-1])
	tv_loss =  tv_weight*(tf.reduce_sum(tf.squared_difference(img_col_after,img_col_before))+\
			tf.reduce_sum(tf.squared_difference(img_row_after,img_row_before)))
	return tv_loss
Example #7
0
 def build_model(self):    
     
     dense_masker01 = tf.sparse_tensor_to_dense(self.mask)
     dense_masker02 = tf.sparse_tensor_to_dense(self.mask1)
     dense_masker03 = tf.sparse_tensor_to_dense(self.mask2)
         
     with tf.name_scope('encoding'):
         encoding = tf.add(tf.sparse_tensor_dense_matmul(self.X, self.W) , self.b, name= 'raw_values')
         encoded_values = self.enc_func(encoding, name = 'encoded_values') - self.enc_func(self.b)  
         
         encoding1 = tf.add(tf.sparse_tensor_dense_matmul(self.X1, self.W) , self.b, name= 'raw_values1')
         encoded_values1 = self.enc_func(encoding1, name = 'encoded_values1') - self.enc_func(self.b)  
         
         encoding2 = tf.add(tf.sparse_tensor_dense_matmul(self.X2, self.W) , self.b, name= 'raw_values2')
         encoded_values2 = self.enc_func(encoding2, name = 'encoded_values2') - self.enc_func(self.b)  
 
         
     with tf.name_scope('decoding'):
         decoding =  tf.nn.xw_plus_b(encoded_values, self.W_prime, self.b_prime)
         decoded_values = self.dec_func(decoding, name = 'decoded_values')
         
         decoding1 =  tf.nn.xw_plus_b(encoded_values1, self.W_prime, self.b_prime)
         decoded_values1 = self.dec_func(decoding1, name = 'decoded_values1')
         
         decoding2 =  tf.nn.xw_plus_b(encoded_values2, self.W_prime, self.b_prime)
         decoded_values2 = self.dec_func(decoding2, name = 'decoded_values2')
         
         masked_decoded_values = tf.multiply(dense_masker01, decoded_values)
     
     with tf.name_scope('training_process'):
         diff01 = tf.squared_difference(tf.sparse_tensor_to_dense(self.Y) , decoded_values)  
         diff02 = tf.squared_difference(tf.sparse_tensor_to_dense(self.Y1) , decoded_values1)  
         diff03 = tf.squared_difference(tf.sparse_tensor_to_dense(self.Y2) , decoded_values2)
         L_R  = tf.reduce_sum( tf.multiply(dense_masker01, diff01)) \
             +  tf.reduce_sum( tf.multiply(dense_masker02, diff02)) \
             + tf.reduce_sum( tf.multiply(dense_masker03, diff03))
         
         L_T = tf.reduce_sum( tf.log(1+ tf.exp( tf.reduce_sum( tf.multiply(encoded_values, encoded_values2), 1) -  tf.reduce_sum(tf.multiply(encoded_values, encoded_values1),1))))
         
         error = L_R + self.alpha_enc * L_T
         
         reg = 0  
         for param in self.params.items():
             reg += tf.nn.l2_loss(param[1])* self.lambda_w
         loss = error + reg
         
     model_params = [p for p in self.params.values()]
        
     train_step = self._optimize(loss, model_params)
       
     tf.summary.scalar('error', error)
     tf.summary.scalar('loss', loss)        
     for param in self.params.items():
         tf.summary.histogram(param[0], param[1])        
     merged_summary = tf.summary.merge_all()                                   
         
     return encoded_values, decoded_values, masked_decoded_values, error, loss, train_step, merged_summary
def create_cost_spacing(self, c, length, normalized = True):
  c_shape = c.get_shape().as_list();
  c1 = tf.slice(c, [1,0], [-1,-1]);
  c2 = tf.slice(c, [0,0], [c_shape[0]-1,-1]);
  d = tf.sqrt(tf.reduce_sum(tf.squared_difference(c1,c2), reduction_indices = 1));
  if normalized:
    return tf.reduce_mean(tf.squared_difference(d, tf.constant(length / (c_shape[0]-1), "float32")));
  else:
    return tf.reduce_mean(tf.squared_difference(d, tf.constant(length, "float32")));
def GMM_M_Step(X, Gama, ClusterNo, name='GMM_Statistics', **kwargs):

    D, h, s = tf.split(X, [1,1,1], axis=3)
    
    WXd = tf.multiply(Gama, tf.tile(D ,[1,1,1,ClusterNo]))
    WXa = tf.multiply(Gama, tf.tile(h ,[1,1,1,ClusterNo]))
    WXb = tf.multiply(Gama, tf.tile(s ,[1,1,1,ClusterNo]))
    
    S = tf.reduce_sum(tf.reduce_sum(Gama, axis=1), axis=1)
    S = tf.add(S, tf.contrib.keras.backend.epsilon())
    S = tf.reshape(S,[1, ClusterNo])
    
    M_d = tf.div(tf.reduce_sum(tf.reduce_sum(WXd, axis=1), axis=1) , S)
    M_a = tf.div(tf.reduce_sum(tf.reduce_sum(WXa, axis=1), axis=1) , S)
    M_b = tf.div(tf.reduce_sum(tf.reduce_sum(WXb, axis=1), axis=1) , S)
    
    Mu = tf.split(tf.concat([M_d, M_a, M_b],axis=0), ClusterNo, 1)  
    
    Norm_d = tf.squared_difference(D, tf.reshape(M_d,[1, ClusterNo]))
    Norm_h = tf.squared_difference(h, tf.reshape(M_a,[1, ClusterNo]))
    Norm_s = tf.squared_difference(s, tf.reshape(M_b,[1, ClusterNo]))
    
    WSd = tf.multiply(Gama, Norm_d)
    WSh = tf.multiply(Gama, Norm_h)
    WSs = tf.multiply(Gama, Norm_s)
    
    S_d = tf.sqrt(tf.div(tf.reduce_sum(tf.reduce_sum(WSd, axis=1), axis=1) , S))
    S_h = tf.sqrt(tf.div(tf.reduce_sum(tf.reduce_sum(WSh, axis=1), axis=1) , S))
    S_s = tf.sqrt(tf.div(tf.reduce_sum(tf.reduce_sum(WSs, axis=1), axis=1) , S))
    
    Std = tf.split(tf.concat([S_d, S_h, S_s],axis=0), ClusterNo, 1)  
    
    dist = list()
    for k in range(0, ClusterNo):
        dist.append(tf.contrib.distributions.MultivariateNormalDiag(tf.reshape(Mu[k],[1,3]), tf.reshape(Std[k],[1,3])))
    
    PI = tf.split(Gama, ClusterNo, axis=3) 
    Prob0 = list()
    for k in range(0, ClusterNo):
        Prob0.append(tf.multiply(tf.squeeze(dist[k].prob(X)), tf.squeeze(PI[k])))
        
    Prob = tf.convert_to_tensor(Prob0, dtype=tf.float32)    
    Prob = tf.minimum(tf.add(tf.reduce_sum(Prob, axis=0), tf.contrib.keras.backend.epsilon()), tf.constant(1.0, tf.float32))
    Log_Prob = tf.negative(tf.log(Prob))
    Log_Likelihood = tf.reduce_mean(Log_Prob)
    
    return Log_Likelihood, Mu, Std
        
Example #10
0
 def test_expected_value(self):
   shape_ = np.array([2, int(1e3)], np.int32)
   shape = (tf.constant(shape_) if self.use_static_shape
            else tf.placeholder_with_default(shape_, shape=None))
   # This shape will require broadcasting before sampling.
   scale_ = np.linspace(0.1, 0.5, 3 * 2).astype(self.dtype).reshape(3, 2)
   scale = (tf.constant(scale_) if self.use_static_shape
            else tf.placeholder_with_default(scale_, shape=None))
   x = tfp.math.random_rayleigh(shape,
                                scale=scale[..., tf.newaxis],
                                dtype=self.dtype,
                                seed=42)
   self.assertEqual(self.dtype, x.dtype.as_numpy_dtype)
   final_shape_ = [3, 2, int(1e3)]
   if self.use_static_shape:
     self.assertAllEqual(final_shape_, x.shape)
   sample_mean = tf.reduce_mean(x, axis=-1, keepdims=True)
   sample_var = tf.reduce_mean(tf.squared_difference(
       x, sample_mean), axis=-1)
   [x_, sample_mean_, sample_var_] = self.evaluate([
       x, sample_mean[..., 0], sample_var])
   self.assertAllEqual(final_shape_, x_.shape)
   self.assertAllEqual(np.ones_like(x_, dtype=np.bool), x_ > 0.)
   self.assertAllClose(np.sqrt(np.pi / 2.) * scale_, sample_mean_,
                       atol=0.05, rtol=0.)
   self.assertAllClose(0.5 * (4. - np.pi) * scale_**2., sample_var_,
                       atol=0.05, rtol=0.)
    def _build_net(self):
        def build_layers(s, c_names, n_l1, w_initializer, b_initializer):
            with tf.variable_scope('l1'):
                w1 = tf.get_variable('w1', [self.n_features, n_l1], initializer=w_initializer, collections=c_names)
                b1 = tf.get_variable('b1', [1, n_l1], initializer=b_initializer, collections=c_names)
                l1 = tf.nn.relu(tf.matmul(s, w1) + b1)

            with tf.variable_scope('l2'):
                w2 = tf.get_variable('w2', [n_l1, self.n_actions], initializer=w_initializer, collections=c_names)
                b2 = tf.get_variable('b2', [1, self.n_actions], initializer=b_initializer, collections=c_names)
                out = tf.matmul(l1, w2) + b2
            return out
        # ------------------ build evaluate_net ------------------
        self.s = tf.placeholder(tf.float32, [None, self.n_features], name='s')  # input
        self.q_target = tf.placeholder(tf.float32, [None, self.n_actions], name='Q_target')  # for calculating loss

        with tf.variable_scope('eval_net'):
            c_names, n_l1, w_initializer, b_initializer = \
                ['eval_net_params', tf.GraphKeys.GLOBAL_VARIABLES], 20, \
                tf.random_normal_initializer(0., 0.3), tf.constant_initializer(0.1)  # config of layers

            self.q_eval = build_layers(self.s, c_names, n_l1, w_initializer, b_initializer)

        with tf.variable_scope('loss'):
            self.loss = tf.reduce_mean(tf.squared_difference(self.q_target, self.q_eval))
        with tf.variable_scope('train'):
            self._train_op = tf.train.RMSPropOptimizer(self.lr).minimize(self.loss)

        # ------------------ build target_net ------------------
        self.s_ = tf.placeholder(tf.float32, [None, self.n_features], name='s_')    # input
        with tf.variable_scope('target_net'):
            c_names = ['target_net_params', tf.GraphKeys.GLOBAL_VARIABLES]

            self.q_next = build_layers(self.s_, c_names, n_l1, w_initializer, b_initializer)
Example #12
0
  def testSampleConsistentStats(self):
    loc = np.float32([[-1., 1], [1, -1]])
    scale = np.float32([1., 0.5])
    n_samp = 1e4
    with self.test_session() as sess:
      ind = tfd.Independent(
          distribution=tfd.MultivariateNormalDiag(
              loc=loc, scale_identity_multiplier=scale),
          reinterpreted_batch_ndims=1)

      x = ind.sample(int(n_samp), seed=42)
      sample_mean = tf.reduce_mean(x, axis=0)
      sample_var = tf.reduce_mean(tf.squared_difference(x, sample_mean), axis=0)
      sample_std = tf.sqrt(sample_var)
      sample_entropy = -tf.reduce_mean(ind.log_prob(x), axis=0)

      [
          sample_mean_, sample_var_, sample_std_, sample_entropy_,
          actual_mean_, actual_var_, actual_std_, actual_entropy_,
          actual_mode_,
      ] = sess.run([
          sample_mean, sample_var, sample_std, sample_entropy,
          ind.mean(), ind.variance(), ind.stddev(), ind.entropy(), ind.mode(),
      ])

      self.assertAllClose(sample_mean_, actual_mean_, rtol=0.02, atol=0.)
      self.assertAllClose(sample_var_, actual_var_, rtol=0.04, atol=0.)
      self.assertAllClose(sample_std_, actual_std_, rtol=0.02, atol=0.)
      self.assertAllClose(sample_entropy_, actual_entropy_, rtol=0.01, atol=0.)
      self.assertAllClose(loc, actual_mode_, rtol=1e-6, atol=0.)
Example #13
0
    def _build_model(self):
        # placeholders
        self.input = tf.placeholder(
            shape=[None, 84, 84, 4], dtype=tf.float32, name='inputs')
        self.actions = tf.placeholder(
            shape=[None], dtype=tf.int32, name='actions')
        self.next_input = tf.placeholder(
            shape=[None], dtype=tf.float32, name='next_inputs')

        # network
        with tf.variable_scope('qnet'):
            self.qvals = self._net(self.input)
        with tf.variable_scope('target'):
            self.target_qvals = self._net(self.input, trainable=False)

        trainable_variables = tf.trainable_variables('qnet')
        batch_size = tf.shape(self.input)[0]
        gather_indices = tf.range(batch_size) * self.n_ac + self.actions
        action_q = tf.gather(tf.reshape(self.qvals, [-1]), gather_indices)
        self.loss = tf.reduce_mean(
            tf.squared_difference(self.next_input, action_q))
        self.max_qval = tf.reduce_max(self.qvals)

        self.train_op = self.optimizer.minimize(
            self.loss,
            global_step=tf.train.get_global_step(),
            var_list=trainable_variables)
        self.update_target_op = self._get_update_target_op()
Example #14
0
File: model.py Project: cning/ehc
    def drawGraph(self, n_row, n_latent, n_col):
        with tf.name_scope('matDecomp'):
            self._p = tf.placeholder(tf.float32, shape=[None, n_col])
            self._c = tf.placeholder(tf.float32, shape=[None, n_col])
            self._lambda = tf.placeholder(tf.float32)
            self._index = tf.placeholder(tf.float32, shape=[None, n_row])
            self._A = tf.Variable(tf.truncated_normal([n_row, n_latent]))
            self._B = tf.Variable(tf.truncated_normal([n_latent, n_col]))
            self._h = tf.matmul(tf.matmul(self._index, self._A), self._B) 
            
            weighted_loss = tf.reduce_mean(tf.mul(self._c, tf.squared_difference(self._p, self._h)))
            self._weighted_loss = weighted_loss
            l2_A = tf.reduce_sum(tf.square(self._A))
            l2_B = tf.reduce_sum(tf.square(self._B))
            n_w = tf.constant(n_row * n_latent + n_latent * n_col, tf.float32)
            l2 = tf.truediv(tf.add(l2_A, l2_B), n_w)
            reg_term = tf.mul(self._lambda, l2)
            self._loss = tf.add(weighted_loss, reg_term)
            
            self._mask = tf.placeholder(tf.float32, shape=[n_row, n_col])
            one = tf.constant(1, tf.float32)
            pred = tf.cast(tf.greater_equal(tf.matmul(self._A, self._B), one), tf.float32)
            cor = tf.mul(tf.cast(tf.equal(pred, self._p), tf.float32), self._c)
            self._vali_err = tf.reduce_sum(tf.mul(cor, self._mask))

            self._saver = tf.train.Saver([v for v in tf.all_variables() if v.name.find('matDecomp') != -1])
            tf.scalar_summary('training_weighted_loss_l2', self._loss)
            tf.scalar_summary('validation_weighted_loss', self._weighted_loss)
            merged = tf.merge_all_summaries()
Example #15
0
  def _testGrad(self, left_shape, right_shape):

    if len(left_shape) > len(right_shape):
      output_shape = left_shape
    else:
      output_shape = right_shape
    l = np.random.randn(*left_shape)
    r = np.random.randn(*right_shape)

    with self.test_session():
      left_tensor = tf.constant(l, shape=left_shape)
      right_tensor = tf.constant(r, shape=right_shape)
      output = tf.squared_difference(left_tensor, right_tensor)
      left_err = tf.test.compute_gradient_error(left_tensor,
                                                left_shape,
                                                output,
                                                output_shape,
                                                x_init_value=l)
      right_err = tf.test.compute_gradient_error(right_tensor,
                                                 right_shape,
                                                 output,
                                                 output_shape,
                                                 x_init_value=r)
    self.assertLess(left_err, 1e-10)
    self.assertLess(right_err, 1e-10)
Example #16
0
  def _apply(self, grad, var, indices=None):
    lr = tf.cast(self._learning_rate_tensor, var.dtype.base_dtype)
    m = self.get_slot(var, "m")
    v = self.get_slot(var, "v")
    beta1_t = tf.cast(self._beta1_t, var.dtype.base_dtype)
    beta2_t = tf.cast(self._beta2_t, var.dtype.base_dtype)
    epsilon_t = tf.cast(self._epsilon_t, var.dtype.base_dtype)

    # m_t = beta1 * m + (1 - beta1) * g_t
    m_scaled_g_values = grad * (1 - beta1_t)
    m_t = tf.assign(m, m * beta1_t, use_locking=self._use_locking)
    with tf.control_dependencies([m_t]):
      m_t = self._assign_add(m, updates=m_scaled_g_values, indices=indices)
    m_gathered = self._gather(m_t, indices=indices)

    # Also see tf.nn.moments.
    variance = tf.squared_difference(grad, m_gathered)

    # v_t = beta2 * v + (1 - beta2) * variance
    v_scaled_new_values = variance * (1 - beta2_t)
    v_t = tf.assign(v, v * beta2_t, use_locking=self._use_locking)
    with tf.control_dependencies([v_t]):
      v_t = self._assign_add(v, updates=v_scaled_new_values, indices=indices)
    v_gathered = self._gather(v_t, indices=indices)

    factor = v_gathered / (variance + epsilon_t)
    update = lr * grad * tf.minimum(factor, 1.0)
    var_update = self._assign_sub(ref=var, updates=update, indices=indices)
    return tf.group(*[var_update, m_t])
Example #17
0
    def _get_cost(self, outputs):
        """Construct the cost function from the outputs of the last layer. This
        will be used through SGD to train the network.

        Parameters
        ----------
        outputs: tuple fo tensors (n_out)
            a tuple of tensor containing the output from the last layer of the
            network

        Returns
        -------
        cost: a tensor computing the cost function of the network.
        reg: a tensor for computing regularization of the parameters.
            It should be None if no regularization is needed.
        """
        Zk, X, lmbd = outputs

        with tf.name_scope("reconstruction_zD"):
            rec = tf.matmul(Zk, tf.constant(self.D))

        with tf.name_scope("norm_2"):
            Er = tf.multiply(
                tf.constant(.5, dtype=tf.float32),
                tf.reduce_mean(tf.reduce_sum(tf.squared_difference(rec, X),
                                             reduction_indices=[1])))

        with tf.name_scope("norm_1"):
            l1 = lmbd * tf.reduce_mean(tf.reduce_sum(
                tf.abs(Zk), reduction_indices=[1]))

        return tf.add(Er, l1, name="cost")
  def testRWM1DNNormal(self):
    """Sampling from the Standard Normal Distribution."""
    dtype = np.float32

    with self.test_session(graph=tf.Graph()) as sess:
      target = tfd.Normal(loc=dtype(0), scale=dtype(1))

      def make_kernel_fn(target_log_prob_fn, seed):
        return tfp.mcmc.HamiltonianMonteCarlo(
            target_log_prob_fn=target_log_prob_fn,
            seed=seed, step_size=1.0, num_leapfrog_steps=3)

      remc = tfp.mcmc.ReplicaExchangeMC(
          target_log_prob_fn=target.log_prob,
          inverse_temperatures=10.**tf.linspace(0., -2., 5),
          make_kernel_fn=make_kernel_fn,
          seed=42)

      samples, _ = tfp.mcmc.sample_chain(
          num_results=1000,
          current_state=dtype(1),
          kernel=remc,
          num_burnin_steps=500,
          parallel_iterations=1)  # For determinism.

      sample_mean = tf.reduce_mean(samples, axis=0)
      sample_std = tf.sqrt(
          tf.reduce_mean(tf.squared_difference(samples, sample_mean),
                         axis=0))
      [sample_mean_, sample_std_] = sess.run([sample_mean, sample_std])

    self.assertAllClose(sample_mean_, 0., atol=0.1, rtol=0.1)
    self.assertAllClose(sample_std_, 1., atol=0.1, rtol=0.1)
Example #19
0
 def _compute_data_loss(self):
     if self.hparams.loss == "cross_entropy_loss":
         data_loss = tf.reduce_mean(
             tf.nn.sigmoid_cross_entropy_with_logits(
                 logits=tf.reshape(self.logit, [-1]),
                 labels=tf.reshape(self.iterator.labels, [-1]),
             )
         )
     elif self.hparams.loss == "square_loss":
         data_loss = tf.sqrt(
             tf.reduce_mean(
                 tf.squared_difference(
                     tf.reshape(self.pred, [-1]),
                     tf.reshape(self.iterator.labels, [-1]),
                 )
             )
         )
     elif self.hparams.loss == "log_loss":
         data_loss = tf.reduce_mean(
             tf.losses.log_loss(
                 predictions=tf.reshape(self.pred, [-1]),
                 labels=tf.reshape(self.iterator.labels, [-1]),
             )
         )
     else:
         raise ValueError("this loss not defined {0}".format(self.hparams.loss))
     return data_loss
Example #20
0
    def build_graph(self, theta, image, gt_image, gt_filter):
        kernel_size = 9

        image = image
        gt_image = gt_image

        theta = tf.reshape(theta, [BATCH, 1, 1, 1]) - np.pi
        image = tf.reshape(image, [BATCH, SHAPE, SHAPE, 1])
        gt_image = tf.reshape(gt_image, [BATCH, SHAPE, SHAPE, 1])

        pred_filter = self._parameter_net(theta)
        pred_image = DynamicConvFilter('dfn', image, pred_filter, 1, kernel_size)

        with tf.name_scope('visualization'):
            pred_filter = tf.reshape(pred_filter, [BATCH, kernel_size, kernel_size, 1])
            gt_filter = tf.reshape(gt_filter, [BATCH, kernel_size, kernel_size, 1])

            filters = tf.concat([pred_filter, gt_filter], axis=2, name="filters")
            images = tf.concat([pred_image, gt_image], axis=2, name="images")
        tf.summary.image('pred_gt_filters', filters, max_outputs=20)
        tf.summary.image('pred_gt_images', images, max_outputs=20)

        cost = tf.reduce_mean(tf.squared_difference(pred_image, gt_image), name="cost")
        summary.add_moving_summary(cost)
        return cost
Example #21
0
  def __init__(self, sess,
               env, strategy, pred_network, target_network, stat,
               discount, batch_size, learning_rate,
               max_steps, update_repeat, max_episodes):
    self.sess = sess
    self.env = env
    self.strategy = strategy
    self.pred_network = pred_network
    self.target_network = target_network
    self.stat = stat

    self.discount = discount
    self.batch_size = batch_size
    self.learning_rate = learning_rate
    self.action_size = env.action_space.shape[0]

    self.max_steps = max_steps
    self.update_repeat = update_repeat
    self.max_episodes = max_episodes

    self.prestates = []
    self.actions = []
    self.rewards = []
    self.poststates = []
    self.terminals = []

    with tf.name_scope('optimizer'):
      self.target_y = tf.placeholder(tf.float32, [None], name='target_y')
      self.loss = tf.reduce_mean(tf.squared_difference(self.target_y, tf.squeeze(self.pred_network.Q)), name='loss')

      self.optim = tf.train.AdamOptimizer(self.learning_rate).minimize(self.loss)
Example #22
0
    def _build_model(self):
        # placeholders
        self.input = tf.placeholder(
            shape=[None, 84, 84, 4], dtype=tf.float32, name='inputs')
        self.actions = tf.placeholder(
            shape=[None], dtype=tf.int32, name='actions')
        self.next_input = tf.placeholder(
            shape=[None], dtype=tf.float32, name='next_inputs')

        qvals = []
        for i in range(self.k + 1):
            with tf.variable_scope('qnet-{}'.format(i)):
                qvals.append(self._net(self.input, i == 0))

        self.qvals = qvals[0]
        self.target_qvals = tf.stack(qvals[1:])

        trainable_variables = tf.trainable_variables('qnet-0/')
        batch_size = tf.shape(self.input)[0]
        gather_indices = tf.range(batch_size) * self.n_ac + self.actions
        action_q = tf.gather(tf.reshape(self.qvals, [-1]), gather_indices)
        self.loss = tf.reduce_mean(
            tf.squared_difference(self.next_input, action_q))
        self.max_qval = tf.reduce_max(self.qvals)

        self.train_op = self.optimizer.minimize(
            self.loss,
            global_step=tf.train.get_global_step(),
            var_list=trainable_variables)

        self.update_target_op = self._get_update_target_op()
Example #23
0
 def __init__(self):
     self.sess = tf.Session()
     # model
     with tf.name_scope('input'):
         self.state = tf.placeholder(tf.float32, [None, self.STATE_SPACE])
     with tf.name_scope('hidden1'):
         y1 = tf.layers.dense(self.state, units=20, activation=tf.nn.tanh)
     with tf.name_scope('hidden2'):
         y2 = tf.layers.dense(y1, units=10, activation=tf.nn.tanh)
     with tf.name_scope('output'):
         self.q_predict = tf.layers.dense(y2, units=self.ACTION_SPACE)
     # processing
     with tf.name_scope('processing'):
         self.action_mask = tf.placeholder(tf.float32, [None, self.ACTION_SPACE])
         q_current = tf.reduce_sum(tf.multiply(self.q_predict, self.action_mask), axis=1)
         self.q_target = tf.placeholder(tf.float32, [None])
     # loss
     with tf.name_scope('loss'):
         loss = tf.reduce_mean(tf.squared_difference(q_current, self.q_target))
         tf.summary.scalar('loss', loss)
     # optimize
     with tf.name_scope('train'):
         self.train_step = tf.train.AdamOptimizer(self.LR).minimize(loss)
     # tensor board
     self.merged = tf.summary.merge_all()
     self.train_writer = tf.summary.FileWriter('/tmp/qlearning_3/train', self.sess.graph)
     self.test_writer = tf.summary.FileWriter('/tmp/qlearning_3/test')
     # init
     self.sess.run(tf.global_variables_initializer())
Example #24
0
    def rmse(self, vp):

        """
        Root Mean Square Error

        Note that this needs to be evaluated on the rated items only

        Args:
            vp (tensor, float32): inferred output (Network prediction)

        Returns:
            err (tensor, float32): root mean square error

        """

        with tf.name_scope("re"):

            mask = tf.not_equal(self.v, 0)  # selects only the rated items
            n_values = tf.reduce_sum(
                tf.cast(mask, "float32"), axis=1
            )  # number of rated items

            # evaluate the square difference between the inferred and the input data on the rated items
            e = tf.where(
                mask, x=tf.squared_difference(self.v, vp), y=tf.zeros_like(self.v)
            )

            # evaluate the msre
            err = tf.sqrt(
                tf.reduce_mean(tf.div(tf.reduce_sum(e, axis=1), n_values)) / 2
            )

        return err
  def testRWM1DNormal(self):
    """Sampling from the Standard Normal Distribution with adaptation."""
    dtype = np.float32

    with self.test_session(graph=tf.Graph()) as sess:
      target = tfd.Normal(loc=dtype(0), scale=dtype(1))
      samples, _ = tfp.mcmc.sample_chain(
          num_results=1000,
          current_state=dtype(1),
          kernel=tfp.mcmc.RandomWalkMetropolis(
              target.log_prob,
              seed=42),
          num_burnin_steps=500,
          parallel_iterations=1)  # For determinism.

      sample_mean = tf.reduce_mean(samples, axis=0)
      sample_std = tf.sqrt(
          tf.reduce_mean(tf.squared_difference(samples, sample_mean),
                         axis=0))

      sess.graph.finalize()  # No more graph building.

      [sample_mean_, sample_std_] = sess.run([sample_mean, sample_std])

    self.assertAllClose(sample_mean_, 0., atol=0.2, rtol=0.2)
    self.assertAllClose(sample_std_, 1., atol=0.1, rtol=0.1)
    def _build_net(self):
        # ------------------ all inputs ------------------------
        self.s = tf.placeholder(tf.float32, [None, self.n_features], name='s')  # input State
        self.s_ = tf.placeholder(tf.float32, [None, self.n_features], name='s_')  # input Next State
        self.r = tf.placeholder(tf.float32, [None, ], name='r')  # input Reward
        self.a = tf.placeholder(tf.int32, [None, ], name='a')  # input Action

        w_initializer, b_initializer = tf.random_normal_initializer(0., 0.3), tf.constant_initializer(0.1)

        # ------------------ build evaluate_net ------------------
        with tf.variable_scope('eval_net'):
            e1 = tf.layers.dense(self.s, 20, tf.nn.relu, kernel_initializer=w_initializer,
                                 bias_initializer=b_initializer, name='e1')
            self.q_eval = tf.layers.dense(e1, self.n_actions, kernel_initializer=w_initializer,
                                          bias_initializer=b_initializer, name='q')

        # ------------------ build target_net ------------------
        with tf.variable_scope('target_net'):
            t1 = tf.layers.dense(self.s_, 20, tf.nn.relu, kernel_initializer=w_initializer,
                                 bias_initializer=b_initializer, name='t1')
            self.q_next = tf.layers.dense(t1, self.n_actions, kernel_initializer=w_initializer,
                                          bias_initializer=b_initializer, name='t2')

        with tf.variable_scope('q_target'):
            q_target = self.r + self.gamma * tf.reduce_max(self.q_next, axis=1, name='Qmax_s_')    # shape=(None, )
            self.q_target = tf.stop_gradient(q_target)
        with tf.variable_scope('q_eval'):
            a_indices = tf.stack([tf.range(tf.shape(self.a)[0], dtype=tf.int32), self.a], axis=1)
            self.q_eval_wrt_a = tf.gather_nd(params=self.q_eval, indices=a_indices)    # shape=(None, )
        with tf.variable_scope('loss'):
            self.loss = tf.reduce_mean(tf.squared_difference(self.q_target, self.q_eval_wrt_a, name='TD_error'))
        with tf.variable_scope('train'):
            self._train_op = tf.train.RMSPropOptimizer(self.lr).minimize(self.loss)
Example #27
0
    def build_net(self):
        self.s = tf.placeholder(tf.float32, [None, self.n_features])
        self.s_ = tf.placeholder(tf.float32, [None, self.n_features])
        self.r = tf.placeholder(tf.float32, [None, ])
        self.a = tf.placeholder(tf.int32, [None, ])
 
        w_initializer = tf.random_normal_initializer(0., 0.3)
        b_initializer = tf.constant_initializer(0.1)
        # q_eval网络架构,输入状态属性,输出4种动作
        with tf.variable_scope('eval_net'):
            eval_layer = tf.layers.dense(self.s, 20, tf.nn.relu, kernel_initializer=w_initializer,
                                         bias_initializer=b_initializer, name='eval_layer')
            self.q_eval = tf.layers.dense(eval_layer, self.n_actions, kernel_initializer=w_initializer,
                                          bias_initializer=b_initializer, name='output_layer1')
        with tf.variable_scope('target_net'):
            target_layer = tf.layers.dense(self.s_, 20, tf.nn.relu, kernel_initializer=w_initializer,
                                           bias_initializer=b_initializer, name='target_layer')
            self.q_next = tf.layers.dense(target_layer, self.n_actions, kernel_initializer=w_initializer,
                                          bias_initializer=b_initializer, name='output_layer2')
        with tf.variable_scope('q_target'):
            # 计算期望价值,并使用stop_gradient函数将其不计算梯度,也就是当做常数对待
            self.q_target = tf.stop_gradient(self.r + self.gamma * tf.reduce_max(self.q_next, axis=1))
        with tf.variable_scope('q_eval'):
            # 将a的值对应起来,
            a_indices = tf.stack([tf.range(tf.shape(self.a)[0]), self.a], axis=1)
            self.q_eval_a = tf.gather_nd(params=self.q_eval, indices=a_indices)
        with tf.variable_scope('loss'):
            self.loss = tf.reduce_mean(tf.squared_difference(self.q_target, self.q_eval_a))
        with tf.variable_scope('train'):
            self.train_op = tf.train.RMSPropOptimizer(self.lr).minimize(self.loss)
    def __init__(self, sess, state_dim, action_dim, learning_rate, gamma, t_replace_iter, a, a_):
        self.sess = sess
        self.s_dim = state_dim
        self.a_dim = action_dim
        self.lr = learning_rate
        self.gamma = gamma
        self.t_replace_iter = t_replace_iter
        self.t_replace_counter = 0

        with tf.variable_scope('Critic'):
            # Input (s, a), output q
            self.a = a
            self.q = self._build_net(S, self.a, 'eval_net', trainable=True)

            # Input (s_, a_), output q_ for q_target
            self.q_ = self._build_net(S_, a_, 'target_net', trainable=False)    # target_q is based on a_ from Actor's target_net

            self.e_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='Critic/eval_net')
            self.t_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope='Critic/target_net')

        with tf.variable_scope('target_q'):
            self.target_q = R + self.gamma * self.q_

        with tf.variable_scope('TD_error'):
            self.loss = tf.reduce_mean(tf.squared_difference(self.target_q, self.q))

        with tf.variable_scope('C_train'):
            self.train_op = tf.train.AdamOptimizer(self.lr).minimize(self.loss)

        with tf.variable_scope('a_grad'):
            self.a_grads = tf.gradients(self.q, a)[0]   # tensor of gradients of each sample (None, a_dim)
    def _test_grads_images(self,
                           interpolation='linear',
                           boundary='replicate',
                           ndim=2):
        if ndim == 2:
            test_image, input_shape = get_multiple_2d_images()
            test_target, target_shape = get_multiple_2d_targets()
            identity_affine = [[1., 0., 0., 0., 1., 0.]] * 4
        else:
            test_image, input_shape = get_multiple_3d_images()
            test_target, target_shape = get_multiple_3d_targets()
            identity_affine = [[1., 0., 0., 0., 1., 0.,
                                1., 0., 0., 0., 1., 0.]] * 4
        affine_var = tf.get_variable('affine', initializer=identity_affine)
        grid = AffineGridWarperLayer(source_shape=input_shape[1:-1],
                                     output_shape=target_shape[1:-1],
                                     constraints=None)
        warp_coords = grid(affine_var)
        resampler = ResamplerLayer(interpolation, boundary=boundary)
        new_image = resampler(tf.constant(test_image, dtype=tf.float32),
                              warp_coords)

        diff = tf.reduce_mean(tf.squared_difference(
            new_image, tf.constant(test_target, dtype=tf.float32)))
        optimiser = tf.train.AdagradOptimizer(0.01)
        grads = optimiser.compute_gradients(diff)
        opt = optimiser.apply_gradients(grads)
        with self.test_session() as sess:
            sess.run(tf.global_variables_initializer())
            init_val, affine_val = sess.run([diff, affine_var])
            for _ in range(5):
                _, diff_val, affine_val = sess.run([opt, diff, affine_var])
                print('{}, {}'.format(diff_val, affine_val[0]))
            self.assertGreater(init_val, diff_val)
def discretize_centroids(x, levels, centroids, thermometer=False):
  """Discretize input into levels using custom centroids.

  Args:
    x: Input tensor to discretize, assumed to be between (0, 1).
    levels: Number of levels to discretize into.
    centroids: Custom centroids into which the input is to be discretized.
    thermometer: Whether to encode the discretized tensor in thermometer encoding
        (Default: False).

  Returns:
    Discretized version of x of shape [-1, height, width, channels * levels]
    using supplied centroids.
  """
  x_stacked = tf.stack(levels * [x], axis=-1)
  dist = tf.to_float(tf.squared_difference(x_stacked, centroids))
  idx = tf.argmin(dist, axis=-1)
  one_hot = tf.one_hot(idx, depth=levels, on_value=1., off_value=0.)

  # Check to see if we are encoding in thermometer
  discretized_x = one_hot
  if thermometer:
    discretized_x = one_hot_to_thermometer(one_hot, levels, flattened=False)

  # Reshape x to [-1, height, width, channels * levels]
  discretized_x = flatten_last(discretized_x)
  return discretized_x
Example #31
0
t_1 = tf.placeholder(tf.float32, name="t_1", shape=[None])


# Construct model (Value Iteration Network)
q_net = ir_Block_coord("q", X, Adj, Dist, Sup, Coord, S,Trainable, config)

logits, nn = q_net.build_model()
  

# Variables for the loss fucntion
arg_idx = tf.cast(tf.argmax(logits, -1), tf.int32)
t = tf.gather(logits[0], arg_idx)

# Define the loss function and optimizer
loss = tf.squared_difference(t, t_1)



optimizer = tf.train.AdamOptimizer(learning_rate=config.lr)##.minimize(loss)
# optimizer = tf.train.RMSPropOptimizer(learning_rate=config.lr, epsilon=1e-6, centered=True)

vars_ = tf.trainable_variables()
grads, _ = tf.clip_by_global_norm(tf.gradients(loss, vars_), 2.0)
optimize = optimizer.apply_gradients(zip(grads, vars_))

# Initializing the variables
init = tf.global_variables_initializer()
saver = tf.train.Saver()

# Preprocess the irregular graph data
Example #32
0
hidden_1 = tf.nn.relu(tf.add(tf.matmul(x, w_hidden_1),
                             bias_hidden_1))
hidden_2 = tf.nn.relu(tf.add(tf.matmul(hidden_1, w_hidden_2),
                             bias_hidden_2))
hidden_3 = tf.nn.relu(tf.add(tf.matmul(hidden_2, w_hidden_3),
                             bias_hidden_3))
hidden_4 = tf.nn.relu(tf.add(tf.matmul(hidden_3, w_hidden_4),
                             bias_hidden_4))

# output layer (must be transposed)

out = tf.transpose(tf.add(tf.matmul(hidden_4, w_out),
                          bias_out))

# cost function
mse = tf.reduce_mean(tf.squared_difference(out, y))

# optimizer
opt = tf.train.AdamOptimizer().minimize(mse)

print("3");

# make session
net = tf.Session()

# run initializer
net.run(tf.global_variables_initializer())

print("2");

# setup interactive plot
Example #33
0
def forecast(train_data_main, validation_data, test_data):
	# Parameters
	learning_rate = 0.1
	training_epochs = 15
	timesteps = 30
	batch_size = 1000
	display_step = 1

	p = 0.95
	B = 10 


	batch_xs = []
	batch_exts = []
	batch_ys = []
	for [batch_x, batch_y, batch_ext] in sequence_build(train_data_main, timesteps = timesteps):
		batch_xs.append(batch_x)
		batch_ys.append(batch_y)
		batch_exts.append(batch_ext)
	print(np.array(batch_xs).shape)
	print(np.array(batch_ys).shape)
	print(np.array(batch_exts).shape)
	train_size = np.array(batch_xs).shape[0]

	valid_batch_xs = []
	valid_batch_exts = []
	valid_batch_ys = []
	for [batch_x, batch_y, batch_ext] in sequence_build(validation_data, timesteps = timesteps):
		valid_batch_xs.append(batch_x)
		valid_batch_ys.append(batch_y)
		valid_batch_exts.append(batch_ext)
	print(np.array(valid_batch_xs).shape)
	print(np.array(valid_batch_ys).shape)
	print(np.array(valid_batch_exts).shape)
	valid_size = np.array(valid_batch_xs).shape[0]

	test_batch_xs = []
	test_batch_exts = []
	test_batch_ys = []
	for [batch_x, batch_y, batch_ext] in sequence_build(test_data, timesteps = timesteps):
		test_batch_xs.append(batch_x)
		test_batch_ys.append(batch_y)
		test_batch_exts.append(batch_ext)
	print(np.array(test_batch_xs).shape)
	print(np.array(test_batch_ys).shape)
	print(np.array(test_batch_exts).shape)

	#print(np.array(test_tokens).shape)
	#print(np.array(test_batch_ys).shape)
	#print(np.array(test_batch_exts).shape)

	size_x = np.array(batch_xs).shape
	n_input = size_x[-1]
	size_ext = np.array(batch_exts).shape
	n_ext = size_ext[-1]

	n_hidden = 25
	n_hidden_0 = n_ext + n_hidden
	n_hidden_1 = 30 # 1st layer number of neurons
	n_hidden_2 = 30 # 2nd layer number of neurons
# tf Graph input
	X = tf.placeholder("float", [None, timesteps, n_input])
	STATE = tf.placeholder("float", [None, n_hidden])	
	EXT = tf.placeholder("float", [None, n_ext])
	Y = tf.placeholder("float", [None, n_input])

	# Store layers weight & bias
	weights = {
		'h1': tf.Variable(tf.random_normal([n_hidden_0, n_hidden_1])),
    		'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])),
    		'out': tf.Variable(tf.random_normal([n_hidden_2, n_input]))
	}
	biases = {
   		'b1': tf.Variable(tf.random_normal([n_hidden_1])),
    		'b2': tf.Variable(tf.random_normal([n_hidden_2])),
    		'out': tf.Variable(tf.random_normal([n_input]))
	}


	# Construct model

	
	MLP_in = tf.concat([STATE, EXT], axis = 1)
	
	#MLP_out = tf.tanh(multilayer_perceptron(MLP_in, weights, biases))
	MLP_out = multilayer_perceptron(MLP_in, weights, biases)

	'''
	dropout_2 = tf.nn.dropout(MLP_out, p)
	for i in range(B):
		dropout_2 = tf.add(dropout_2, tf.nn.dropout(MLP_out, p))
		error = tf.add(error, tf.pow(Y - tf.nn.dropout(MLP_out, p), 2))
	dropout_mean_2 = dropout_2/B
	'''
	dropouts = []
	sqr_errors = []
	for i in range(B):
		dropouts.append(tf.nn.dropout(MLP_out, p))
		sqr_errors.append(tf.square(Y - dropouts[-1]))

	logits = tf.reduce_mean(tf.stack(dropouts))
	sqr_error = tf.reduce_mean(tf.reduce_mean(tf.stack(sqr_errors)))
	loss = tf.reduce_mean(Y - logits)
	sqr_loss = tf.reduce_mean(tf.square(Y - logits))
	# Define loss and optimizer
	#loss_op = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=Y))
	loss_op = tf.reduce_mean(tf.squared_difference(Y, logits))
	#loss_op = tf.reduce_mean(tf.reduce_sum(tf.sqrt(logits - Y), -1))
	optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
	train_op = optimizer.minimize(loss_op)
	# Initializing the variables
	saver = tf.train.import_meta_graph('encoder.meta')
	init = tf.global_variables_initializer()

	with tf.Session() as sess:
		saver.restore(sess, tf.train.latest_checkpoint('./'))
    		sess.run(init)
		graph = tf.get_default_graph()
		X_enc = graph.get_tensor_by_name("X_enc:0")
		Y_enc = graph.get_tensor_by_name("Y_enc:0")	
    		# Training cycle
    		for epoch in range(training_epochs):
      			avg_cost = 0.
			avg_error = 0.
        		total_batch = int(len(batch_xs)/batch_size)
        		# Loop over all batches
        		for i in range(total_batch):
            			#batch_x, batch_y = mnist.train.next_batch(batch_size)
				batch_x = np.array(batch_xs)[i * batch_size: min(i * batch_size + batch_size, len(batch_xs))]
				batch_y = np.array(batch_ys)[i * batch_size: min(i * batch_size + batch_size, len(batch_xs))]
				batch_ext = np.array(batch_exts)[i * batch_size: min(i * batch_size + batch_size, len(batch_xs))]
            			# Run optimization op (backprop) and cost op (to get loss value)
				#sqr_err = graph.get_tensor_by_name("mean_sqr_error:0")
				#print(sqr_err.eval({X_enc:batch_x, Y_enc:batch_y}))

				state = graph.get_tensor_by_name("mean_states:0")
				batch_state = np.mean(state.eval({X_enc:batch_x, Y_enc: batch_y}), axis = 0)
            			# Compute average loss
            			_, l, c = sess.run([train_op, sqr_loss, sqr_error], feed_dict={
										STATE: batch_state,
                                                            			Y: batch_y,
										EXT: batch_ext})
            			avg_cost += l / total_batch
				avg_error += c / total_batch
        			# Display logs per epoch step
        		if epoch % display_step == 0:
            			print("Epoch:", '%04d' % (epoch+1), "squre error ={:.9f}".format(math.sqrt(avg_error)), "cost={:.9f}".format(avg_cost))
            			#print("Epoch:", '%04d' % (epoch+1), "cost={:.9f}".format(l))
    		print("Optimization Finished!")
	
		valid_batch_state = np.mean(state.eval({X_enc: valid_batch_xs, Y_enc: valid_batch_ys}), axis = 0)
		error_val = sqr_error.eval({STATE: valid_batch_state, Y: valid_batch_ys, EXT: valid_batch_exts})
		#error_val = math.sqrt(error_val)
		print("Validation loss %0.4f" % error_val)

		output_file = open('logfile', 'a')
		output_file.write(str(train_data_main) + ':' + str(validation_data) + ':' + str(test_data) + '\n')
		output_file.write("validation_error:" + str(error_val) + '\n')

		test_results = []
		test_loss = []
		for i in range(len(test_batch_xs)):
			test_batch_state = np.mean(state.eval({X_enc: test_batch_xs, Y_enc: test_batch_ys}), axis = 0)
			error_val = sqr_error.eval({STATE: test_batch_state, Y: test_batch_ys, EXT: test_batch_exts})
			pred = logits.eval({STATE: [test_batch_state[i]], 
					    Y: [test_batch_ys[i]],
					    EXT: [test_batch_exts[i]]})
			output_file.write(str(test_batch_ys[i]) + ':' +  str(pred) + ':' + str(test_batch_ys[i] - pred) + '\n')
			test_loss.append(test_batch_ys[i] - pred)	
			test_results.append(pred)
		print("Test loss %0.4f" % np.mean(np.array(test_loss)))
		output_file.close()
Example #34
0
ones = tf.ones(shape=[3, 3])

lowThresh = tf.Variable(minValue)
highThresh = tf.Variable(maxValue)

firstLayer = tf.maximum(x, lowThresh)
secondLayer = tf.minimum(firstLayer, highThresh)
# firstLayer = tf.maximum(x-lowThresh,zeros)
# secondLayer = tf.maximum(highThresh-x,zeros)

# Need to convert it into binary form
# predict = tf.multiply(firstLayer, secondLayer)
training = tf.placeholder(tf.bool)
predict = unet.make_unet(x, training, unet.read_flags())

cost = tf.reduce_mean(tf.squared_difference(predict, y))
learningrate = 0.01
train = tf.train.AdamOptimizer(learningrate).minimize(cost)

init = tf.global_variables_initializer()
numepochs = 1000
C = 0.0
with tf.Session() as sess:
    sess.run(init)
    for i in xrange(numepochs):
        for j in 109 / BATCH_SIZE:
            inputValue, outputValue = data.data_in_batches().next()
            print("jfiejfierfukjferf: " + str(inputValue.shape))
            _, lowValue, highValue, c = sess.run(
                [train, lowThresh, highThresh, cost],
                feed_dict={
Example #35
0
    def __init__(
        self,
        act_dim,
        obs_dim,
        lr_actor,
        lr_value,
        gamma,
        tau,
        alpha=0.2,
    ):
        self.act_dim = act_dim
        self.obs_dim = obs_dim
        self.lr_actor = lr_actor
        self.lr_value = lr_value
        self.gamma = gamma
        self.tau = tau
        self.name = 'soccer'

        self.OBS0 = tf.placeholder(tf.float32, [None, self.obs_dim],
                                   name=self.name + "observations0")
        self.OBS1 = tf.placeholder(tf.float32, [None, self.obs_dim],
                                   name=self.name + "observations1")
        self.ACT = tf.placeholder(tf.float32, [None, self.act_dim],
                                  name=self.name + "action")
        self.RWD = tf.placeholder(tf.float32, [
            None,
        ],
                                  name=self.name + "reward")
        self.DONE = tf.placeholder(tf.float32, [
            None,
        ],
                                   name=self.name + "done")
        self.policy_loss = tf.placeholder(tf.float32, [None, 1],
                                          name=self.name + "policy_loss")
        self.q_value1_loss = tf.placeholder(tf.float32, [None, 1],
                                            name=self.name + "q_value1_loss")
        self.q_value2_loss = tf.placeholder(tf.float32, [None, 1],
                                            name=self.name + "q_value2_loss")
        self.value_loss = tf.placeholder(tf.float32, [None, 1],
                                         name=self.name + "value_loss")
        self.total_value_loss = tf.placeholder(tf.float32, [None, 1],
                                               name=self.name +
                                               "total_value_loss")
        self.ST_RWD = tf.placeholder(tf.float32, [], name=self.name + 'ST_RWD')
        self.EP_RWD = tf.placeholder(tf.float32, [], name=self.name + 'EP_RWD')

        policy = ActorNetwork(self.act_dim, self.name + 'Actor')
        q_value_net_1 = QValueNetwork(self.name + 'Q_value1')
        q_value_net_2 = QValueNetwork(self.name + 'Q_value2')
        value_net = ValueNetwork(self.name + 'Value')
        target_value_net = ValueNetwork(self.name + 'Target_Value')

        self.replay_buffer = ReplayBuffer(capacity=int(1e6))

        mu, self.pi, logp_pi = policy.evaluate(self.OBS0)

        q_value1 = q_value_net_1.get_q_value(self.OBS0, self.ACT)
        q_value1_pi = q_value_net_1.get_q_value(self.OBS0, self.pi, reuse=True)
        q_value2 = q_value_net_2.get_q_value(self.OBS0, self.ACT)
        q_value2_pi = q_value_net_2.get_q_value(self.OBS0, self.pi, reuse=True)
        value = value_net.get_value(self.OBS0)
        target_value = target_value_net.get_value(self.OBS1)

        min_q_value_pi = tf.minimum(q_value1_pi, q_value2_pi)
        next_q_value = tf.stop_gradient(self.RWD + self.gamma *
                                        (1 - self.DONE) * target_value)
        next_value = tf.stop_gradient(min_q_value_pi - alpha * logp_pi)

        self.policy_loss = tf.reduce_mean(alpha * logp_pi - q_value1_pi)
        self.q_value1_loss = tf.reduce_mean(
            tf.squared_difference(next_q_value, q_value1))
        self.q_value2_loss = tf.reduce_mean(
            tf.squared_difference(next_q_value, q_value2))
        self.value_loss = tf.reduce_mean(
            tf.squared_difference(next_value, value))
        self.total_value_loss = self.q_value1_loss + self.q_value2_loss + self.value_loss

        actor_optimizer = tf.train.AdamOptimizer(learning_rate=self.lr_actor)
        actor_train_op = actor_optimizer.minimize(
            self.policy_loss,
            var_list=tf.global_variables(self.name + 'Actor'))
        value_optimizer = tf.train.AdamOptimizer(learning_rate=self.lr_value)
        value_params = tf.global_variables(
            self.name + 'Q_value') + tf.global_variables(self.name + 'Value')

        with tf.control_dependencies([actor_train_op]):
            value_train_op = value_optimizer.minimize(self.total_value_loss,
                                                      var_list=value_params)
        with tf.control_dependencies([value_train_op]):
            self.target_update = [
                tf.assign(tv, self.tau * tv + (1 - self.tau) * v)
                for v, tv in zip(
                    tf.global_variables(self.name + 'Value'),
                    tf.global_variables(self.name + 'Target_Value'))
            ]

        target_init = [
            tf.assign(tv, v)
            for v, tv in zip(tf.global_variables(self.name + 'Value'),
                             tf.global_variables(self.name + 'Target_Value'))
        ]

        gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.6)
        self.sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
        # self.sess = tf.Session()
        tf.summary.scalar(self.name + 'policy_loss', self.policy_loss)
        tf.summary.scalar(self.name + 'q_value1_loss', self.q_value1_loss)
        tf.summary.scalar(self.name + 'q_value2_loss', self.q_value2_loss)
        tf.summary.scalar(self.name + 'value_loss', self.value_loss)
        tf.summary.scalar(self.name + 'total_value_loss',
                          self.total_value_loss)
        tf.summary.scalar(self.name + 'ST_RWD', self.ST_RWD)
        tf.summary.scalar(self.name + 'EP_RWD', self.EP_RWD)
        self.merged = tf.summary.merge_all()
        self.writer = tf.summary.FileWriter(
            './../logs/' + NAME + '/' + self.name + '/', self.sess.graph)
        self.saver = tf.train.Saver()
        self.path = './' + NAME + self.name
        if LOAD:
            self.saver.restore(self.sess,
                               tf.train.latest_checkpoint(self.path))
        else:
            self.sess.run(tf.global_variables_initializer())
            self.sess.run(target_init)
Example #36
0
    def _build_model(self):
        """
        Builds the Tensorflow graph.
        """

        # Placeholders for our input
        # Our input are 4 RGB frames of shape 160, 160 each
        self.X_pl = tf.placeholder(shape=[None, 84, 84, 4],
                                   dtype=tf.uint8,
                                   name="X")
        # The TD target value
        self.y_pl = tf.placeholder(shape=[None], dtype=tf.float32, name="y")
        # Integer id of which action was selected
        self.actions_pl = tf.placeholder(shape=[None],
                                         dtype=tf.int32,
                                         name="actions")

        X = tf.to_float(self.X_pl) / 255.0
        batch_size = tf.shape(self.X_pl)[0]

        # Three convolutional layers
        conv1 = tf.contrib.layers.conv2d(X, 32, 8, 4, activation_fn=tf.nn.relu)
        conv2 = tf.contrib.layers.conv2d(conv1,
                                         64,
                                         4,
                                         2,
                                         activation_fn=tf.nn.relu)
        conv3 = tf.contrib.layers.conv2d(conv2,
                                         64,
                                         3,
                                         1,
                                         activation_fn=tf.nn.relu)

        # Fully connected layers
        flattened = tf.contrib.layers.flatten(conv3)
        fc1 = tf.contrib.layers.fully_connected(flattened, 512)
        self.predictions = tf.contrib.layers.fully_connected(
            fc1, len(VALID_ACTIONS))

        # Get the predictions for the chosen actions only
        gather_indices = tf.range(batch_size) * tf.shape(
            self.predictions)[1] + self.actions_pl
        self.action_predictions = tf.gather(tf.reshape(self.predictions, [-1]),
                                            gather_indices)

        # Calculate the loss
        self.losses = tf.squared_difference(self.y_pl, self.action_predictions)
        self.loss = tf.reduce_mean(self.losses)

        # Optimizer Parameters from original paper
        self.optimizer = tf.train.RMSPropOptimizer(0.00025, 0.99, 0.0, 1e-6)
        self.train_op = self.optimizer.minimize(
            self.loss, global_step=tf.contrib.framework.get_global_step())

        # Summaries for Tensorboard
        self.summaries = tf.summary.merge([
            tf.summary.scalar("loss", self.loss),
            tf.summary.histogram("loss_hist", self.losses),
            tf.summary.histogram("q_values_hist", self.predictions),
            tf.summary.scalar("max_q_value", tf.reduce_max(self.predictions))
        ])
Example #37
0
    def _build_net(self):
        tf.reset_default_graph()
        # -------------- 创建 eval 神经网络, 及时提升参数 --------------
        #0 * 2,这里是网络的输入,用来接收 observation
        self.s = tf.placeholder(tf.float32, [None, self.n_features],
                                name='s')  #创建一个变量,只指定类型和大小,
        #用来接收 q_target 的值, 这个之后会通过计算得到
        self.q_target = tf.placeholder(tf.float32, [None, self.n_actions],
                                       name='Q_target')

        with tf.variable_scope(
                'eval_net'):  #variable_scope是给get_variable()创建的变量的名字加前缀
            # 是在更新 target_net 参数时会用到
            c_names = [
                'eval_net_params', tf.GraphKeys.GLOBAL_VARIABLES
            ]  #[collections_names],功能相当于一个变量集合,指定某些变量属于哪个集合,默认是get_variable新建的变量都会加到tf.GraphKeys.GLOBAL_VARIABLES中区.
            #第一层的神经元数量
            n_l1 = 10
            #w参数初始化方法
            w_initializer = tf.random_normal_initializer(0., 0.3)
            #偏置项b的初始化方法
            b_initializer = tf.constant_initializer(0.1)
            # eval_net 的第一层. collections 是在更新 target_net 参数时会用到
            with tf.variable_scope('l1'):
                # 2 * 10
                w1 = tf.get_variable('w1', [self.n_features, n_l1],
                                     initializer=w_initializer,
                                     collections=c_names)
                # 1 * 10
                b1 = tf.get_variable('b1', [1, n_l1],
                                     initializer=b_initializer,
                                     collections=c_names)
                # relu (s * w1 + b1) 第一层隐层的输出 1 * 10
                l1 = tf.nn.relu(tf.matmul(self.s, w1) + b1)

            # eval_net 的第二层. collections 是在更新 target_net 参数时会用到
            with tf.variable_scope('l2'):
                #10*4
                w2 = tf.get_variable('w2', [n_l1, self.n_actions],
                                     initializer=w_initializer,
                                     collections=c_names)
                #1*4
                b2 = tf.get_variable('b2', [1, self.n_actions],
                                     initializer=b_initializer,
                                     collections=c_names)
                #估计的Q(s,a)值 l1 * w2 + b2, 大小为1*4
                self.q_eval = tf.matmul(l1, w2) + b2
        #定义损失为均方误差
        with tf.variable_scope('loss'):
            self.loss = tf.reduce_mean(
                tf.squared_difference(
                    self.q_target,
                    self.q_eval))  # q_target外面算好传进来,它跟q_eval只有在max action上的值不同
        #定义梯度优化方式为RMSPro,学习率为self.lr,最优化目标为最小化self.loss
        with tf.variable_scope('train'):
            self._train_op = tf.train.RMSPropOptimizer(self.lr).minimize(
                self.loss)

        # ---------------- 创建 target 神经网络, 提供 target Q ---------------------
        self.s_ = tf.placeholder(tf.float32, [None, self.n_features],
                                 name='s_')  # 接收下个 observation
        with tf.variable_scope('target_net'):
            # c_names(collections_names) 是在更新 target_net 参数时会用到
            c_names = ['target_net_params', tf.GraphKeys.GLOBAL_VARIABLES]

            # target_net 的第一层. collections 是在更新 target_net 参数时会用到
            with tf.variable_scope('l1'):
                w1 = tf.get_variable('w1', [self.n_features, n_l1],
                                     initializer=w_initializer,
                                     collections=c_names)
                b1 = tf.get_variable('b1', [1, n_l1],
                                     initializer=b_initializer,
                                     collections=c_names)
                l1 = tf.nn.relu(tf.matmul(self.s_, w1) + b1)

            # target_net 的第二层. collections 是在更新 target_net 参数时会用到
            with tf.variable_scope('l2'):
                w2 = tf.get_variable('w2', [n_l1, self.n_actions],
                                     initializer=w_initializer,
                                     collections=c_names)
                b2 = tf.get_variable('b2', [1, self.n_actions],
                                     initializer=b_initializer,
                                     collections=c_names)
                self.q_next = tf.matmul(l1, w2) + b2
Example #38
0
    def _build_net(self):
        def weight_variable(name, shape):
            return tf.get_variable(name,
                                   shape,
                                   initializer=w_initializer,
                                   collections=c_names)

        def bias_variable(name, shape):
            return tf.get_variable(name,
                                   shape,
                                   initializer=b_initializer,
                                   collections=c_names)

        def conv2d(x, W):
            return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')

        # ------------------ build evaluate_net ------------------
        self.s = tf.placeholder(tf.float32, [None, self.n_features],
                                name='s')  # input
        self.q_target = tf.placeholder(tf.float32, [None, self.n_actions],
                                       name='Q_target')  # for calculating loss
        self.keep_prob = tf.placeholder(tf.float32)

        with tf.variable_scope('eval_net', reuse=tf.AUTO_REUSE):
            # c_names(collections_names) are the collections to store variables
            c_names, w_initializer, b_initializer = \
                ['eval_net_params', tf.GraphKeys.GLOBAL_VARIABLES], \
                tf.random_normal_initializer(0.0, 0.1), tf.constant_initializer((-0.01, 0.01))  # config of layers

            # first layer. conv3-32
            w1 = weight_variable('w1', [3, 3, 1, 32])
            b1 = bias_variable('b1', [32])
            # second layer. conv3-32
            w2 = weight_variable('w2', [3, 3, 32, 32])
            b2 = bias_variable('b2', [32])

            # second layer. conv3-64
            w3 = weight_variable('w3', [3, 3, 32, 64])
            b3 = bias_variable('b3', [64])

            # fourth layer. collapse into 1 row 64 channels
            w4 = weight_variable('w4', [self.state_shape[0], 1, 64, 64])
            b4 = bias_variable('b4', [64])

            # fifth layer. conv3-128
            w5 = weight_variable('w5', [1, 3, 64, 128])
            b5 = bias_variable('b5', [128])

            # sixth layer. conv1-128
            w6 = weight_variable('w6', [1, 1, 128, 128])
            b6 = bias_variable('b6', [128])

            # seventh layer. conv3-128
            w7 = weight_variable('w7', [1, 3, 128, 128])
            b7 = bias_variable('b7', [128])

            # eighth layer. FC - 128
            w8 = weight_variable('w8', [128 * self.state_shape[1], 128])
            b8 = bias_variable('b8', [128])

            # nineth layer. FC - 512
            w9 = weight_variable('w9', [128, 512])
            b9 = bias_variable('b9', [512])

            # output layer. FC - 1
            w10 = weight_variable('w10', [512, 1])
            # b10 = bias_variable('b10', [1])
            b10 = tf.Variable(tf.constant(0, shape=[1], dtype=tf.float32),
                              name='b10',
                              collections=c_names)

            s = tf.reshape(self.s, [-1, self.n_actions, self.n_state])
            input_series = tf.unstack(s, axis=1)

            predictions_series = []
            for current_input in input_series:
                s = tf.reshape(
                    current_input,
                    [-1, self.state_shape[0], self.state_shape[1], 1])

                l1 = tf.nn.relu(conv2d(s, w1) + b1)
                l2 = tf.nn.relu(conv2d(l1, w2) + b2)
                l3 = tf.nn.relu(conv2d(l2, w3) + b3)
                l4 = tf.nn.relu(
                    tf.nn.conv2d(l3, w4, strides=[1, 1, 1, 1], padding='VALID')
                    + b4)
                l5 = tf.nn.relu(conv2d(l4, w5) + b5)
                l6 = tf.nn.relu(conv2d(l5, w6) + b6)
                l7 = tf.nn.relu(conv2d(l6, w7) + b7)
                l7_flat = tf.reshape(l7, [-1, 128 * self.state_shape[1]])
                l8 = tf.nn.relu(tf.matmul(l7_flat, w8) + b8)
                l8_drop = tf.nn.dropout(l8, self.keep_prob)
                l9 = tf.nn.relu(tf.matmul(l8_drop, w9) + b9)
                l9_drop = tf.nn.dropout(l9, self.keep_prob)
                l10 = tf.matmul(l9_drop, w10) + b10
                predictions_series.append(l10)

            self.q_eval = tf.reshape(tf.stack(predictions_series, axis=1),
                                     [-1, self.n_actions])

        with tf.variable_scope('loss', reuse=tf.AUTO_REUSE):
            self.loss = tf.reduce_mean(
                tf.squared_difference(self.q_target, self.q_eval))
        with tf.variable_scope('train', reuse=tf.AUTO_REUSE):
            self._train_op = tf.train.RMSPropOptimizer(self.lr).minimize(
                self.loss)

        # ------------------ build target_net ------------------
        self.s_ = tf.placeholder(tf.float32, [None, self.n_features],
                                 name='s_')  # input

        with tf.variable_scope('target_net', reuse=tf.AUTO_REUSE):
            # c_names(collections_names) are the collections to store variables
            c_names = ['target_net_params', tf.GraphKeys.GLOBAL_VARIABLES]

            # first layer. conv3-32
            w1 = weight_variable('w1', [3, 3, 1, 32])
            b1 = bias_variable('b1', [32])
            # second layer. conv3-32
            w2 = weight_variable('w2', [3, 3, 32, 32])
            b2 = bias_variable('b2', [32])

            # second layer. conv3-64
            w3 = weight_variable('w3', [3, 3, 32, 64])
            b3 = bias_variable('b3', [64])

            # fourth layer. collapse into 1 row 64 channels
            w4 = weight_variable('w4', [self.state_shape[0], 1, 64, 64])
            b4 = bias_variable('b4', [64])

            # fifth layer. conv3-128
            w5 = weight_variable('w5', [1, 3, 64, 128])
            b5 = bias_variable('b5', [128])

            # sixth layer. conv1-128
            w6 = weight_variable('w6', [1, 1, 128, 128])
            b6 = bias_variable('b6', [128])

            # seventh layer. conv3-128
            w7 = weight_variable('w7', [1, 3, 128, 128])
            b7 = bias_variable('b7', [128])

            # eighth layer. FC - 128
            w8 = weight_variable('w8', [128 * self.state_shape[1], 128])
            b8 = bias_variable('b8', [128])

            # nineth layer. FC - 512
            w9 = weight_variable('w9', [128, 512])
            b9 = bias_variable('b9', [512])

            # output layer. FC - actions
            w10 = weight_variable('w10', [512, 1])
            # b10 = bias_variable('b10', [1])
            b10 = tf.Variable(tf.constant(0, shape=[1], dtype=tf.float32),
                              name='b10',
                              collections=c_names)

            s = tf.reshape(self.s_, [-1, self.n_actions, self.n_state])
            input_series = tf.unstack(s, axis=1)

            predictions_series = []
            for current_input in input_series:
                s = tf.reshape(
                    current_input,
                    [-1, self.state_shape[0], self.state_shape[1], 1])

                l1 = tf.nn.relu(conv2d(s, w1) + b1)
                l2 = tf.nn.relu(conv2d(l1, w2) + b2)
                l3 = tf.nn.relu(conv2d(l2, w3) + b3)
                l4 = tf.nn.relu(
                    tf.nn.conv2d(l3, w4, strides=[1, 1, 1, 1], padding='VALID')
                    + b4)
                l5 = tf.nn.relu(conv2d(l4, w5) + b5)
                l6 = tf.nn.relu(conv2d(l5, w6) + b6)
                l7 = tf.nn.relu(conv2d(l6, w7) + b7)
                l7_flat = tf.reshape(l7, [-1, 128 * self.state_shape[1]])
                l8 = tf.nn.relu(tf.matmul(l7_flat, w8) + b8)
                l8_drop = tf.nn.dropout(l8, self.keep_prob)
                l9 = tf.nn.relu(tf.matmul(l8_drop, w9) + b9)
                l9_drop = tf.nn.dropout(l9, self.keep_prob)
                l10 = tf.matmul(l9_drop, w10) + b10
                predictions_series.append(l10)

            self.q_next = tf.reshape(tf.stack(predictions_series, axis=1),
                                     [-1, self.n_actions])
Example #39
0
    def __init__(self, sess, action_dim, state_dim, input_length, a_lr, c_lr, agent_num, log):
        self.sess = sess
        self.action_dim = action_dim
        self.state_dim = state_dim
        self.input_length = input_length
        self.agent_num = agent_num
        self.log = log
        self.online_inputs = tf.placeholder(tf.float32, (None, self.input_length, state_dim))
        self.target_inputs = tf.placeholder(tf.float32, (None, self.input_length, state_dim))
        self.q = self.create_policy_network(self.online_inputs, 'online')
        self.target_q = self.create_policy_network(self.target_inputs, 'target')
        self.actions = tf.placeholder(tf.int32, None)
        self.onehot_action = tf.one_hot(self.actions, depth=self.action_dim)
        self.alive_label = tf.placeholder(tf.float32, (None, 1))
        self.next_alive_label = tf.placeholder(tf.float32, (None, 1))
        self.actor_target_values = tf.placeholder(tf.float32, None)
        self.mixer_target_values = tf.placeholder(tf.float32, None)
        self.update_step = 0

        params = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='online_network')
        target_params = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='target_network')
        self.copy_ops = []
        for (online_var, target_var) in zip(params, target_params):
            assign_op = tf.assign(ref=target_var, value=online_var)
            self.copy_ops.append(assign_op)
        if qpd_utils.enable_qpd:
            self.mixer_online_s_a = tf.placeholder(tf.float32, (None, (state_dim + action_dim) * self.agent_num))
            self.mixer_target_s_a = tf.placeholder(tf.float32, (None, (state_dim + action_dim) * self.agent_num))
            self.online_q_tot = self.create_mixer(self.mixer_online_s_a, 'online_mixer')
            self.target_q_tot = self.create_mixer(self.mixer_target_s_a, 'target_mixer')
            if qpd_utils.enable_qdqn_target:
                self.mixer_grads = tf.gradients(self.target_q_tot, self.mixer_target_s_a)
            else:
                self.mixer_grads = tf.gradients(self.online_q_tot, self.mixer_online_s_a)

        if qpd_utils.enable_qpd:
            mixer_params = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='online_mixer_network')
            mixer_target_params = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, scope='target_mixer_network')
            for (online_var, target_var) in zip(mixer_params, mixer_target_params):
                assign_op = tf.assign(ref=target_var, value=online_var)
                self.copy_ops.append(assign_op)

        with tf.name_scope('loss'):
            if qpd_utils.enable_qpd:
                self.mixer_loss = tf.reduce_mean(tf.squared_difference(self.online_q_tot, self.mixer_target_values))

            # if the unit is dead now, its q = 0 and target q = 0
            target_values = tf.reshape(self.actor_target_values, (-1, 1)) * self.alive_label
            # self.oa = self.q * self.onehot_action
            self.act_q = tf.reshape(tf.reduce_sum(self.q * self.onehot_action, axis=1), (-1, 1))
            self.sd_op = tf.squared_difference(self.act_q, target_values)
            self.actor_loss = tf.reduce_sum(self.sd_op) / tf.reduce_sum(self.alive_label)
            if qpd_utils.enable_l2_loss:
                if qpd_utils.enable_qpd:
                    for par in params:
                        self.actor_loss += qpd_utils.l2_loss_scale_actor * tf.nn.l2_loss(par)
                    for par in mixer_params:
                        self.mixer_loss += qpd_utils.l2_loss_scale_critic * tf.nn.l2_loss(par)
                else:
                    for par in params:
                        self.actor_loss += qpd_utils.l2_loss_scale_dqn * tf.nn.l2_loss(par)

        # actor_trainer = tf.train.RMSPropOptimizer(learning_rate=a_lr, decay=0.99, epsilon=1e-5)
        actor_trainer = tf.train.RMSPropOptimizer(learning_rate=a_lr)
        # actor_trainer = tf.train.AdamOptimizer(learning_rate=a_lr)
        # critic_trainer = tf.train.RMSPropOptimizer(learning_rate=c_lr, decay=0.99, epsilon=1e-5)
        critic_trainer = tf.train.AdamOptimizer(learning_rate=c_lr)
        # actor_trainer = tf.train.GradientDescentOptimizer(learning_rate=a_lr)
        # critic_trainer = tf.train.GradientDescentOptimizer(learning_rate=c_lr)
        # Clip the gradients (normalize)
        if qpd_utils.clip_method == 'global_norm':
            agrads = tf.gradients(self.actor_loss, params)
            cgrads = tf.gradients(self.mixer_loss, mixer_params)
            agrads, self.a_g_norm = tf.clip_by_global_norm(agrads, qpd_utils.clip_size)
            cgrads, self.c_g_norm = tf.clip_by_global_norm(cgrads, qpd_utils.clip_size)
            self.mean_grads = [tf.reduce_mean(grad) for grad in cgrads if grad is not None]
            agrads = list(zip(agrads, params))
            cgrads = list(zip(cgrads, mixer_params))
        elif qpd_utils.clip_method == 'norm':
            agrads = actor_trainer.compute_gradients(self.actor_loss, params)
            cgrads = critic_trainer.compute_gradients(self.mixer_loss, mixer_params)
            self.mean_grads = []
            for i, (g, v) in enumerate(agrads):
                if g is not None:
                    agrads[i] = (tf.clip_by_norm(g, qpd_utils.clip_size), v)  # clip gradients
                    self.mean_grads.append(tf.reduce_mean(agrads[i]))
            for i, (g, v) in enumerate(cgrads):
                if g is not None:
                    cgrads[i] = (tf.clip_by_norm(g, qpd_utils.clip_size), v)  # clip gradients
                    self.mean_grads.append(tf.reduce_mean(cgrads[i]))
        elif qpd_utils.clip_method == 'value':
            agrads = actor_trainer.compute_gradients(self.actor_loss, params)
            cgrads = critic_trainer.compute_gradients(self.mixer_loss, mixer_params)
            self.mean_grads = []
            for i, (g, v) in enumerate(agrads):
                if g is not None:
                    agrads[i] = (tf.clip_by_value(g, -qpd_utils.clip_size, qpd_utils.clip_size), v)  # clip gradients
                    self.mean_grads.append(tf.reduce_mean(agrads[i]))
            for i, (g, v) in enumerate(cgrads):
                if g is not None:
                    cgrads[i] = (tf.clip_by_value(g, -qpd_utils.clip_size, qpd_utils.clip_size), v)  # clip gradients
                    self.mean_grads.append(tf.reduce_mean(cgrads[i]))
        else:  # no clip
            agrads = tf.gradients(self.actor_loss, params)
            cgrads = tf.gradients(self.mixer_loss, mixer_params)
            # self.mean_grads = [tf.reduce_mean(grad) for grad in cgrads if grad is not None]
            agrads = list(zip(agrads, params))
            cgrads = list(zip(cgrads, mixer_params))
        self.train_op_a = actor_trainer.apply_gradients(agrads)
        self.train_op_c = critic_trainer.apply_gradients(cgrads)
        # self.train_op = tf.train.RMSPropOptimizer(learning_rate).minimize(self.loss) # min(v) = max(-v)
        # self.sess.run(tf.global_variables_initializer())

        # SAVE/LOAD
        self.saver = tf.train.Saver(max_to_keep=10)

        if qpd_utils.is_load_checkpoints:
            print('Loading checkpoint ...')
            self.saver.restore(self.sess, qpd_utils.load_path)
            print('Checkpoingt: ' + qpd_utils.load_path + ' is loaded.')
        else:
            self.sess.run(tf.global_variables_initializer())
            print('Parameters initialized.')

        self.sess.graph.finalize()
Example #40
0
def _reduce_variance(x, axis=None, keepdims=False):
  sample_mean = tf.reduce_mean(x, axis, keepdims=True)
  return tf.reduce_mean(
      tf.squared_difference(x, sample_mean), axis, keepdims)
Example #41
0
def _norm(x, *, axis, g, b, e=1e-5):
    assert x.shape.ndims == g.shape.ndims == b.shape.ndims
    u = tf.reduce_mean(x, axis=axis, keepdims=True)
    s = tf.reduce_mean(tf.squared_difference(x, u), axis=axis, keepdims=True)
    x = (x - u) * tf.rsqrt(s + e)
    return x * g + b
Example #42
0
    def custom_loss(self, y_true, y_pred):

        y_true_shape = (-1, self.grid_size, self.grid_size,
                        self.bbox_params + len(self.classes))
        y_pred_shape = (-1, self.grid_size, self.grid_size,
                        self.bbox_count * self.bbox_params + len(self.classes))

        y_true = tf.reshape(y_true, y_true_shape, name='reshape_y_true')
        y_pred = tf.reshape(y_pred, y_pred_shape, name='reshape_y_pred')
        #y_print = tf.Print(y_true, [y_true[0, 10, 10, :], y_pred[0, :, :, :]], message='\n\ny_true, y_pred: ', summarize=10000)

        # # # # # # # # # # # # # # # # # # #
        # parse data
        # # # # # # # # # # # # # # # # # # #

        # 0 , 1,  2,  3,  4,  5,  6,  7,  8,  9,  10, 11, ..., pn
        # x1, y1, w1, h1, C1, x2, y2, w2, h2, C2, p1, p2, ..., pn
        predicted_bbox_1 = y_pred[:, :, :, :5]

        predicted_bbox_2 = y_pred[:, :, :, 5:10]

        predicted_class_prob = y_pred[:, :, :, 10:]

        #print(predicted_bbox_1) #(?, 21, 21, 5)
        #print(predicted_bbox_2) #(?, 21, 21, 5)
        #print(predicted_class_prob) #(?, 21, 21, 10)

        true_box = y_true[:, :, :, :4]
        true_object_confidence = y_true[:, :, :, 4]
        true_class_prob = y_true[:, :, :, 5:]

        #print(true_box) #(?, 21, 21, 4)
        #print(true_object_confidence) #(?, 21, 21)
        #print(true_class_prob) #(?, 21, 21, 10)

        # # # # # # # # # # # # # # # # # # #
        # find responsible bboxes
        # # # # # # # # # # # # # # # # # # #

        iou_bbox1 = calculate_IOU(predicted_bbox_1, true_box)
        #(?, 21, 21)
        iou_bbox2 = calculate_IOU(predicted_bbox_2, true_box)
        #(?, 21, 21)

        responsible_pred_bbox = tf.greater(iou_bbox1, iou_bbox2)
        #print(responsible_pred_bbox)#(?, 21, 21)
        #(?, 21, 21) 1 or 0 1代表 bbox1 负责 0代表bbox2负责
        responsible_pred_bbox = tf.cast(responsible_pred_bbox, tf.float32)
        responsible_pred_bbox = tf.expand_dims(responsible_pred_bbox, axis=3)
        #print(responsible_pred_bbox) #(?, 21, 21, 1)

        responsible_pred_bbox = tf.tile(responsible_pred_bbox, [1, 1, 1, 5])
        #print(responsible_pred_bbox)#(?, 21, 21, 5)

        #responsible_pred_bbox = tf.where(responsible_pred_bbox, predicted_bbox_1, predicted_bbox_2)
        responsible_pred_bbox = tf.multiply(
            predicted_bbox_1, responsible_pred_bbox) + tf.multiply(
                predicted_bbox_2,
                tf.ones_like(responsible_pred_bbox) - (responsible_pred_bbox))

        #print(responsible_pred_bbox)#(?, 21, 21, 5)

        # # # # # # # # # # # # # # # # # # #
        # x,y loss
        # # # # # # # # # # # # # # # # # # #

        # (x - x')^2 + (y - y')^2
        x_loss = tf.squared_difference(true_box[..., 0],
                                       responsible_pred_bbox[..., 0])
        #print(x_loss) #(?, 21, 21)
        y_loss = tf.squared_difference(true_box[..., 1],
                                       responsible_pred_bbox[..., 1])
        #print(y_loss) #(?, 21, 21)
        xy_loss = tf.add(x_loss, y_loss)

        # if the object is not present in the cell that the sum is zero
        #print(true_object_confidence)#(?, 21, 21)
        xy_loss = tf.multiply(xy_loss, true_object_confidence)  #(?, 21, 21)

        xy_loss = self.lambda_coord * tf.reduce_sum(xy_loss)
        tf.losses.add_loss(xy_loss)
        #print(xy_loss)  # shape=()

        # # # # # # # # # # # # # # # # # # #
        # w,h loss
        # # # # # # # # # # # # # # # # # # #

        # (sqrt(w) - sqrt(w'))^2 + (sqrt(h) - sqrt(h'))^2
        #true_box = tf.Print(true_box, [true_box], message='true_box \t', summarize=1000)
        #responsible_pred_bbox = tf.Print(responsible_pred_bbox, [responsible_pred_bbox], message='responsible_pred_bbox \t', summarize=1000)
        w_loss = tf.squared_difference(
            tf.sqrt(true_box[..., 2] + 1e-10),
            tf.sqrt(responsible_pred_bbox[..., 2] + 1e-10))
        h_loss = tf.squared_difference(
            tf.sqrt(true_box[..., 3] + 1e-10),
            tf.sqrt(responsible_pred_bbox[..., 3] + 1e-10))
        wh_loss = tf.add(w_loss, h_loss)

        # if the object is not present in the cell that the sum is zero
        wh_loss = tf.multiply(wh_loss, true_object_confidence)
        wh_loss = self.lambda_coord * tf.reduce_sum(wh_loss)
        tf.losses.add_loss(wh_loss)
        # # # # # # # # # # # # # # # # # # #
        # bbox confidence loss
        # # # # # # # # # # # # # # # # # # #

        object_loss = tf.squared_difference(true_object_confidence,
                                            responsible_pred_bbox[..., 4])
        object_loss = tf.reduce_sum(
            tf.multiply(object_loss, true_object_confidence))

        no_object_loss = tf.squared_difference(true_object_confidence,
                                               responsible_pred_bbox[..., 4])
        no_object_loss = tf.reduce_sum(self.lambda_noobj * tf.multiply(
            no_object_loss,
            tf.ones_like(true_object_confidence) - true_object_confidence))

        confidence_loss = tf.add(object_loss, no_object_loss)
        tf.losses.add_loss(confidence_loss)
        #print(confidence_loss)

        # # # # # # # # # # # # # # # # # # #
        # classification loss
        # # # # # # # # # # # # # # # # # # #
        classification_loss = tf.squared_difference(true_class_prob,
                                                    predicted_class_prob)
        classification_loss = tf.reduce_sum(classification_loss, axis=3)
        classification_loss = tf.reduce_sum(
            tf.multiply(classification_loss, true_object_confidence))
        tf.losses.add_loss(classification_loss)
        #print(classification_loss)

        # # # # # # # # # # # # # # # # # # #
        # Total loss
        # # # # # # # # # # # # # # # # # # #
        loss = tf.losses.get_total_loss()
        #xy_loss ok

        #loss = wh_loss
        # # # # # # # # # # # # # # # # # # #
        # Debug Info
        # # # # # # # # # # # # # # # # # # #
        #loss_print = tf.Print(loss, [loss], message='Total Loss \t', summarize=1000)
        '''
        loss = loss - loss
        #loss = tf.Print(loss, [xy_loss], message='Loss XY \t', summarize=1000)
        #loss = tf.Print(loss, [wh_loss], message='Loss WH \t', summarize=1000)
        #loss = tf.Print(loss, [confidence_loss], message='Loss Conf \t', summarize=1000)
        #loss = tf.Print(loss, [classification_loss], message='Loss Class \t', summarize=1000)
        
        #loss_1 = tf.losses.absolute_difference(y_true,y_pred)
        '''
        loss = tf.Print(loss, [loss], message='Total Loss \t', summarize=1000)
        return loss
Example #43
0
def train(train_record_file, train_log_step, train_param, val_record_file,
          val_log_step, labels_nums, data_shape, snapshot, snapshot_prefix):
    '''
    :param train_record_file: 训练的tfrecord文件
    :param train_log_step: 显示训练过程log信息间隔
    :param train_param: train参数
    :param val_record_file: 验证的tfrecord文件
    :param val_log_step: 显示验证过程log信息间隔
    :param val_param: val参数
    :param labels_nums: labels数
    :param data_shape: 输入数据shape
    :param snapshot: 保存模型间隔
    :param snapshot_prefix: 保存模型文件的前缀名
    :return:
    '''
    [base_lr, max_steps] = train_param
    [batch_size, resize_height, resize_width, depths] = data_shape

    # 获得训练和测试的样本数
    train_nums = get_example_nums(train_record_file)
    val_nums = get_example_nums(val_record_file)
    print('train nums:%d,val nums:%d' % (train_nums, val_nums))

    # 从record中读取图片和labels数据
    # train数据,训练数据一般要求打乱顺序shuffle=True
    train_images, train_labels = read_records(train_record_file,
                                              resize_height,
                                              resize_width,
                                              type='normalization')
    train_images_batch, train_labels_batch = get_batch_images(
        train_images,
        train_labels,
        batch_size=batch_size,
        labels_nums=labels_nums,
        one_hot=False,
        shuffle=True)
    # val数据,验证数据可以不需要打乱数据
    val_images, val_labels = read_records(val_record_file,
                                          resize_height,
                                          resize_width,
                                          type='normalization')
    val_images_batch, val_labels_batch = get_batch_images(
        val_images,
        val_labels,
        batch_size=batch_size,
        labels_nums=labels_nums,
        one_hot=False,
        shuffle=False)

    # Define the model:
    with slim.arg_scope(vgg.vgg_arg_scope()):
        out, end_points = vgg.vgg_16(inputs=input_images,
                                     num_classes=labels_nums,
                                     dropout_keep_prob=keep_prob,
                                     is_training=is_training)

    loss = tf.reduce_sum(tf.squared_difference(x=out, y=input_labels))
    # loss1=tf.squared_difference(x=out,y=input_labels)

    # loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=output, labels=y))
    train_op = tf.train.AdamOptimizer(learning_rate=base_lr).minimize(loss)

    # tf.losses.add_loss(loss1)
    # # slim.losses.add_loss(my_loss)
    # loss = tf.losses.get_total_loss(add_regularization_losses=True)  # 添加正则化损失loss=2.2
    # # Specify the optimization scheme:
    # optimizer = tf.train.GradientDescentOptimizer(learning_rate=base_lr)
    # # create_train_op that ensures that when we evaluate it to get the loss,
    # # the update_ops are done and the gradient updates are computed.
    # train_op = slim.learning.create_train_op(total_loss=loss, optimizer=optimizer)

    saver = tf.train.Saver()
    max_acc = 0.0
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        sess.run(tf.local_variables_initializer())
        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)
        for i in range(max_steps + 1):
            batch_input_images, batch_input_labels = sess.run(
                [train_images_batch, train_labels_batch])
            _, train_loss = sess.run(
                [train_op, loss],
                feed_dict={
                    input_images: batch_input_images,
                    input_labels: batch_input_labels,
                    keep_prob: 0.5,
                    is_training: True
                })
            if i % train_log_step == 0:
                print("%s: Step [%d]  train Loss : %f" %
                      (datetime.now(), i, train_loss))
            # # train测试(这里仅测试训练集的一个batch)
            # if i%train_log_step == 0:
            #     train_acc = sess.run(accuracy, feed_dict={input_images:batch_input_images,
            #                                               input_labels: batch_input_labels,
            #                                               keep_prob:1.0, is_training: False})
            #     print "%s: Step [%d]  train Loss : %f, training accuracy :  %g" % (datetime.now(), i, train_loss, train_acc)
            #
            # # val测试(测试全部val数据)
            # if i%val_log_step == 0:
            #     _, train_loss = sess.run([train_step, loss], feed_dict={input_images: batch_input_images,
            #                                                             input_labels: batch_input_labels,
            #                                                             keep_prob: 1.0, is_training: False})
            #     print "%s: Step [%d]  val Loss : %f, val accuracy :  %g" % (datetime.now(), i, mean_loss, mean_acc)
            #
            # 模型保存:每迭代snapshot次或者最后一次保存模型
            if (i % snapshot == 0 and i > 0) or i == max_steps:
                print('-----save:{}-{}'.format(snapshot_prefix, i))
                saver.save(sess, snapshot_prefix, global_step=i)
            # # 保存val准确率最高的模型
            # if mean_acc>max_acc and mean_acc>0.5:
            #     max_acc=mean_acc
            #     path = os.path.dirname(snapshot_prefix)
            #     best_models=os.path.join(path,'best_models_{}_{:.4f}.ckpt'.format(i,max_acc))
            #     print('------save:{}'.format(best_models))
            #     saver.save(sess, best_models)

        coord.request_stop()
        coord.join(threads)
    def _build_net(self):
        # ------------------ all inputs ------------------------
        # Like q-learning: S.A.R.S.
        self.s = tf.placeholder(tf.float32, [None, self.n_features],
                                name='s')  # input State
        self.s_ = tf.placeholder(tf.float32, [None, self.n_features],
                                 name='s_')  # input Next State
        self.r = tf.placeholder(tf.float32, [
            None,
        ], name='r')  # input Reward
        self.a = tf.placeholder(tf.int32, [
            None,
        ], name='a')  # input Action

        # Initializer that generates tensors with constant/random values.
        w_initializer = tf.random_normal_initializer(0., 0.3)
        b_initializer = tf.constant_initializer(0.1)

        # ------------------ build evaluate_net ------------------
        # Tensorboard: To clean up the visualization of our model in tensorboard we need to add the scope of our
        # variables and a name for our placeholders and variables.

        with tf.variable_scope('eval_net'):
            # FC with activation ReLu: outputs (=e1) = activation(inputs.kernel + bias)
            # units: Integer or Long, dimensionality of the output space = 20 hidden layers
            e1 = tf.layers.dense(
                inputs=self.s,
                units=128,  # 20 hidden layers
                activation=tf.nn.
                relu,  # tf.contrib.fully_connected has relu as it's default activation,
                # while tf.layers.dense is a linear activation by default.
                kernel_initializer=w_initializer,
                bias_initializer=b_initializer,
                name='e1')

            self.q_eval = tf.layers.dense(
                inputs=e1,
                units=self.n_actions,  # output is an action
                kernel_initializer=w_initializer,
                bias_initializer=b_initializer,
                name='q')

        # ------------------ build target_net ------------------
        with tf.variable_scope('target_net'):
            t1 = tf.layers.dense(
                inputs=self.s_,
                units=128,  # also 20 hidden layers
                activation=tf.nn.relu,
                kernel_initializer=w_initializer,
                bias_initializer=b_initializer,
                name='t1')

            # it should be renamed q_target ?
            self.q_next = tf.layers.dense(inputs=t1,
                                          units=self.n_actions,
                                          kernel_initializer=w_initializer,
                                          bias_initializer=b_initializer,
                                          name='t2')

        with tf.variable_scope('q_target'):
            # Computes the maximum of elements across dimensions of tensor "self.q_next"
            q_target = normalization_function(
                self.r + self.gamma * tf.reduce_max(
                    input_tensor=inverse_normalization_function(self.q_next),
                    axis=1,
                    name='Qmax_s_'))  # shape=(None, )

            # q_target = self.r + self.gamma * tf.reduce_max(
            #     input_tensor=self.q_next,
            #     axis=1,
            #     name='Qmax_s_'
            # )  # shape=(None, )

            # clip
            tf.clip_by_value(q_target, 0, 1)

            # Stops gradient computation - I don't want to improve the target net now
            self.q_target = tf.stop_gradient(input=q_target)

        with tf.variable_scope('q_eval'):
            # To estimate Q w.r.t. action a
            # Stacks a list of rank-R tensors into one rank-(R+1) tensor.
            a_indices = tf.stack(
                values=[tf.range(tf.shape(self.a)[0], dtype=tf.int32), self.a],
                axis=1)

            # Gather slices from params into a Tensor with shape specified by indices
            self.q_eval_wrt_a = tf.gather_nd(
                params=self.q_eval, indices=a_indices)  # shape=(None, )

        with tf.variable_scope('loss'):
            # Computes the mean of elements across dimensions of the TD error tensor
            self.losses = tf.squared_difference(self.q_target,
                                                self.q_eval_wrt_a,
                                                name='TD_error')

            self.loss = tf.reduce_mean(input_tensor=self.losses)

        with tf.variable_scope('train'):
            # Optimizer that implements the RMSProp algorithm
            # self._train_op = tf.train.RMSPropOptimizer(
            #     learning_rate=self.lr
            # ).minimize(
            #     loss=self.loss
            # )

            # Optimizer Parameters from original paper
            self.optimizer = tf.train.RMSPropOptimizer(  # Optimizer that implements the RMSProp algorithm
                learning_rate=self.lr,
                decay=0.99,
                momentum=0.0,
                epsilon=1e-6)
            self._train_op = self.optimizer.minimize(
                loss=self.loss,
                global_step=tf.contrib.framework.get_global_step()
                # increment by one after the variables have been updated
            )

        # create and merge all summaries into a single "operation" which we can execute in a session
        self.summaries_op = tf.summary.merge([
            tf.summary.scalar("loSSS", self.loss),
            tf.summary.histogram("loss_hist", self.losses),
            tf.summary.histogram("q_values_hist", self.q_eval),
            tf.summary.scalar("max_q_value", tf.reduce_max(self.q_eval)),
            tf.summary.scalar("min_q_value", tf.reduce_min(self.q_eval)),
            tf.summary.scalar(
                "q_target", self.q_target[0]
            ),  # printing the order of magnitude of the output
            tf.summary.scalar("q_eval_wrt_a", self.q_eval_wrt_a[0])
        ])
    def __init__(self, environment, summary_dir="./", gpu=False, greyscale=True):
        os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
        config = tf.ConfigProto(log_device_placement=False, device_count={'GPU': gpu})
        config.gpu_options.per_process_gpu_memory_fraction = 0.1

        if len(environment.action_space.shape) > 0:
            self.discrete = False
            self.s_dim, self.a_dim = environment.observation_space.shape, environment.action_space.shape[0]
            self.a_bound = (environment.action_space.high - environment.action_space.low) / 2
            self.actions = tf.placeholder(tf.float32, [None, self.a_dim], 'action')
        else:
            self.discrete = True
            self.s_dim, self.a_dim = environment.observation_space.shape, environment.action_space.n
            self.actions = tf.placeholder(tf.int32, [None, 1], 'action')
        self.cnn = len(self.s_dim) == 3
        self.greyscale = greyscale  # If not greyscale and using RGB, make sure to divide the images by 255

        self.sess = tf.Session(config=config)
        self.state = tf.placeholder(tf.float32, [None] + list(self.s_dim), 'state')
        self.advantage = tf.placeholder(tf.float32, [None, 1], 'advantage')
        self.rewards = tf.placeholder(tf.float32, [None, 1], 'discounted_r')
        self.keep_prob = tf.placeholder(tf.float32, name='dropout_keep_prob')

        # Use the TensorFlow Dataset API
        self.dataset = tf.data.Dataset.from_tensor_slices({"state": self.state, "actions": self.actions,
                                                           "rewards": self.rewards, "advantage": self.advantage})
        self.dataset = self.dataset.batch(MINIBATCH, drop_remainder=True)
        self.iterator = self.dataset.make_initializable_iterator()
        batch = self.iterator.get_next()
        self.global_step = tf.train.get_or_create_global_step()

        # Create an old & new policy function but also
        # make separate value & policy functions for evaluation & training (with shared variables)
        pi_old, vf_old, old_params, _, _ = self._build_net(batch["state"], 'oldnet')
        pi, self.v, params, self.i_state, self.f_state = self._build_net(batch["state"], 'net')
        pi_eval, self.vf_eval, _, self.eval_i_state, self.eval_f_state = self._build_net(self.state, 'net', reuse=True, batch_size=1)

        self.sample_op = tf.squeeze(pi_eval.sample(1), axis=0, name="sample_action")
        self.eval_action = pi_eval.mode()  # Used mode for discrete case. Mode should equal mean in continuous
        self.saver = tf.train.Saver()

        with tf.variable_scope('loss'):
            epsilon_decay = tf.train.polynomial_decay(EPSILON, self.global_step, 1e5, 0.01, power=0.0)

            with tf.variable_scope('policy'):
                # Use floor functions for the probabilities to prevent NaNs when prob = 0
                ratio = tf.maximum(pi.prob(batch["actions"]), 1e-6) / tf.maximum(pi_old.prob(batch["actions"]), 1e-6)
                ratio = tf.clip_by_value(ratio, 0, 10)
                surr1 = batch["advantage"] * ratio
                surr2 = batch["advantage"] * tf.clip_by_value(ratio, 1 - epsilon_decay, 1 + epsilon_decay)
                loss_pi = -tf.reduce_mean(tf.minimum(surr1, surr2))
                tf.summary.scalar("loss", loss_pi)

            with tf.variable_scope('value_function'):
                clipped_value_estimate = vf_old + tf.clip_by_value(self.v - vf_old, -epsilon_decay, epsilon_decay)
                loss_vf1 = tf.squared_difference(clipped_value_estimate, batch["rewards"])
                loss_vf2 = tf.squared_difference(self.v, batch["rewards"])
                loss_vf = tf.reduce_mean(tf.maximum(loss_vf1, loss_vf2)) * 0.5
                # loss_vf = tf.reduce_mean(tf.square(self.v - batch["rewards"])) * 0.5
                tf.summary.scalar("loss", loss_vf)

            with tf.variable_scope('entropy'):
                entropy = pi.entropy()
                pol_entpen = -ENTROPY_BETA * tf.reduce_mean(entropy)

            loss = loss_pi + loss_vf * VF_COEFF + pol_entpen
            tf.summary.scalar("total", loss)
            # tf.summary.scalar("epsilon", epsilon_decay)

        with tf.variable_scope('train'):
            opt = tf.train.AdamOptimizer(LR)
            self.train_op = opt.minimize(loss, global_step=self.global_step, var_list=params)

            # grads, vs = zip(*opt.compute_gradients(loss, var_list=params))
            # grads, _ = tf.clip_by_global_norm(grads, 1.0)
            # self.train_op = opt.apply_gradients(zip(grads, vs), global_step=self.global_step)

        with tf.variable_scope('update_old'):
            self.update_old_op = [oldp.assign(p) for p, oldp in zip(params, old_params)]

        self.writer = tf.summary.FileWriter(summary_dir, self.sess.graph)
        self.sess.run(tf.global_variables_initializer())

        tf.summary.scalar("value", tf.reduce_mean(self.v))
        tf.summary.scalar("policy_entropy", tf.reduce_mean(entropy))
        if not self.discrete:
            tf.summary.scalar("sigma", tf.reduce_mean(pi.scale))
        self.summarise = tf.summary.merge(tf.get_collection(tf.GraphKeys.SUMMARIES))
Example #46
0
    def _build_net(self):
        # ------------------ build evaluate_net ------------------
        self.s = tf.placeholder(tf.float32, [None, self.n_features],
                                name='s')  # input
        self.q_target = tf.placeholder(tf.float32, [None, self.n_actions],
                                       name='Q_target')  # for calculating loss
        with tf.variable_scope('eval_net'):
            # c_names(collections_names) are the collections to store variables                                512*512的网络结构
            c_names, n_l1, n_l2, w_initializer, b_initializer = \
                ['eval_net_params', tf.GraphKeys.GLOBAL_VARIABLES], 512, 512,\
                tf.random_normal_initializer(0., 0.3), tf.constant_initializer(0.1)  # config of layers

            # first layer. collections is used later when assign to target net
            with tf.variable_scope('l1'):
                w1 = tf.get_variable('w1', [self.n_features, n_l1],
                                     initializer=w_initializer,
                                     collections=c_names)
                b1 = tf.get_variable('b1', [1, n_l1],
                                     initializer=b_initializer,
                                     collections=c_names)
                l1 = tf.nn.relu(tf.matmul(self.s, w1) + b1)

            # second layer. collections is used later when assign to target net
            with tf.variable_scope('l2'):
                w2 = tf.get_variable('w2', [n_l1, n_l2],
                                     initializer=w_initializer,
                                     collections=c_names)
                b2 = tf.get_variable('b2', [1, n_l2],
                                     initializer=b_initializer,
                                     collections=c_names)
                l2 = tf.nn.relu(tf.matmul(l1, w2) + b2)

            # third layer. collections is used later when assign to target net
            with tf.variable_scope('l3'):
                w3 = tf.get_variable('w3', [n_l2, self.n_actions],
                                     initializer=w_initializer,
                                     collections=c_names)
                b3 = tf.get_variable('b3', [1, self.n_actions],
                                     initializer=b_initializer,
                                     collections=c_names)
                self.q_eval = tf.matmul(l2, w3) + b3

        with tf.variable_scope('loss'):
            self.loss = tf.reduce_mean(
                tf.squared_difference(self.q_target, self.q_eval))

        with tf.variable_scope('train'):
            self._train_op = tf.train.RMSPropOptimizer(self.lr).minimize(
                self.loss)

        # ------------------ build target_net ------------------
        self.s_ = tf.placeholder(tf.float32, [None, self.n_features],
                                 name='s_')  # input
        with tf.variable_scope('target_net'):
            # c_names(collections_names) are the collections to store variables
            c_names = ['target_net_params', tf.GraphKeys.GLOBAL_VARIABLES]

            # first layer. collections is used later when assign to target net
            with tf.variable_scope('l1'):
                w1 = tf.get_variable('w1', [self.n_features, n_l1],
                                     initializer=w_initializer,
                                     collections=c_names)
                b1 = tf.get_variable('b1', [1, n_l1],
                                     initializer=b_initializer,
                                     collections=c_names)
                l1 = tf.nn.relu(tf.matmul(self.s_, w1) + b1)

            # second layer. collections is used later when assign to target net
            with tf.variable_scope('l2'):
                w2 = tf.get_variable('w2', [n_l1, n_l2],
                                     initializer=w_initializer,
                                     collections=c_names)
                b2 = tf.get_variable('b2', [1, n_l2],
                                     initializer=b_initializer,
                                     collections=c_names)
                l2 = tf.matmul(l1, w2) + b2

            # third layer. collections is used later when assign to target net
            with tf.variable_scope('l3'):
                w3 = tf.get_variable('w3', [n_l2, self.n_actions],
                                     initializer=w_initializer,
                                     collections=c_names)
                b3 = tf.get_variable('b3', [1, self.n_actions],
                                     initializer=b_initializer,
                                     collections=c_names)
                self.q_next = tf.matmul(l2, w3) + b3
Example #47
0
def squared_dist(A): 
    expanded_a = tf.expand_dims(A, 1)
    expanded_b = tf.expand_dims(A, 0)
    distances = tf.reduce_sum(tf.squared_difference(expanded_a, expanded_b), 2)
    return distances
Example #48
0
	def __init__(self,img_size=32,num_layers=32,feature_size=256,scale=2,output_channels=3):
		print("Building EDSR...")
		self.img_size = img_size
		self.scale = scale
		self.output_channels = output_channels

		#Placeholder for image inputs
		self.input = x = tf.placeholder(tf.float32,[None,img_size,img_size,output_channels])
		#Placeholder for upscaled image ground-truth
		self.target = y = tf.placeholder(tf.float32,[None,img_size*scale,img_size*scale,output_channels])
	
		"""
		Preprocessing as mentioned in the paper, by subtracting the mean
		However, the subtract the mean of the entire dataset they use. As of
		now, I am subtracting the mean of each batch
		"""
		mean_x = tf.reduce_mean(self.input)
		image_input =x- mean_x
		mean_y = tf.reduce_mean(self.target)
		image_target =y- mean_y

		#One convolution before res blocks and to convert to required feature depth
		x = slim.conv2d(image_input,feature_size,[3,3])
	
		#Store the output of the first convolution to add later
		conv_1 = x	

		"""
		This creates `num_layers` number of resBlocks
		a resBlock is defined in the paper as
		(excuse the ugly ASCII graph)
		x
		|\
		| \
		|  conv2d
		|  relu
		|  conv2d
		| /
		|/
		+ (addition here)
		|
		result
		"""

		"""
		Doing scaling here as mentioned in the paper:

		`we found that increasing the number of feature
		maps above a certain level would make the training procedure
		numerically unstable. A similar phenomenon was
		reported by Szegedy et al. We resolve this issue by
		adopting the residual scaling with factor 0.1. In each
		residual block, constant scaling layers are placed after the
		last convolution layers. These modules stabilize the training
		procedure greatly when using a large number of filters.
		In the test phase, this layer can be integrated into the previous
		convolution layer for the computational efficiency.'

		"""
		scaling_factor = 0.1
		
		#Add the residual blocks to the model
		for i in range(num_layers):
			x = utils.resBlock(x,feature_size,scale=scaling_factor)

		#One more convolution, and then we add the output of our first conv layer
		x = slim.conv2d(x,feature_size,[3,3])
		x += conv_1
		
		#Upsample output of the convolution		
		x = utils.upsample(x,scale,feature_size,None)

		#One final convolution on the upsampling output
		output =x# slim.conv2d(x,output_channels,[3,3])
		self.out = tf.clip_by_value(output+mean_x,0.0,255.0)

		self.loss = loss = tf.reduce_mean(tf.losses.absolute_difference(image_target,output))
	
		#Calculating Peak Signal-to-noise-ratio
		#Using equations from here: https://en.wikipedia.org/wiki/Peak_signal-to-noise_ratio
		mse = tf.reduce_mean(tf.squared_difference(image_target,output))	
		PSNR = tf.constant(255**2,dtype=tf.float32)/mse
		PSNR = tf.constant(10,dtype=tf.float32)*utils.log10(PSNR)
	
		#Scalar to keep track for loss
		tf.summary.scalar("loss",self.loss)
		tf.summary.scalar("PSNR",PSNR)
		#Image summaries for input, target, and output
		tf.summary.image("input_image",tf.cast(self.input,tf.uint8))
		tf.summary.image("target_image",tf.cast(self.target,tf.uint8))
		tf.summary.image("output_image",tf.cast(self.out,tf.uint8))
		
		#Tensorflow graph setup... session, saver, etc.
		self.sess = tf.Session()
		self.saver = tf.train.Saver()
		print("Done building!")
Example #49
0
    def __init__(self,
                 sess,
                 n_features,
                 config,
                 dic_traffic_env_conf,
                 demo,
                 lr=0.01):
        self.sess = sess
        self.config = config
        self._activation_fn = tf.nn.leaky_relu
        self.dic_traffic_env_conf = dic_traffic_env_conf

        # replay buffer
        self.replay_memory = Memory(capacity=self.config.replay_buffer_size,
                                    permanent_data=len(demo))
        # self.replay_memory = None
        self.demo_memory = Memory(capacity=self.config.demo_buffer_size,
                                  permanent_data=self.config.demo_buffer_size)
        self.add_demo_to_memory(demo_transitions=demo)
        self.state_dim = 16
        self.action_dim = 8

        self.s = tf.placeholder(tf.float32, [None, n_features], "state")
        self.v_ = tf.placeholder(tf.float32, [None, 1], "v_next")
        self.q_a_ = tf.placeholder(tf.float32, [None, 1], "q_next")
        self.r = tf.placeholder(tf.float32, [None, 1], 'r')
        self.a = tf.placeholder(tf.int32, [None, 1], 'act')
        self.act_probs = tf.placeholder(tf.float32, [None, 8], 'act_probs')

        self.action_batch = tf.placeholder("int32", [None])
        self.y_input = tf.placeholder("float", [None, self.action_dim])
        self.ISWeights = tf.placeholder("float", [None, 1])
        self.n_step_y_input = tf.placeholder(
            "float", [None, self.action_dim])  # for n-step reward
        self.isdemo = tf.placeholder("float", [None])

        # self.v = self.build_net("Value")
        # self.q = self.construct_forward(self.s, True, 'None', True, "Q-Value", prefix='fc')
        # self.q_target = self.construct_forward(self.s, True, 'None', True, "Q-Target", prefix='fc')
        self.q = self.build_q_net("Q-Value")
        self.q_target = self.build_q_net("Q-Target")
        self.q_a = tf.batch_gather(self.q, self.a)
        self.q_a_target = tf.batch_gather(self.q_target, self.a)
        self.v = tf.reduce_sum(self.q * self.act_probs, axis=1, keepdims=True)
        # self.v_target = self.build_net("Target")
        self.t_params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,
                                          scope='Q-Target')
        self.params = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,
                                        scope='Q-Value')
        self.replace_target_op = [
            tf.assign(t, p) for t, p in zip(self.t_params, self.params)
        ]

        self.loss
        self.optimize
        self.update_target_net
        self.abs_errors
        self.time_step = 0

        with tf.variable_scope('squared_TD_error'):
            # self.td_error = self.r + 0.8 * self.v_ - self.v
            self.td_error = self.q_a - self.v
            q_loss = tf.reduce_mean(
                tf.squared_difference(self.q_a, self.r + 0.8 * self.q_a_))
            # v_loss = tf.reduce_mean(tf.squared_difference(self.v, self.r + 0.8 * self.v_))
            # self.loss = tf.square(self.td_error)  # TD_error = (r+gamma*V_next) - V_eval
            # self.loss = q_loss + v_loss
            self.los = q_loss
        with tf.variable_scope('train'):
            self.train_op = tf.train.AdamOptimizer(lr).minimize(self.los)
Example #50
0
def loss(pred_quater,
         gt_quater,
         pred_transl,
         gt_transl,
         pred_mask,
         pred_traj,
         gt_traj,
         pred_score,
         gt_score,
         pred_r,
         gt_r,
         pred_flow,
         gt_flow,
         batch_size,
         dim=3,
         h=240,
         w=320):
    obj_mask_origin = tf.greater(gt_traj[:, :, :, 2],
                                 tf.zeros_like(gt_traj[:, :, :, 2]))
    obj_mask_origin = tf.cast(obj_mask_origin, tf.float32)
    obj_mask_1 = tf.reshape(obj_mask_origin, [-1, h, w, 1])
    obj_mask_6 = tf.tile(obj_mask_1, [1, 1, 1, 6])
    obj_mask_3 = tf.tile(obj_mask_1, [1, 1, 1, 3])
    obj_tmp = tf.zeros_like(obj_mask_3)
    obj_final = tf.concat([obj_mask_3, obj_tmp], 3)

    loss_mask = tf.reduce_mean(
        tf.nn.sparse_softmax_cross_entropy_with_logits(labels=tf.cast(
            obj_mask_origin, dtype=tf.int32),
                                                       logits=pred_mask))
    score_weight = obj_mask_origin + 0.00001

    loss_score = tf.reduce_sum(
        (score_weight) * tf.nn.sparse_softmax_cross_entropy_with_logits(
            labels=tf.cast(gt_score, dtype=tf.int32), logits=pred_score)) / (
                tf.reduce_sum(score_weight) + 0.000001)

    loss_elem3 = tf.reduce_sum(
        tf.squared_difference(pred_traj[:, :, :, 0:3], gt_traj[:, :, :, 0:3]) *
        obj_mask_3) / (tf.reduce_sum(obj_mask_1) + 0.000001)
    loss_elem6 = tf.reduce_sum(
        tf.squared_difference(pred_traj[:, :, :, 3:6], gt_traj[:, :, :, 3:6]) *
        obj_mask_3) / (tf.reduce_sum(obj_mask_1) + 0.000001)

    loss_boundary = tf.reduce_sum(
        tf.squared_difference(gt_r, pred_r) *
        obj_mask_1) / (tf.reduce_sum(obj_mask_1) + 0.000001)
    loss_flow = tf.reduce_sum(
        tf.squared_difference(pred_flow, gt_flow) *
        obj_mask_3) / (tf.reduce_sum(obj_mask_1) + 0.000001)
    loss_transl = tf.reduce_sum(
        tf.squared_difference(pred_transl, gt_transl) *
        obj_mask_3) / (tf.reduce_sum(obj_mask_1) + 0.000001)
    gt_angle = tf.acos(gt_quater[:, :, :, 0:1]) * 2
    gt_angle_axis = gt_quater[:, :, :, 1:4] / (tf.sin(gt_angle / 2) +
                                               0.000001) * gt_angle

    loss_quater = tf.reduce_sum(
        tf.squared_difference(gt_angle_axis, pred_quater) *
        obj_mask_3) / (tf.reduce_sum(obj_mask_1 + 0.000001))

    loss_variance = 0.0
    loss_violation = 0.0

    for b_i in xrange(batch_size):
        tmp = gt_traj[b_i, :, :, 2]
        tmp = tf.reshape(tmp, (-1, ))
        y, idx = tf.unique(tmp)
        idx = tf.reshape(idx, (h, w, 1))

        ins_tmp = tf.ones_like(idx)
        ones = tf.ones_like(gt_traj[b_i, :, :, 2])

        obj_mask = obj_mask_origin[b_i]

        def instance_variance_loss(z):
            idx_mask = tf.equal(gt_traj[b_i, :, :, 2], ones * z)
            idx_mask = tf.reshape(idx_mask, [h, w, 1])
            idx_mask = tf.cast(idx_mask, tf.float32)
            idx_mask_6d = tf.tile(idx_mask, [1, 1, 6])
            tmp_prd = idx_mask_6d * pred_traj[b_i]
            tmp_prd = tf.reshape(tmp_prd, (-1, 6))
            tmp_mean = tf.reduce_sum(
                tmp_prd, axis=0) / (tf.reduce_sum(idx_mask) + 0.000001)
            tmp_mean = tf.reshape(tmp_mean, (1, 1, 6))
            tmp_mean_final = tf.tile(tmp_mean, [h, w, 1])
            loss_variance_instance = tf.reduce_sum(
                idx_mask_6d *
                tf.squared_difference(tmp_mean_final, pred_traj[b_i])) / (
                    tf.reduce_sum(idx_mask) + 0.000001)
            return loss_variance_instance

        #loss_variance += tf.reduce_mean(tf.map_fn(instance_variance_loss,y))

        def instance_violation_loss(z):
            idx_mask = tf.equal(gt_traj[b_i, :, :, dim - 1], ones * z)
            idx_mask = tf.logical_and(idx_mask, tf.cast(obj_mask, tf.bool))
            idx_mask = tf.reshape(idx_mask, [h, w, 1])
            idx_mask = tf.cast(idx_mask, tf.float32)
            idx_mask_6d = tf.tile(idx_mask, [1, 1, 6])

            tmp_prd = idx_mask_6d * pred_traj[b_i, :, :, 0:6]
            tmp_prd = tf.reshape(tmp_prd, (-1, 6))

            tmp_r = idx_mask_6d[:, :, 0:1] * pred_r[b_i]
            tmp_r_mean = tf.reduce_sum(tmp_r) / (tf.reduce_sum(idx_mask) +
                                                 0.000001)
            r = tmp_r_mean * 0.5

            friend_mask = idx_mask_6d[:, :, 0]
            l2_error = tf.reduce_sum(
                tf.squared_difference(pred_traj[b_i], gt_traj[b_i]), 2)
            dist = tf.sqrt(l2_error)
            pull_mask = tf.less(r * ones, dist)
            pull_mask = tf.cast(pull_mask, tf.float32)
            pos = tf.reduce_sum(friend_mask * pull_mask * l2_error) / (
                tf.reduce_sum(friend_mask * pull_mask) + 0.000001)
            return pos

        #loss_violation += tf.reduce_mean(tf.map_fn(instance_violation_loss,y))

        def instance_sceneflow_loss(z):
            idx_mask = tf.equal(gt_traj[b_i, :, :, dim - 1], ones * z)
            idx_mask = tf.logical_and(idx_mask, tf.cast(obj_mask, tf.bool))
            idx_mask = tf.reshape(idx_mask, [h, w, 1])
            idx_mask = tf.cast(idx_mask, tf.float32)
            idx_mask_3d = tf.tile(idx_mask, [1, 1, 3])

            tmp_quater = idx_mask_3d * pred_quater[b_i, :, :, 0:3]
            tmp_quater = tf.reshape(tmp_quater, (-1, 3))
            tmp_quater = tf.reduce_sum(
                tmp_quater, axis=0) / (tf.reduce_sum(idx_mask) + 0.000001)

            tmp_transl = idx_mask_3d * pred_transl[b_i, :, :, 0:3]
            tmp_transl = tf.reshape(tmp_transl, (-1, 3))
            tmp_transl = tf.reduce_sum(
                tmp_transl, axis=0) / (tf.reduce_sum(idx_mask) + 0.000001)

            w1, x1, y1, z1 = tf.unstack(tmp_quater, axis=-1)
            x2, y2, z2 = tf.unstack(frame2_input_xyz[b_i], axis=-1)
            wm = -x1 * x2 - y1 * y2 - z1 * z2
            xm = w1 * x2 + y1 * z2 - z1 * y2
            ym = w1 * y2 + z1 * x2 - x1 * z2
            zm = w1 * z2 + x1 * y2 - y1 * x2

            x = -wm * x1 + xm * w1 - ym * z1 + zm * y1
            y = -wm * y1 + ym * w1 - zm * x1 + xm * z1
            z = -wm * z1 + zm * w1 - xm * y1 + ym * x1

            tmp_flow = tf.stack((x, y, z), axis=-1)
            tmp_flow = tmp_flow + tmp_transl - frame2_input_xyz[b_i]
            tmp_loss = tf.reduce_sum(idx_mask_3d * tf.squared_difference(
                tmp_flow, gt_flow[b_i])) / (tf.reduce_sum(idx_mask) + 0.000001)
            return tmp_loss

    return loss_quater, loss_transl, loss_boundary, loss_flow, loss_elem3, loss_elem6, loss_mask, loss_score
combination_op = tf.group(*ops)

gen_params_mean = []
disc_params_mean = []
for o in range(len(gen_params[0])):
    gen_params_mean.append(1.0 / N_NODES *
                           tf.add_n([gen_params[j][o] for j in NODES]))
for p in range(len(disc_params[0])):
    disc_params_mean.append(1.0 / N_NODES *
                            tf.add_n([disc_params[j][p] for j in NODES]))

gen_dist = [
    tf.reduce_sum([
        tf.reduce_sum(
            tf.squared_difference(gen_params[i][o], gen_params_mean[o]))
        for o in range(len(gen_params[i]))
    ]) for i in NODES
]

disc_dist = [
    tf.reduce_sum([
        tf.reduce_sum(
            tf.squared_difference(disc_params[i][o], disc_params_mean[o]))
        for o in range(len(disc_params[i]))
    ]) for i in NODES
]

gen_mean_norm = tf.reduce_sum([
    tf.reduce_sum(tf.square(gen_params_mean[o]))
    for o in range(len(gen_params_mean))
Example #52
0
policy = PolicyModel(sess, img_height, img_width, c_dim*n_stack, a_dim)


#Placeholders
#----------------------------
action_ph = tf.placeholder(tf.int32, [None], name="action")
adv_ph = tf.placeholder(tf.float32, [None], name="advantage")
discount_return_ph = tf.placeholder(tf.float32, [None], name="discounted_return")
lr_ph = tf.placeholder(tf.float32, [])


#Loss
#----------------------------
nll_loss = -policy.cat_dist.log_prob(action_ph)
pg_loss = tf.reduce_mean(adv_ph * nll_loss)
value_loss = tf.reduce_mean(tf.squared_difference(tf.squeeze(policy.value), discount_return_ph) / 2.0)
entropy_bonus = tf.reduce_mean(policy.cat_dist.entropy())
loss = pg_loss + value_weight*value_loss - ent_weight*entropy_bonus


#Optimizer
#----------------------------
t_var = tf.trainable_variables()
grads = tf.gradients(loss, t_var)
grads, grad_norm = tf.clip_by_global_norm(grads, max_grad_norm)
grads = list(zip(grads, t_var))
opt = tf.train.RMSPropOptimizer(lr_ph, decay=lr_decay, epsilon=eps).apply_gradients(grads)

tf.contrib.slim.model_analyzer.analyze_vars(t_var, print_info=True)

Example #53
0
def build_graph():
    aspect_ratio = tf.placeholder(dtype=tf.float32, shape=[
        2,
    ])
    input_size = tf.placeholder(dtype=tf.int32, shape=[
        4,
    ])  #BHWC
    images = tf.placeholder(dtype=tf.float32, shape=[None, None, None, 3])

    input_ = tf.cast(input_size, tf.float32)
    aspect_ratio_HR = [
        input_[1] * input_[1] / aspect_ratio[0],
        input_[2] * input_[2] / aspect_ratio[1]
    ]
    aspect_ratio_HR = tf.floor(aspect_ratio_HR)

    # Forward retargeting
    Sh, Sw, AttentionMap = CycleIR(images, reuse=False)
    images_LR = reconstruct_image(images, Sh, Sw, input_size, aspect_ratio)
    images_HR = reconstruct_image(images, (2 - Sh), (2 - Sw), input_size,
                                  aspect_ratio_HR)

    # Reverse retargeting (top)
    aspect_LR = tf.cast(aspect_ratio, tf.int32)
    aspect_LR = [input_size[0], aspect_LR[0], aspect_LR[1], 3]
    images_LR = tf.reshape(images_LR, aspect_LR)
    featH_LR, featW_LR, salmap_LR = CycleIR(images_LR, reuse=True)
    images_HR_from_LR = reconstruct_image(images_LR, (2 - featH_LR),
                                          (2 - featW_LR), aspect_LR,
                                          [input_[1], input_[2]])

    # Reverse retargeting (bottom)
    aspect_HR = tf.cast(aspect_ratio_HR, tf.int32)
    aspect_HR = [input_size[0], aspect_HR[0], aspect_HR[1], 3]
    images_HR = tf.reshape(images_HR, aspect_HR)
    featH_HR, featW_HR, salmap_HR = CycleIR(images_HR, reuse=True)
    images_LR_from_HR = reconstruct_image(images_HR, featH_HR, featW_HR,
                                          aspect_HR, [input_[1], input_[2]])

    PerceptualMap_Input = PerceptualCompute(images)
    PerceptualMap_RevTop = PerceptualCompute(images_HR_from_LR)
    PerceptualMap_RevBom = PerceptualCompute(images_LR_from_HR)

    CycleLoss = tf.reduce_mean(tf.squared_difference(PerceptualMap_Input, PerceptualMap_RevTop)) + \
                tf.reduce_mean(tf.squared_difference(PerceptualMap_Input, PerceptualMap_RevBom))

    # CycleLoss = tf.reduce_mean(tf.squared_difference(PerceptualMap_Input, PerceptualMap_RevTop)) + \
    #             tf.reduce_mean(tf.squared_difference(PerceptualMap_Input, PerceptualMap_RevBom)) + \
    #             tf.reduce_mean(tf.squared_difference(images, images_HR_from_LR)) + \
    #             tf.reduce_mean(tf.squared_difference(images, images_LR_from_HR))

    tf.summary.image("images_HR", images_HR)
    tf.summary.image("images_LR", images_LR)
    tf.summary.image("images", images)
    tf.summary.image("AttentionMap", tf.expand_dims(AttentionMap, 3) * 255)
    tf.summary.image("PerceptualMap_Input", PerceptualMap_Input * 255)
    tf.summary.image("PerceptualMap_RevTop", PerceptualMap_RevTop * 255)
    tf.summary.image("PerceptualMap_RevBom", PerceptualMap_RevBom * 255)

    theta_g = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,
                                scope='CycleIR')
    counter_g = tf.Variable(trainable=False, initial_value=0, dtype=tf.int32)

    opt_g = ly.optimize_loss(loss=CycleLoss,
                             learning_rate=learning_rate_ger,
                             optimizer=tf.train.AdamOptimizer
                             if is_adam is True else tf.train.RMSPropOptimizer,
                             variables=theta_g,
                             global_step=counter_g)

    return opt_g, aspect_ratio, images, CycleLoss, images_LR, AttentionMap, input_size, Sh, Sw
Example #54
0
def train(train_data_main, validation_data, test_data):

	# Parameters
	learning_rate = 0.1
	training_epochs = 15
	timesteps = 5
	batch_size = 10000
	display_step = 1

	p = 1.0
	B = 10 

	# Network Parameters
	batch_xs = []
	batch_exts = []
	batch_ys = []
	for [batch_x, batch_y, batch_ext] in sequence_build(train_data_main, timesteps = timesteps):
		batch_xs.append(batch_x)
		batch_ys.append(batch_y)
		batch_exts.append(batch_ext)
	'''
	print(np.array(batch_xs).shape)
	print(np.array(batch_ys).shape)
	print(np.array(batch_exts).shape)
	train_size = np.array(batch_xs).shape[0]
	'''

	valid_batch_xs = []
	valid_batch_exts = []
	valid_batch_ys = []
	for [batch_x, batch_y, batch_ext] in sequence_build(validation_data, timesteps = timesteps):
		valid_batch_xs.append(batch_x)
		valid_batch_ys.append(batch_y)
		valid_batch_exts.append(batch_ext)
	'''
	print(np.array(valid_batch_xs).shape)
	print(np.array(valid_batch_ys).shape)
	valid_size = np.array(valid_batch_xs).shape[0]
	'''

	test_batch_xs = []
	test_batch_exts = []
	test_batch_ys = []
	for [batch_x, batch_y, batch_ext] in sequence_build(test_data, timesteps = timesteps):
		test_batch_xs.append(batch_x)
		test_batch_ys.append(batch_y)
		test_batch_exts.append(batch_ext)
	'''
	print(np.array(test_batch_xs).shape)
	print(np.array(test_batch_ys).shape)
	print(np.array(test_batch_exts).shape)
	'''
	test_tokens = []
	for token_batch in sequence_build(test_data, normalize = [False, False]):
		test_tokens.append(token_batch[2])
	print(np.array(test_tokens).shape)
	#print(np.array(test_batch_ys).shape)
	#print(np.array(test_batch_exts).shape)

	size_x = np.array(batch_xs).shape
	n_input = size_x[-1]
	size_ext = np.array(batch_exts).shape
	n_ext = size_ext[-1]

	n_hidden = 5
	n_hidden_0 = n_ext + n_hidden
	n_hidden_1 = 5 # 1st layer number of neurons
	n_hidden_2 = 5 # 2nd layer number of neurons
# tf Graph input
	X = tf.placeholder("float", [None, timesteps, n_input])
	Y = tf.placeholder("float", [None, n_input])
	EXT = tf.placeholder("float", [None, n_ext])

	# Store layers weight & bias
	weights = {
		'h1': tf.Variable(tf.random_normal([n_hidden_0, n_hidden_1])),
    		'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])),
    		'out': tf.Variable(tf.random_normal([n_hidden_2, n_input]))
	}
	biases = {
   		'b1': tf.Variable(tf.random_normal([n_hidden_1])),
    		'b2': tf.Variable(tf.random_normal([n_hidden_2])),
    		'out': tf.Variable(tf.random_normal([n_input]))
	}


	# Construct model
	RNN_out, _  = RNN_2l(X, timesteps, n_hidden, n_hidden)

	dropout_1 = tf.nn.dropout(RNN_out, p)
	for i in range(B):
		dropout_1 = tf.add(dropout_1, tf.nn.dropout(RNN_out, p))
	dropout_mean_1 = dropout_1/B

	
	MLP_in = tf.concat([dropout_mean_1, EXT], axis = 1)
	
	#MLP_out = tf.tanh(multilayer_perceptron(MLP_in, weights, biases))
	MLP_out = multilayer_perceptron(MLP_in, weights, biases)

	'''
	dropout_2 = tf.nn.dropout(MLP_out, p)
	for i in range(B):
		dropout_2 = tf.add(dropout_2, tf.nn.dropout(MLP_out, p))
		error = tf.add(error, tf.pow(Y - tf.nn.dropout(MLP_out, p), 2))
	dropout_mean_2 = dropout_2/B
	'''
	dropouts_2 = []
	sqr_errors = []
	for i in range(B):
		dropouts_2.append(tf.nn.dropout(MLP_out, p))
		sqr_errors.append(tf.square(Y - tf.nn.dropout(MLP_out, p)))

	logits = tf.reduce_mean(tf.stack(dropouts_2))
	sqr_error = tf.reduce_mean(tf.stack(sqr_errors))
	loss = tf.reduce_mean(Y - logits)
	sqr_loss = tf.reduce_mean(tf.square(Y - logits))
	# Define loss and optimizer
	#loss_op = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=Y))
	loss_op = tf.reduce_mean(tf.squared_difference(Y, logits))
	#loss_op = tf.reduce_mean(tf.reduce_sum(tf.sqrt(logits - Y), -1))
	optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
	train_op = optimizer.minimize(loss_op)
	# Initializing the variables
	init = tf.global_variables_initializer()

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

    		# Training cycle
    		for epoch in range(training_epochs):
      			avg_cost = 0.
			avg_error = 0.
        		total_batch = int(len(batch_xs)/batch_size)
        		# Loop over all batches
        		for i in range(total_batch):
            			#batch_x, batch_y = mnist.train.next_batch(batch_size)
				batch_x = np.array(batch_xs)[i * batch_size: min(i * batch_size + batch_size, len(batch_xs))]
				batch_y = np.array(batch_ys)[i * batch_size: min(i * batch_size + batch_size, len(batch_xs))]
				batch_ext = np.array(batch_exts)[i * batch_size: min(i * batch_size + batch_size, len(batch_xs))]
            			# Run optimization op (backprop) and cost op (to get loss value)
            			_, l, c = sess.run([train_op, sqr_loss, sqr_error], feed_dict={X: batch_x,
                                                            			Y: batch_y,
										EXT: batch_ext})
            			# Compute average loss
            			avg_cost += l / total_batch
				avg_error += c / total_batch
        			# Display logs per epoch step
        		if epoch % display_step == 0:
            			print("Epoch:", '%04d' % (epoch+1), "squre error ={:.9f}".format(math.sqrt(avg_error)), "cost={:.9f}".format(avg_cost))
            			#print("Epoch:", '%04d' % (epoch+1), "cost={:.9f}".format(l))
    		print("Optimization Finished!")

		loss_val = sqr_loss.eval({X: valid_batch_xs, Y: valid_batch_ys, EXT: valid_batch_exts})
		#error_val = math.sqrt(error_val)
		print("Validation loss %0.4f" % loss_val)

		output_file = open('logfile', 'w')
		output_file.write(str(train_data_main) + ':' + str(validation_data) + ':' + str(test_data) + '\n')
		output_file.write('train_error:' + str(avg_error) + ":validation_error:" + str(loss_val) + '\n')

		test_results = []
		test_loss = []
		for i in range(len(test_batch_xs)):
			pred = logits.eval({X: [test_batch_xs[i]], 
						 Y: [test_batch_ys[i]],
						 EXT: [test_batch_exts[i]]})
			output_file.write(str(test_batch_ys[i]) + ':' +  str(pred) + '\n')
			test_loss.append(test_batch_ys[i] - pred)	
			test_results.append(pred)
		print("Test loss %0.4f" % np.mean(np.array(test_loss)))
		output_file.close()
Example #55
0
def genGenericModel(savePath,
                    queries,
                    restDicts,
                    numEpochs,
                    batchSize,
                    learningRate=0.002,
                    restoreVars=False,
                    restorePath=None):

    #Get input tensors from raw data
    numBatches, wideInputFeatures, deepInputFeatures = genInputFeatures(
        queries, restDicts, batchSize=batchSize)
    #TODO: Figure out format of answers
    answers = makeAnswers(restDicts, batchSize)
    answers = np.array(answers)
    deepInputFeatures = np.array(deepInputFeatures)
    wideInputFeatures = np.array(wideInputFeatures)

    #Define tf model:
    wideFeatures = tf.placeholder(tf.float32, shape=[None, 3])
    deepFeatures = tf.placeholder(tf.float32, shape=[None, 40])
    y = tf.placeholder(tf.float32, shape=[None, 1])

    deepW1 = weight_variable([40, 30], name="deepW1")
    deepB1 = bias_variable([30], name="deepB1")
    deepW2 = weight_variable([30, 20], name="deepW2")
    deepB2 = bias_variable([20], name="deepB2")
    deepW3 = weight_variable([20, 10], name="deepW3")
    deepB3 = bias_variable([10], name="deepB3")
    fc_W = weight_variable([13, 1], name="fc_W")
    fc_B = bias_variable([1], name="fc_B")

    deepLayer1 = tf.nn.relu(tf.matmul(deepFeatures, deepW1) + deepB1)
    deepLayer2 = tf.nn.relu(tf.matmul(deepLayer1, deepW2) + deepB2)
    deepLayer3 = tf.nn.relu(tf.matmul(deepLayer2, deepW3) + deepB3)
    fc_layer = tf.concat([deepLayer3, wideFeatures], 1)
    readout = tf.matmul(fc_layer, fc_W) + fc_B
    #loss = tf.reduce_sum(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=readout))
    loss = tf.reduce_mean(tf.squared_difference(readout, y))
    trainer = tf.train.AdamOptimizer(learningRate).minimize(loss)
    saver = tf.train.Saver()
    restorer = tf.train.Saver()

    print("Started Training")
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        if (restoreVars):
            restorer.restore(sess, restorePath)
        for e in range(numEpochs):
            lossForEpoch = 0
            for i in range(numBatches):
                #print(deepInputFeatures[i])
                #print(wideFeatures[i])
                #print(answers[i])
                _, r, l = sess.run(
                    [trainer, readout, loss],
                    feed_dict={
                        deepFeatures: deepInputFeatures[i],
                        wideFeatures: wideInputFeatures[i],
                        y: answers[i]
                    })
                lossForEpoch += l
            if e % 5 == 0 and e > 0:
                APIKEY.append(validKeys[e / 5])
            if e % 10 == 0:
                print("For epoch " + str(e) + ", the cost is " +
                      str(lossForEpoch))
                saver.save(sess, savePath)
                print("Saved model at " + savePath)
        saver.save(sess, savePath)
    print("Completed training generic model")
    print("Model stored at: " + savePath)
Example #56
0
def memnet_m6r6(name,
                clean_data=None,
                noisy_data=None,
                num_filters=64,
                image_c=1,
                is_training=False,
                reuse=False):
    # memnet_m6r6(): clean_image(label), noisy_image(data).
    # num_filters: 64. image_c: image channel, is 1. is_training: BN used.
    with tf.variable_scope(name, reuse=reuse):
        # FNet: bn + relu + conv
        conv1 = res_mod_layers(noisy_data,
                               num_filters=num_filters,
                               kernel_size=3,
                               strides=[1, 1],
                               padding="SAME",
                               is_training=is_training)

        # 1st Memory block.
        # 1st Recursive block: bn + relu + conv; bn + relu + conv; skip connection
        c_in = conv1
        for _ in range(2):
            conv01_01 = res_mod_layers(c_in,
                                       num_filters=num_filters,
                                       kernel_size=3,
                                       strides=[1, 1],
                                       padding="SAME",
                                       is_training=is_training)
            c_in = conv01_01
        # skip connection, conv1 + c_in
        # c_in = conv01_01
        eltwise01_01 = conv1 + c_in

        # 2nd Recursive block:bn + relu + conv; bn + relu + conv; skip connection
        c_in = eltwise01_01
        for _ in range(2):
            conv01_02 = res_mod_layers(c_in,
                                       num_filters=num_filters,
                                       kernel_size=3,
                                       strides=[1, 1],
                                       padding="SAME",
                                       is_training=is_training)
            c_in = conv01_02
        # skip connection, eltwise01_01 + c_in
        # c_in = conv01_02
        eltwise01_02 = eltwise01_01 + c_in

        # 3rd Recursive block:bn + relu + conv; bn + relu + conv; skip connection
        c_in = eltwise01_02
        for _ in range(2):
            conv01_03 = res_mod_layers(c_in,
                                       num_filters=num_filters,
                                       kernel_size=3,
                                       strides=[1, 1],
                                       padding="SAME",
                                       is_training=is_training)
            c_in = conv01_03
        # skip connection, eltwise01_02 + c_in
        # c_in = conv01_03
        eltwise01_03 = eltwise01_02 + c_in

        # 4th Recursive block:bn + relu + conv; bn + relu + conv; skip connection
        c_in = eltwise01_03
        for _ in range(2):
            conv01_04 = res_mod_layers(c_in,
                                       num_filters=num_filters,
                                       kernel_size=3,
                                       strides=[1, 1],
                                       padding="SAME",
                                       is_training=is_training)
            c_in = conv01_04
        # skip connection, eltwise01_03 + c_in
        # c_in = conv01_04
        eltwise01_04 = eltwise01_03 + c_in

        # 5th Recursive block:bn + relu + conv; bn + relu + conv; skip connection
        c_in = eltwise01_04
        for _ in range(2):
            conv01_05 = res_mod_layers(c_in,
                                       num_filters=num_filters,
                                       kernel_size=3,
                                       strides=[1, 1],
                                       padding="SAME",
                                       is_training=is_training)
            c_in = conv01_05
        # skip connection, eltwise01_04 + c_in
        # c_in = conv01_05
        eltwise01_05 = eltwise01_04 + c_in

        # 6th Recursive block:bn + relu + conv; bn + relu + conv; skip connection
        c_in = eltwise01_05
        for _ in range(2):
            conv01_06 = res_mod_layers(c_in,
                                       num_filters=num_filters,
                                       kernel_size=3,
                                       strides=[1, 1],
                                       padding="SAME",
                                       is_training=is_training)
            c_in = conv01_06
        # skip connection, eltwise01_05 + c_in
        # c_in = conv01_06
        eltwise01_06 = eltwise01_05 + c_in

        # concat
        concat01 = tf.concat([
            conv1, eltwise01_01, eltwise01_02, eltwise01_03, eltwise01_04,
            eltwise01_05, eltwise01_06
        ],
                             axis=3)
        # tf.concat(). 参数为: values: A list of Tensor. axis.
        # Caffe, data format is NCHW(axis=1). TensorFlow, data format is NHWC(axis=3).

        conv_transition_01 = res_mod_layers(concat01,
                                            num_filters=num_filters,
                                            kernel_size=1,
                                            strides=[1, 1],
                                            padding="SAME",
                                            is_training=is_training)
        # Tensorflow不能指定padding个数. kernel_size=1, stride=1, SAME=VALID

        # 2nd Memory block
        # 1st Recursive block: bn + relu + conv; bn + relu + conv; skip connection
        c_in = conv_transition_01
        for _ in range(2):
            conv02_01 = res_mod_layers(c_in,
                                       num_filters=num_filters,
                                       kernel_size=3,
                                       strides=[1, 1],
                                       padding="SAME",
                                       is_training=is_training)
            c_in = conv02_01
        # skip connection, conv_transition_01 + c_in
        # c_in = conv02_01
        eltwise02_01 = conv_transition_01 + c_in

        # 2nd Recursive block: bn + relu + conv; bn + relu + conv; skip connection
        c_in = eltwise02_01
        for _ in range(2):
            conv02_02 = res_mod_layers(c_in,
                                       num_filters=num_filters,
                                       kernel_size=3,
                                       strides=[1, 1],
                                       padding="SAME",
                                       is_training=is_training)
            c_in = conv02_02
        # skip connection, eltwise02_01 + c_in
        # c_in = conv02_02
        eltwise02_02 = eltwise02_01 + c_in

        # 3rd Recursive block: bn + relu + conv; bn + relu + conv; skip connection
        c_in = eltwise02_02
        for _ in range(2):
            conv02_03 = res_mod_layers(c_in,
                                       num_filters=num_filters,
                                       kernel_size=3,
                                       strides=[1, 1],
                                       padding="SAME",
                                       is_training=is_training)
            c_in = conv02_03
        # skip connection, eltwise02_02 + c_in
        # c_in = conv02_03
        eltwise02_03 = eltwise02_02 + c_in

        # 4th Recursive block: bn + relu + conv; bn + relu + conv; skip connection
        c_in = eltwise02_03
        for _ in range(2):
            conv02_04 = res_mod_layers(c_in,
                                       num_filters=num_filters,
                                       kernel_size=3,
                                       strides=[1, 1],
                                       padding="SAME",
                                       is_training=is_training)
            c_in = conv02_04
        # skip connection, eltwise02_03 + c_in
        # c_in = conv02_04
        eltwise02_04 = eltwise02_03 + c_in

        # 5th Recursive block: bn + relu + conv; bn + relu + conv; skip connection
        c_in = eltwise02_04
        for _ in range(2):
            conv02_05 = res_mod_layers(c_in,
                                       num_filters=num_filters,
                                       kernel_size=3,
                                       strides=[1, 1],
                                       padding="SAME",
                                       is_training=is_training)
            c_in = conv02_05
        # skip connection, eltwise02_04 + c_in
        # c_in = conv02_05
        eltwise02_05 = eltwise02_04 + c_in

        # 6th Recursive block: bn + relu + conv; bn + relu + conv; skip connection
        c_in = eltwise02_05
        for _ in range(2):
            conv02_06 = res_mod_layers(c_in,
                                       num_filters=num_filters,
                                       kernel_size=3,
                                       strides=[1, 1],
                                       padding="SAME",
                                       is_training=is_training)
            c_in = conv02_06
        # skip connection, eltwise02_05 + c_in
        # c_in = conv02_06
        eltwise02_06 = eltwise02_05 + c_in

        # concat
        concat02 = tf.concat([
            conv1, conv_transition_01, eltwise02_01, eltwise02_02,
            eltwise02_03, eltwise02_04, eltwise02_05, eltwise02_06
        ],
                             axis=3)

        conv_transition_02 = res_mod_layers(concat02,
                                            num_filters=num_filters,
                                            kernel_size=1,
                                            strides=[1, 1],
                                            padding="SAME",
                                            is_training=is_training)
        # kernel_size=1, stride=1, SAME=VALID

        # 3rd Memory block
        # 1st Recursive block: bn + relu + conv; bn + relu + conv; skip connection
        c_in = conv_transition_02
        for _ in range(2):
            conv03_01 = res_mod_layers(c_in,
                                       num_filters=num_filters,
                                       kernel_size=3,
                                       strides=[1, 1],
                                       padding="SAME",
                                       is_training=is_training)
            c_in = conv03_01
        # skip connection, conv_transition_02 + c_in
        # c_in = conv03_01
        eltwise03_01 = conv_transition_02 + c_in

        # 2nd Recursive block: bn + relu + conv; bn + relu + conv; skip connection
        c_in = eltwise03_01
        for _ in range(2):
            conv03_02 = res_mod_layers(c_in,
                                       num_filters=num_filters,
                                       kernel_size=3,
                                       strides=[1, 1],
                                       padding="SAME",
                                       is_training=is_training)
            c_in = conv03_02
        # skip connection, eltwise03_01 + c_in
        # c_in = conv03_02
        eltwise03_02 = eltwise03_01 + c_in

        # 3rd Recursive block: bn + relu + conv; bn + relu + conv; skip connection
        c_in = eltwise03_02
        for _ in range(2):
            conv03_03 = res_mod_layers(c_in,
                                       num_filters=num_filters,
                                       kernel_size=3,
                                       strides=[1, 1],
                                       padding="SAME",
                                       is_training=is_training)
            c_in = conv03_03
        # skip connection, eltwise03_02 + c_in
        # c_in = conv03_03
        eltwise03_03 = eltwise03_02 + c_in

        # 4th Recursive block: bn + relu + conv; bn + relu + conv; skip connection
        c_in = eltwise03_03
        for _ in range(2):
            conv03_04 = res_mod_layers(c_in,
                                       num_filters=num_filters,
                                       kernel_size=3,
                                       strides=[1, 1],
                                       padding="SAME",
                                       is_training=is_training)
            c_in = conv03_04
        # skip connection, eltwise03_03 + c_in
        # c_in = conv03_04
        eltwise03_04 = eltwise03_03 + c_in

        # 5th Recursive block: bn + relu + conv; bn + relu + conv; skip connection
        c_in = eltwise03_04
        for _ in range(2):
            conv03_05 = res_mod_layers(c_in,
                                       num_filters=num_filters,
                                       kernel_size=3,
                                       strides=[1, 1],
                                       padding="SAME",
                                       is_training=is_training)
            c_in = conv03_05
        # skip connection, eltwise03_04 + c_in
        # c_in = conv03_05
        eltwise03_05 = eltwise03_04 + c_in

        # 6th Recursive block: bn + relu + conv; bn + relu + conv; skip connection
        c_in = eltwise03_05
        for _ in range(2):
            conv03_06 = res_mod_layers(c_in,
                                       num_filters=num_filters,
                                       kernel_size=3,
                                       strides=[1, 1],
                                       padding="SAME",
                                       is_training=is_training)
            c_in = conv03_06
        # skip connection, eltwise03_05 + c_in
        # c_in = conv03_06
        eltwise03_06 = eltwise03_05 + c_in

        # concat
        concat03 = tf.concat([
            conv1, conv_transition_01, conv_transition_02, eltwise03_01,
            eltwise03_02, eltwise03_03, eltwise03_04, eltwise03_05,
            eltwise03_06
        ],
                             axis=3)

        conv_transition_03 = res_mod_layers(concat03,
                                            num_filters=num_filters,
                                            kernel_size=1,
                                            strides=[1, 1],
                                            padding="SAME",
                                            is_training=is_training)
        # kernel_size=1, stride=1, SAME=VALID

        # 4th Memory block
        # 1st Recursive block: bn + relu + conv; bn + relu + conv; skip connection
        c_in = conv_transition_03
        for _ in range(2):
            conv04_01 = res_mod_layers(c_in,
                                       num_filters=num_filters,
                                       kernel_size=3,
                                       strides=[1, 1],
                                       padding="SAME",
                                       is_training=is_training)
            c_in = conv04_01
        # skip connection, conv_transition_03 + c_in
        # c_in = conv04_01
        eltwise04_01 = conv_transition_03 + c_in

        # 2nd Recursive block: bn + relu + conv; bn + relu + conv; skip connection
        c_in = eltwise04_01
        for _ in range(2):
            conv04_02 = res_mod_layers(c_in,
                                       num_filters=num_filters,
                                       kernel_size=3,
                                       strides=[1, 1],
                                       padding="SAME",
                                       is_training=is_training)
            c_in = conv04_02
        # skip connection, eltwise04_01 + c_in
        # c_in = conv04_02
        eltwise04_02 = eltwise04_01 + c_in

        # 3rd Recursive block: bn + relu + conv; bn + relu + conv; skip connection
        c_in = eltwise04_02
        for _ in range(2):
            conv04_03 = res_mod_layers(c_in,
                                       num_filters=num_filters,
                                       kernel_size=3,
                                       strides=[1, 1],
                                       padding="SAME",
                                       is_training=is_training)
            c_in = conv04_03
        # skip connection, eltwise04_02 + c_in
        # c_in = conv04_03
        eltwise04_03 = eltwise04_02 + c_in

        # 4th Recursive block: bn + relu + conv; bn + relu + conv; skip connection
        c_in = eltwise04_03
        for _ in range(2):
            conv04_04 = res_mod_layers(c_in,
                                       num_filters=num_filters,
                                       kernel_size=3,
                                       strides=[1, 1],
                                       padding="SAME",
                                       is_training=is_training)
            c_in = conv04_04
        # skip connection, eltwise04_03 + c_in
        # c_in = conv04_04
        eltwise04_04 = eltwise04_03 + c_in

        # 5th Recursive block: bn + relu + conv; bn + relu + conv; skip connection
        c_in = eltwise04_04
        for _ in range(2):
            conv04_05 = res_mod_layers(c_in,
                                       num_filters=num_filters,
                                       kernel_size=3,
                                       strides=[1, 1],
                                       padding="SAME",
                                       is_training=is_training)
            c_in = conv04_05
        # skip connection, eltwise04_04 + c_in
        # c_in = conv04_05
        eltwise04_05 = eltwise04_04 + c_in

        # 6th Recursive block: bn + relu + conv; bn + relu + conv; skip connection
        c_in = eltwise04_05
        for _ in range(2):
            conv04_06 = res_mod_layers(c_in,
                                       num_filters=num_filters,
                                       kernel_size=3,
                                       strides=[1, 1],
                                       padding="SAME",
                                       is_training=is_training)
            c_in = conv04_06
        # skip connection, eltwise04_05 + c_in
        # c_in = conv04_06
        eltwise04_06 = eltwise04_05 + c_in

        # concat
        concat04 = tf.concat([
            conv1, conv_transition_01, conv_transition_02, conv_transition_03,
            eltwise04_01, eltwise04_02, eltwise04_03, eltwise04_04,
            eltwise04_05, eltwise04_06
        ],
                             axis=3)

        conv_transition_04 = res_mod_layers(concat04,
                                            num_filters=num_filters,
                                            kernel_size=1,
                                            strides=[1, 1],
                                            padding="SAME",
                                            is_training=is_training)
        # kernel_size=1, stride=1, SAME=VALID

        # 5th Memory block
        # 1st Recursive block: bn + relu + conv; bn + relu + conv; skip connection
        c_in = conv_transition_04
        for _ in range(2):
            conv05_01 = res_mod_layers(c_in,
                                       num_filters=num_filters,
                                       kernel_size=3,
                                       strides=[1, 1],
                                       padding="SAME",
                                       is_training=is_training)
            c_in = conv05_01
        # skip connection, conv_transition_04 + c_in
        # c_in = conv05_01
        eltwise05_01 = conv_transition_04 + c_in

        # 2nd Recursive block: bn + relu + conv; bn + relu + conv; skip connection
        c_in = eltwise05_01
        for _ in range(2):
            conv05_02 = res_mod_layers(c_in,
                                       num_filters=num_filters,
                                       kernel_size=3,
                                       strides=[1, 1],
                                       padding="SAME",
                                       is_training=is_training)
            c_in = conv05_02
        # skip connection, eltwise05_01 + c_in
        # c_in = conv05_02
        eltwise05_02 = eltwise05_01 + c_in

        # 3rd Recursive block: bn + relu + conv; bn + relu + conv; skip connection
        c_in = eltwise05_02
        for _ in range(2):
            conv05_03 = res_mod_layers(c_in,
                                       num_filters=num_filters,
                                       kernel_size=3,
                                       strides=[1, 1],
                                       padding="SAME",
                                       is_training=is_training)
            c_in = conv05_03
        # skip connection, eltwise05_02 + c_in
        # c_in = conv05_03
        eltwise05_03 = eltwise05_02 + c_in

        # 4th Recursive block: bn + relu + conv; bn + relu + conv; skip connection
        c_in = eltwise05_03
        for _ in range(2):
            conv05_04 = res_mod_layers(c_in,
                                       num_filters=num_filters,
                                       kernel_size=3,
                                       strides=[1, 1],
                                       padding="SAME",
                                       is_training=is_training)
            c_in = conv05_04
        # skip connection, eltwise05_03 + c_in
        # c_in = conv05_04
        eltwise05_04 = eltwise05_03 + c_in

        # 5th Recursive block: bn + relu + conv; bn + relu + conv; skip connection
        c_in = eltwise05_04
        for _ in range(2):
            conv05_05 = res_mod_layers(c_in,
                                       num_filters=num_filters,
                                       kernel_size=3,
                                       strides=[1, 1],
                                       padding="SAME",
                                       is_training=is_training)
            c_in = conv05_05
        # skip connection, eltwise05_04 + c_in
        # c_in = conv05_05
        eltwise05_05 = eltwise05_04 + c_in

        # 6th Recursive block: bn + relu + conv; bn + relu + conv; skip connection
        c_in = eltwise05_05
        for _ in range(2):
            conv05_06 = res_mod_layers(c_in,
                                       num_filters=num_filters,
                                       kernel_size=3,
                                       strides=[1, 1],
                                       padding="SAME",
                                       is_training=is_training)
            c_in = conv05_06
        # skip connection, eltwise05_05 + c_in
        # c_in = conv05_06
        eltwise05_06 = eltwise05_05 + c_in

        # concat
        concat05 = tf.concat([
            conv1, conv_transition_01, conv_transition_02, conv_transition_03,
            conv_transition_04, eltwise05_01, eltwise05_02, eltwise05_03,
            eltwise05_04, eltwise05_05, eltwise05_06
        ],
                             axis=3)

        conv_transition_05 = res_mod_layers(concat05,
                                            num_filters=num_filters,
                                            kernel_size=1,
                                            strides=[1, 1],
                                            padding="SAME",
                                            is_training=is_training)
        # kernel_size=1, stride=1, SAME=VALID

        # 6th Memory block
        # 1st Recursive block: bn + relu + conv; bn + relu + conv; skip connection
        c_in = conv_transition_05
        for _ in range(2):
            conv06_01 = res_mod_layers(c_in,
                                       num_filters=num_filters,
                                       kernel_size=3,
                                       strides=[1, 1],
                                       padding="SAME",
                                       is_training=is_training)
            c_in = conv06_01
        # skip connection, conv_transition_05 + c_in
        # c_in = conv06_01
        eltwise06_01 = conv_transition_05 + c_in

        # 2nd Recursive block: bn + relu + conv; bn + relu + conv; skip connection
        c_in = eltwise06_01
        for _ in range(2):
            conv06_02 = res_mod_layers(c_in,
                                       num_filters=num_filters,
                                       kernel_size=3,
                                       strides=[1, 1],
                                       padding="SAME",
                                       is_training=is_training)
            c_in = conv06_02
        # skip connection, eltwise06_01 + c_in
        # c_in = conv06_02
        eltwise06_02 = eltwise06_01 + c_in

        # 3rd Recursive block: bn + relu + conv; bn + relu + conv; skip connection
        c_in = eltwise06_02
        for _ in range(2):
            conv06_03 = res_mod_layers(c_in,
                                       num_filters=num_filters,
                                       kernel_size=3,
                                       strides=[1, 1],
                                       padding="SAME",
                                       is_training=is_training)
            c_in = conv06_03
        # skip connection, eltwise06_02 + c_in
        # c_in = conv06_03
        eltwise06_03 = eltwise06_02 + c_in

        # 4th Recursive block: bn + relu + conv; bn + relu + conv; skip connection
        c_in = eltwise06_03
        for _ in range(2):
            conv06_04 = res_mod_layers(c_in,
                                       num_filters=num_filters,
                                       kernel_size=3,
                                       strides=[1, 1],
                                       padding="SAME",
                                       is_training=is_training)
            c_in = conv06_04
        # skip connection, eltwise06_03 + c_in
        # c_in = conv06_04
        eltwise06_04 = eltwise06_03 + c_in

        # 5th Recursive block: bn + relu + conv; bn + relu + conv; skip connection
        c_in = eltwise06_04
        for _ in range(2):
            conv06_05 = res_mod_layers(c_in,
                                       num_filters=num_filters,
                                       kernel_size=3,
                                       strides=[1, 1],
                                       padding="SAME",
                                       is_training=is_training)
            c_in = conv06_05
        # skip connection, eltwise06_04 + c_in
        # c_in = conv06_05
        eltwise06_05 = eltwise06_04 + c_in

        # 6th Recursive block: bn + relu + conv; bn + relu + conv; skip connection
        c_in = eltwise06_05
        for _ in range(2):
            conv06_06 = res_mod_layers(c_in,
                                       num_filters=num_filters,
                                       kernel_size=3,
                                       strides=[1, 1],
                                       padding="SAME",
                                       is_training=is_training)
            c_in = conv06_06
        # skip connection, eltwise06_05 + c_in
        # c_in = conv06_06
        eltwise06_06 = eltwise06_05 + c_in

        # concat
        concat06 = tf.concat([
            conv1, conv_transition_01, conv_transition_02, conv_transition_03,
            conv_transition_04, conv_transition_05, eltwise06_01, eltwise06_02,
            eltwise06_03, eltwise06_04, eltwise06_05, eltwise06_06
        ],
                             axis=3)

        conv_transition_06 = res_mod_layers(concat06,
                                            num_filters=num_filters,
                                            kernel_size=1,
                                            strides=[1, 1],
                                            padding="SAME",
                                            is_training=is_training)
        # kernel_size=1, stride=1, SAME=VALID

        conv_end_01 = res_mod_layers(conv_transition_01,
                                     num_filters=image_c,
                                     kernel_size=3,
                                     strides=[1, 1],
                                     padding="SAME",
                                     is_training=is_training)
        HR_recovery_01 = conv_end_01 + noisy_data

        if is_training:
            # loss_01, L2 loss
            loss_01 = tf.reduce_mean(
                tf.squared_difference(HR_recovery_01, clean_data))
        # scale, In Tensorflow, Scale use tf.matmul(x, W) + b don't satisfy. tf.matmul shape is same.
        # alpha and beta is Variable, shape=(channel_num,).
        alpha1 = tf.get_variable(
            'alpha1', [image_c],
            initializer=tf.contrib.layers.xavier_initializer(),
            trainable=True)
        weight_output_end_01 = alpha1 * HR_recovery_01

        conv_end_02 = res_mod_layers(conv_transition_02,
                                     num_filters=image_c,
                                     kernel_size=3,
                                     strides=[1, 1],
                                     padding="SAME",
                                     is_training=is_training)
        HR_recovery_02 = conv_end_02 + noisy_data
        if is_training:
            # loss_02, L2 loss
            loss_02 = tf.reduce_mean(
                tf.squared_difference(HR_recovery_02, clean_data))
        # scale
        alpha2 = tf.get_variable(
            'alpha2', [image_c],
            initializer=tf.contrib.layers.xavier_initializer(),
            trainable=True)
        weight_output_end_02 = alpha2 * HR_recovery_02

        conv_end_03 = res_mod_layers(conv_transition_03,
                                     num_filters=image_c,
                                     kernel_size=3,
                                     strides=[1, 1],
                                     padding="SAME",
                                     is_training=is_training)
        HR_recovery_03 = conv_end_03 + noisy_data
        if is_training:
            # loss_03, L2 loss
            loss_03 = tf.reduce_mean(
                tf.squared_difference(HR_recovery_03, clean_data))
        # scale
        alpha3 = tf.get_variable(
            'alpha3', [image_c],
            initializer=tf.contrib.layers.xavier_initializer(),
            trainable=True)
        weight_output_end_03 = alpha3 * HR_recovery_03

        conv_end_04 = res_mod_layers(conv_transition_04,
                                     num_filters=image_c,
                                     kernel_size=3,
                                     strides=[1, 1],
                                     padding="SAME",
                                     is_training=is_training)
        HR_recovery_04 = conv_end_04 + noisy_data
        if is_training:
            # loss_04, L2 loss
            loss_04 = tf.reduce_mean(
                tf.squared_difference(HR_recovery_04, clean_data))
        # scale
        alpha4 = tf.get_variable(
            'alpha4', [image_c],
            initializer=tf.contrib.layers.xavier_initializer(),
            trainable=True)
        weight_output_end_04 = alpha4 * HR_recovery_04

        conv_end_05 = res_mod_layers(conv_transition_05,
                                     num_filters=image_c,
                                     kernel_size=3,
                                     strides=[1, 1],
                                     padding="SAME",
                                     is_training=is_training)
        HR_recovery_05 = conv_end_05 + noisy_data
        if is_training:
            # loss_05, L2 loss
            loss_05 = tf.reduce_mean(
                tf.squared_difference(HR_recovery_05, clean_data))
        # scale
        alpha5 = tf.get_variable(
            'alpha5', [image_c],
            initializer=tf.contrib.layers.xavier_initializer(),
            trainable=True)
        weight_output_end_05 = alpha5 * HR_recovery_05

        conv_end_06 = res_mod_layers(conv_transition_06,
                                     num_filters=image_c,
                                     kernel_size=3,
                                     strides=[1, 1],
                                     padding="SAME",
                                     is_training=is_training)
        HR_recovery_06 = conv_end_06 + noisy_data
        if is_training:
            # loss_06, L2 loss
            loss_06 = tf.reduce_mean(
                tf.squared_difference(HR_recovery_06, clean_data))
        # scale
        alpha6 = tf.get_variable(
            'alpha6', [image_c],
            initializer=tf.contrib.layers.xavier_initializer(),
            trainable=True)
        weight_output_end_06 = alpha6 * HR_recovery_06

        # HR_recovery, MemNet output.
        HR_recovery = weight_output_end_01 + weight_output_end_02 + weight_output_end_03 + weight_output_end_04 + \
            weight_output_end_05 + weight_output_end_06

        if is_training:
            # loss_07
            loss_07 = tf.reduce_mean(
                tf.squared_difference(HR_recovery, clean_data))

            # loss
            loss = loss_01 + loss_02 + loss_03 + loss_04 + loss_05 + loss_06 + loss_07
            # In every loss layer of caffe, loss_weight is 1, so total loss can be that.
            # every subloss  coefficient is 1.
            return HR_recovery, loss
        else:
            return HR_recovery
Example #57
0
    def __init__(self,
                 is_train,
                 aq_stations=10,
                 meo_stations=10,
                 learning_rate=0.001,
                 learning_rate_decay_factor=0.9,
                 aq_features=3,
                 meo_features=25,
                 dist_features=4,
                 keep_prob=0.7):
        self.x_ = tf.placeholder(
            tf.float32,
            (None, meo_stations, meo_features))  # n * meo_n * meo_d
        self.y_ = tf.placeholder(
            tf.float32, (None, aq_stations, aq_features))  # n * aq_n * aq_d
        self.dist_mat = tf.placeholder(
            tf.float32,
            (aq_stations, meo_stations, dist_features))  # aq_n * meo_n * d
        self.keep_prob = keep_prob

        self.x_lin1 = tf.reshape(self.x_, [-1, meo_features])

        lin1 = tf.layers.dense(
            self.x_lin1,
            128,
            use_bias=False,
            kernel_regularizer=tf.contrib.layers.l2_regularizer(LAMBDA),
            activation=None)
        bn1 = tf.layers.batch_normalization(lin1, training=is_train)
        relu1 = tf.nn.relu(bn1)
        relu1_drop = tf.layers.dropout(relu1,
                                       1 - self.keep_prob,
                                       training=is_train)

        lin2 = tf.layers.dense(
            relu1_drop,
            dist_features * 128,
            use_bias=False,
            kernel_regularizer=tf.contrib.layers.l2_regularizer(LAMBDA),
            activation=None)
        bn2 = tf.layers.batch_normalization(lin2, training=is_train)
        relu2 = tf.nn.relu(bn2)

        relu2 = tf.reshape(relu2, [-1, meo_stations, dist_features, 128])
        relu2 = tf.transpose(relu2, (0, 3, 1, 2))  # n * 128 * meo_n * d
        relu2 = tf.reshape(relu2, [-1, meo_stations * dist_features])

        dist_m = tf.transpose(self.dist_mat, (1, 2, 0))
        dist_m = tf.reshape(dist_m, (-1, tf.shape(dist_m)[2]))

        lin3 = tf.matmul(relu2, dist_m)  # 128n * aq_n
        lin3 = tf.reshape(lin3, (-1, 128, aq_stations))
        lin3 = tf.transpose(lin3, (0, 2, 1))

        lin4 = tf.layers.dense(
            lin3,
            128,
            use_bias=True,
            kernel_regularizer=tf.contrib.layers.l2_regularizer(LAMBDA),
            activation=None)

        bn4 = tf.layers.batch_normalization(lin4, training=is_train)
        relu4 = tf.nn.relu(bn4)
        relu4_drop = tf.layers.dropout(relu4,
                                       1 - self.keep_prob,
                                       training=is_train)

        lin5 = tf.layers.dense(
            relu4_drop,
            aq_features,
            use_bias=True,
            kernel_regularizer=tf.contrib.layers.l2_regularizer(LAMBDA),
            activation=None)

        self.pred = lin5

        # self.loss = 2 * tf.reduce_mean(tf.abs(self.pred - self.y_) / (tf.abs(self.pred) + tf.abs(self.y_) + 1e-3))
        self.loss = tf.reduce_mean(tf.squared_difference(self.pred, self.y_))

        self.learning_rate = tf.Variable(float(learning_rate),
                                         trainable=False,
                                         dtype=tf.float32)
        self.learning_rate_decay_op = self.learning_rate.assign(
            self.learning_rate *
            learning_rate_decay_factor)  # Learning rate decay

        self.global_step = tf.Variable(0, trainable=False)
        self.params = tf.trainable_variables()

        update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
        with tf.control_dependencies(update_ops):
            self.train_op = tf.train.GradientDescentOptimizer(
                self.learning_rate).minimize(
                    self.loss,
                    global_step=self.global_step,
                    var_list=self.params)  # Use Adam Optimizer

        self.saver = tf.train.Saver(tf.global_variables(),
                                    write_version=tf.train.SaverDef.V2,
                                    max_to_keep=3,
                                    pad_step_number=True,
                                    keep_checkpoint_every_n_hours=1.0)
Example #58
0
def pre_train(train_data_main, validation_data, test_data):

	# Parameters
	learning_rate = 0.1
	training_epochs = 15
	timesteps = 30
	batch_size = 10000
	display_step = 1

	p = 0.95
	B = 10 

	# Network Parameters
	batch_xs = []
	batch_ys = []
	for [batch_x, batch_y, _] in sequence_build(train_data_main, timesteps = timesteps):
		batch_xs.append(batch_x)
		batch_ys.append(batch_y)
	'''
	print(np.array(batch_xs).shape)
	print(np.array(batch_ys).shape)
	print(np.array(batch_exts).shape)
	train_size = np.array(batch_xs).shape[0]
	'''

	valid_batch_xs = []
	valid_batch_ys = []
	for [batch_x, batch_y, _] in sequence_build(validation_data, timesteps = timesteps):
		valid_batch_xs.append(batch_x)
		valid_batch_ys.append(batch_y)
	'''
	print(np.array(valid_batch_xs).shape)
	print(np.array(valid_batch_ys).shape)
	valid_size = np.array(valid_batch_xs).shape[0]
	'''

	n_input = np.array(batch_xs).shape[-1]
	n_output = np.array(batch_xs).shape[-1] 
	n_hidden = 25

	weights = {
    		'out': tf.Variable(tf.random_normal([n_hidden, n_input]))
	}
	biases = {
    		'out': tf.Variable(tf.random_normal([n_input]))
	}


	X = tf.placeholder("float", [None, timesteps, n_input], name = 'X_enc')
	Y = tf.placeholder("float", [None, n_input], name = 'Y_enc')

	# Construct model
	RNN_out, RNN_state = RNN_2l(X, timesteps, n_hidden, n_hidden)
    	MLP_out = tf.matmul(RNN_out, weights['out']) + biases['out']

	dropouts = []
	dropouts_ = []
	sqr_errors = []
	for i in range(B):
		dropouts.append(tf.nn.dropout(MLP_out, p))
		sqr_errors.append(tf.square(Y - dropouts[-1]))
		dropouts_.append(tf.nn.dropout(RNN_state, p))

	logits = tf.reduce_mean(tf.stack(dropouts), axis = 0)
	sqr_error = tf.reduce_mean(tf.reduce_mean(tf.stack(sqr_errors), axis = 0), axis = 0, name = 'mean_sqr_error')
	states = tf.reduce_mean(tf.stack(dropouts_), axis = 0, name = 'mean_states')

	loss = tf.reduce_mean(Y - logits)
	sqr_loss = tf.reduce_mean(tf.square(Y - logits))
	# Define loss and optimizer
	#loss_op = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=Y))
	loss_op = tf.reduce_mean(tf.squared_difference(Y, logits))
	#loss_op = tf.reduce_mean(tf.reduce_sum(tf.sqrt(logits - Y), -1))
	optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
	train_op = optimizer.minimize(loss_op)
	# Initializing the variables
	init = tf.global_variables_initializer()

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

    		# Training cycle
    		for epoch in range(training_epochs):
      			avg_cost = 0.
			avg_error = 0.
        		total_batch = int(len(batch_xs)/batch_size)
        		# Loop over all batches
        		for i in range(total_batch):
            			#batch_x, batch_y = mnist.train.next_batch(batch_size)
				batch_x = np.array(batch_xs)[i * batch_size: min(i * batch_size + batch_size, len(batch_xs))]
				batch_y = np.array(batch_ys)[i * batch_size: min(i * batch_size + batch_size, len(batch_xs))]
            			# Run optimization op (backprop) and cost op (to get loss value)
            			_, l, e = sess.run([train_op, sqr_loss, sqr_error], feed_dict={X: batch_x,
                                                            			Y: batch_y})
            			# Compute average loss
            			avg_cost += l / total_batch
				avg_error += e / total_batch
        			# Display logs per epoch step
        		if epoch % display_step == 0:
            			print("Epoch:", '%04d' % (epoch+1), "sqr_loss = ", str(avg_cost), "sqr_error=", str(avg_error))
            			#print("Epoch:", '%04d' % (epoch+1), "cost={:.9f}".format(l))
    		print("Optimization Finished!")

		sqr_error_val = sqr_error.eval({X: valid_batch_xs, Y: valid_batch_ys})
		#error_val = math.sqrt(error_val)
		print("Validation sqr_error %0.4f" % sqr_error_val)

		output_file = open('logfile', 'w')
		output_file.write(str(train_data_main) + ':' + str(validation_data) + ':' + str(test_data) + '\n')
		output_file.write('train_error:' + str(avg_error) + '\n')

		
		tf.train.Saver().save(sess, './encoder')
Example #59
0
 def loss(self):
     loss = tf.reduce_mean(tf.squared_difference(self.target, self.prediction), name='lossOp_')
     return loss
Example #60
0
tf.rsqrt()
tf.sign()
tf.sin()
tf.sqrt()
tf.square()
'''
special math functions
'''
tf.digamma()
tf.erf()
tf.erfc()
tf.igamma()
tf.igammac()
tf.lbeta()
tf.lgamma()
tf.squared_difference()
'''
Tangent function (tan(pi/4)=1)
'''
print(sess.run(tf.div(tf.div(tf.sin(3.1416 / 4.), tf.cos(3.1416 / 4.)))))


def custom_polynomial(value):
    return (tf.sub(3 * tf.square(value), value) + 10)


print(sess.run(custom_polynomial(11)))
'''
11.Activation Functions
---------------for introducing nonliearities in neural networks or other computational graphs in the future.
'''