Esempio n. 1
0
def get_top_nearest_neigbors(num_generated, nearneig, real_features_hdf5, gen_features_hdf5, maximum=False, random_select=False, save_path=None):

    real_img_hdf5 = real_features_hdf5.replace('_features_', '_images_')
    gen_img_hdf5 = gen_features_hdf5.replace('_features_', '_images_')

    real_features_file = h5py.File(real_features_hdf5, 'r')
    gen_features_file = h5py.File(gen_features_hdf5, 'r')
    real_img_file = h5py.File(real_img_hdf5, 'r')
    gen_img_file = h5py.File(gen_img_hdf5, 'r')

    real_features = real_features_file['features']
    gen_features = gen_features_file['features']
    real_img = real_img_file['images']
    gen_img = gen_img_file['images']

    with tf.Session() as sess:
        real_features = tf.constant(np.array(real_features), dtype=tf.float32)
        gen_features = tf.constant(np.array(gen_features), dtype=tf.float32)

        # Get Nearest Neighbors for all generated images.
        gen_real_distances = tf.sqrt(tf.abs(euclidean_distance(gen_features, real_features)))
        neg = tf.negative(gen_real_distances)
        neg_s_distances, s_indices = tf.math.top_k(input=neg, k=nearneig, sorted=True)
        s_distances = tf.negative(neg_s_distances)


        # Getting the top smallest distances between Generated and Real images.
        neg_s_distances1, s_indices1 = tf.math.top_k(input=neg, k=1, sorted=True)
        neg_s_distances1 = tf.transpose(neg_s_distances1)
        if not random_select:
            if maximum:
                neg_s_distances1 = tf.negative(neg_s_distances1)
            neg_s_distances1, s_indices1 = tf.math.top_k(input=neg_s_distances1, k=num_generated, sorted=True)
            s_indices1 = tf.transpose(s_indices1)
            s_indices1 = s_indices1.eval()
        else:
            lin = list(range(int(gen_real_distances.shape[0])))
            random.shuffle(lin)
            s_indices1 = np.zeros((num_generated,1), dtype=np.int8)
            s_indices1[:, 0] = lin[:num_generated]
            
        s_indices = s_indices.eval()
        s_distances = s_distances.eval()
        # For the images with top smallest distances, show nearest neighbors.
        height, width, channels = real_img.shape[1:]
        neighbors = dict()
        grid = np.zeros((num_generated*height, (nearneig+1)*width, channels))
        for i, ind in enumerate(s_indices1):
            ind = ind[0]
            total = gen_img[ind]
            neighbors[ind] = list() 
            for j in range(nearneig):
                neighbors[ind].append((s_indices[ind,j], s_distances[ind,j]))
                real = real_img[s_indices[ind,j]]/255.
                total = np.concatenate([total, real], axis=1)
            grid[i*height:(i+1)*height, :, :] = total
        plt.imshow(grid)
        if save_path is not None:
            plt.imsave(save_path, grid)
        return neighbors
Esempio n. 2
0
def logG(x, y, theta):
    fv = tff(theta,y)
    gv = tfg(theta,y)
    mu = tf.add(y,tf.multiply(fv,gl.h))
    pr = tf.subtract(x,mu)
    pr2 = tf.square(pr)
    gv2 = tf.square(gv)
    my2 = tf.constant(2.0,dtype=gl.myftype)
    mypi = tf.constant(np.pi,dtype=gl.myftype)
    lgp1 = tf.negative(tf.divide(tf.log(tf.multiply(my2*mypi*gl.h,gv2)),my2))
    lgp2 = tf.negative(tf.divide(pr2,tf.multiply(my2*gl.h,gv2)))
    lg = tf.add(lgp1,lgp2)        
    return lg
 def test_all(self):
     with self.test_context() as session:
         models = self.prepare()
         likelihoods = []
         for m in models:
             opt = gpflow.train.ScipyOptimizer()
             opt.minimize(m, maxiter=300)
             neg_obj = tf.negative(m.objective)
             likelihoods.append(session.run(neg_obj).squeeze())
         assert_allclose(likelihoods, likelihoods[0], rtol=1e-6)
         variances, lengthscales = [], []
         for m in models:
             if hasattr(m.kern, 'rbf'):
                 variances.append(m.kern.rbf.variance.read_value())
                 lengthscales.append(m.kern.rbf.lengthscales.read_value())
             else:
                 variances.append(m.kern.variance.read_value())
                 lengthscales.append(m.kern.lengthscales.read_value())
         variances, lengthscales = np.array(variances), np.array(lengthscales)
         assert_allclose(variances, variances[0], 1e-5)
         assert_allclose(lengthscales, lengthscales.mean(), 1e-4)
         mu0, var0 = models[0].predict_y(self.Xtest)
         for i, m in enumerate(models[1:]):
             mu, var = m.predict_y(self.Xtest)
             assert_allclose(mu, mu0, 1e-3)
             assert_allclose(var, var0, 1e-4)
Esempio n. 4
0
    def __init__(self, product_size, embedding_size, batch_size):
        self.batch_size = batch_size
        self.graph = tf.Graph()

        with self.graph.as_default():
            self.train_inputs = tf.placeholder(tf.int32, shape=[batch_size])
            self.train_labels = tf.placeholder(tf.int32, shape=[batch_size, 1])

            with tf.device('/cpu:0'):
                embeddings = tf.Variable(tf.random_uniform([product_size, embedding_size], -1.0, 1.0))
                embed = tf.nn.embedding_lookup(embeddings, self.train_inputs)

                output_embed = tf.nn.embedding_lookup(embeddings, self.train_labels)

            weights = tf.Variable(tf.random_normal([embedding_size, embedding_size]))
            bias = tf.Variable(tf.random_normal([embedding_size]))
            output_layer = tf.matmul(embed, weights) + bias

            self.loss = tf.reduce_sum(tf.abs(tf.add(output_layer, tf.negative(output_embed))), reduction_indices=1)
            self.optimizer = tf.train.GradientDescentOptimizer(learning_rate=1.0).minimize(self.loss)

            norm = tf.sqrt(tf.reduce_sum(tf.square(embeddings), 1, keep_dims=True))
            self.normalized_embeddings = embeddings / norm

            self.init = tf.initialize_all_variables()
Esempio n. 5
0
    def build_graph(self, image_pos):
        image_pos = image_pos / 128.0 - 1

        z = tf.random_normal([self.batch, self.zdim], name='z_train')
        z = tf.placeholder_with_default(z, [None, self.zdim], name='z')

        with argscope([Conv2D, Conv2DTranspose, FullyConnected],
                      kernel_initializer=tf.truncated_normal_initializer(stddev=0.02)):
            with tf.variable_scope('gen'):
                image_gen = self.generator(z)
            tf.summary.image('generated-samples', image_gen, max_outputs=30)

            alpha = tf.random_uniform(shape=[self.batch, 1, 1, 1],
                                      minval=0., maxval=1., name='alpha')
            interp = image_pos + alpha * (image_gen - image_pos)

            with tf.variable_scope('discrim'):
                vecpos = self.discriminator(image_pos)
                vecneg = self.discriminator(image_gen)
                vec_interp = self.discriminator(interp)

        # the Wasserstein-GAN losses
        self.d_loss = tf.reduce_mean(vecneg - vecpos, name='d_loss')
        self.g_loss = tf.negative(tf.reduce_mean(vecneg), name='g_loss')

        # the gradient penalty loss
        gradients = tf.gradients(vec_interp, [interp])[0]
        gradients = tf.sqrt(tf.reduce_sum(tf.square(gradients), [1, 2, 3]))
        gradients_rms = symbolic_functions.rms(gradients, 'gradient_rms')
        gradient_penalty = tf.reduce_mean(tf.square(gradients - 1), name='gradient_penalty')
        add_moving_summary(self.d_loss, self.g_loss, gradient_penalty, gradients_rms)

        self.d_loss = tf.add(self.d_loss, 10 * gradient_penalty)

        self.collect_variables()
Esempio n. 6
0
 def disable_some_fgs():
     # We want to delete a randomly-selected subset of fg_inds of
     # size `fg_inds.shape[0] - max_fg`.
     # We shuffle along the dimension 0 and then we get the first
     # num_fg_inds - max_fg indices and we disable them.
     shuffled_inds = tf.random_shuffle(fg_inds, seed=self._seed)
     disable_place = (tf.shape(fg_inds)[0] - max_fg)
     # This function should never run if num_fg_inds <= max_fg, so we
     # add an assertion to catch the wrong behaviour if it happens.
     integrity_assertion = tf.assert_positive(
         disable_place,
         message="disable_place in disable_some_fgs is negative."
     )
     with tf.control_dependencies([integrity_assertion]):
         disable_inds = shuffled_inds[:disable_place]
     is_disabled = tf.sparse_to_dense(
         sparse_indices=disable_inds,
         sparse_values=True, default_value=False,
         output_shape=tf.cast(proposals_label_shape, tf.int64),
         # We are shuffling the indices, so they may not be ordered.
         validate_indices=False
     )
     return tf.where(
         condition=is_disabled,
         # We set it to -label for debugging purposes.
         x=tf.negative(proposals_label),
         y=proposals_label
     )
Esempio n. 7
0
def cross_entropy_loss(y, yhat):
    """
    Compute the cross entropy loss in tensorflow.
    The loss should be summed over the current minibatch.

    y is a one-hot tensor of shape (n_samples, n_classes) and yhat is a tensor
    of shape (n_samples, n_classes). y should be of dtype tf.int32, and yhat should
    be of dtype tf.float32.

    The functions tf.to_float, tf.reduce_sum, and tf.log might prove useful. (Many
    solutions are possible, so you may not need to use all of these functions).

    Note: You are NOT allowed to use the tensorflow built-in cross-entropy
                functions.

    Args:
        y:    tf.Tensor with shape (n_samples, n_classes). One-hot encoded.
        yhat: tf.Tensorwith shape (n_sample, n_classes). Each row encodes a
                    probability distribution and should sum to 1.
    Returns:
        out:  tf.Tensor with shape (1,) (Scalar output). You need to construct this
                    tensor in the problem.
    """

    ### YOUR CODE HERE
    l_yhat = tf.log(yhat)                           # log yhat
    product = tf.multiply(tf.to_float(y), l_yhat)   # multiply element-wise
    out = tf.negative(tf.reduce_sum(product))       # negative summation to scalar
    ### END YOUR CODE

    return out
def gabor(n_values=32, sigma=1.0, mean=0.0):
	x = tf.linspace(-3.0, 3.0, n_values)
	z = (tf.exp(tf.negative(tf.pow(x - mean, 2.0)/ (2.0 * tf.pow(sigma, 2.0)))) * (1.0 / (sigma * tf.sqrt(2.0 * 3.145))))
	gauss_kernel = tf.matmul(tf.reshape(z, [n_values, 1]), tf.reshape(z,[1, n_values]))
	x = tf.reshape(tf.sin(tf.linspace(-3.0, 3.0, n_values)), [n_values, 1])
	y = tf.reshape(tf.ones_like(x), [1, n_values])
	gabor_kernel = tf.multiply(tf.matmul(x ,y), gauss_kernel)
	return gabor_kernel
    def build_graph(self, graph):
        self.xtr = tf.placeholder(dtype=tf.float32, shape=[None, 784])
        self.xte = tf.placeholder(dtype=tf.float32, shape=[784])    # one vector compares with all in self.xtr
        self.distance = tf.reduce_sum(tf.abs(tf.add(self.xtr, tf.negative(self.xte))), reduction_indices=1)
        self.pred = tf.argmin(self.distance, 0)

        self.global_step_t = tf.Variable(0, trainable=False, name='global_step_t')

        return graph
Esempio n. 10
0
def integrandmat(inx, iny, th):
    my2 = tf.constant(2.0,gl.myftype)
    tfmu = tf.add(iny,tf.multiply(tff(theta=th,x=iny),gl.h))
    tfsig = tf.multiply(tf.sqrt(gl.h),tfg(theta=th,x=iny))
    tfc0 = tf.reciprocal(tf.multiply(tf.sqrt(tf.multiply(my2,tf.constant(np.pi,dtype=gl.myftype))),tfsig))
    tfnumer = tf.negative(tf.square(tf.subtract(inx,tfmu)))
    tfdenom = tf.multiply(my2,tf.square(tfsig))
    tfprop = tf.multiply(tfc0,tf.exp(tf.divide(tfnumer,tfdenom)))
    return tfprop
Esempio n. 11
0
  def calculate_loss(self, predictions, labels, **unused_params):
    with tf.name_scope("loss_xent"):
      epsilon = 10e-6
      alpha = FLAGS.alpha

      float_labels = tf.cast(labels, tf.float32)
      cross_entropy_loss = 2*(alpha*float_labels * tf.log(predictions + epsilon) + (1-alpha)*(
          1 - float_labels) * tf.log(1 - predictions + epsilon))
      cross_entropy_loss = tf.negative(cross_entropy_loss)
      return tf.reduce_mean(tf.reduce_sum(cross_entropy_loss, 1))
Esempio n. 12
0
def find_top_nearest_neigbors(generated_list, nearneig, real_features_hdf5, gen_features_hdf5, maximum=False, save_path=None):
    real_img_hdf5 = real_features_hdf5.replace('_features_', '_images_')
    gen_img_hdf5 = gen_features_hdf5.replace('_features_', '_images_')

    real_features_file = h5py.File(real_features_hdf5, 'r')
    gen_features_file = h5py.File(gen_features_hdf5, 'r')
    real_img_file = h5py.File(real_img_hdf5, 'r')
    gen_img_file = h5py.File(gen_img_hdf5, 'r')

    real_features = real_features_file['features']
    gen_features = gen_features_file['features']
    real_img = real_img_file['images']
    gen_img = gen_img_file['images']

    with tf.Session() as sess:
        real_features = tf.constant(np.array(real_features), dtype=tf.float32)
        gen_features = tf.constant(np.array(gen_features), dtype=tf.float32)

        # Get Nearest Neighbors for all generated images.
        gen_real_distances = tf.sqrt(tf.abs(euclidean_distance(gen_features, real_features)))
        neg = tf.negative(gen_real_distances)
        neg_s_distances, s_indices = tf.math.top_k(input=neg, k=nearneig, sorted=True)
        s_distances = tf.negative(neg_s_distances)

        s_indices = s_indices.eval()
        s_distances = s_distances.eval()
        # For the images with top smallest distances, show nearest neighbors.
        height, width, channels = real_img.shape[1:]
        neighbors = dict()
        grid = np.zeros((len(generated_list)*height, (nearneig+1)*width, channels))
        for i, ind in enumerate(generated_list):
            total = gen_img[ind]
            neighbors[ind] = list() 
            for j in range(nearneig):
                neighbors[ind].append((s_indices[ind,j], s_distances[ind,j]))
                real = real_img[s_indices[ind,j]]/255.
                total = np.concatenate([total, real], axis=1)
            grid[i*height:(i+1)*height, :, :] = total
        plt.imshow(grid)
        if save_path is not None:
            plt.imsave(save_path, grid)
        return neighbors
Esempio n. 13
0
    def create_network(self):
        networks = {}

        with tf.variable_scope('q_net'):

            # Input parameters
            x = networks['x'] = tf.placeholder(tf.float32, \
                            shape=[None, self.states], name='states')
            u = networks['u'] = tf.placeholder(tf.float32, \
                            shape=[None, self.actions], name='actions')

            # hidden layers
            init = 1./self.hidden_nodes/self.actions

            hid = tf.concat([x,  u], axis=1)
            hid = fully_connected(hid, self.hidden_nodes, \
                weights_initializer=tf.random_normal_initializer(init, init/5), \
                biases_initializer=tf.random_normal_initializer(init, init/5), \
                activation_fn=tf.tanh)

            for i in range(self.hidden_layers-1):
                hid = fully_connected(hid, self.hidden_nodes, \
                    weights_initializer=tf.random_normal_initializer(init, init/5), \
                    biases_initializer=tf.random_normal_initializer(init, init/5), \
                    activation_fn=tf.nn.relu)

            # Output parameters
            pos_layer = fully_connected(hid, 1, \
                weights_initializer=tf.random_normal_initializer(1./self.actions, 0.1), \
                biases_initializer=tf.random_normal_initializer(1./self.actions, 0.1))
            neg_layer = tf.negative(fully_connected(hid, 1, \
                weights_initializer=tf.random_normal_initializer(1./self.actions, 0.1), \
                biases_initializer=tf.random_normal_initializer(1./self.actions, 0.1)))

            Q = networks['Q'] = pos_layer + neg_layer

            # Describe loss functions.
            y_ = networks['y_'] = tf.placeholder(tf.float32, [None, 1], name='y_i')


            # Tensor outputs to calculate y_i values
            networks['reward'] = tf.placeholder(tf.float32, [None, 1], name='reward')
            networks['y_calc'] = tf.add(networks['reward'], tf.multiply(Q, self.gamma))

            networks['mse'] = tf.reduce_mean(tf.squared_difference(y_, \
                            Q), name='mse')
            networks['cross_entropy'] = -tf.reduce_sum(y_ * tf.log(Q), name='cross_entropy')
                            
            networks['optimize'] = tf.train.AdamOptimizer(\
                        learning_rate=self.alpha) \
                        .minimize(networks['mse'])
        
        self.tensors = networks
        return
Esempio n. 14
0
def _GradientReversalGrad(_, grad):
  """The gradients for `gradient_reversal`.

  Args:
    _: The `gradient_reversal` `Operation` that we are differentiating,
      which we can use to find the inputs and outputs of the original op.
    grad: Gradient with respect to the output of the `gradient_reversal` op.

  Returns:
    Gradient with respect to the input of `gradient_reversal`, which is simply
    the negative of the input gradient.

  """
  return tf.negative(grad)
def k_nearest_neighbor_tf_part(x, y, k):
    x_samples = tf.shape(x)[0]
    y_samples = tf.shape(y)[0]

    xx_d = euclidean_distance(x, x)
    yy_d = euclidean_distance(y, y)
    xy_d = euclidean_distance(x, y)

    labels = tf.concat([tf.ones((x_samples,1)), tf.zeros((y_samples,1))], axis=0)

    x_dist = tf.concat([xx_d, xy_d], axis=-1)
    y_dist = tf.concat([tf.transpose(xy_d), yy_d], axis=-1)
    total_dist = tf.concat([x_dist, y_dist], axis=0)
    '''
    x1x1   x1x2   ... x1x100   | x1y1   x1xy2   ... x1y200
    ...						   |   				  ...
    x100x1 x100x2 ... x100x100 | x100y1 x100xy2 ... x100y200
    ________________________________________________________
    y1x1   y1x2   ... y1x100   | y1y1   y1xy2   ... y1y200
    ...						   |  				  ...
    y100x1 y100x2 ... y100x100 | y100y1 y1xy2   ... y100y100
    ...						   |  				  ...
    y200x1 y200x2 ... y200x100 | y200y1 y200xy2 ... y200y200

    Diagonals of this tensor are the distance for the vector with itself.
    '''
    total_dist = tf.sqrt(tf.abs(total_dist))
    inf_eye = tf.eye(tf.shape(total_dist)[0])*1e+7

    #All element positive now, no smallest elements functions.
    all_dist = tf.math.add(inf_eye, total_dist)
    neg_all_dist = tf.negative(all_dist)
    values, indices = tf.math.top_k(input=neg_all_dist, k=k, sorted=True)
    values = tf.negative(values)

    return indices, labels
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
        
Esempio n. 17
0
  def testGradientReversalOp(self):
    with tf.Graph().as_default():
      with self.test_session():
        # Test that in forward prop, gradient reversal op acts as the
        # identity operation.
        examples = tf.constant([5.0, 4.0, 3.0, 2.0, 1.0])
        output = grl_ops.gradient_reversal(examples)
        expected_output = examples
        self.assertAllEqual(output.eval(), expected_output.eval())

        # Test that shape inference works as expected.
        self.assertAllEqual(output.get_shape(), expected_output.get_shape())

        # Test that in backward prop, gradient reversal op multiplies
        # gradients by -1.
        examples = tf.constant([[1.0]])
        w = tf.get_variable(name='w', shape=[1, 1])
        b = tf.get_variable(name='b', shape=[1])
        init_op = tf.global_variables_initializer()
        init_op.run()
        features = tf.nn.xw_plus_b(examples, w, b)
        # Construct two outputs: features layer passes directly to output1, but
        # features layer passes through a gradient reversal layer before
        # reaching output2.
        output1 = features
        output2 = grl_ops.gradient_reversal(features)
        gold = tf.constant([1.0])
        loss1 = gold - output1
        loss2 = gold - output2
        opt = tf.train.GradientDescentOptimizer(learning_rate=0.01)
        grads_and_vars_1 = opt.compute_gradients(loss1,
                                                 tf.trainable_variables())
        grads_and_vars_2 = opt.compute_gradients(loss2,
                                                 tf.trainable_variables())
        self.assertAllEqual(len(grads_and_vars_1), len(grads_and_vars_2))
        for i in range(len(grads_and_vars_1)):
          g1 = grads_and_vars_1[i][0]
          g2 = grads_and_vars_2[i][0]
          # Verify that gradients of loss1 are the negative of gradients of
          # loss2.
          self.assertAllEqual(tf.negative(g1).eval(), g2.eval())
Esempio n. 18
0
def qfun(theta, allout, init, final):
    q = []

    # first term in the summation (j=1 case)
    part1 = logG(gl.grid,init,theta)
    q.append(tf.reduce_sum(tf.multiply(part1,allout[1][:,0]))*gl.k)

    # last term in the summation (j=F case)
    part2 = logG(final,gl.grid,theta)
    q.append(tf.reduce_sum(tf.multiply(part2,allout[2][:,0]))*gl.k)
    
    # all intermediate terms
    part3 = logG(gl.gridx,gl.gridy,theta)
    # for j in range(gl.numsteps-2):
    #     q.append(tf.tensordot(part3,allout[3][j,:],axes=[[0,1],[0,1]])*gl.k*gl.k)
    # test = tf.add_n(test)
    
    q.append(tf.reduce_sum(tf.multiply(tf.expand_dims(part3,0),allout[3]))*gl.k*gl.k)
    
    qout = tf.negative(tf.add_n(q))

    return qout
Esempio n. 19
0
File: utils.py Progetto: gojira/CADL
def gauss(mean, stddev, ksize):
    """Use Tensorflow to compute a Gaussian Kernel.

    Parameters
    ----------
    mean : float
        Mean of the Gaussian (e.g. 0.0).
    stddev : float
        Standard Deviation of the Gaussian (e.g. 1.0).
    ksize : int
        Size of kernel (e.g. 16).

    Returns
    -------
    kernel : np.ndarray
        Computed Gaussian Kernel using Tensorflow.
    """
    g = tf.Graph()
    with tf.Session(graph=g):
        x = tf.linspace(-3.0, 3.0, ksize)
        z = (tf.exp(tf.negative(tf.pow(x - mean, 2.0) /
                           (2.0 * tf.pow(stddev, 2.0)))) *
             (1.0 / (stddev * tf.sqrt(2.0 * 3.1415))))
        return z.eval()
Esempio n. 20
0
def _word_dropout(words, input_keep_prob):
  """Drops words with probability 1 - input_keep_prob.

  Args:
    words: a list of lemmas from the paths.
    input_keep_prob: the probability to keep the word.

  Returns:
    The revised list where some of the words are <UNK>ed.
  """
  # Create the mask: (-1) to drop, 1 to keep
  prob = tf.random_uniform(tf.shape(words), 0, 1)
  condition = tf.less(prob, (1 - input_keep_prob))
  mask = tf.where(condition,
                  tf.negative(tf.ones_like(words)), tf.ones_like(words))

  # We need to keep zeros (<PAD>), and change other numbers to 1 (<UNK>)
  # if their mask is -1. First, we multiply the mask and the words.
  # Zeros will stay zeros, and words to drop will become negative.
  # Then, we change negative values to 1.
  masked_words = tf.multiply(mask, words)
  condition = tf.less(masked_words, 0)
  dropped_words = tf.where(condition, tf.ones_like(words), words)
  return dropped_words
Esempio n. 21
0
    def _build_likelihood(self):
        """
        Construct a tensorflow function to compute the bound on the marginal
        likelihood. For a derivation of the terms in here, see the associated
        SGPR notebook.
        """

        num_inducing = len(self.feature)
        num_data = tf.cast(tf.shape(self.Y)[0], settings.float_type)
        output_dim = tf.cast(tf.shape(self.Y)[1], settings.float_type)

        err = self.Y - self.mean_function(self.X)
        Kdiag = self.kern.Kdiag(self.X)
        Kuf = self.feature.Kuf(self.kern, self.X)
        Kuu = self.feature.Kuu(self.kern, jitter=settings.numerics.jitter_level)
        L = tf.cholesky(Kuu)
        sigma = tf.sqrt(self.likelihood.variance)

        # Compute intermediate matrices
        A = tf.matrix_triangular_solve(L, Kuf, lower=True) / sigma
        AAT = tf.matmul(A, A, transpose_b=True)
        B = AAT + tf.eye(num_inducing, dtype=settings.float_type)
        LB = tf.cholesky(B)
        Aerr = tf.matmul(A, err)
        c = tf.matrix_triangular_solve(LB, Aerr, lower=True) / sigma

        # compute log marginal bound
        bound = -0.5 * num_data * output_dim * np.log(2 * np.pi)
        bound += tf.negative(output_dim) * tf.reduce_sum(tf.log(tf.matrix_diag_part(LB)))
        bound -= 0.5 * num_data * output_dim * tf.log(self.likelihood.variance)
        bound += -0.5 * tf.reduce_sum(tf.square(err)) / self.likelihood.variance
        bound += 0.5 * tf.reduce_sum(tf.square(c))
        bound += -0.5 * output_dim * tf.reduce_sum(Kdiag) / self.likelihood.variance
        bound += 0.5 * output_dim * tf.reduce_sum(tf.matrix_diag_part(AAT))

        return bound
Esempio n. 22
0
 def _flip_gradients(op, grad):
     return [tf.negative(grad) * hp_lambda]
my_kernel = tf.exp(tf.multiply(gamma, tf.abs(sq_dists)))


# Declare function to do reshape/batch multiplication
def reshape_matmul(mat, _size):
    v1 = tf.expand_dims(mat, 1)
    v2 = tf.reshape(v1, [3, _size, 1])
    return tf.matmul(v2, v1)

# Compute SVM Model
first_term = tf.reduce_sum(b)
b_vec_cross = tf.matmul(tf.transpose(b), b)
y_target_cross = reshape_matmul(y_target, batch_size)

second_term = tf.reduce_sum(tf.multiply(my_kernel, tf.multiply(b_vec_cross, y_target_cross)), [1, 2])
loss = tf.reduce_sum(tf.negative(tf.subtract(first_term, second_term)))

# Gaussian (RBF) prediction kernel
rA = tf.reshape(tf.reduce_sum(tf.square(x_data), 1), [-1, 1])
rB = tf.reshape(tf.reduce_sum(tf.square(prediction_grid), 1), [-1, 1])
pred_sq_dist = tf.add(tf.subtract(rA, tf.multiply(2., tf.matmul(x_data, tf.transpose(prediction_grid)))), tf.transpose(rB))
pred_kernel = tf.exp(tf.multiply(gamma, tf.abs(pred_sq_dist)))

prediction_output = tf.matmul(tf.multiply(y_target, b), pred_kernel)
prediction = tf.argmax(prediction_output - tf.expand_dims(tf.reduce_mean(prediction_output, 1), 1), 0)
accuracy = tf.reduce_mean(tf.cast(tf.equal(prediction, tf.argmax(y_target, 0)), tf.float32))

# Declare optimizer
my_opt = tf.train.GradientDescentOptimizer(0.01)
train_step = my_opt.minimize(loss)
Esempio n. 24
0
 def build_losses(self, vecpos, vecneg):
     # the Wasserstein-GAN losses
     self.d_loss = tf.reduce_mean(vecneg - vecpos, name='d_loss')
     self.g_loss = tf.negative(tf.reduce_mean(vecneg), name='g_loss')
     add_moving_summary(self.d_loss, self.g_loss)
Esempio n. 25
0
 def logprob_grads():
     logprob = tf.negative(model.build_objective())
     grads = tf.gradients(logprob, xs)
     return logprob, grads
Esempio n. 26
0
 def _flip_gradients(op, grad):
     return [tf.negative(grad) * l]
Esempio n. 27
0
File: DRL.py Progetto: wsgan001/DRL
    def buildNetwork(self):
        self.stateTrain = tf.placeholder(tf.float32,
                                         shape=[None, self.inputSize],
                                         name="stateTrain")
        self.critic_rewards = tf.placeholder(tf.float32,
                                             shape=[None],
                                             name="critic_rewards")

        self.new_lr = tf.placeholder(tf.float32,
                                     shape=[],
                                     name="learning_rate")
        self.lr = tf.Variable(0.01, trainable=False)

        # PolicyNetwork

        with tf.variable_scope("Policy"):

            self.weights1 = tf.placeholder(
                tf.float32,
                shape=[self.inputSize, self.hiddenSize],
                name="weights1")
            self.biases1 = tf.placeholder(tf.float32,
                                          shape=[self.hiddenSize],
                                          name="biases1")
            self.weights2 = tf.placeholder(
                tf.float32,
                shape=[self.hiddenSize, self.hiddenSize],
                name="weights2")
            self.biases2 = tf.placeholder(tf.float32,
                                          shape=[self.hiddenSize],
                                          name="biases2")
            self.weights3 = tf.placeholder(
                tf.float32,
                shape=[self.hiddenSize, self.hiddenSize],
                name="weights3")
            self.biases3 = tf.placeholder(tf.float32,
                                          shape=[self.hiddenSize],
                                          name="biases3")
            #变量保存
            self.w1 = tf.get_variable(
                "w1", [self.inputSize, self.hiddenSize],
                dtype=tf.float32,
                initializer=tf.truncated_normal_initializer(
                    stddev=1.0 / math.sqrt(float(self.hiddenSize))),
                trainable=True)
            self.b1 = tf.get_variable("b1", [self.hiddenSize],
                                      dtype=tf.float32,
                                      initializer=tf.zeros_initializer(),
                                      trainable=True)
            self.w2 = tf.get_variable(
                "w2", [self.hiddenSize, self.hiddenSize],
                dtype=tf.float32,
                initializer=tf.truncated_normal_initializer(
                    stddev=1.0 / math.sqrt(float(self.hiddenSize))),
                trainable=True)
            self.b2 = tf.get_variable("b2", [self.hiddenSize],
                                      dtype=tf.float32,
                                      initializer=tf.zeros_initializer(),
                                      trainable=True)
            self.w3 = tf.get_variable(
                "w3", [self.hiddenSize, self.hiddenSize],
                dtype=tf.float32,
                initializer=tf.truncated_normal_initializer(
                    stddev=1.0 / math.sqrt(float(self.hiddenSize))),
                trainable=True)
            self.b3 = tf.get_variable("b3", [self.hiddenSize],
                                      dtype=tf.float32,
                                      initializer=tf.zeros_initializer(),
                                      trainable=True)

            self.update_w1 = tf.assign(self.w1, self.weights1)
            self.update_w2 = tf.assign(self.w2, self.weights2)
            self.update_w3 = tf.assign(self.w3, self.weights3)
            self.update_b1 = tf.assign(self.b1, self.biases1)
            self.update_b2 = tf.assign(self.b2, self.biases2)
            self.update_b3 = tf.assign(self.b3, self.biases3)

            activation = tf.nn.relu

            L0 = activation(tf.matmul(self.stateTrain, self.w1) + self.b1)
            L1 = activation(tf.matmul(L0, self.w2) + self.b2)
            L2 = activation(tf.matmul(L1, self.w2) + self.b2)

            #L2 = tf.reshape(L2, [-1, self.stepNum, self.hiddenSize])
            L2 = tf.reshape(L2, [1, -1, self.hiddenSize])
            #construct a lstmcell ,the size is neuronNum
            lstmcell = tf.contrib.rnn.BasicRNNCell(self.neuronNum)
            cell = tf.contrib.rnn.DropoutWrapper(lstmcell,
                                                 output_keep_prob=0.5)
            #lstmcell = tf.contrib.rnn.BasicLSTMCell(self.neuronNum, forget_bias=1.0, state_is_tuple=True,activation=tf.nn.relu)
            #cell_drop=tf.contrib.rnn.DropoutWrapper(lstmcell, output_keep_prob=0.5)
            #construct 5 layers of LSTM
            #cell = tf.contrib.rnn.MultiRNNCell([cell_drop for _ in range(2)], state_is_tuple=True)

            #系统下一时刻的状态仅由当前时刻的状态产生
            outputnew, statenew = tf.nn.dynamic_rnn(cell, L2, dtype=tf.float32)

            #outputs = self.outputs = outputnew[:,self.stepNum-1,:] # 取最后一个step的结果
            outputs = self.outputs = tf.reshape(
                outputnew, [-1, self.stepNum, self.neuronNum])
            outputs = outputs[:, 0, :]

            softmax_w = tf.get_variable(
                "softmax_w", [self.neuronNum, 3],
                dtype=tf.float32,
                initializer=tf.truncated_normal_initializer(
                    stddev=1.0 / math.sqrt(float(self.neuronNum))))
            softmax_b = tf.get_variable("softmax_b", [3], dtype=tf.float32)
            logits = tf.matmul(outputs, softmax_w) + softmax_b
            self.probs = tf.nn.softmax(logits, name="action")

            # fetch the maximum probability # fetch the index of the maximum probability
            self.action0 = tf.reduce_max(self.probs, axis=1)
            self.argAction = tf.argmax(self.probs, axis=1)

            #--------------------------continuous action-----------
            #curact = tf.reshape(self.action0,[-1,30])
            #curarg = tf.reshape(self.argAction,[-1,30])
            #--------------------------result-----------------------
            #self.curact = curact[:,29]
            #self.curarg = curarg[:,29]
            #self.policyloss = policyloss = tf.log(self.curact)*self.critic_rewards
            #self.loss = loss = tf.negative(tf.reduce_mean(policyloss),name="loss")

            #loss,train
            self.lr_update = tf.assign(self.lr, self.new_lr)
            self.policyloss = policyloss = tf.log(
                self.action0) * self.critic_rewards
            self.loss = loss = tf.negative(tf.reduce_mean(policyloss),
                                           name="loss")
            tf.summary.scalar('actor_loss', loss)
            #self.actor_train = tf.train.AdamOptimizer(self.lr).minimize(loss)
            tvars = tf.trainable_variables()  #得到可以训练的参数
            self.agrads, _ = tf.clip_by_global_norm(tf.gradients(loss, tvars),
                                                    5)  #防止梯度爆炸
            optimizer = tf.train.AdamOptimizer(self.lr)
            self.actor_train = optimizer.apply_gradients(
                zip(self.agrads, tvars))
    def __init__(self, board_width, board_height, model_file=None):
        self.board_width = board_width
        self.board_height = board_height
        self.action_numbers = 96
        # Define the tensorflow neural network
        # 1. Input: 第一个参数为样本数量,第二个参数为通道数,第三个参数为棋盘高,第四个参数为棋盘宽
        self.input_states = tf.placeholder(
            tf.float32, shape=[None, 4, board_height, board_width])
        #将通道数放在最后。第一个通道为自己占据的位置表示,第二个棋盘为敌方占据位置,第三个通道为最后一颗棋
        self.input_state = tf.transpose(self.input_states, [0, 2, 3, 1])
        # 2. Common Networks Layers
        #data_format:channels_last对应于具有形状(批次,高度,宽度,通道)的输入,而channels_first对应于具有形状的输入(批次,通道,高度,宽度)
        self.conv1 = tf.layers.conv2d(inputs=self.input_state,
                                      filters=32,
                                      kernel_size=[3, 3],
                                      padding="same",
                                      data_format="channels_last",
                                      activation=tf.nn.relu)
        self.conv2 = tf.layers.conv2d(inputs=self.conv1,
                                      filters=64,
                                      kernel_size=[3, 3],
                                      padding="same",
                                      data_format="channels_last",
                                      activation=tf.nn.relu)
        self.conv3 = tf.layers.conv2d(inputs=self.conv2,
                                      filters=128,
                                      kernel_size=[3, 3],
                                      padding="same",
                                      data_format="channels_last",
                                      activation=tf.nn.relu)
        # 3-1 Action Networks
        self.action_conv = tf.layers.conv2d(inputs=self.conv3,
                                            filters=4,
                                            kernel_size=[1, 1],
                                            padding="same",
                                            data_format="channels_last",
                                            activation=tf.nn.relu)
        # Flatten the tensor
        self.action_conv_flat = tf.reshape(
            self.action_conv, [-1, 4 * board_height * board_width])
        # 3-2 Full connected layer, the output is the log probability of moves
        # on each slot on the board
        self.action_fc = tf.layers.dense(inputs=self.action_conv_flat,
                                         units=self.action_numbers,
                                         activation=tf.nn.log_softmax)
        # 4 Evaluation Networks
        self.evaluation_conv = tf.layers.conv2d(inputs=self.conv3,
                                                filters=2,
                                                kernel_size=[1, 1],
                                                padding="same",
                                                data_format="channels_last",
                                                activation=tf.nn.relu)
        self.evaluation_conv_flat = tf.reshape(
            self.evaluation_conv, [-1, 2 * board_height * board_width])
        self.evaluation_fc1 = tf.layers.dense(inputs=self.evaluation_conv_flat,
                                              units=64,
                                              activation=tf.nn.relu)
        # output the score of evaluation on current state
        self.evaluation_fc2 = tf.layers.dense(inputs=self.evaluation_fc1,
                                              units=1,
                                              activation=tf.nn.tanh)

        # Define the Loss function
        # 1. Label: the array containing if the game wins or not for each state
        self.labels = tf.placeholder(tf.float32, shape=[None, 1])
        # 2. Predictions: the array containing the evaluation score of each state
        # which is self.evaluation_fc2
        # 3-1. Value Loss function
        self.value_loss = tf.losses.mean_squared_error(self.labels,
                                                       self.evaluation_fc2)
        # 3-2. Policy Loss function
        self.mcts_probs = tf.placeholder(tf.float32,
                                         shape=[None, self.action_numbers])
        self.policy_loss = tf.negative(
            tf.reduce_mean(
                tf.reduce_sum(tf.multiply(self.mcts_probs, self.action_fc),
                              1)))
        # 3-3. L2 penalty (regularization)
        l2_penalty_beta = 1e-4
        vars = tf.trainable_variables()
        l2_penalty = l2_penalty_beta * tf.add_n(
            [tf.nn.l2_loss(v) for v in vars if 'bias' not in v.name.lower()])
        # 3-4 Add up to be the Loss function
        self.loss = self.value_loss + self.policy_loss + l2_penalty

        # Define the optimizer we use for training
        self.learning_rate = tf.placeholder(tf.float32)
        self.optimizer = tf.train.AdamOptimizer(
            learning_rate=self.learning_rate).minimize(self.loss)

        # Make a session
        self.session = tf.Session()

        # calc policy entropy, for monitoring only
        self.entropy = tf.negative(
            tf.reduce_mean(
                tf.reduce_sum(tf.exp(self.action_fc) * self.action_fc, 1)))

        # Initialize variables
        init = tf.global_variables_initializer()
        self.session.run(init)

        # For saving and restoring
        self.saver = tf.train.Saver()
        if model_file is not None:
            self.restore_model(model_file)
Esempio n. 29
0

Xtr = normalizedFunc(Xtr)

Xte = normalizedFunc(Xte)

# In[84]:

# tf Graph Input

xtr = tf.placeholder("float", [None, 5])
xte = tf.placeholder("float", [5])

# Nearest Neighbor calculation using L1 Distance
# Calculate L1 Distance
distance = tf.reduce_sum(tf.abs(tf.add(xtr, tf.negative(xte))),
                         reduction_indices=1)
# Prediction: Get min distance index (Nearest neighbor)
pred = tf.arg_min(distance, 0)

accuracy = 0.

# Initialize the variables (i.e. assign their default value)
init = tf.global_variables_initializer()

predi = []
truePred = []
# Start training
with tf.Session() as sess:

    # Run the initializer
Esempio n. 30
0
def sigma(x):
    return tf.div(tf.constant(1.0),
                  tf.add(tf.constant(1.0), tf.exp(tf.negative(x))))
Esempio n. 31
0
 def __neg__(self):
     return tf.negative(self)
Esempio n. 32
0
train_pixels,train_list_values = mnist.train.next_batch(100) 
test_pixels,test_list_of_values  = mnist.test.next_batch(10) 


train_pixel_tensor = tf.placeholder\
                     ("float", [None, 784])
test_pixel_tensor = tf.placeholder\
                     ("float", [784])

#Cost Function and distance optimization

distance = tf.reduce_sum\
           (tf.abs\
            (tf.add(train_pixel_tensor, \
                    tf.negative(test_pixel_tensor))), \
            reduction_indices=1)

pred = tf.arg_min(distance, 0)

# Testing and algorithm evaluation

accuracy = 0.
init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    for i in range(len(test_list_of_values)):
        nn_index = sess.run(pred,\
		feed_dict={train_pixel_tensor:train_pixels,\
		test_pixel_tensor:test_pixels[i,:]})
        print "Test N ", i,"Predicted Class: ", \
Esempio n. 33
0
    global testBatchCounter
    batchX = test_images[testBatchCounter:testBatchCounter + batchSize, :, :]
    batchY = test_labels[testBatchCounter:testBatchCounter + batchSize, :]
    testBatchCounter = testBatchCounter + int(batchSize)
    return (batchX, batchY)


train_batchX, train_batchY = nextTrainBatch(25000)
test_batchX, test_batchY = nextTestBatch(1000)

X = tf.placeholder(tf.float32, [None, 32, 32, 3])
XX = tf.reshape(X, [-1, 3 * 32 * 32])
X_test = tf.placeholder(tf.float32, [32, 32, 3])
XX_test = tf.reshape(X_test, [-1, 3 * 32 * 32])

distance = tf.reduce_sum(tf.abs(tf.add(XX, tf.negative(XX_test))),
                         reduction_indices=1)
pred = tf.arg_min(distance, 0)
accuracy = 0.

init = tf.global_variables_initializer()

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

    for i in range(len(test_batchX)):
        nn_index = sess.run(pred,
                            feed_dict={
                                X: train_batchX,
                                X_test: test_batchX[i, :]
                            })
Esempio n. 34
0
#导入mnist数据
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("mnist_data/", one_hot=True)

#数据集和标签
Xtrain, Ytrain = mnist.train.next_batch(5000)
Xtest, Ytest = mnist.test.next_batch(1000)
print("Xtrain.shape: ", Xtrain.shape, "Xtest.shape: ", Xtest.shape)
print("Ytrain.shape: ", Ytrain.shape, "Ytest.shape: ", Ytest.shape)

#计算图
xtrain = tf.placeholder("float", [None, 784])
xtest = tf.placeholder("float", [784])

tmp = tf.add(xtrain, tf.negative(xtest))
distance = tf.reduce_sum(tf.abs(tmp), axis=1)

pred = tf.arg_min(distance, 0)

init = tf.global_variables_initializer()

#启动会话
with tf.Session() as sess:
    sess.run(init)
    accuracy = 0.
    Ntest = len(Xtest)
    for i in range(Ntest):
        nn_index = sess.run(pred,
                            feed_dict={
                                xtrain: Xtrain,
Esempio n. 35
0
def sigmoid(v):
    return tf.div(tf.constant(1.0),tf.add(tf.constant(1.0),tf.exp(tf.negative(v))))
Esempio n. 36
0
 def bond(bl):
     return tf.add(K.relu(tf.negative(bl)), K.relu(bl - 1.0))
Esempio n. 37
0
 def poros(poroi, porof):
     return K.relu(tf.negative(porof)) + K.relu(porof - poroi)
Esempio n. 38
0
 def log_jacobian_tensor(self, x):
     return tf.negative(tf.reduce_sum(tf.nn.softplus(tf.negative(x))))
Esempio n. 39
0
                label_array.append([1, 0])
    except:
        pass


path_images('train', train_images, train_labels)

# opencv
cap = cv2.VideoCapture(0)

# tensorflow
tf.reset_default_graph()

trainX = tf.placeholder(tf.float32, shape=[None, 2500])
centroid = tf.placeholder(tf.float32, 2500)
distance = tf.reduce_sum(tf.abs(tf.add(trainX, tf.negative(centroid))),
                         reduction_indices=1)
prediction = tf.arg_min(distance, 0)

_ = tf.Variable(initial_value='fake_variable')

accuracy = 0

init = tf.global_variables_initializer()

saver = tf.train.Saver()

with tf.Session() as sess:
    sess.run(init)
    saver.restore(sess, './neighbor_model.ckpt')
    while True:
Esempio n. 40
0
 def negative_network():
     input_ = tf.placeholder(tf.float32, shape=[1, 2, 2, 3], name="input")
     return input_, tf.negative(input_)
dist = tf.reshape(dist, [-1,1])
sq_dists = tf.add(tf.subtract(dist, tf.multiply(2., tf.matmul(x_data, tf.transpose(x_data)))), tf.transpose(dist))
my_kernel = tf.exp(tf.multiply(gamma, tf.abs(sq_dists)))

def reshape_matmul(mat):
    v1 = tf.expand_dims(mat, 1)
    v2 = tf.reshape(v1, [3, batch_size, 1])
    return(tf.matmul(v2, v1))
    
model_output = tf.matmul(b, my_kernel)
first_term = tf.reduce_sum(b)
b_vec_cross = tf.matmul(tf.transpose(b), b)
y_target_cross = reshape_matmul(y_target)

second_term = tf.reduce_sum(tf.multiply(my_kernel, tf.multiply(b_vec_cross, y_target_cross)),[1,2])
loss = tf.reduce_sum(tf.negative(tf.subtract(first_term, second_term)))

rA = tf.reshape(tf.reduce_sum(tf.square(x_data), 1),[-1,1])
rB = tf.reshape(tf.reduce_sum(tf.square(prediction_grid), 1),[-1,1])
pred_sq_dist = tf.add(tf.subtract(rA, tf.multiply(2., tf.matmul(x_data,tf.transpose(prediction_grid)))), tf.transpose(rB))
pred_kernel = tf.exp(tf.multiply(gamma, tf.abs(pred_sq_dist)))

prediction_output = tf.matmul(tf.multiply(y_target,b), pred_kernel)
prediction = tf.argmax(prediction_output-tf.expand_dims(tf.reduce_mean(prediction_output,1), 1), 0)
accuracy = tf.reduce_mean(tf.cast(tf.equal(prediction,tf.argmax(y_target,0)), tf.float32))

my_opt = tf.train.GradientDescentOptimizer(0.01)
train_step = my_opt.minimize(loss)

# Initialize variables
init = tf.global_variables_initializer()
Esempio n. 42
0
def sigma(x):
    return tf.div(tf.constant(1.0),
                  tf.add(tf.constant(1.0), tf.exp(tf.negative(x))))
Esempio n. 43
0
# Import MNIST data
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("../../data/MNIST_data", one_hot=True)

# In this example, we limit mnist data
Xtr, Ytr = mnist.train.next_batch(5000) #5000 for training (nn candidates)
Xte, Yte = mnist.test.next_batch(200) #200 for testing

# tf Graph Input
xtr = tf.placeholder("float", [None, 784])
xte = tf.placeholder("float", [784])

# Nearest Neighbor calculation using L1 Distance
# Calculate L1 Distance
distance = tf.reduce_sum(tf.abs(tf.add(xtr, tf.negative(xte))), reduction_indices=1)
# Prediction: Get min distance index (Nearest neighbor)
pred = tf.arg_min(distance, 0)

accuracy = 0.

# Initializing the variables
init = tf.global_variables_initializer()

# Launch the graph
with tf.Session() as sess:
    sess.run(init)

    # loop over test data
    for i in range(len(Xte)):
        # Get nearest neighbor
Esempio n. 44
0
def sigmoid(x):
    sigma = tf.div(tf.constant(1.0), tf.add(tf.constant(1.0), tf.exp(tf.negative(x))))
    return sigma
Esempio n. 45
0
def tanh(x):
    return tf.div(tf.subtract(tf.exp(x, tf.exp(tf.negative(x)))), tf.add(tf.exp(x, tf.exp(tf.negative(x)))))
# I think here I am right and the book is wrong, mine is RBF actually
sq_dist = tf.subtract(tf.multiply(2., dist), tf.matmul(x_data, tf.transpose(x_data)))
# sq_dist = tf.add(tf.subtract(dist, tf.multiply(2., tf.matmul(x_data, tf.transpose(x_data)))), tf.transpose(dist))

my_kernel = tf.exp(tf.multiply(gamma, tf.abs(sq_dist)))
# linear
# my_kernel = tf.matmul(x_data, tf.transpose(x_data))

# dual part
# max(sum(ai) - 1/2 sum(aiajyiyj <xi,xj>)), where sum(aiyi) = 0
# let the negative to be minimal == max
first_term = tf.reduce_sum(b)
b_vec_cross = tf.matmul(tf.transpose(b), b)
y_target_cross = tf.matmul(y_target, tf.transpose(y_target))
second_term = tf.reduce_sum(tf.multiply(my_kernel, tf.multiply(b_vec_cross, y_target_cross)))
loss = tf.negative(tf.subtract(first_term, tf.multiply(1/2, second_term)))


rA = tf.reshape(tf.reduce_sum(tf.square(x_data), 1), [-1, 1])
rB = tf.reshape(tf.reduce_sum(tf.square(prediction_grid), 1), [-1, 1])
pred_sq_dist = tf.add(tf.subtract(rA, tf.multiply(
    2., tf.matmul(x_data, tf.transpose(prediction_grid)))), tf.transpose(rB))
pred_kernel = tf.exp(tf.multiply(gamma, tf.abs(pred_sq_dist)))


prediction_output = tf.matmul(tf.multiply(tf.transpose(y_target), b), pred_kernel)
prediction = tf.sign(prediction_output-tf.reduce_mean(prediction_output))
accuracy = tf.reduce_mean(tf.cast(tf.equal(
    tf.squeeze(prediction), tf.squeeze(y_target)), tf.float32))

train_step = tf.train.GradientDescentOptimizer(0.001).minimize(loss)
Esempio n. 47
0
def train(x_train, y_train, supervised=0.10, epochs=1000, delta=0.001):
    """Training function, takes in a training set and its labels and uses gradient descent w/
    logistic loss to calculate feature weights and bias for a classifier

    Arguments:
        x_train: ndarray (samplesxfeatures)
            The training set
        y_train: ndarray (samplesx1)
            The labels of the training set
        epochs: int, default 100
            Number of training iterations
        delta: float, default 0.01
            Gradient descent change parameter
    
    Returns
        c: ndarray (featuresx1)
            The calculated sample weights after training
        b: float
            The calculated bias after training
    """
    m = x_train.shape[0]
    k = int(m * supervised)
    for i in range(k, m):
        y_train[i] = 0
    print("\t# of total samples: {}".format(m))
    print("\t# of supervised samples: {}".format(k))

    K = tf.constant(utils.getK(x_train, x_train), name="K")
    L = tf.constant(getL(x_train), name="L")

    c = tf.Variable(np.random.rand(m, 1).astype(dtype='float64'),
                    name="c")  # Gaussian thing (samplesx1)
    b = tf.Variable(0.0, dtype=tf.float64, name="b")  # Bias offset (scalar)

    y = tf.placeholder(dtype=tf.float64, name='y',
                       shape=[m, 1])  # Training set labels (samplesx1)

    # First part of formula
    l = lambda i: tf.log(1 + tf.exp(
        tf.negative(y[i][0]) *
        (tf.reduce_sum(tf.multiply(c, tf.reshape(K[i], [-1, 1]))) + b)))
    # Second part of formula
    term1 = 0.5 * tf.matmul(tf.transpose(c), tf.matmul(K, c))[0][0]
    # Third part of formula
    term2 = tf.matmul(tf.transpose(c),
                      tf.matmul(K, tf.matmul(L, tf.matmul(K, c))))[0][0]

    _, loss = tf.while_loop(lambda i, s: tf.less(i, k), lambda i, s:
                            (i + 1, s + l(i) + term1 + term2), [
                                tf.constant(0, name="loss_i"),
                                tf.constant(0, dtype=tf.float64, name="loss_s")
                            ])

    train = tf.train.GradientDescentOptimizer(delta).minimize(loss)

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        for i in range(epochs):
            # print("Epoch {}".format(i))
            sess.run([train], feed_dict={y: y_train})
            # print(sess.run(loss, feed_dict={y: y_train}))
        curr_c, curr_b = sess.run([c, b])

    return curr_c, curr_b
Esempio n. 48
0
    def buildNetwork(self):
        with tf.name_scope('input'):
          #  self.x = tf.placeholder(tf.float32, shape=[100,784], name='x')
          #  self.y_= tf.placeholder(tf.float32,shape=[100,10],name="y_")
            self.states = tf.placeholder(tf.float32,shape=[None,120,20],name= "states")
            self.actions_taken = tf.placeholder(tf.float32,shape=[None],name= "actions_taken")
            self.critic_feedback = tf.placeholder(tf.float32,shape=[None],name= "critic_feedback")
            self.critic_rewards = tf.placeholder(tf.float32,shape=[None],name= "critic_rewards")

        def lstm_cell(size):
            return tf.contrib.rnn.BasicLSTMCell(size, forget_bias=0.0, state_is_tuple=True)

        # ActorNetwork
        with tf.variable_scope("actor") :
           
            L1= tf.contrib.layers.fully_connected(
                inputs=self.states,
                num_outputs= 20, #hidden 
                activation_fn= tf.tanh,
                weights_initializer = tf.random_normal_initializer(),
                biases_initializer = tf.zeros_initializer()
            )
            print("L1:",L1)
            
            lstmcell=tf.contrib.rnn.BasicLSTMCell(20, forget_bias=0.0, state_is_tuple=True)
            cell = tf.contrib.rnn.MultiRNNCell([lstmcell for _ in range(5)], state_is_tuple=True)
            #self._initial_state=cell.zero_state(batchsize,tf.float32)

            #input = tf.reshape(L1,[100,28,28])
            
            state = cell.zero_state(100,tf.float32)  #batchsize*hidden cells
            outputs = []
            for time_step in range(120):
                if time_step > 0: tf.get_variable_scope().reuse_variables()
                (cell_output, state) = cell(L1[:,time_step,:], state)
                outputs.append(cell_output)
            #    tf.get_variable_scope().reuse_variables()

            output = tf.reshape(tf.concat(axis=1, values=outputs), [-1, 20])
            print("output",output)
            weights = tf.Variable(tf.truncated_normal([20, 3],stddev=1.0 / math.sqrt(float(1200))),name='weights')
            biases = tf.Variable(tf.zeros([3]),name='biases')
            logits = tf.matmul(output, weights) + biases
            #weights = tf.Variable("softmax_w", [20, 3], dtype=float32)
            #softmax_w = tf.get_variable( "softmax_w", [20, 3], dtype=tf.float32)
            #softmax_b = tf.get_variable("softmax_b", [3], dtype=tf.float32)
            #logits = tf.matmul(output, softmax_w) + softmax_b
            self.probs = tf.nn.softmax(logits,name="action")
            #print("action",self.action)
            #print(tf.shape(self.action))
            #self.action =np.amax(self.action0,axis=self.action0[0])
            #self.action =tf.multinomial(tf.log(self.action0),1)
            #self.action = tf.shape(self.action0)[1]
            #gather_indices = tf.range(100) * tf.shape(self.action0)[1] + self.actions_taken
            #self.action = tf.gather(tf.reshape(self.action0, [-1]), gather_indices)
            #print("action",self.action)
            gather_indices = tf.range(tf.shape(self.probs)[0]) * tf.shape(self.probs)[1] + self.actions_taken
            self.action = tf.gather(tf.reshape(self.probs, [-1]), gather_indices)
            print("action",self.action)
          

            #loss,train
            policyloss = tf.log(self.action)*(self.critic_rewards-self.critic_feedback)
            loss = tf.negative(tf.reduce_mean(policyloss),name="loss")
            #with tf.variable_scope("actor-train"):
            #    self.actor_train = tf.train.AdamOptimizer(0.01).minimize(loss)
            #    tf.get_variable_scope().reuse_variables()
            
            
            tvars = tf.trainable_variables()
            grads, _ = tf.clip_by_global_norm(tf.gradients(loss, tvars),5)
            optimizer = tf.train.GradientDescentOptimizer(0.01)
            self.actor_train = optimizer.apply_gradients(zip(grads, tvars))

            

    
        # Critic Network
        with tf.variable_scope("critic") as scopeB:
            
            self.critic_target = tf.placeholder(tf.float32,name= "critic_target")
    
            critic_L1= tf.contrib.layers.fully_connected(
                inputs=self.states,
                num_outputs= 20, #hidden 
                activation_fn= tf.tanh,
                weights_initializer = tf.random_normal_initializer(),
                biases_initializer = tf.zeros_initializer()
            )

           # lstmcell=lstm_cell()
            lstmcell=tf.contrib.rnn.BasicLSTMCell(20, forget_bias=0.0, state_is_tuple=True)
            cell = tf.contrib.rnn.MultiRNNCell([lstmcell for _ in range(5)], state_is_tuple=True)
            self._initial_state=cell.zero_state(100,tf.float32)

            #scopeB.reuse_variables()
            state = cell.zero_state(100,tf.float32)  #batchsize*hidden cells
            outputs = []
            for time_step in range(120):
                if time_step > 0: tf.get_variable_scope().reuse_variables()
                (cell_output, state) = cell(critic_L1[:,time_step,:], state)
                outputs.append(cell_output)

           # weights = tf.Variable(tf.truncated_normal([28, 10],stddev=1.0 / math.sqrt(float(28))),name='weights')
           # biases = tf.Variable(tf.zeros([10]),name='biases')
           # logits = tf.matmul(cell_output, weights) + biases
           # self.critic_value = tf.nn.softmax(logits,name="action")
            output = tf.reshape(tf.concat(axis=1, values=outputs), [-1, 20])
            self.critic_value = tf.contrib.layers.fully_connected(
                inputs=output,
                num_outputs= 1, #hidden 
                activation_fn= tf.tanh,
                weights_initializer = tf.random_normal_initializer(),
                biases_initializer = tf.zeros_initializer()
            )

            #loss,train
            critic_loss = tf.reduce_mean(tf.square(self.critic_target - self.critic_value) , name ="loss" )
            #self.critic_train = tf.train.AdamOptimizer(0.01).minimize(critic_loss) #global_step

            tvar = tf.trainable_variables()
            grads, _ = tf.clip_by_global_norm(tf.gradients(critic_loss, tvar),5)
            optimizer = tf.train.GradientDescentOptimizer(0.01)
            self.critic_train = optimizer.apply_gradients(zip(grads, tvar))
    def _tower_som(self):
        """ Build a single SOM tower on the TensorFlow graph """
        # Randomly initialized weights for all neurons, stored together
        # as a matrix Variable of shape [num_neurons, input_dims]
        with tf.name_scope('Weights'):
            # Each tower will get its own copy of the weights variable. Since the towers are constructed sequentially,
            # the handle to the Tensors will be different for each tower even if we reference "self"
            self._weights = tf.get_variable(
                name='weights',
                shape=[self._m * self._n, self._dim],
                initializer=tf.random_uniform_initializer(maxval=1))

            with tf.name_scope('summaries'):
                # All summary ops are added to a list and then the merge() function is called at the end of
                # this method
                mean = tf.reduce_mean(self._weights)
                self._summary_list.append(tf.summary.scalar('mean', mean))
                with tf.name_scope('stdev'):
                    stdev = tf.sqrt(
                        tf.reduce_mean(
                            tf.squared_difference(self._weights, mean)))
                self._summary_list.append(tf.summary.scalar('stdev', stdev))
                self._summary_list.append(
                    tf.summary.scalar('max', tf.reduce_max(self._weights)))
                self._summary_list.append(
                    tf.summary.scalar('min', tf.reduce_min(self._weights)))
                self._summary_list.append(
                    tf.summary.histogram('histogram', self._weights))

        # Matrix of size [m*n, 2] for SOM grid locations of neurons.
        # Maps an index to an (x,y) coordinate of a neuron in the map for calculating the neighborhood distance
        self._location_vects = tf.constant(np.array(
            list(self._neuron_locations())),
                                           name='Location_Vectors')

        with tf.name_scope('Input'):
            self._input = tf.identity(self._input_tensor)

        with tf.name_scope('Epoch'):
            self._epoch = tf.placeholder("float", [], name="iter")

        # Start by computing the best matching units / winning units for each input vector in the batch.
        # Basically calculates the Euclidean distance between every
        # neuron's weight vector and the inputs, and returns the index of the neurons which give the least value
        # Since we are doing batch processing of the input, we need to calculate a BMU for each of the individual
        # inputs in the batch. Will have the shape [batch_size]

        # Oh also any time we call expand_dims it's almost always so we can make TF broadcast stuff properly
        with tf.name_scope('BMU_Indices'):
            # Distance between weights and the input vector
            # Note we are reducing along 2nd axis so we end up with a tensor of [batch_size, num_neurons]
            # corresponding to the distance between a particular input and each neuron in the map
            # Also note we are getting the squared distance because there's no point calling sqrt or tf.norm
            # if we're just doing a strict comparison
            squared_distance = tf.reduce_sum(
                tf.pow(
                    tf.subtract(tf.expand_dims(self._weights, axis=0),
                                tf.expand_dims(self._input, axis=1)), 2), 2)

            # Get the index of the minimum distance for each input item, shape will be [batch_size],
            bmu_indices = tf.argmin(squared_distance, axis=1)

        # This will extract the location of the BMU in the map for each input based on the BMU's indices
        with tf.name_scope('BMU_Locations'):
            # Using tf.gather we can use `bmu_indices` to index the location vectors directly
            bmu_locs = tf.reshape(tf.gather(self._location_vects, bmu_indices),
                                  [-1, 2])

        with tf.name_scope('Learning_Rate'):
            # With each epoch, the initial sigma value decreases linearly
            radius = tf.subtract(
                self._initial_radius,
                tf.multiply(
                    self._epoch,
                    tf.divide(
                        tf.cast(tf.subtract(self._initial_radius, 1),
                                tf.float32),
                        tf.cast(tf.subtract(self._max_epochs, 1),
                                tf.float32))))

            alpha = tf.subtract(
                self._initial_learning_rate,
                tf.multiply(
                    self._epoch,
                    tf.divide(
                        tf.cast(tf.subtract(self._initial_learning_rate, 1),
                                tf.float32),
                        tf.cast(tf.subtract(self._max_epochs, 1),
                                tf.float32))))

            # Construct the op that will generate a matrix with learning rates for all neurons and all inputs,
            # based on iteration number and location to BMU

            # Start by getting the squared difference between each BMU location and every other unit in the map
            # bmu_locs is [batch_size, 2], i.e. the coordinates of the BMU for each input vector.
            # location vects shape should be [1, num_neurons, 2]
            # bmu_locs should be [batch_size, 1, 2]
            # Output needs to be [batch_size, num_neurons], i.e. a row vector of distances for each input item
            bmu_distance_squares = tf.reduce_sum(
                tf.pow(
                    tf.subtract(tf.expand_dims(self._location_vects, axis=0),
                                tf.expand_dims(bmu_locs, axis=1)), 2), 2)

            # Using the distances between each BMU, construct the Gaussian neighborhood function.
            # Basically, neurons which are close to the winner will move more than those further away.
            # The radius tensor decreases the width of the Gaussian over time, so early in training more
            # neurons will be affected by the winner and by the end of training only the winner will move.
            # This tensor will be of shape [batch_size, num_neurons] as well and will be the value multiplied to
            # each neuron based on its distance from the BMU for each input vector
            neighbourhood_func = tf.exp(
                tf.divide(
                    tf.negative(tf.cast(bmu_distance_squares, "float32")),
                    tf.multiply(
                        tf.square(tf.multiply(radius, self._std_coeff)), 2)))

            # Finally multiply by the learning rate to decrease overall neuron movement over time
            learning_rate_op = tf.multiply(neighbourhood_func, alpha)

        # The batch formula for SOMs multiplies a neuron's neighborhood by all of the input vectors in the batch,
        # then divides that by just the sum of the neighborhood function for each of the inputs.
        # We are writing this in a way that performs that operation for each of the neurons in the map.
        with tf.name_scope('Update_Weights'):
            # The numerator needs to be shaped [num_neurons, dimensions] to represent the new weights
            # for each of the neurons. At this point, the learning rate tensor will be
            # shaped [batch_size, neurons].
            # The end result is that, for each neuron in the network, we use the learning
            # rate between it and each of the input vectors, to calculate a new set of weights.
            numerator = tf.reduce_sum(tf.multiply(
                tf.expand_dims(learning_rate_op, axis=-1),
                tf.expand_dims(self._input, axis=1)),
                                      axis=0)

            # The denominator is just the sum of the neighborhood functions for each neuron, so we get the sum
            # along axis 1 giving us an output shape of [num_neurons]. We then expand the dims so we can
            # broadcast for the division op. Again we transpose the learning rate tensor so it's
            # [num_neurons, batch_size] representing the learning rate of each neuron for each input vector
            denominator = tf.expand_dims(
                tf.reduce_sum(learning_rate_op, axis=0) + float(1e-12),
                axis=-1)

        # We on;y really care about summaries from one of the tower SOMs, so assign the merge op to
        # the last tower we make. Otherwise there's way too many on Tensorboard.
        # self._merged = tf.summary.merge(self._summary_list)

        # With multi-gpu training we collect the results and do the weight assignment on the CPU
        return numerator, denominator
Esempio n. 50
0
computed_x = s.run(x)
print(computed_x)
s.close()

print('Another way to perform the same computation:')
s = tf.Session()
computed_x = x.eval(session = s)
print(computed_x)
s.close()

print('Generate and plot a Gaussian...')
# Keep in mind that in tf.pow(x, y) BOTH arguments MUST be of the same type!
# Both integers, both float, ...
mean = 0.0   #float!
stddev = 1.0 #float!
z = (tf.exp(tf.negative(tf.pow(x - mean, 2.0) /
                   (2.0 * tf.pow(stddev, 2.0)))) *
     (1.0 / (stddev * tf.sqrt(2.0 * 3.1415))))
s = tf.Session()
result = z.eval(session = s)
s.close()

import matplotlib.pyplot as plt
plt.plot(result)

print('Generate and plot a 2D Gaussian...')
ksize = z.get_shape().as_list()[0]
# Clever way to get a 2-D Gaussian: multiply a 1 x k vectot by its traspose!
z_2d = tf.matmul(tf.reshape(z, [ksize, 1]), tf.reshape(z, [1, ksize]))

s = tf.Session()
result = z_2d.eval(session = s)
Esempio n. 51
0
    def __build_graph(self):
        self.__graph = tf.Graph()
        with self.__graph.as_default(), self.__graph.device(_device_for_node):
            count_max = tf.constant([self.cooccurrence_cap],
                                    dtype=tf.float32,
                                    name='max_cooccurrence_count')
            scaling_factor = tf.constant([self.scaling_factor],
                                         dtype=tf.float32,
                                         name="scaling_factor")

            self.__focal_input = tf.placeholder(tf.int32,
                                                shape=[self.batch_size],
                                                name="focal_words")
            self.__context_input = tf.placeholder(tf.int32,
                                                  shape=[self.batch_size],
                                                  name="context_words")
            self.__cooccurrence_count = tf.placeholder(
                tf.float32, shape=[self.batch_size], name="cooccurrence_count")

            bias_C = tf.Variable(0.5, name="bias_C")
            focal_embeddings = tf.Variable(tf.random_uniform(
                [self.vocab_size, self.embedding_size], 1.0, -1.0),
                                           name="focal_embeddings")
            context_embeddings = tf.Variable(tf.random_uniform(
                [self.vocab_size, self.embedding_size], 1.0, -1.0),
                                             name="context_embeddings")

            focal_biases = tf.Variable(tf.random_uniform([self.vocab_size],
                                                         1.0, -1.0),
                                       name='focal_biases')
            context_biases = tf.Variable(tf.random_uniform([self.vocab_size],
                                                           1.0, -1.0),
                                         name="context_biases")

            focal_embedding = tf.nn.embedding_lookup([focal_embeddings],
                                                     self.__focal_input)
            context_embedding = tf.nn.embedding_lookup([context_embeddings],
                                                       self.__context_input)
            focal_bias = tf.nn.embedding_lookup([focal_biases],
                                                self.__focal_input)
            context_bias = tf.nn.embedding_lookup([context_biases],
                                                  self.__context_input)

            weighting_factor = tf.minimum(
                1.0,
                tf.pow(tf.div(self.__cooccurrence_count, count_max),
                       scaling_factor))

            embedding_product = tf.reduce_sum(
                tf.multiply(focal_embedding, context_embedding), 1)

            log_cooccurrences = tf.log(tf.to_float(self.__cooccurrence_count))

            distance_expr = tf.square(
                tf.add_n([
                    embedding_product, focal_bias, context_bias,
                    tf.negative(log_cooccurrences)
                ]))

            single_losses = tf.multiply(weighting_factor, distance_expr)
            self.__total_loss = tf.reduce_sum(single_losses)
            tf.summary.scalar("GloVe_loss", self.__total_loss)
            self.__optimizer = tf.train.AdagradOptimizer(
                self.learning_rate).minimize(self.__total_loss)
            self.__summary = tf.summary.merge_all()

            self.__combined_embeddings = tf.add(focal_embeddings,
                                                context_embeddings,
                                                name="combined_embeddings")
Esempio n. 52
0
    def __init__(self, dtype, gs):
        # shortcuts
        action_dim = int(gs.policyOut.shape[-1])
        state_dim = int(gs.input.shape[-1])

        action = tf.identity(gs.policyOut, name="action")  ## 3D
        value = tf.identity(gs.valueOut, name="value")  ## 2D

        # standard deviation layer
        with tf.name_scope('stdevconcatOutput'):
            wo = tf.Variable(tf.zeros(shape=[1, action_dim], dtype=dtype),
                             name='W',
                             trainable=True)  # Log standard deviation
            action_stdev = tf.identity(tf.exp(wo), name='stdev')

        gs.l_param_list.append(wo)
        gs.a_param_list.append(wo)

        super(RecurrentStochasticPolicyValue, self).__init__(dtype, gs)

        stdev_assign_placeholder = tf.placeholder(dtype,
                                                  shape=[1, action_dim],
                                                  name='Stdev_placeholder')
        Stdev_assign = tf.assign(wo,
                                 tf.log(stdev_assign_placeholder),
                                 name='assignStdev')
        Stdev_get = tf.exp(wo, name='getStdev')

        # Algorithm placeholders
        old_stdv = tf.placeholder(dtype, shape=[1, action_dim], name='stdv_o')
        old_action_sampled = tf.placeholder(dtype,
                                            shape=[None, None, action_dim],
                                            name='sampledAction')
        old_action_noise = tf.placeholder(dtype,
                                          shape=[None, None, action_dim],
                                          name='actionNoise')
        advantage_in = tf.placeholder(dtype,
                                      shape=[None, None],
                                      name='advantage')
        value_target = tf.placeholder(dtype,
                                      shape=[None, None],
                                      name='targetValue')
        value_pred = tf.placeholder(dtype,
                                    shape=[None, None],
                                    name='predictedValue')

        # Algorithm params
        v_coeff = tf.Variable(tf.constant(0.5, dtype=dtype), name='v_coeff')
        ent_coeff = tf.Variable(tf.constant(0.01, dtype=dtype),
                                name='ent_coeff')
        clip_param = tf.Variable(tf.constant(0.2, dtype=dtype),
                                 name='clip_param')
        clip_param_decay_rate = tf.Variable(tf.constant(1, dtype=dtype),
                                            name='clip_param_decay_rate')

        # Assign ops.
        PPO_params_placeholder = tf.placeholder(dtype=dtype,
                                                shape=[1, 4],
                                                name='PPO_params_placeholder')

        param_assign_op_list = []
        param_assign_op_list += [
            tf.assign(v_coeff,
                      tf.reshape(
                          tf.slice(PPO_params_placeholder, [0, 0], [1, 1]),
                          []),
                      name='kl_coeff_assign')
        ]
        param_assign_op_list += [
            tf.assign(ent_coeff,
                      tf.reshape(
                          tf.slice(PPO_params_placeholder, [0, 1], [1, 1]),
                          []),
                      name='ent_coeff_assign')
        ]
        param_assign_op_list += [
            tf.assign(clip_param,
                      tf.reshape(
                          tf.slice(PPO_params_placeholder, [0, 2], [1, 1]),
                          []),
                      name='clip_param_assign')
        ]
        param_assign_op_list += [
            tf.assign(clip_param_decay_rate,
                      tf.reshape(
                          tf.slice(PPO_params_placeholder, [0, 3], [1, 1]),
                          []),
                      name='clip_decayrate_assign')
        ]

        PPO_param_assign_ops = tf.group(*param_assign_op_list,
                                        name='PPO_param_assign_ops')

        with tf.name_scope('trainUsingGrad'):
            gradient_from_critic = tf.placeholder(dtype,
                                                  shape=[1, None],
                                                  name='Inputgradient')
            train_using_grad_optimizer = tf.train.AdamOptimizer(
                learning_rate=self.learningRate, epsilon=1e-5)
            # train_using_grad_optimizer = tf.train.RMSPropOptimizer(learning_rate=train_using_critic_learning_rate)

            split_parameter_gradients = tf.split(gradient_from_critic, [
                reduce(mul,
                       param.get_shape().as_list(), 1)
                for param in gs.a_param_list
            ], 1)
            manipulated_parameter_gradients = []
            for grad, param in zip(split_parameter_gradients, gs.l_param_list):
                manipulated_parameter_gradients += [
                    tf.reshape(grad, tf.shape(param))
                ]

            manipulated_parameter_gradients_and_parameters = zip(
                manipulated_parameter_gradients, gs.l_param_list)
            train_using_gradients = train_using_grad_optimizer.apply_gradients(
                manipulated_parameter_gradients_and_parameters,
                name='applyGradients',
                global_step=tf.train.get_global_step())
        util = Utils.Utils(dtype)

        with tf.name_scope('Algo'):
            mask = tf.sequence_mask(gs.seq_length,
                                    maxlen=tf.shape(gs.input)[1],
                                    name='mask')
            logp_n = tf.boolean_mask(
                util.log_likelihood(action, action_stdev, old_action_sampled),
                mask)
            logp_old = tf.boolean_mask(
                util.log_likelihood(old_action_noise, old_stdv), mask)
            advantage = tf.boolean_mask(advantage_in, mask)
            ratio = tf.exp(logp_n - logp_old)
            ent = tf.reduce_sum(
                wo + .5 * tf.cast(tf.log(2.0 * np.pi * np.e), dtype=dtype),
                axis=-1)
            meanent = tf.reduce_mean(ent)

            with tf.name_scope('RPPO'):
                clip_range = tf.train.exponential_decay(
                    clip_param,
                    tf.train.get_global_step(),
                    self.decayStep_lr,
                    clip_param_decay_rate,
                    name='clip_range')
                #POLICY LOSS
                surr1 = tf.multiply(ratio, advantage)
                surr2 = tf.multiply(
                    tf.clip_by_value(ratio, 1.0 - clip_range,
                                     1.0 + clip_range), advantage)
                PPO_loss = tf.reduce_mean(tf.maximum(
                    surr1, surr2))  # PPO's pessimistic surrogate (L^CLIP)

                kl_ = tf.boolean_mask(
                    util.kl_divergence((old_action_sampled - old_action_noise),
                                       old_stdv, action, action_stdev), mask)
                kl_mean = tf.reshape(tf.reduce_mean(kl_),
                                     shape=[],
                                     name='kl_mean')

                #VALUE LOSS
                vpredclipped = value_pred + tf.clip_by_value(
                    value - value_pred, tf.negative(clip_param), clip_param)
                vf_err1 = tf.square(value - value_target)
                vf_err2 = tf.square(vpredclipped - value_target)
                vf_err1_masked = tf.boolean_mask(vf_err1, mask)
                vf_err2_masked = tf.boolean_mask(vf_err2, mask)

                vf_loss = .5 * tf.reduce_mean(
                    tf.maximum(vf_err1_masked, vf_err2_masked))

                Total_loss = tf.identity(PPO_loss -
                                         tf.multiply(ent_coeff, meanent) +
                                         v_coeff * vf_loss,
                                         name='loss')
                policy_gradient = tf.identity(tf.expand_dims(util.flatgrad(
                    Total_loss, gs.l_param_list, self.max_grad_norm),
                                                             axis=0),
                                              name='Pg')  # flatgrad

                testTensor = tf.identity(surr1, name='test')
Esempio n. 53
0
# Placeholders
x_data_train = tf.placeholder(shape=[None, 784], dtype=tf.float32)
x_data_test = tf.placeholder(shape=[None, 784], dtype=tf.float32)
y_target_train = tf.placeholder(shape=[None, 10], dtype=tf.float32)
y_target_test = tf.placeholder(shape=[None, 10], dtype=tf.float32)

# Declare distance metric
# L1
distance = tf.reduce_sum(tf.abs(tf.subtract(x_data_train, tf.expand_dims(x_data_test,1))), axis=2)

# L2
#distance = tf.sqrt(tf.reduce_sum(tf.square(tf.subtract(x_data_train, tf.expand_dims(x_data_test,1))), reduction_indices=1))

# Predict: Get min distance index (Nearest neighbor)
top_k_xvals, top_k_indices = tf.nn.top_k(tf.negative(distance), k=k)
prediction_indices = tf.gather(y_target_train, top_k_indices)
# Predict the mode category
count_of_predictions = tf.reduce_sum(prediction_indices, axis=1)
prediction = tf.argmax(count_of_predictions)

# Calculate how many loops over training data
num_loops = int(np.ceil(len(x_vals_test)/batch_size))

test_output = []
actual_vals = []
for i in range(num_loops):
    min_index = i*batch_size
    max_index = min((i+1)*batch_size,len(x_vals_train))
    x_batch = x_vals_test[min_index:max_index]
    y_batch = y_vals_test[min_index:max_index]
Esempio n. 54
0
#-*- codeing:utf-8 -*-
'''
Created on 2019年1月12日

@author: adp
'''
import tensorflow as tf

sess = tf.InteractiveSession()
x = tf.constant([1, 2])
neg_x = tf.negative(x)

result = neg_x.eval()
print(result)

sess.close()
Esempio n. 55
0
# Import MNIST data
from tensorflow.examples.tutorials.mnist import input_data

# Store the MNIST data in /tmp/data
mnist = input_data.read_data_sets("mnist_data/", one_hot=True)

training_digits, training_labels = mnist.train.next_batch(5000)
test_digits, test_labels = mnist.test.next_batch(200)

training_digits_pl = tf.placeholder("float", [None, 784])

test_digit_pl = tf.placeholder("float", [784])

# Nearest Neighbor calculation using L1 distance
l1_distance = tf.abs(tf.add(training_digits_pl, tf.negative(test_digit_pl)))

distance = tf.reduce_sum(l1_distance, axis=1)

# Prediction: Get min distance index (Nearest neighbor)
pred = tf.arg_min(distance, 0)

accuracy = 0.

# Initializing the variables
init = tf.global_variables_initializer()

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

    # loop over test data
Esempio n. 56
0
 def negative(self):
   backing = [tf.negative(xi) % mi for xi, mi in zip(self.backing, MODULI)]
   return DenseTensor(backing)
 def build_losses(self, vecpos, vecneg):
     # the Wasserstein-GAN losses
     self.d_loss = tf.reduce_mean(vecneg - vecpos, name='d_loss')
     self.g_loss = tf.negative(tf.reduce_mean(vecneg), name='g_loss')
     add_moving_summary(self.d_loss, self.g_loss)
Esempio n. 58
0
 def _flip_gradients(op, grad):
     return [tf.negative(grad) * l]
from tensorflow.examples.tutorials.mnist import input_data

mnist = input_data.read_data_sets("/tmp/data",one_hot=True)

# In this example, we limit mnist data
X_train, Y_train = mnist.train.next_batch(5000) #5000 for training (nn candidates)
X_test, Y_test = mnist.test.next_batch(200) #200 for testing

# tf Graph Input
x_train = tf.placeholder("float", [None, 784])
x_test = tf.placeholder("float", [784])

# Nearest Neighbor calculation using L1 Distance
# Calculate L1 Distance
distance = tf.reduce_sum(tf.abs(tf.add(x_train, tf.negative(x_test))), reduction_indices=1)

# Prediction: Get min distance index (Nearest neighbor)
pred = tf.arg_min(distance, 0)

accuracy = 0.

# Initialize the variables (i.e. assign their default value)
init = tf.global_variables_initializer()

# Start training
with tf.Session() as sess:
    sess.run(init)

    for i in range(len(X_test)):
        # Get nearest neighbor
Esempio n. 60
0
    def __init__(self, m, n, dim, n_iterations=100, alpha=None, sigma=None):
        """
        Initializes all necessary components of the TensorFlow
        Graph.
 
        m X n are the dimensions of the SOM. 'n_iterations' should
        should be an integer denoting the number of iterations undergone
        while training.
        'dim' is the dimensionality of the training inputs.
        'alpha' is a number denoting the initial time(iteration no)-based
        learning rate. Default value is 0.3
        'sigma' is the the initial neighbourhood value, denoting
        the radius of influence of the BMU while training. By default, its
        taken to be half of max(m, n).
        """

        #Assign required variables first
        self._m = m
        self._n = n
        if alpha is None:
            alpha = 0.3
        else:
            alpha = float(alpha)
        if sigma is None:
            sigma = max(m, n) / 2.0
        else:
            sigma = float(sigma)
        self._n_iterations = abs(int(n_iterations))

        ##INITIALIZE GRAPH
        self._graph = tf.Graph()

        ##POPULATE GRAPH WITH NECESSARY COMPONENTS
        with self._graph.as_default():

            ##VARIABLES AND CONSTANT OPS FOR DATA STORAGE

            #Randomly initialized weightage vectors for all neurons,
            #stored together as a matrix Variable of size [m*n, dim]
            self._weightage_vects = tf.Variable(tf.random_normal([m * n, dim]))

            #Matrix of size [m*n, 2] for SOM grid locations
            #of neurons
            self._location_vects = tf.constant(
                np.array(list(self._neuron_locations(m, n))))

            ##PLACEHOLDERS FOR TRAINING INPUTS
            #We need to assign them as attributes to self, since they
            #will be fed in during training

            #The training vector
            self._vect_input = tf.placeholder("float", [dim])
            #Iteration number
            self._iter_input = tf.placeholder("float")

            ##CONSTRUCT TRAINING OP PIECE BY PIECE
            #Only the final, 'root' training op needs to be assigned as
            #an attribute to self, since all the rest will be executed
            #automatically during training

            #To compute the Best Matching Unit given a vector
            #Basically calculates the Euclidean distance between every
            #neuron's weightage vector and the input, and returns the
            #index of the neuron which gives the least value
            bmu_index = tf.argmin(
                tf.sqrt(
                    tf.reduce_sum(
                        tf.pow(
                            tf.subtract(
                                self._weightage_vects,
                                tf.stack(
                                    [self._vect_input for i in range(m * n)])),
                            2), 1)), 0)

            #This will extract the location of the BMU based on the BMU's
            #index
            slice_input = tf.pad(tf.reshape(bmu_index, [1]), np.array([[0,
                                                                        1]]))
            bmu_loc = tf.reshape(
                tf.slice(self._location_vects, slice_input,
                         tf.constant(np.array([1, 2]))), [2])

            #To compute the alpha and sigma values based on iteration
            #number
            learning_rate_op = tf.subtract(
                1.0, tf.div(self._iter_input, self._n_iterations))
            _alpha_op = tf.multiply(alpha, learning_rate_op)
            _sigma_op = tf.multiply(sigma, learning_rate_op)

            #Construct the op that will generate a vector with learning
            #rates for all neurons, based on iteration number and location
            #wrt BMU.
            bmu_distance_squares = tf.reduce_sum(
                tf.pow(
                    tf.subtract(self._location_vects,
                                tf.stack([bmu_loc for i in range(m * n)])), 2),
                1)
            neighbourhood_func = tf.exp(
                tf.negative(
                    tf.div(tf.cast(bmu_distance_squares, "float32"),
                           tf.pow(_sigma_op, 2))))
            learning_rate_op = tf.multiply(_alpha_op, neighbourhood_func)

            #Finally, the op that will use learning_rate_op to update
            #the weightage vectors of all neurons based on a particular
            #input
            learning_rate_multiplier = tf.stack([
                tf.tile(
                    tf.slice(learning_rate_op, np.array([i]), np.array([1])),
                    [dim]) for i in range(m * n)
            ])
            weightage_delta = tf.multiply(
                learning_rate_multiplier,
                tf.subtract(tf.stack([self._vect_input for i in range(m * n)]),
                            self._weightage_vects))
            new_weightages_op = tf.add(self._weightage_vects, weightage_delta)
            self._training_op = tf.assign(self._weightage_vects,
                                          new_weightages_op)

            ##INITIALIZE SESSION
            self._sess = tf.Session()

            ##INITIALIZE VARIABLES
            init_op = tf.global_variables_initializer()
            self._sess.run(init_op)