def init_data(inputFile, K):
    global training_data, validation_data, centroids, training_num, data_dim, centroids_num 
    global tf_data_set, tf_centroids
    # initialize data and centroids
    data = np.float32( np.load(inputFile))
    data = (data - data.mean()) / data.std()
    # update data_num and centroids_num
    data_num, data_dim = data.shape
    centroids_num = K
    # training data and validation data
    training_num = int(2./3 * data_num)
    training_data = data[:training_num]
    validation_data = data[training_num:]
    centroids = tf.truncated_normal(shape=[centroids_num, data_dim])
    # update tf_data_set and tf_centroids
    tf_data_set = tf.placeholder(tf.float32, shape=[None, data_dim])
    tf_centroids = tf.Variable(tf.convert_to_tensor(centroids, dtype=tf.float32))
    ########### for the training cases #####################
    # get the euclidean distance
    tf_train_dist = euclidean_dist(tf_data_set, tf_centroids, training_num, centroids_num)
    # get the min index for data set
    tf_train_min_index = tf.argmin(tf_train_dist, dimension=1)
    # loss and optimizer
    tf_train_loss = tf.reduce_sum(tf.reduce_min(euclidean_dist(tf_data_set, tf_centroids, training_num, centroids_num), 
        1, keep_dims=True))
    tf_train_opt = tf.train.AdamOptimizer(learning_rate=0.1, beta1=0.9, beta2=0.99, epsilon=1e-5).minimize(tf_train_loss)
    ########### for the validation cases ####################
    tf_valid_dist = euclidean_dist(tf_data_set, tf_centroids, (data_num-training_num), centroids_num)
    tf_valid_min_index = tf.argmin(tf_valid_dist, dimension=1)
    tf_valid_loss = tf.reduce_sum(tf.reduce_min(euclidean_dist(tf_data_set, tf_centroids, (data_num-training_num), centroids_num), 
        1, keep_dims=True))
    return tf_train_min_index, tf_train_loss, tf_train_opt, tf_valid_loss
Exemplo n.º 2
0
 def testArgMinMax(self):
   with self.cached_session():
     self.assertAllEqual(
         tf.argmin([[1, 2, 3], [4, 1, 0]], dimension=1).eval(),
         [0, 2])
     self.assertAllEqual(
         tf.argmin([[1, 2, 3], [4, 1, 0]], dimension=0).eval(),
         [0, 1, 1])
     self.assertAllEqual(
         tf.argmax([[1, 2, 3], [4, 1, 0]], dimension=1).eval(),
         [2, 0])
     self.assertAllEqual(
         tf.argmax([[1, 2, 3], [4, 1, 0]], dimension=0).eval(),
         [1, 0, 0])
Exemplo n.º 3
0
 def precision(self, y_):
     y_true = tf.cast(tf.argmin(y_, 1), tf.bool)
     y_pred = tf.cast(tf.argmin(self.y, 1), tf.bool)
     # 1 stands for positive, 0 stands for negative
     tp = tf.reduce_sum(tf.cast(tf.logical_and(y_true, y_pred), tf.float32))
     # tn = tf.reduce_sum(tf.cast(tf.logical_not(tf.logical_or(y_true, y_pred)), tf.float32))
     p = tf.reduce_sum(tf.cast(y_true, tf.float32))
     # n = tf.reduce_sum(tf.cast(tf.logical_not(y_true), tf.float32))
     # fp = p - tp
     # fn = n - tn
     # t = tf.add(tp, tn)
     # f = tf.add(fp, fn)
     # relevant = tf.add(tp, fn)
     precision = tf.div(tp, p)
     return precision
Exemplo n.º 4
0
 def recall(self, y_):
     y_true = tf.cast(tf.argmin(y_, 1), tf.bool)
     y_pred = tf.cast(tf.argmin(self.y, 1), tf.bool)
     # 1 stands for positive, 0 stands for negative
     tp = tf.reduce_sum(tf.cast(tf.logical_and(y_true, y_pred), tf.float32))
     tn = tf.reduce_sum(tf.cast(tf.logical_not(tf.logical_or(y_true, y_pred)), tf.float32))
     p = tf.reduce_sum(tf.cast(y_true, tf.float32))
     n = tf.reduce_sum(tf.cast(tf.logical_not(y_true), tf.float32))
     fp = p - tp
     fn = n - tn
     # t = tf.add(tp, tn)
     # f = tf.add(fp, fn)
     relevant = tf.add(tp, fn)
     recall = tf.div(tp, relevant)
     return recall
def get_train(train_ph_dict,var_dict,var_ph_dict,arg_dict):
    mid0 = tf.one_hot(train_ph_dict['choice_0'], 9, axis=-1, dtype=tf.float32)
    mid0 = mid0 * get_q(train_ph_dict['state_0'],var_dict)
    mid0 = tf.reduce_sum(mid0, reduction_indices=[1])

    mid1 = get_q(train_ph_dict['state_1'],var_ph_dict)
    mid1 = tf.reduce_max(mid1, reduction_indices=[1])  
    mid1 = mid1 * train_ph_dict['cont']
    mid1 = mid1 * tf.constant(arg_dict['train_beta'])

#     l2r = tf.constant(0.0)
#     cell_count = tf.constant(0.0)
#     for v in var_dict.values():
#         l2r = l2r + get_l2(v)
#         cell_count = cell_count + tf.to_float(tf.size(v))
#     l2r = l2r / cell_count
#     l2r = l2r / tf.constant(ELEMENT_L2_FACTOR*ELEMENT_L2_FACTOR)
#     l2r = l2r * tf.constant(L2_WEIGHT)
    
    mid = mid0+mid1-train_ph_dict['reward_1']
#    mid = mid * mid
    mid = tf.abs(mid)
    min_loss_idx = tf.argmin(mid, dimension=0)
    mid = tf.reduce_mean(mid)
    score_diff = mid
#     mid = mid + l2r
#     mid = mid + ( tf.abs( tf.reduce_mean(var_dict['b5']) ) * tf.constant(L2_WEIGHT) )

    loss = mid

    mid = tf.train.AdamOptimizer().minimize(mid,var_list=var_dict.values())
    train = mid
    
    return train, loss, score_diff, min_loss_idx
Exemplo n.º 6
0
def argmin(x, axis=-1):
    '''Returns the index of the minimum value
    along a tensor axis.
    '''
    if axis < 0:
        axis = axis % len(x.get_shape())
    return tf.argmin(x, axis)
def model_train(k):
    data = np.float32(np.load('data100D.npy'))
    sample_num = data.shape[0]
    dim = data.shape[1]
    cluster = k

    tf_data = tf.placeholder(tf.float32, shape=(sample_num, dim))
    tf_centroids = tf.Variable(tf.truncated_normal([k, dim], mean=0.0, stddev=1.0))
    tf_min_index = tf.argmin(eucl_distance(tf_data, tf_centroids), dimension = 1)
    tf_loss = tf.reduce_sum(tf.reduce_min(eucl_distance(tf_data, tf_centroids),1,keep_dims=True))
    optimizer = tf.train.AdamOptimizer(0.01,0.9,0.99,1e-5).minimize(tf_loss)

    sess = tf.InteractiveSession()

    init = tf.initialize_all_variables()
    init.run()

    epoch = 1000
    loss_list = []
    for i in range(epoch):
        feed_dict = {tf_data: data}
        _, loss, assignments, centroids = sess.run([optimizer, tf_loss, tf_min_index, tf_centroids], feed_dict = feed_dict)
        loss_list.append(loss)
        if (i % 50== 0):
            print("Loss at step %d: %f" % (i, loss))

    cal_percentage(assignments, k)

    plt.title('the loss vs the number of updates 100-D')
    plt.xlabel('the number of updates')
    plt.ylabel('the value of the loss')
    plt.plot(range(len(loss_list)), loss_list)
    plt.show()
    return loss
Exemplo n.º 8
0
    def _build_graph(self):
        """Construct tensorflow nodes for round of clustering"""
        # N.B. without tf.Variable, makes awesome glitchy clustered images
        self.centroids_in = tf.Variable(tf.slice(tf.random_shuffle(self.arr),
                                     [0, 0], [self.k, -1]), name="centroids_in")
        # tiled should be shape(self.n_pixels, self.k, size_data = 2 + self.channels)
        tiled_pix = tf.tile(tf.expand_dims(self.arr, 1),
                            multiples=[1, self.k, 1], name="tiled_pix")

        # no need to take square root b/c positive reals and sqrt are isomorphic
        def radical_euclidean_dist(x, y):
            """Takes in 2 tensors and returns euclidean distance radical, i.e. dist**2"""
            with tf.name_scope("radical_euclidean"):
                return tf.square(tf.sub(x, y))

        # should be shape(self.n_pixels, self.k)
        distances = tf.reduce_sum(radical_euclidean_dist(tiled_pix, self.centroids_in),
                                  reduction_indices=2, name="distances")
        # should be shape(self.n_pixels)
        nearest = tf.to_int32(tf.argmin(distances, 1), name="nearest")

        # should be list of len self.k with tensors of shape(size_cluster, size_data)
        self.clusters = tf.dynamic_partition(self.arr, nearest, self.k)
        # should be shape(self.k, size_data)
        self.centroids = tf.pack([tf.reduce_mean(cluster, 0) for cluster in self.clusters],
            name="centroids_out")
        self.update_roids = tf.assign(self.centroids_in, self.centroids)
Exemplo n.º 9
0
 def assign_to_nearest(self, samples, centroids):
     expanded_vectors = tf.expand_dims(samples, 0)
     expanded_centroids = tf.expand_dims(centroids, 1)
     distances = tf.reduce_sum(tf.square(tf.sub(expanded_vectors, expanded_centroids)), 2)
     mins = tf.argmin(distances, 0)
     nearest_indices = mins
     return nearest_indices
Exemplo n.º 10
0
  def __call__(self, codes):
    """Use codebook to find nearest neighbor for each code.

    Args:
      codes: A `float`-like `Tensor` containing the latent
        vectors to be compared to the codebook. These are rank-3 with shape
        `[batch_size, latent_size, code_size]`.

    Returns:
      nearest_codebook_entries: The 1-nearest neighbor in Euclidean distance for
        each code in the batch.
      one_hot_assignments: The one-hot vectors corresponding to the matched
        codebook entry for each code in the batch.
    """
    distances = tf.norm(
        tf.expand_dims(codes, 2) -
        tf.reshape(self.codebook, [1, 1, self.num_codes, self.code_size]),
        axis=3)
    assignments = tf.argmin(distances, 2)
    one_hot_assignments = tf.one_hot(assignments, depth=self.num_codes)
    nearest_codebook_entries = tf.reduce_sum(
        tf.expand_dims(one_hot_assignments, -1) *
        tf.reshape(self.codebook, [1, 1, self.num_codes, self.code_size]),
        axis=2)
    return nearest_codebook_entries, one_hot_assignments
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
Exemplo n.º 12
0
def assign_to_cluster(X, centroids):  

    expanded_vectors = tf.expand_dims(X, 0)
    expanded_centroids = tf.expand_dims(centroids, 1)
    distances = tf.reduce_sum(tf.square(tf.subtract(expanded_vectors, expanded_centroids)), 2)
    mins = tf.argmin(distances, 0)

    return mins
Exemplo n.º 13
0
 def cal_loss(self, X, Y, D):
     ED = ed.Euclid_Distance(X, Y, D)
     dist = ED.cal_Euclid_dis()
     cluster = tf.argmin(dist, 1)
     correspond_cluster = tf.gather(Y,cluster)
     offset = tf.sub(X, correspond_cluster)
     loss = tf.reduce_sum(tf.square(offset))
     return loss, cluster
Exemplo n.º 14
0
  def model(data, train=False):
    """The Model definition."""
    # 2D convolution, with 'SAME' padding (i.e. the output feature map has
    # the same size as the input). Note that {strides} is a 4D array whose
    # shape matches the data layout: [image index, y, x, depth].
    conv = tf.nn.conv2d(data,
                        conv1_weights,
                        strides=[1, 1, 1, 1],
                        padding='SAME')
    # Bias and rectified linear non-linearity.
    relu = tf.nn.relu(tf.nn.bias_add(conv, conv1_biases))
    # Max pooling. The kernel size spec {ksize} also follows the layout of
    # the data. Here we have a pooling window of 2, and a stride of 2.
    pool = tf.nn.max_pool(relu,
                          ksize=[1, 2, 2, 1],
                          strides=[1, 2, 2, 1],
                          padding='SAME')
    conv = tf.nn.conv2d(pool,
                        conv2_weights,
                        strides=[1, 1, 1, 1],
                        padding='SAME')
    relu = tf.nn.relu(tf.nn.bias_add(conv, conv2_biases))
    pool = tf.nn.max_pool(relu,
                          ksize=[1, 2, 2, 1],
                          strides=[1, 2, 2, 1],
                          padding='SAME')
    # Reshape the feature map cuboid into a 2D matrix to feed it to the
    # fully connected layers.
    pool_shape = pool.get_shape().as_list()
    reshape = tf.reshape(
        pool,
        [pool_shape[0], pool_shape[1] * pool_shape[2] * pool_shape[3]])

    C_max = 4 * np.sqrt(6. / (20+512))
    C_init = tf.random_uniform(shape=[20,512],
                       minval=-C_max,maxval=C_max)
    C = tf.Variable(C_init)

    C2 = tf.expand_dims(C,0)
    ipdb.set_trace()
    X2 = tf.expand_dims(reshape)
    
    dist = tf.reduce_sum(tf.square(tf.sub(X2,C2)),2)
    loss1 = tf.reduce_mean(tf.reduce_min(dist,1))
    choice = tf.argmin(dist,1)
    
    # Fully connected layer. Note that the '+' operation automatically
    # broadcasts the biases.
    hidden = tf.nn.relu(tf.matmul(reshape, fc1_weights) + fc1_biases)
    # Add a 50% dropout during training only. Dropout also scales
    # activations such that no rescaling is needed at evaluation time.
    if train:
      hidden = tf.nn.dropout(hidden, 0.5, seed=SEED)

    logits = tf.matmul(hidden, fc2_weights) + fc2_biases

    return loss1, logits 
    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
Exemplo n.º 16
0
    def get_bmu_loc(self, x):

        expanded_x = tf.expand_dims(x, 0)
        sqr_diff = tf.square(tf.subtract(expanded_x, self.nodes))
        dists = tf.reduce_sum(sqr_diff, 1)
        bmu_idx = tf.argmin(dists, 0)
        bmu_loc = tf.pack([tf.mod(bmu_idx, self.width), tf.div(bmu_idx, self.width)])

        return bmu_loc
Exemplo n.º 17
0
 def testArgMaxMin(self):
   self.assertAllClose(
       [1],
       tf.argmax([[1, 3, 2]], name='abc', dimension=1))
   self.assertAllClose(
       [0, 0, 0],
       tf.argmax([[1, 3, 2]], dimension=0))
   self.assertAllClose(
       [0],
       tf.argmin([[1, 3, 2]], name='abc', dimension=1))
Exemplo n.º 18
0
def get_index_of_thresholds(output):
    DATASET_SIZE = 125
    a = tf.abs(output - THRESHOLD_0)
    b = tf.abs(output - THRESHOLD_128)
    c = tf.abs(output - THRESHOLD_192)
    d = tf.abs(output - THRESHOLD_254)
    distances = tf.concat(0, [a, b, c, d])
    distances = tf.reshape(distances, [4, 256 * 256, DATASET_SIZE])

    return tf.argmin(distances, 0)
Exemplo n.º 19
0
def _find_interval_containing_new_value(x, new_value):
  """Find the index of x (ascending-ordered) after which new_value occurs."""
  new_value_shape = shape_utils.combined_static_and_dynamic_shape(new_value)[0]
  x_shape = shape_utils.combined_static_and_dynamic_shape(x)[0]
  compare = tf.cast(tf.reshape(new_value, shape=(new_value_shape, 1)) >=
                    tf.reshape(x, shape=(1, x_shape)),
                    dtype=tf.int32)
  diff = compare[:, 1:] - compare[:, :-1]
  interval_idx = tf.argmin(diff, axis=1)
  return interval_idx
Exemplo n.º 20
0
def update_centroids(samples, centroids, num_clusters):
    # First, lets find the data samples closest to a centroid, then we update 
    # its value using all vectors within that cluster
    expanded_data_vectors = tf.expand_dims(samples, 0)
    expanded_centroids = tf.expand_dims(centroids, 1)
    distances = tf.reduce_sum( tf.square( tf.sub( expanded_data_vectors, expanded_centroids ) ), 2 )
    nearest_samples = tf.to_int32( tf.argmin(distances, 0) )
    partitioned_data = tf.dynamic_partition(samples, nearest_samples, num_clusters)
    new_centroids = tf.concat(0, [tf.expand_dims(tf.reduce_mean(partition, 0), 0) for partition in partitioned_data])
    return new_centroids
Exemplo n.º 21
0
def main(CPoints_init = [1,6,10,15], numP = 15,numC = 4):
	
	cur_dir = os.path.dirname(__file__)
	outfile = os.path.join(cur_dir, "kMeans_output.txt")
	if os.path.exists(outfile):
		os.remove(outfile)
	writefile = open(outfile, "a") 

	data = np.array([[1,1],[1,2],[2,2],[2,3],[5,10],[5,12],[6,2],[6,4],[6,10],[7,2],[7,5],[8,8],[8,9],[9,9],[15,8]], dtype="float32")
	
	writefile.write("initialized centroids")
	line = str(data[[x-1 for x in CPoints_init]]) + "\n"
	writefile.write(line)
	graph = tf.Graph()

	dim = 2
	with graph.as_default():
		points = tf.constant(data)
		
		centroids = tf.placeholder("float32",[numC, dim])
		dists = tf.Variable(np.zeros([numP,numC]))
		expanded_p = tf.expand_dims(points,0) # 1 * 15 *2
		expanded_c = tf.expand_dims(centroids,1) #4 * 1 *2
		dists = tf.reduce_sum(tf.square(tf.sub(expanded_p, expanded_c)),2)
		assign_clusters = tf.argmin(dists,0)
		vec_in_clusters = tf.placeholder("float32", [None, dim])
		new_centroids = tf.reduce_mean(vec_in_clusters,0)



	MAX_iterations = 10
	with tf.Session(graph = graph) as sess:
		tf.initialize_all_variables().run()
		centers = data[[x-1 for x in CPoints_init]]
		#
		for k in xrange(MAX_iterations):
			# compute the assignment
			new_assign = sess.run(assign_clusters, feed_dict = {centroids: centers})
			new_assign = list(new_assign)
			line = "assigments: " +  str([x+1 for x in new_assign]) + "\n"
			writefile.write(line)
			# computer the enw centroids
			new_centroids_List = []
			for c in xrange(numC):
				new_vec_in_clusters = [data[k] for k in xrange(numP) if new_assign[k] == c]
				num_vec_in_clusters = np.array(new_vec_in_clusters)
				center = sess.run(new_centroids,feed_dict = {vec_in_clusters:new_vec_in_clusters})
				new_centroids_List.append(center)
			
			centers = np.array(new_centroids_List)
			line = "new centers: " + str(centers) + "\n"
			writefile.write(line)
		writefile.close()
Exemplo n.º 22
0
def main1_2():
    # Load the data
    data2D = np.load("data2D.npy")

    # Set constants.
    K = 3
    DATASET_SIZE, DATA_DIM  = data2D.shape
    LEARNINGRATE = 0.01
    ITERATIONS = 500
        
    # Initialize tf graph.
    graph = tf.Graph()
    with graph.as_default():
        # Load data into tf.
        tf_data2D = tf.cast(tf.constant(data2D), tf.float32)
        
        # Initialize mu array.
        tf_mu = tf.Variable(tf.truncated_normal([K, DATA_DIM], dtype=tf.float32, stddev=1.0/np.sqrt(DATA_DIM)))
        
        ed = tf_eucl_dist(tf_data2D, tf_mu)
        
        cluster_assignments = tf.argmin(ed, 1)
        
        loss = tf.reduce_sum(tf.reduce_min(ed, 1))

        optimizer = tf.train.AdamOptimizer(LEARNINGRATE, beta1=0.9, beta2=0.99, epsilon=1e-5).minimize(loss)

    # Run session.
    with tf.Session(graph=graph) as session:
        
        losses = np.zeros(ITERATIONS, dtype=np.float32)
        tf.initialize_all_variables().run()

        for i in range(ITERATIONS):
            _, ca, l, m = session.run([optimizer, cluster_assignments, loss, tf_mu])
            losses[i] = l
            
        print ca
        red = [1, 0, 0]
        green = [0, 1, 0]
        blue = [0, 0, 1]
        colours = [red, green, blue]
        colour_list = [colours[ca[i]] for i in range(DATASET_SIZE)]
        
        # Plot data points labelled by the closest mean  
        plt.scatter(data2D[:,0], data2D[:,1], c=colour_list, marker='.')
        # Plot mean
        plt.scatter(m[:,0], m[:,1], marker='h')
        print m
        # TODO: Add plot title, axis labels
        plt.show()

    return 
Exemplo n.º 23
0
def assign_to_nearest(samples, centroids):
    # Finds the nearest centroid for each sample

    # START from http://esciencegroup.com/2016/01/05/an-encounter-with-googles-tensorflow/
    expanded_vectors = tf.expand_dims(samples, 0)
    expanded_centroids = tf.expand_dims(centroids, 1)
    distances = tf.reduce_sum( tf.square(
               tf.sub(expanded_vectors, expanded_centroids)), 2)
    mins = tf.argmin(distances, 0)
    # END from http://esciencegroup.com/2016/01/05/an-encounter-with-googles-tensorflow/
    nearest_indices = mins
    return nearest_indices
Exemplo n.º 24
0
def run_one_step(dataframe, start_centers):
    """
    Performs one iteration of K-Means.

    This function takes a dataframe with dense feature vectors, a set of centroids, and returns
    a new set of centroids along with the total distance of points to centroids.

    This function calculates for each point the closest centroid and then aggregates the newly
    formed clusters to find the new centroids.

    This function uses Spark to distribute the aggregation amongst the node.

    :param dataframe: a dataframe containing a column of features (an array of doubles)
    :param start_centers: a k x m matrix with k the number of centroids and m the number of features
    :return: a k x m matrix, and a positive double
    """
    # The dimensions in the problem
    (num_centroids, num_features) = np.shape(start_centers)
    # For each feature vector, compute the nearest centroid and the distance to that centroid.
    # The index of the nearest centroid is stored in the 'indexes' column.
    # We also add a column of 1's that will be reduced later to count the number of elements in
    # each cluster.
    with tf.Graph().as_default() as g:
        # The placeholder for the input: we use the block format
        points = tf.placeholder(tf.double, shape=[None, num_features], name='features')
        # The shape of the block is extracted as a TF variable.
        num_points = tf.stack([tf.shape(points)[0]], name="num_points")
        distances = tf_compute_distances(points, start_centers)
        # The outputs of the program.
        # The closest centroids are extracted.
        indexes = tf.argmin(distances, 1, name='indexes')
        # This could be done based on the indexes as well.
        min_distances = tf.reduce_min(distances, 1, name='min_distances')
        counts = tf.tile(tf.constant([1]), num_points, name='count')
        df2 = tfs.map_blocks([indexes, counts, min_distances], dataframe)
    # Perform the reduction: we regroup the points by their centroid indexes.
    gb = df2.groupBy("indexes")
    with tf.Graph().as_default() as g:
        # Look at the documentation of tfs.aggregate for the naming conventions of the placeholders.
        x_input = tfs.block(df2, "features", tf_name="features_input")
        count_input = tfs.block(df2, "count", tf_name="count_input")
        md_input = tfs.block(df2, "min_distances", tf_name="min_distances_input")
        # Each operation is just the sum.
        x = tf.reduce_sum(x_input, [0], name='features')
        count = tf.reduce_sum(count_input, [0], name='count')
        min_distances = tf.reduce_sum(md_input, [0], name='min_distances')
        df3 = tfs.aggregate([x, count, min_distances], gb)
    # Get the new centroids
    df3_c = df3.collect()
    # The new centroids.
    new_centers = np.array([np.array(row.features) / row['count'] for row in df3_c])
    total_distances = np.sum([row['min_distances'] for row in df3_c])
    return (new_centers, total_distances)
Exemplo n.º 25
0
def kMeansTF(data, center, nMaxIter, th): # data: nDim x nData, center: nDim x  nCenter
    """Clustering data using the kMeans method implemented with tensorflow.

    :param data: 2D matrix as data input with dimensions: nDim x nData.
    :type data: numpy array.
    :param center: 2D matrix with initial cluster centers with dimensions: nDim x nCenter.
    :type center: numpy array.
    :param nMaxIter: Maximum number of iterations.
    :type nMaxIter: int.
    :param th: Threshold applied to RMS error between prior and current cluster centers.
    :type th: float.
    :return 2D matrix with computed cluster centers with dimensions> nDim x nCenter.
    """
    nData   = data.shape[1]
    nCenter = center.shape[1]
    center  = tf.Variable(center)

    # Replicate data to have the dimensions: nDim x nData x nCenter
    rData       = tf.tile(tf.expand_dims(data,-1),[1, 1, nCenter]) # replicate for nCenter
    rCenter     = tf.transpose(tf.tile(tf.expand_dims(center,-1),[1, 1, nData]),perm=[0, 2, 1]) # replicate for nData

    # Get the cluster center of minimum distance for each data point.
    ssq         = tf.reduce_sum(tf.square(rData - rCenter), 0, keep_dims=True) # over nDim
    index       = tf.squeeze(tf.argmin(ssq, 2)) # min index over nCenter and remove leading dimension

    # Compute the new cluster centers based on the closest data points.
    newSum      = tf.unsorted_segment_sum(tf.transpose(data,[1,0]), index, nCenter)
    count       = tf.unsorted_segment_sum(tf.transpose(tf.ones_like(data),[1,0]), index, nCenter)
    newCenter   = tf.transpose(newSum / count,[1,0])

    # Compute the differences between the new and old cluster centers and threshold them.
    rms             = tf.reduce_sum(tf.sqrt(tf.reduce_sum((center-newCenter)*(center-newCenter), 0)), 0)
    changeCenter    = rms > th

    # Update the cluster centers if they have changed by more than the threshold value.
    with tf.control_dependencies([changeCenter]):
        doUpdates = center.assign(newCenter)

    # Initialize the tensor variables.
    init = tf.initialize_all_variables()
    sess = tf.Session()
    sess.run(init)

    # As long as there are enough changes in the cluster centers and we have not reached the maximum number of
    # iterations, repeat the steps from above.
    changed = True
    iter    = 0
    while changed and iter < nMaxIter:
        iter += 1
        [changed, _] = sess.run([changeCenter, doUpdates])

    return sess.run(center)
Exemplo n.º 26
0
def clusterize(batch):
    start = time.time()
    
    n = len(batch)
    dim = len(batch[0])
    print batch
    points = tf.placeholder(tf.int32, [n,dim])
    cluster_assignments = tf.Variable(tf.zeros([n], dtype=tf.int64))

    # Use  K random points as the starting centroids
    centroids = tf.Variable(tf.slice(tf.random_shuffle(points), [0,0], [K,dim]))


    # Replicate to n copies of each centroid and K copies of each
    # point, then subtract and compute the sum of squared distances.
    rep_centroids = tf.reshape(tf.tile(centroids, [n, 1]), [n, K, dim])
    rep_points = tf.reshape(tf.tile(points, [1, K]), [n, K, dim])
    sum_squares = tf.reduce_sum(tf.square(rep_points - rep_centroids), reduction_indices=2)

    # Use argmin to select the lowest-distance point
    best_centroids = tf.argmin(sum_squares, 1)
    did_assignments_change = tf.reduce_any(tf.not_equal(best_centroids, 
                                                    cluster_assignments))

    def bucket_mean(data, bucket_ids, num_buckets):
        total = tf.unsorted_segment_sum(data, bucket_ids, num_buckets)
        count = tf.unsorted_segment_sum(tf.ones_like(data), bucket_ids, num_buckets)
        return total / count

    means = bucket_mean(points, best_centroids, K)

    # Do not write to the assigned clusters variable until after
    # computing whether the assignments have changed - hence with_dependencies
    with tf.control_dependencies([did_assignments_change]):
        do_updates = tf.group(
            centroids.assign(means),
            cluster_assignments.assign(best_centroids))

    changed = True
    iters = 0
    sess = tf.Session()
    sess.run(tf.initialize_all_variables(), feed_dict={points: batch})

    while changed and iters < MAX_ITERS:
        iters += 1
        [changed, _] = sess.run([did_assignments_change, do_updates], feed_dict={points: batch})

    [centers, assignments] = sess.run([centroids, cluster_assignments], feed_dict={points: batch})
    end = time.time()
    print ("Found in %.2f seconds" % (end-start)), iters, "iterations"

    return [centers, assignments]
Exemplo n.º 27
0
 def _setup_enqueuing(self, queues, **loom_kwargs):
   """Sets up enqueuing to the approx. smallest (least full) of `queues`."""
   self.compiler.init_loom(loom_input_tensor=None, **loom_kwargs)
   input_tensor = self.compiler.loom_input_tensor
   fns = [lambda r=q: r.enqueue_many([input_tensor]) for q in queues]
   self.train_op = _tf_nth(fns, tf.argmin(_noised_q_sizes(queues), axis=0))
   self.losses.clear()
   self.losses['dummy'] = tf.constant(0.0)
   self.save_summaries_secs = 0
   self.dev_examples = None
   self.train_feeds.clear()
   self.save_model_secs = 0
   self.exact_batch_sizes = True
Exemplo n.º 28
0
def kMeansCluster(vector_values, num_clusters, max_num_steps, stop_coeficient = 0.0):
  vectors = tf.constant(vector_values)
  centroids = tf.Variable(tf.slice(tf.random_shuffle(vectors),
                                   [0,0],[num_clusters,-1]))
  old_centroids = tf.Variable(tf.zeros([num_clusters,2]))
  centroid_distance = tf.Variable(tf.zeros([num_clusters,2]))

  expanded_vectors = tf.expand_dims(vectors, 0)
  expanded_centroids = tf.expand_dims(centroids, 1)

  print expanded_vectors.get_shape()
  print expanded_centroids.get_shape()

  distances = tf.reduce_sum(
    tf.square(tf.sub(expanded_vectors, expanded_centroids)), 2)
  assignments = tf.argmin(distances, 0)

  means = tf.concat(0, [
    tf.reduce_mean(
        tf.gather(vectors,
                  tf.reshape(
                    tf.where(
                      tf.equal(assignments, c)
                    ),[1,-1])
                 ),reduction_indices=[1])
    for c in xrange(num_clusters)])

  save_old_centroids = tf.assign(old_centroids, centroids)

  update_centroids = tf.assign(centroids, means)
  init_op = tf.initialize_all_variables()

  performance = tf.assign(centroid_distance, tf.sub(centroids, old_centroids))
  check_stop = tf.reduce_sum(tf.abs(performance))

  with tf.Session() as sess:
    sess.run(init_op)
    for step in xrange(max_num_steps):
      print "Running step " + str(step)
      sess.run(save_old_centroids)
      _, centroid_values, assignment_values = sess.run([update_centroids,
                                                        centroids,
                                                        assignments])
      sess.run(check_stop)
      current_stop_coeficient = check_stop.eval()
      print "coeficient:", current_stop_coeficient
      if current_stop_coeficient <= stop_coeficient:
        break

    return centroid_values, assignment_values
Exemplo n.º 29
0
def sample_prob(y_prob, out_size):
	y_acc = []
	for idx in range(out_size):
	    val = y_prob[0, idx] if idx==0 else val+y_prob[0, idx]
	    y_acc.append(val)
	sample_ = tf.random_uniform([1])
	dist_list = []
	for idx in range(out_size):
	    dist = y_acc[idx] - sample_
	    val = tf.select(tf.greater(dist, tf.expand_dims(tf.constant(0.), 0)), dist, tf.expand_dims(tf.constant(1.), 0))
	    dist_list.append(val)
	dist_arr = tf.pack(dist_list)
	pos = tf.argmin(dist_arr, 0)
	onehot = tf.one_hot(pos, out_size, dtype=tf.float32)
	return pos, onehot
Exemplo n.º 30
0
def assign_data(dataset, clusters):
    '''

    Calculates the cluster assignments given a dataset and cluster centers
    Args:
        dataset: Set of points
        clusters: Centers of clusters

    Returns:
        min_dist: List of point classes in the same order they appear in the dataset

    '''
    dists = square_distance(clusters, dataset)  # ||x - mu||
    min_dist = tf.argmin(dists, 1)  # argmin ||x - mu||
    return min_dist
Exemplo n.º 31
0
def argmin(x, axis=-1):
    if axis < 0:
        axis = axis % len(x.get_shape())
    return tf.argmin(x, axis)
Exemplo n.º 32
0
    print("result: ", result)

    observer.manual_fix()

    result, new_history = bo.optimize(15 - len(history), history[-1].dataset,
                                      history[-1].model, acquisition_rule,
                                      history[-1].acquisition_state).astuple()

    history.extend(new_history)

# %% [markdown]
# We can repeat this until we've spent our optimization budget, using a loop if appropriate. But here, we'll just plot the data if it exists, safely by using `result`'s `is_ok` attribute.

# %%
from util.plotting import plot_bo_points, plot_function_2d

if result.is_ok:
    data = result.unwrap().dataset
    arg_min_idx = tf.squeeze(tf.argmin(data.observations, axis=0))
    _, ax = plot_function_2d(branin,
                             search_space.lower,
                             search_space.upper,
                             30,
                             contour=True)
    plot_bo_points(data.query_points.numpy(), ax[0, 0], 5, arg_min_idx)

# %% [markdown]
# ## LICENSE
#
# [Apache License 2.0](https://github.com/secondmind-labs/trieste/blob/develop/LICENSE)
Exemplo n.º 33
0
def vector_quantization(
        x: tf.Tensor,
        n: int,
        alpha: Union[float, tf.Tensor] = 0.1,
        beta: Union[float, tf.Tensor] = 1e-4,
        gamma: Union[float, tf.Tensor] = 1e-6,
        lookup_ord: int = 2,
        dim_reduction: str = None,
        num_dim_reduction_components: int = -1,
        embedding_initializer: Union[
            str,
            tf.keras.initializers.Initializer] = tf.random_normal_initializer,
        constant_init: bool = False,
        num_splits: int = 1,
        num_embeds_replaced: int = 0,
        is_training: Union[bool, tf.Tensor] = False,
        return_endpoints: bool = False,
        name: str = 'vq') -> Union[tf.Tensor, VQEndpoints]:
    """
    Vector quantization layer.
    :param x: Tensor of shape [batch, r, q], where this function quantizes along dimension q
    :param n: Size of the embedding space (number of contained vectors)
    :param alpha: Weighting of the alpha-loss term (lookup vector distance penalty)
    :param beta: Weighting of the beta-loss term (all vectors distance penalty)
    :param gamma: Weighting of the coulomb-loss term (embedding space spacing)
    :param dim_reduction: If not None, will use the given technique to reduce the dimensionality of inputs and
           embedding vectors before comparing them using the distance measure given by lookup_ord; one of
           ['pca-batch', 'pca-emb-space'].
    :param num_dim_reduction_components: When using dimensionality reduction, this specifies the number of components
           (dimensions) that each embedding vector (and corresponding input) is reduced to.
    :param lookup_ord: Order of the distance function; one of [np.inf, 1, 2]
    :param embedding_initializer: Initializer for the embedding space variable or 'batch'
    :param constant_init: Whether the initializer is constant (in this case, the shape will not be passed to
                          'get_variable' explicitly.
    :param num_splits: Number of splits along the input dimension q (defaults to 1)
    :param num_embeds_replaced: If greater than 0, this adds an op to the endpoints tuple which replaces the respective
           number of least used embedding vectors since the last replacement with vectors distant from the embedding
           vectors. If 'return_endpoints' is False, changing this to a number != 0 will not result in anything.
    :param is_training: Whether or not to update replacement accumulators.
    :param return_endpoints: Whether or not to return a plurality of endpoints (defaults to False)
    :param name: Name to use for the variable scope
    :return: Only the layer output if return_endpoints is False
             VQEndpoints-tuple with the values:
                layer_out: Layer output
                emb_space: Embedding space
                access_count: Access counter with integral values indicating how often each embedding vector was used
                distance: Distance of inputs from the embedding space vectors
                emb_spacing: Embedding spacing vector where each entry indicates the distance between embedding vectors
                emb_closest_spacing: Distance of embedding vectors to the closest other embedding vector
                replace_embeds: Op that replaces the least used embedding vectors with the most distant input vectors
                emb_space_batch_init: Embedding space batch init op (is set if embedding_initializer is 'batch')
    """
    dynamic_emb_space_init = (embedding_initializer == 'batch')
    if dynamic_emb_space_init:
        embedding_initializer = tf.zeros_initializer

    in_shape, vec_size = __extract_vq_dimensions(x, num_splits)
    __validate_vq_parameters(n, vec_size, lookup_ord, dim_reduction,
                             num_dim_reduction_components, num_embeds_replaced)

    x = tf.reshape(x, [in_shape[0], in_shape[1] * num_splits, vec_size])
    with tf.variable_scope(name):
        emb_space = __create_embedding_space(x, constant_init,
                                             embedding_initializer, n,
                                             vec_size)

        adjusted_x = x
        adjusted_emb_space = emb_space
        if dim_reduction is not None:
            adjusted_x, adjusted_emb_space = __transform_lookup_space(
                x, emb_space, dim_reduction, in_shape, n, vec_size,
                num_dim_reduction_components)

        # map x to y, where y is the vector from emb_space that is closest to x
        # distance of (adjusted) x from all vectors in the (adjusted) embedding space
        diff = tf.expand_dims(tf.stop_gradient(adjusted_x),
                              axis=2) - adjusted_emb_space
        dist = tf.norm(diff, lookup_ord,
                       axis=3)  # distance between x and all vectors in emb
        emb_index = tf.argmin(dist, axis=2)
        y = tf.gather(emb_space, emb_index, axis=0)

        # update access counter
        one_hot_access = tf.one_hot(emb_index, depth=n)
        access_count = tf.reduce_sum(one_hot_access,
                                     axis=[0, 1],
                                     name='access_count')

        # add losses
        if alpha != 0:
            __add_alpha_loss(x, y, lookup_ord, alpha)

        if beta != 0:
            __add_beta_loss(dist, beta)

        emb_spacing, emb_closest_spacing = None, None
        if gamma != 0 or return_endpoints:
            emb_spacing, emb_closest_spacing = __calculate_emb_spacing(
                emb_space, n, lookup_ord)
            if gamma != 0:
                __add_coulomb_loss(emb_closest_spacing, gamma)

        replace_embeds_and_reset = None
        if num_embeds_replaced > 0 and return_endpoints:
            y, replace_embeds_and_reset = __create_embedding_space_replacement_op(
                x, y, access_count, emb_space, dist, num_embeds_replaced,
                vec_size, is_training)

        emb_space_batch_init = None
        if dynamic_emb_space_init:
            replace_value = tf.slice(tf.reshape(x, [-1, vec_size]),
                                     begin=tf.zeros_like(emb_space.shape),
                                     size=emb_space.shape)
            emb_space_batch_init = tf.assign(emb_space,
                                             value=replace_value,
                                             validate_shape=True)

        # return selection in original size
        # skip this layer when doing back-prop
        layer_out = tf.reshape(tf.stop_gradient(y - x) + x, in_shape)

        if return_endpoints:
            return VQEndpoints(layer_out, emb_space, access_count, dist,
                               emb_spacing, emb_closest_spacing,
                               replace_embeds_and_reset, emb_space_batch_init)
        return layer_out
Exemplo n.º 34
0
    def build_model(self):

        module = hub.Module(self.config.module_path, trainable=True)
        self.height, self.width = hub.get_expected_image_size(module)
        assert (self.height == self.config.image_height)
        assert (self.width == self.config.image_width)
        self.input, self.labels = self.data.next_element

        print(self.labels)
        self.input = tf.reshape(self.input, [-1, self.height, self.width, 3])
        self.labels = tf.reshape(self.labels, [-1])

        self.hidden_layer = module(self.input)
        self.logits = tf.layers.dense(self.hidden_layer, (self.n_classes + 1) *
                                      self.config.n_views,
                                      activation=None,
                                      name="logits")

        #n_objects_per_batch,n_views_in_batch,n_views_logits,n_classes+1
        self.probs = tf.nn.softmax(tf.reshape(self.logits, [
            self.n_objects, self.config.n_views, self.config.n_views,
            self.n_classes + 1
        ]),
                                   axis=-1)
        self.log_p = tf.math.log(self.probs)

        self.scores = self.log_p[..., :-1] - tf.tile(self.log_p[..., -1:],
                                                     [1, 1, 1, self.n_classes])

        tiled = tf.tile(tf.reshape(self.labels, [self.n_objects, -1]),
                        [1, self.n_cands])
        tiled = tf.reshape(
            tiled, [self.n_objects, self.n_cands, self.config.n_views, 1])
        #tensor of shape [n_objs, n_cands, n_views,4]
        self.gather_candidate_scores = tf.concat([self.indexes, tiled],
                                                 axis=-1)
        # candidates[i,j] is the score for object i and view order candidate j
        self.candidate_scores = tf.reduce_sum(tf.gather_nd(
            self.scores, self.gather_candidate_scores),
                                              axis=-1)

        best_candidates = tf.reshape(tf.argmin(self.candidate_scores, -1),
                                     [self.n_objects, 1])
        # pair [[0,cand_0],[1,cand_1],...]
        best_candidates = tf.concat([
            tf.reshape(tf.range(0, self.n_objects, dtype=tf.int64),
                       [self.n_objects, 1]), best_candidates
        ],
                                    axis=-1)
        """
        Calculate loss considering best order candidate
        """

        var_list = tf.trainable_variables()[-2:]
        # Train op
        with tf.name_scope("train"):
            #loss function
            with tf.name_scope("loss"):
                self.labels = tf.reshape(self.labels,
                                         [-1, self.config.n_views])[:, 0]
                #Indexes to calculate cross-entropy loss of best view candidates
                #shape [n_objects,n_views,3]
                self.gather_candidate_log_prob = tf.gather_nd(
                    self.gather_candidate_scores, best_candidates)

                #Indexes to dum the loss of i_view
                #Discount the loss of i_view for the best view point
                discount_iview = tf.concat(
                    [(self.n_classes + 1) *
                     tf.ones([self.n_objects, self.config.n_views, 1],
                             dtype=tf.int64),
                     self.gather_candidate_log_prob[..., :-1]],
                    axis=-1)
                self.discount_iview = discount_iview

                self.loss = -tf.gather_nd(self.log_p,
                                          self.gather_candidate_log_prob)
                self.loss = self.loss - tf.reduce_mean(self.log_p[:, :, :, -1],
                                                       axis=-1)
                self.loss = self.loss + tf.gather_nd(self.log_p,
                                                     discount_iview)
                self.loss = tf.reduce_mean(self.loss)

                #l2 loss (improves the performance)
                for var in var_list:
                    self.loss += tf.nn.l2_loss(var) * self.config.weight_decay

                self.predictions = self.select_best(self.logits)

            # setting different training ops for each part of the network
            # Get gradients of all trainable variables
            gradients = tf.gradients(self.loss, var_list)

            optimizer = tf.train.MomentumOptimizer(
                self.config.warmup_learning_rate, self.config.momentum)
            training_op = optimizer.apply_gradients(
                zip(gradients, var_list), global_step=self.global_step_tensor)
            self.train_step_warmup = training_op

            var_list = tf.trainable_variables()
            # learning rate
            self.config.learning_rate = tf.train.exponential_decay(
                self.config.learning_rate,
                self.global_step_tensor,
                self.config.decay_steps,
                self.config.decay_rate,
                staircase=True)

            gradients = tf.gradients(self.loss, var_list)
            optimizer = tf.train.MomentumOptimizer(self.config.learning_rate,
                                                   self.config.momentum)
            training_op = optimizer.apply_gradients(
                zip(gradients, var_list), global_step=self.global_step_tensor)
            self.train_step = training_op
            self.sess.run(tf.global_variables_initializer())
Exemplo n.º 35
0
})
sns.lmplot("x", "y", data=df, fit_reg=False, size=7)
plt.show()
vectors = tf.constant(vector_values)
centroids = tf.Variable(
    tf.slice(tf.random_shuffle(vectors), [0, 0], [num_clusters, -1]))
expanded_vectors = tf.expand_dims(vectors, 0)
expanded_centroids = tf.expand_dims(centroids, 1)

print(expanded_vectors.get_shape())
print(expanded_centroids.get_shape())

#distances = tf.reduce_sum( tf.square(tf.sub(expanded_vectors, expanded_centroids)), 2 )
distances = tf.reduce_sum(
    tf.square(tf.math.subtract(expanded_vectors, expanded_centroids)), 2)
assignments = tf.argmin(distances, 0)

# 시벌 이게 뭐여

#means = tf.concat(0, [ tf.reduce_mean( tf.gather(vectors, tf.reshape( tf.where( tf.equal(assignments, c) ),[1,-1] )  ),reduction_indices=[1])  for c in range( 0 , num_clusters)] )
means = tf.concat(0, [
    tf.reduce_mean(tf.gather(
        vectors, tf.reshape(tf.where(tf.equal(assignments, c)), [1, -1])),
                   reduction_indices=[1]) for c in range(0, num_clusters)
])

update_centroids = tf.assign(centroids, means)
init_op = tf.initialize_all_variables()

#with tf.Session('local') as sess:
sess = tf.Session()
Exemplo n.º 36
0
def main():
    rospy.init_node(name='grasping', anonymous=True)
    f = open(args.output_path + '/grasping_label.txt', 'a+')
    img_id = len(f.readlines())
    pnp = BaxterAPI(args.limb, args.hover_distance,
                    args.reach_distance)  # pick and place
    pnp.move_to_start(initial_pose)
    image_tools = ImageTools(img_id=img_id)

    images_t = tf.placeholder(dtype=tf.float32, shape=[None, 224, 224, 3])
    images_t = images_t - [123.68, 116.779, 103.939]
    with slim.arg_scope(model_arg_scope()):
        net, end_points = model(inputs=images_t,
                                num_classes=num_classes,
                                is_training=False,
                                dropout_keep_prob=1.0,
                                scope='finetune')
        angle_index_t = tf.argmin(net, axis=1)

        saver = tf.train.Saver()
        session_config = tf.ConfigProto(allow_soft_placement=True,
                                        log_device_placement=False)
        session_config.gpu_options.allow_growth = True
        sess = tf.Session(config=session_config)
        saver.restore(sess, tf.train.latest_checkpoint(args.checkpoint_dir))
        print 'Successfully loading model.'

    t = 0
    success = 0

    while not rospy.is_shutdown() and t != 20:
        flag = 0
        while flag != 1:
            c_x, c_y = utils.get_center_coordinate_from_kinect(
                args.center_coor, args.limb)
            d_x = float(c_x) / 1000.0 + 0.07
            d_y = float(c_y) / 1000.0 + 0.03
            x = origin[0] + d_x
            y = origin[1] - d_y
            z = origin[2]
            flag = pnp.approach(
                Pose(position=Point(x, y, z), orientation=initial_orientation))
        rospy.sleep(0.7)
        utils.listener(args.topic_name, image_tools)
        coors, images = image_tools.sampling_image(args.patch_size,
                                                   args.num_patches)
        scores = sess.run(net, feed_dict={images_t: images})
        patch, coor, new_coors = image_tools.resampling_image(
            scores, coors, args.patch_size)
        angle_index, scores = sess.run([angle_index_t, net],
                                       feed_dict={images_t: patch})
        angle_index = angle_index[0]
        print angle_index
        print scores
        grasp_angle = (angle_index * 10 - 90) * 1.0 / 180 * pi
        x -= float(coor[0] - 140) * 0.6 / 1000
        y -= float(coor[1] - 320) * 0.5 / 1000
        z = z
        pose = Pose(position=Point(x, y, z), orientation=initial_orientation)
        label = pnp.pick(pose, image_tools, args.output_path, new_coors, coor,
                         args.patch_size, grasp_angle)
        success += 1 - label
        f.write('{:06d} {} {}\n'.format(image_tools.img_id - 1, angle_index,
                                        label))
        pnp.move_to_start(initial_pose)
        t += 1
    print success * 1.0 / t
    f.close()
Exemplo n.º 37
0
	def get_labels(self, centroids, X):
		centroids_ = tf.expand_dims(centroids, 1)
		X_ = tf.expand_dims(X, 2)
		return tf.argmin(tf.norm(X_ - centroids_, axis=3), axis=2, output_type=tf.int32)
Exemplo n.º 38
0
def cw(x,
       y=None,
       eps=2.5,
       ord_='inf',
       T=512,
       optimizer=tf.train.AdamOptimizer(learning_rate=0.1),
       alpha=0.9,
       min_prob=0.,
       clip=(0.0, 1.0)):
    xshape = x.get_shape().as_list()
    noise = tf.get_variable('noise',
                            xshape,
                            tf.float32,
                            initializer=tf.initializers.zeros)
    x = x + tf.random_uniform(tf.shape(x), minval=-1e-2, maxval=1e-2) * \
        create_perlin_noise(seed=None, color=False, batch_size=configs['batch_size'], image_size=28, normalize=True,
                            precalc_fade=None)
    # scale input to (0, 1)
    x_scaled = (x - clip[0]) / (clip[1] - clip[0])

    # change to sigmoid-space, clip to avoid overflow.
    z = tf.clip_by_value(x_scaled, 1e-8, 1 - 1e-8)
    xinv = tf.log(z / (1 - z))

    # add noise in sigmoid-space and map back to input domain
    xadv = tf.sigmoid(xinv + T * noise)
    xadv = xadv * (clip[1] - clip[0]) + clip[0]

    logits = []
    ybars = []
    for i in rand_index:
        logit, ybar = models[i](xadv)
        logits.append(logit)
        ybars.append(ybar)
    ybar = tf.add_n(ybars)
    logits = tf.add_n(logits)
    ydim = ybar.get_shape().as_list()[1]

    if y is not None:
        y_temp = tf.cond(tf.equal(tf.rank(y),
                                  0), lambda: tf.fill([xshape[0]], y),
                         lambda: tf.identity(y))
    else:
        # we set target to the least-likely label
        y_temp = tf.argmin(ybar, axis=1, output_type=tf.int32)

    mask = tf.one_hot(y_temp, ydim, on_value=0.0, off_value=float('inf'))
    yt = tf.reduce_max(logits - mask, axis=1)
    yo = tf.reduce_max(logits, axis=1)

    # encourage to classify to a wrong category
    loss0 = tf.nn.relu(yo - yt + min_prob)

    axis = list(range(1, len(xshape)))
    ord_ = float(ord_)

    # make sure the adversarial images are visually close
    if 2 == ord_:
        # CW-L2 Original paper uses the reduce_sum version.  These two
        # implementation does not differ much.

        # loss1 = tf.reduce_sum(tf.square(xadv-x), axis=axis)
        loss1 = tf.reduce_mean(tf.square(xadv - x))
    else:
        # CW-Linf
        tau0 = tf.fill([xshape[0]] + [1] * len(axis), clip[1])
        tau = tf.get_variable('cw8-noise-upperbound',
                              dtype=tf.float32,
                              initializer=tau0,
                              trainable=False)
        diff = xadv - x - tau

        # if all values are smaller than the upper bound value tau, we reduce
        # this value via tau*0.9 to make sure L-inf does not get stuck.
        tau = alpha * tf.to_float(tf.reduce_all(diff < 0, axis=axis))
        loss1 = tf.nn.relu(tf.reduce_sum(diff, axis=axis))

    loss = eps * loss0 + loss1
    train_op = optimizer.minimize(loss, var_list=[noise])

    # We may need to update tau after each iteration.  Refer to the CW-Linf
    # section in the original paper.
    if 2 != ord_:
        train_op = tf.group(train_op, tau)

    return train_op, xadv, noise
Exemplo n.º 39
0
    def _setup_action_selection(self, state_ph, CEM_mode):
        """
            Computes the best action from the current state by using randomly sampled action sequences
            to predict future states, evaluating these predictions according to a cost function,
            selecting the action sequence with the lowest cost, and returning the first action in that sequence

            returns:
                best_action: the action that minimizes the cost function (tensor with shape [self._action_dim])

            implementation details (in order):
                (a) We will assume state_ph has a batch size of 1 whenever action selection is performed
                (b) Randomly sample uniformly self._num_random_action_selection number of action sequences,
                    each of length self._horizon
                (c) Starting from the input state, unroll each action sequence using your neural network
                    dynamics model
                (d) While unrolling the action sequences, keep track of the cost of each action sequence
                    using self._cost_fn
                (e) Find the action sequence with the lowest cost, and return the first action in that sequence

            Hints:
                (i) self._cost_fn takes three arguments: states, actions, and next states. These arguments are
                    2-dimensional tensors, where the 1st dimension is the batch size and the 2nd dimension is the
                    state or action size
                (ii) You should call self._dynamics_func and self._cost_fn a total of self._horizon times
                (iii) Use tf.random_uniform(...) to generate the random action sequences

        """
        ### PROBLEM 2
        ### YOUR CODE HERE
        states = tf.tile(state_ph, [self._num_random_action_selection, 1])

        if not CEM_mode:

            random_actions = tf.random_uniform([
                self._num_random_action_selection, self._horizon,
                self._action_dim
            ],
                                               minval=self._action_space_low,
                                               maxval=self._action_space_high)
            horizon_costs = 0

            for i in range(self._horizon):
                actions = random_actions[:, i, :]
                next_states = self._dynamics_func(states, actions, True)
                horizon_costs += self._cost_fn(states, actions, next_states)
                states = next_states

            index = tf.argmin(horizon_costs)
            best_action_sequence = random_actions[index]
            best_action = best_action_sequence[0]

        else:

            a_mean, a_std = self._init_dataset.action_mean, self._init_dataset.action_std
            top_10_percent = int(0.1 * self._num_random_action_selection)
            itr, max_iter = 0, 20

            while itr < max_iter:
                itr += 1
                dist = tfp.distributions.MultivariateNormalDiag(
                    loc=a_mean, scale_diag=a_std)
                random_actions = tf.cast(dist.sample(
                    [self._num_random_action_selection, self._horizon]),
                                         dtype=tf.float32)
                horizon_costs = 0

                for i in range(self._horizon):
                    actions = random_actions[:, i, :]
                    next_states = self._dynamics_func(states, actions, True)
                    horizon_costs += -self._cost_fn(states, actions,
                                                    next_states)
                    states = next_states

                _, indices = tf.nn.top_k(horizon_costs,
                                         top_10_percent,
                                         sorted=True)
                first_actions = random_actions[:, 0, :]
                selected_actions = tf.gather(first_actions, indices, axis=0)
                a_mean, a_var = tf.nn.moments(selected_actions, axes=[0])
                a_std = a_var**0.5

            index = tf.argmax(horizon_costs)
            best_action_sequence = random_actions[index]
            best_action = best_action_sequence[0]

        return best_action
def ProtoLoss(prototypes, x_latent, q_latent, labels_onehot, num_classes,
              num_unlabeled, num_support, num_queries):
    """
        calculates the prototype network loss using the latent representation of x
        and the latent representation of the query set
        Args:
        desired_latent: num_classes unlabeled examples to refine 
        x_latent: latent representation of supports with shape [N*S, D], where D is the latent dimension
        q_latent: latent representation of queries with shape [N*Q, D], where D is the latent dimension
        labels_onehot: one-hot encodings of the labels of the queries with shape [N, Q, N]
        num_classes: number of classes (N) for classification
        num_support: number of examples (S) in the support set
        num_queries: number of examples (Q) in the query set
        Returns:
        ce_loss: the cross entropy loss between the predicted labels and true labels
        acc: the accuracy of classification on the queries
    """

    latent_dim = x_latent.shape[-1]

    # prototypes are just centroids of each class's examples in latent space
    #x_class_split = tf.reshape(x_latent, (num_classes, num_support, latent_dim))
    #prototypes = tf.reduce_mean(x_class_split, axis=1) # (num_classes, latent_dim)

    #closest_centroids = []
    #for u in range(num_unlabeled):
    #  dists = []
    #  for p in range(num_classes):
    #      dists.append(tf.norm(desired_latent[u] - prototypes[p]))
    #   closest_centroids.append(tf.argmax(dists))
    #for u in range(num_unlabeled):
    #new_class = x_class_split[closest_centroids[u],:,:]
    #unlabeled_sample = tf.expand_dims(desired_latent[u], axis=0)
    #new_class = tf.concat([new_class, unlabeled_sample], axis = 1)
    #prototypes = prototypes[closest_centroids[u]].assign(tf.reduce_mean(new_class, axis = 1))

    # need to repeat prototypes for easy distance calculation
    query_split = tf.reshape(q_latent,
                             (num_classes, num_queries, 1, latent_dim))
    expanded = tf.expand_dims(prototypes,
                              axis=0)  # (1, num_classes, latent_dim)
    expanded = tf.expand_dims(expanded,
                              axis=0)  # (1, 1, num_classes, latent_dim)
    expanded = tf.repeat(expanded, repeats=(num_classes),
                         axis=0)  # (num_classes, 1, num_classes, latent_dim)
    expanded = tf.repeat(
        expanded, repeats=(num_queries),
        axis=1)  # (num_classes, num_queries, num_classes, latent_dim)

    # calculate distances (L2 norm), add small value for degenerate case
    dists = tf.norm(query_split - expanded, axis=3) + np.random.normal(
        1e-5, scale=1e-6)

    # use negative distance as logits for CE loss
    ce_loss = cross_entropy_loss(-1 * dists, labels_onehot)

    # predictions use argmin if distance, argmax if logits/normalized distribution
    preds = tf.argmin(dists, axis=2)
    gt = tf.argmax(labels_onehot, axis=2)
    acc = accuracy(gt, preds)

    return ce_loss, acc
Exemplo n.º 41
0
expanded_vectors = tf.expand_dims(vectors, 0)
expanded_centroids = tf.expand_dims(centroids, 1)

# 유클리드 제곱거리
# assignments를 나눠쓴 코드
# expanded_vectors,expanded_centroids에 대해 뺄셈을 한 결과(D0차원에는 중심(4개), D1차원에는 데이터의 인덱스(2000개), D2차원에는 x,y)
# v1.0 : sub -> subtract
# diff = tf.subtract(expanded_vectors, expanded_centroids)
# - diff의 제곱
# sqr = tf.square(diff)
# - 텐서의 차원을 감소시킴
# distance = tf.reduce_sum(sqr)
# - 지정된 차원에서 가장 작은 값의 인덱스를 리턴하는 argmin을 통해 각 데이터의 중심이 assignmanets에 할당됨
# assignments = tf.argmin(distance)
assignments = tf.argmin(
    tf.reduce_sum(tf.square(tf.subtract(expanded_vectors, expanded_centroids)),
                  2), 0)

# 알고리즘에서 매 반복마다 새롭게 그룹화하면서 각 그룹에 해당하는 새로운 중심을 다시 계산함
# equal 함수를 사용하여 한 군집과 매칭되는(c에 매핑함) assignments텐서의 각 원소 위치를 True로 표시하는 Boolean Tensor(Dimension(2000))를 만듬
# where 함수를 사용하여 매개변수로 받은 Boolean Tensor에서 True로 표시된 위치를 값으로 가지는 텐서(Dimension(2000)*Dimension(1))를 만듬
# reshape 함수를 사용하여 c군집에 속한 vectors텐서의 포인트들의 인덱스로 구성된 텐서(Dimension(1)*Dimension(2000))을 만듬 => 텐서의 크기를 지정하는 매개변수의 두번째 배열원소가 -1이라 바로 앞 단계에서 만든 텐서의 차원을 뒤집는 효과를 가져옴
# gather 함수를 사용하여 c군집을 이루는 점들의 좌표를 모은 텐서(Dimension(1)*Dimension(2000)*Dimension(2))를 만듬
# reduce_mean 함수를 사용하여 c군집에 속한 모든 점의 평균값을 가진 텐서(Dimension(1)*Dimension(2))를 만듬
means = tf.concat([
    tf.reduce_mean(tf.gather(
        vectors, tf.reshape(tf.where(tf.equal(assignments, c)), [1, -1])),
                   reduction_indices=[1]) for c in xrange(k)
], 0)
print means
Exemplo n.º 42
0
    def _call(self, inputs):

        ids, num_samples = inputs

        adj_lists = tf.nn.embedding_lookup(self.adj_info, ids)
        neig_num = np.int(self.adj_info.shape[1])

        #unif_rand = tf.random.uniform([np.int(neig_num/num_samples)*num_samples], minval=0, maxval=np.int(neig_num), dtype=tf.int32)
        #adj_lists = tf.gather(adj_lists, unif_rand, axis=1)

        adj_lists = tf.slice(
            adj_lists, [0, 0],
            [-1, np.int(neig_num / num_samples) * num_samples])

        vert_num = np.int(adj_lists.shape[0])
        neig_num = np.int(adj_lists.shape[1])

        # build model
        # l = W*x1
        # l = relu(l*x2^t)
        with tf.variable_scope("MLsampler"):

            v_f = tf.nn.embedding_lookup(self.features, ids)
            n_f = tf.nn.embedding_lookup(self.features,
                                         tf.reshape(adj_lists, [-1]))

            # debug
            node_dim = np.int(v_f.shape[1])

            n_f = tf.reshape(n_f, shape=[-1, neig_num, node_dim])

            if FLAGS.nonlinear_sampler == True:

                v_f = tf.tile(tf.expand_dims(v_f, axis=1), [1, neig_num, 1])

                l = tf.layers.dense(tf.concat([v_f, n_f], axis=2),
                                    1,
                                    activation=tf.nn.relu,
                                    trainable=False,
                                    reuse=self.reuse,
                                    name='dense')

                #out = tf.nn.relu(tf.exp(l), name='relu')
                out = tf.exp(l)

            else:

                l = tf.layers.dense(v_f,
                                    node_dim,
                                    activation=None,
                                    trainable=False,
                                    reuse=self.reuse,
                                    name='dense')

                l = tf.expand_dims(l, axis=1)
                l = tf.matmul(l, n_f, transpose_b=True, name='matmul')

                out = tf.nn.relu(l, name='relu')

            out = tf.squeeze(out)

            # group min
            group_dim = np.int(neig_num / num_samples)
            out = tf.reshape(out, [vert_num, num_samples, group_dim])
            idx_y = tf.argmin(out, axis=-1, output_type=tf.int32)
            #idx_y = tf.squeeze(tf.nn.top_k(-out, k=1)[1])
            delta = tf.expand_dims(tf.range(0, group_dim * num_samples,
                                            group_dim),
                                   axis=0)
            delta = tf.tile(delta, [vert_num, 1])
            idx_y = idx_y + delta

            idx_x = tf.range(vert_num)
            idx_x = tf.tile(tf.expand_dims(idx_x, -1), [1, num_samples])

            adj_lists = tf.gather_nd(
                adj_lists,
                tf.stack([tf.reshape(idx_x, [-1]),
                          tf.reshape(idx_y, [-1])],
                         axis=1))
            adj_lists = tf.reshape(adj_lists, [vert_num, num_samples])

            condition = tf.equal(adj_lists, self.adj_info.shape[0] - 1)
            case_true = tf.zeros(adj_lists.shape, tf.float32)
            case_false = tf.ones(adj_lists.shape, tf.float32)
            adj_lists_numnz = tf.reduce_sum(tf.where(condition, case_true,
                                                     case_false),
                                            axis=1)

            #out = tf.exp(out)
            #norm = tf.tile(tf.expand_dims(tf.reduce_sum(out, axis=1), -1), [1, num_samples])
            #att = tf.div(out, norm)

            #att = tf.nn.softmax(out,axis=1)
            #att_lists = tf.reshape(1+att, [vert_num, num_samples])
            att_lists = tf.ones(adj_lists.shape)
            dummy_ = adj_lists_numnz

            self.reuse = True

        #return adj_lists
        return adj_lists, att_lists, adj_lists_numnz, dummy_
Exemplo n.º 43
0
def htru2_classifier(pos_ratio = 0.5, train_ratio = 0.15, op_type = 1):
    # init feeding
    x_train = tf.placeholder(shape=[None, 8], dtype=tf.float32, name='x-train')
    x_test = tf.placeholder(shape=[8], dtype=tf.float32, name='x-test')

    # 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)
    #distance = tf.sqrt(tf.reduce_sum(tf.square(tf.add(x_train, tf.negative(x_test))), reduction_indices=1))
    # Prediction: Get min distance index (Nearest neighbor)
    pred = tf.argmin(distance, 0)

    # Initializing the variables
    init = tf.global_variables_initializer()

    # Launch the graph
    with tf.Session() as sess, open('./pythonWork/HTRU2_classifier/result.txt', 'a+') as f_write:
        sess.run(init)

        for _ in range(10):
            # init
            accuracy = 0.
            TP = 0.
            FP = 0.
            FN = 0.
            # load data
            if op_type == 1:
                f_write.write(">>>> Process Task1\n")
                f_write.write("args: train_ratio = " + str(train_ratio) + "\n")
                x_vals_train, x_vals_test, y_vals_train, y_vals_test = process_data(train_ratio)
            else:
                f_write.write(">>>> Process Task2\n")
                f_write.write("args: pos_ratio = " + str(pos_ratio) + ", train_ratio = " + str(train_ratio) + "\n")
                x_vals_train, x_vals_test, y_vals_train, y_vals_test = choice_data(pos_ratio, train_ratio) \
                if choice_data(pos_ratio, train_ratio) != -1 \
                    else f_write.write("\nError: train_ratio and pos_ratio are irrational! please reset!\n") and exit()
            # loop over test data
            for i in range(len(x_vals_test)):
                # Get nearest neighbor
                nn_index = sess.run(pred, feed_dict={x_train: x_vals_train, x_test: x_vals_test[i, :]})
                # Get nearest neighbor class label and compare it to its true label
                #print("Test", i, "Prediction:", y_vals_train[nn_index], "True Class:", y_vals_test[i])
                # Calculate accuracy
                if y_vals_train[nn_index] == y_vals_test[i]:
                    accuracy += 1.
                TP += 1. if y_vals_train[nn_index] == y_vals_test[i] and y_vals_train[nn_index] == 1 else 0.
                FP += 1. if y_vals_train[nn_index] != y_vals_test[i] and y_vals_train[nn_index] == 1 else 0.
                FN += 1. if y_vals_train[nn_index] != y_vals_test[i] and y_vals_train[nn_index] == 0 else 0.
            P = TP/(TP+FP)
            R = TP/(TP+FN)
            f_write.write(str(_) + " ->\n")
            f_write.write(" P = TP/(TP + FP):" + str(P) + "\n")
            f_write.write(" R = TP/(TP + FN):" + str(R) + "\n")
            f_write.write(" F1 = 2.*P*R/(P + R):" + str(2.*P*R/(P + R)) + "\n")
            f_write.write(" Accuracy:" + str(accuracy/len(x_vals_test)) + "\n")
            f_write.write("\n")
            print(_,"->")
            print(" P = TP/(TP + FP):", P)
            print(" R = TP/(TP + FN):",R)
            print(" F1 = 2.*P*R/(P + R):",2.*P*R/(P + R))
            print(" Accuracy:", accuracy/len(x_vals_test))
            print()

        f_write.write("\n#################################################\n")
Exemplo n.º 44
0
centroid_indices = tf.slice(random_indices, begin, size)
centroids = tf.Variable(tf.gather(vector_values, centroid_indices))

# 앞에서 정의한 텐서들인 vectors와 centroids를 처리하기 위해 두 인자값에 지정된 크기로 확장해주는 텐서플로 함수 expand_dims를 사용한다.
expanded_vectors = tf.expand_dims(vectors, 0)
expanded_centroids = tf.expand_dims(centroids, 1)

# tf.subtract 함수로 차이를 구하기 위해 두 텐서의 형태를 표준화해준다.
vectors_subtration = tf.subtract(expanded_vectors, expanded_centroids)

# 텐서의 각 차원들에 대해 개체들의 합을 계산하는 tf.reduce_sum 함수와
# vectors_subtration의 각 원소들의 제곱을 계산하는 tf.square 함수를 이용해
# 유클리디언 거리에 대한 비용 함수를 작성한다.
euclidean_distances = tf.reduce_sum(tf.square(vectors_subtration), 2)
# 할당은 텐서 간의 가장 짧은 거리를 갖는 인덱스 값으로 지정
assignments = tf.to_int32(tf.argmin(euclidean_distances, 0))

# 새로운 그룹으로 묶는다.
partitions = tf.dynamic_partition(vectors, assignments, num_clusters)
# 센트로이드를 업데이트 한다.
# 하나의 그룹에 대해 reduce_mean을 실행해 군집의 평균을 찾고 새로운 센트로이드로 지정한다.
for partition in partitions:
    update_centroids = tf.concat(
        tf.expand_dims(tf.reduce_mean(partition, 0), 0), 0)

# 테스트와 알고리즘을 평가 시작.
init_op = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init_op)

for step in range(num_steps):
Exemplo n.º 45
0
     kernel = [kernel_G1.forward(h_n),kernel_G2.forward(h_n),kernel_G3.forward(h_n),kernel_G4.forward(h_n),kernel_G5.forward(h_n)]
     out_res, hidden = adap_conv.forward(input = tf.expand_dims(h[bs], axis = 0), hidden_state = hidden_state[bs], kernel = kernel)
     h_hat.append(out_res)
     hidden_hat.append(hidden)
 h_hat = tf.concat(h_hat, axis = 0)
 hidden_state = tf.concat(hidden_hat, axis = 0)
 print 'h,', h.get_shape()
 print 'h_hat,', h_hat.get_shape()
 out = tf.unstack(h_hat, axis = -1) 
 h_pred = [tf.tanh(out[0] + prev), tf.tanh(out[1] + prev), tf.tanh(out[2] + prev), tf.tanh(out[3] + prev), tf.tanh(out[4] + prev)]
 x_pred_array = [decoder.forward(h_pred[0], skip), decoder.forward(h_pred[1], skip), decoder.forward(h_pred[2], skip), decoder.forward(h_pred[3], skip), decoder.forward(h_pred[4], skip)]
 mse_array = [tf.reduce_mean(tf.squared_difference(cur_pred, train_x[:,i]), axis = [-3,-2,-1], keepdims = True) for cur_pred in x_pred_array]
 print 'mse_array[0],', mse_array[0].get_shape()
 mse_tensor = tf.concat(axis = -1, values = mse_array) #(batch_size, 5)
 print 'mse_tensor', mse_tensor.get_shape()
 mask = tf.one_hot(indices = tf.argmin(mse_tensor, axis = -1), depth = 5, axis = -1, on_value = 1.0, off_value = 0.0) #(batch_size, 5)
 
 mask = tf.split(mask, 5, axis = -1)
 mask = tf.stop_gradient(mask)
 print 'mask', mask.get_shape()
 final_pred = tf.add_n([x_pred_array[k] * tf.tile(mask[k], [1, 64, 64, 3]) for k in range(5)])
 final_feat = tf.add_n([h_pred[k] * tf.tile(tf.squeeze(tf.squeeze(mask[k], axis = -1), axis = -1), [1, 256]) for k in range(5)])
 print 'final_pred', final_pred.get_shape()
 real_logits = discriminator.forward(tf.concat([train_x[:,i], train_x[:,i-1], train_x[:,i-2]], axis = -1))
 fake_logits = discriminator.forward(tf.concat([final_pred, train_x[:,i-1], train_x[:,i-2]], axis = -1))
 _, h_next, _ = encoder.forward(train_x[:,i])
 feat_real_logits = feat_D.forward(h_next)
 feat_fake_logits = feat_D.forward(final_feat)
 dx_loss_real += tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits = real_logits, labels = tf.ones_like(real_logits))) / (opt.n_past+opt.n_future)
 dx_loss_fake += tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits = fake_logits, labels = tf.zeros_like(fake_logits))) / (opt.n_past+opt.n_future)
 gx_loss += tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits = fake_logits, labels = tf.ones_like(fake_logits))) / (opt.n_past+opt.n_future)
Exemplo n.º 46
0
 def test_ArgMin(self):
     t = tf.argmin(self.random(3, 4, 2), 1)
     self.check(t)
Exemplo n.º 47
0
    else:
        vectors_set.append(
            [np.random.normal(3.0, 0.5),
             np.random.normal(1.0, 0.5)])

vectors = np.array(vectors_set)

k = 4

evec = [[v for i in range(k)] for v in vectors]
evec = np.array(evec)

centroides = tf.Variable(tf.slice(tf.random_shuffle(vectors), [0, 0], [k, -1]))

assignments = tf.argmin(tf.reduce_sum(tf.square(tf.subtract(evec, centroides)),
                                      axis=2),
                        axis=1)

means = tf.concat([
    tf.reduce_mean(tf.gather(
        vectors, tf.reshape(tf.where(tf.equal(assignments, i)), [1, -1])),
                   reduction_indices=[1]) for i in range(k)
],
                  axis=0)

update_centroids = tf.assign(centroides, means)
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for step in range(20):
        _, nvec = sess.run([update_centroids, assignments])
        data = {"x": [], "y": [], "cluster": []}
def stepll_adversarial_images(x, eps):
    _, logits, end_points = bulid_Net(x)
    least_likely_class = tf.argmin(logits, 1)
    one_hot_ll_class = tf.one_hot(least_likely_class, 10)
    return step_target_class_adversarial_images(x, eps, one_hot_ll_class)
Exemplo n.º 49
0
# 样本特征   20 * 10

# print('')

trainDataInput = tf.placeholder(shape=[None, 20, 10], dtype=tf.float32)
testDataInput = tf.placeholder(shape=[20, 10], dtype=tf.float32)

x_train = tf.reshape(trainDataInput, [-1, 200])
x_test = tf.reshape(testDataInput, [200])

distance = tf.reduce_sum(tf.abs(tf.add(tf.negative(x_test), x_train)),
                         reduction_indices=1)

# predict = tf.nn.top_k(tf.negative(distance), k)
predict = tf.argmin(distance, axis=0)

# accuracy = 0.  # 准确率


class KnnCaptcha():
    def __init__(self):
        print('captcha_knn_init')
        init = tf.global_variables_initializer()
        sess = tf.Session()
        sess.run(init)
        self.sess = sess
        self.trainDataInput = trainDataInput
        self.y_train = y_train

    def __del__(self):
Exemplo n.º 50
0
    def build_graph(self, x, bboxes_xyz, bboxes_lwh, semantic_labels):
        # def build_graph(self, x, bboxes_xyz, bboxes_lwh, semantic_labels, heading_labels, heading_residuals, size_labels, size_residuals):
        l0_xyz = x
        l0_points = None

        # Set Abstraction layers
        l1_xyz, l1_points, l1_indices = pointnet_sa_module(l0_xyz,
                                                           l0_points,
                                                           npoint=2048,
                                                           radius=0.2,
                                                           nsample=64,
                                                           mlp=[64, 64, 128],
                                                           mlp2=None,
                                                           group_all=False,
                                                           scope='sa1')
        l2_xyz, l2_points, l2_indices = pointnet_sa_module(l1_xyz,
                                                           l1_points,
                                                           npoint=1024,
                                                           radius=0.4,
                                                           nsample=64,
                                                           mlp=[128, 128, 256],
                                                           mlp2=None,
                                                           group_all=False,
                                                           scope='sa2')
        l3_xyz, l3_points, l3_indices = pointnet_sa_module(l2_xyz,
                                                           l2_points,
                                                           npoint=512,
                                                           radius=0.8,
                                                           nsample=64,
                                                           mlp=[128, 128, 256],
                                                           mlp2=None,
                                                           group_all=False,
                                                           scope='sa3')
        l4_xyz, l4_points, l4_indices = pointnet_sa_module(l3_xyz,
                                                           l3_points,
                                                           npoint=256,
                                                           radius=1.2,
                                                           nsample=64,
                                                           mlp=[128, 128, 256],
                                                           mlp2=None,
                                                           group_all=False,
                                                           scope='sa4')
        # Feature Propagation layers
        l3_points = pointnet_fp_module(l3_xyz,
                                       l4_xyz,
                                       l3_points,
                                       l4_points, [256, 256],
                                       scope='fp1')
        seeds_points = pointnet_fp_module(l2_xyz,
                                          l3_xyz,
                                          l2_points,
                                          l3_points, [256, 256],
                                          scope='fp2')
        seeds_xyz = l2_xyz

        # Voting Module layers
        offset = self.hough_voting_mlp(seeds_points)

        votes_xyz_points = tf.concat([seeds_xyz, seeds_points], 2) + offset
        votes_xyz, votes_points = tf.slice(votes_xyz_points, (0, 0, 0), (-1, -1, 3)), \
            tf.slice(votes_xyz_points, (0, 0, 3), (-1, -1, -1))

        vote_reg_loss = self.vote_reg_loss(seeds_xyz, votes_xyz, bboxes_xyz,
                                           bboxes_lwh)

        # Proposal Module layers
        # Farthest point sampling on seeds
        proposals_xyz, proposals_output, _ = pointnet_sa_module(
            votes_xyz,
            votes_points,
            npoint=config.PROPOSAL_NUM,
            radius=0.3,
            nsample=64,
            mlp=[128, 128, 128],
            mlp2=[128, 128, 5 + 2 * config.NH + 4 * config.NS + config.NC],
            group_all=False,
            scope='proposal')

        object_scores_pred, center_scores_pred, sementic_classes_pred = self.parse_outputs_to_tensor(
            proposals_output)

        nms_iou = tf.get_variable('nms_iou',
                                  shape=[],
                                  initializer=tf.constant_initializer(0.25),
                                  trainable=False)
        if not get_current_tower_context().is_training:

            def get_3d_bbox(box_size, center):
                batch_size = tf.shape(box_size)[0]
                l, w, h = box_size[..., 0], box_size[..., 1], box_size[
                    ..., 2]  # lwh(xzy) order!!!
                corners = tf.reshape(
                    tf.stack([
                        l / 2, l / 2, -l / 2, -l / 2, l / 2, l / 2, -l / 2,
                        -l / 2, h / 2, h / 2, h / 2, h / 2, -h / 2, -h / 2,
                        -h / 2, -h / 2, w / 2, -w / 2, -w / 2, w / 2, w / 2,
                        -w / 2, -w / 2, w / 2
                    ], -1), tf.stack([batch_size, -1, 3, 8]))
                return corners + tf.expand_dims(center, 2)  # B * N * 8 * 3

            center_pred = proposals_xyz + center_scores_pred  # (B, proposal_num, 3)

            # with tf.control_dependencies([tf.print(size_residual_pred[0, :10, :]), tf.print(size_pred[0, :10, :])]):
            bboxes = get_3d_bbox(
                size_pred, center_pred)  # B * N * 8 * 3,  lhw(xyz) order!!!

            # bbox_corners = tf.concat([bboxes[:, :, 6, :], bboxes[:, :, 0, :]], axis=-1)  # B * N * 6,  lhw(xyz) order!!!
            # with tf.control_dependencies([tf.print(bboxes[0, 0])]):
            nms_idx = NMS3D(bboxes,
                            tf.reduce_max(proposals_output[..., -config.NC:],
                                          axis=-1), proposals_output[..., :2],
                            nms_iou)  # Nnms * 2

            bboxes_pred = tf.gather_nd(bboxes, nms_idx,
                                       name='bboxes_pred')  # Nnms * 8 * 3
            class_scores_pred = tf.gather_nd(
                proposals_output[..., -config.NC:],
                nms_idx,
                name='class_scores_pred')  # Nnms * C
            batch_idx = tf.identity(
                nms_idx[:, 0], name='batch_idx'
            )  # Nnms, this is used to identify between batches

            return

        dist_mat = tf.norm(tf.expand_dims(proposals_xyz, axis=[2]) -
                           tf.expand_dims(bboxes_xyz, axis=[1]),
                           axis=-1)
        min_dist = tf.reduce_min(dist_mat, axis=-1)
        bboxes_assignment = tf.argmin(dist_mat, axis=-1)  # (B, N'), e:0~K

        thres_mid = tf.reduce_mean(min_dist, axis=-1, keepdims=True)
        thres_min = tf.reduce_min(min_dist, axis=-1, keepdims=True)
        thres_max = tf.reduce_max(min_dist, axis=-1, keepdims=True)
        POSITIVE_THRES, NEGATIVE_THRES = (thres_mid + thres_min) / 2.0, (
            thres_mid + thres_max) / 2.0

        positive_pro_idx = tf.where(min_dist < POSITIVE_THRES)
        negative_pro_idx = tf.where(min_dist > NEGATIVE_THRES)

        positive_gt_idx = tf.stack([
            positive_pro_idx[:, 0],
            tf.gather_nd(bboxes_assignment, positive_pro_idx)
        ],
                                   axis=1)

        # objectiveness loss
        pos_obj_cls_score = tf.gather_nd(object_scores_pred, positive_pro_idx)
        pos_obj_cls_gt = tf.ones([tf.shape(positive_pro_idx)[0]],
                                 dtype=tf.int32)
        neg_obj_cls_score = tf.gather_nd(object_scores_pred, negative_pro_idx)
        neg_obj_cls_gt = tf.zeros([tf.shape(negative_pro_idx)[0]],
                                  dtype=tf.int32)
        pos_obj_cls_loss = tf.reduce_mean(
            tf.nn.sparse_softmax_cross_entropy_with_logits(
                logits=pos_obj_cls_score, labels=pos_obj_cls_gt))
        neg_obj_cls_loss = tf.reduce_mean(
            tf.nn.sparse_softmax_cross_entropy_with_logits(
                logits=neg_obj_cls_score, labels=neg_obj_cls_gt))
        obj_cls_loss = tf.identity((pos_obj_cls_loss + neg_obj_cls_loss) / 2.0,
                                   name='obj_cls_loss')
        obj_correct = tf.concat([
            tf.cast(tf.nn.in_top_k(pos_obj_cls_score, pos_obj_cls_gt, 1),
                    tf.float32),
            tf.cast(tf.nn.in_top_k(neg_obj_cls_score, neg_obj_cls_gt, 1),
                    tf.float32)
        ],
                                axis=0,
                                name='obj_correct')
        obj_accuracy = tf.reduce_mean(obj_correct, name='obj_accuracy')

        # center regression losses
        # positive_gt_idx: (B,N',2)
        center_gt = tf.gather_nd(bboxes_xyz, positive_gt_idx)
        center_pred = tf.gather_nd(proposals_xyz + center_scores_pred,
                                   positive_gt_idx)
        center_loss_left = tf.reduce_mean(tf.reduce_sum(tf.losses.huber_loss(
            labels=center_gt,
            predictions=center_pred,
            reduction=tf.losses.Reduction.NONE),
                                                        axis=-1),
                                          name='center_left_loss')

        bboxes_assignment_dual = tf.argmin(dist_mat, axis=1)
        proposals_to_boxes_idx = tf.stack([
            tf.tile(
                tf.expand_dims(
                    tf.range(
                        tf.shape(bboxes_assignment_dual,
                                 out_type=tf.int64)[0]), -1),
                [1, tf.shape(bboxes_assignment_dual)[1]]),
            bboxes_assignment_dual
        ], 2)  # B * BB' * 2

        nearest_proposals_assigned2box = tf.gather_nd(
            proposals_xyz + center_scores_pred, proposals_to_boxes_idx)
        center_loss_right = tf.reduce_mean(tf.reduce_sum(tf.losses.huber_loss(
            labels=bboxes_xyz,
            predictions=nearest_proposals_assigned2box,
            reduction=tf.losses.Reduction.NONE),
                                                         axis=-1),
                                           name='center_right_loss')
        center_loss = tf.identity(center_loss_left + center_loss_right,
                                  name='center_loss')

        box_loss = tf.identity(center_loss, name='box_loss')

        # semantic loss
        sem_cls_score = tf.gather_nd(sementic_classes_pred, positive_pro_idx)
        sem_cls_gt = tf.gather_nd(semantic_labels, positive_gt_idx)
        sem_cls_loss = tf.reduce_mean(
            tf.nn.sparse_softmax_cross_entropy_with_logits(
                logits=sem_cls_score, labels=sem_cls_gt, name='sem_cls_loss'))

        sem_correct = tf.cast(tf.nn.in_top_k(sem_cls_score, sem_cls_gt, 1),
                              tf.float32,
                              name='sem_correct')
        sem_accuracy = tf.reduce_mean(sem_correct, name='sem_accuracy')

        wd_cost = tf.multiply(1e-5,
                              regularize_cost('.*/W', tf.nn.l2_loss),
                              name='regularize_loss')

        total_cost = vote_reg_loss + 0.5 * obj_cls_loss + 1. * box_loss + 0.1 * sem_cls_loss
        total_cost = tf.identity(total_cost, name='total_cost')
        summary.add_moving_summary(total_cost,
                                   vote_reg_loss,
                                   obj_cls_loss,
                                   box_loss,
                                   center_loss,
                                   center_loss_left,
                                   center_loss_right,
                                   sem_cls_loss,
                                   wd_cost,
                                   obj_accuracy,
                                   sem_accuracy,
                                   decay=0)
        # monitor histogram of all weight (of conv and fc layers) in tensorboard
        summary.add_param_summary(('.*/W', ['histogram', 'rms']))
        # the function should return the total cost to be optimized
        return total_cost
def kmeans(epochs, clusters, step, data, val_data):
    D = data.shape[1]
    N = data.shape[0]
    K = clusters

    #Initializing Placeholders & Variables
    centers = tf.get_variable(
        name='MU',
        dtype=tf.float64,
        shape=(K, D),
        initializer=tf.initializers.random_normal(seed=1))
    inputs = tf.placeholder(name='inputs', dtype=tf.float64, shape=(None, D))

    training_losses = []
    valid_losses = []
    loss = tf.reduce_sum(tf.reduce_min(distanceFunc(inputs, centers), axis=1))
    optimizer = tf.train.AdamOptimizer(learning_rate=step,
                                       beta1=0.9,
                                       beta2=0.99,
                                       epsilon=1e-5).minimize(loss)
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        for epoch in range(epochs):
            #Why do we feed in the same data twice?

            training_loss = sess.run(loss, feed_dict={inputs: data})
            valid_loss = sess.run(loss, feed_dict={inputs: val_data})
            sess.run(optimizer, feed_dict={inputs: data})
            training_losses.append(training_loss)
            valid_losses.append(valid_loss)
            print('Training loss = {} | Validation loss = {}'.format(
                training_loss, valid_loss))
        centroids = sess.run(centers)

        plt.plot(training_losses)
        plt.xlabel("Number Of Iterations")
        plt.ylabel("Loss")
        plt.title("Training Loss, K={}".format(K))
        plt.savefig("Loss{}Clusters".format(K))
        plt.show()

        #print(type(centroids))
        #print(centroids.shape)
        #print(data.shape)

        dist = distanceFunc(data, centroids)
        cluster_class = tf.argmin(dist, axis=1)
        cluster_class = cluster_class.eval()

        clusterID, count = np.unique(cluster_class, return_counts=True)
        counts = dict(zip(clusterID, count))

        print(clusterID, count)

        for i in range(K):
            p = counts[i] / N * 100
            print("Percentage of points in cluster {}: {}%".format(i, p))

        #print(cluster_class.shape)
        #print(cluster_class)

        datanp = data

        x = datanp[:, 0]
        y = datanp[:, 1]
        centroids_x = centroids[:, 0]
        centroids_y = centroids[:, 1]

        plt.scatter(x,
                    y,
                    c=cluster_class,
                    label='Data Points',
                    s=25,
                    alpha=0.8,
                    cmap='Dark2')

        mark = ['o', '+', 'h', 'd', '*', 'P']
        for i in range(centroids_x.shape[0]):
            plt.scatter(centroids_x[i],
                        centroids_y[i],
                        marker=mark[i],
                        label='Cluster {} Mean'.format(i),
                        s=100,
                        c='k')

        #plt.scatter(centroids_x, centroids_y, marker='x', label='Cluster Means', c='k', s=100)
        plt.legend()
        plt.title("K-Means_{}Clusters".format(K))
        plt.savefig("K-Means_{}Clusters".format(K))
        plt.show()
Exemplo n.º 52
0
                cmap=plt.cm.coolwarm)
    plt.show()

points = tf.Variable(data)
cluster_assignments = tf.Variable(tf.zeros([N], dtype=tf.int64))
centroids = tf.Variable(tf.slice(points.initialized_value(), [0, 0], [K, 2]))

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

rep_centroids = tf.reshape(tf.tile(centroids, [N, 1]), [N, K, 2])
rep_points = tf.reshape(tf.tile(points, [1, K]), [N, K, 2])
sum_squares = tf.reduce_sum(tf.square(rep_points - rep_centroids), 
reduction_indices=2)

best_centroids = tf.argmin(sum_squares, 1)

did_assignments_change = tf.reduce_any(tf.not_equal(best_centroids, cluster_assignments))


def bucket_mean(data, bucket_ids, num_buckets):
    total = tf.unsorted_segment_sum(data, bucket_ids, num_buckets)
    count = tf.unsorted_segment_sum(tf.ones_like(data), bucket_ids, num_buckets)
    return total / count


means = bucket_mean(points, best_centroids, K)


with tf.control_dependencies([did_assignments_change]):
    do_updates = tf.group(
Exemplo n.º 53
0
    [iris.data[np.random.choice(len(iris.data))] for _ in range(k)])

centroids = tf.Variable(rand_starts)

# In order to calculate the distance between every data point and every centroid, we
#  repeat the centroids into a (num_points) by k matrix.
centroid_matrix = tf.reshape(tf.tile(centroids, [num_pts, 1]),
                             [num_pts, k, num_feats])
# Then we reshape the data points into k (3) repeats
point_matrix = tf.reshape(tf.tile(data_points, [1, k]),
                          [num_pts, k, num_feats])
distances = tf.reduce_sum(tf.square(point_matrix - centroid_matrix),
                          reduction_indices=2)

#Find the group it belongs to with tf.argmin()
centroid_group = tf.argmin(distances, 1)


# Find the group average
def data_group_avg(group_ids, data):
    # Sum each group
    sum_total = tf.unsorted_segment_sum(data, group_ids, 3)
    # Count each group
    num_total = tf.unsorted_segment_sum(tf.ones_like(data), group_ids, 3)
    # Calculate average
    avg_by_group = sum_total / num_total
    return (avg_by_group)


means = data_group_avg(centroid_group, data_points)
Exemplo n.º 54
0
    def VQVAE_layer(self, inputs):
        # Assert last dimension is same as self._embedding_dim

        input_shape = tf.shape(inputs)
        with tf.control_dependencies([
                tf.Assert(tf.equal(input_shape[-1], self._embedding_dim),
                          [input_shape])
        ]):
            flat_inputs = tf.reshape(
                inputs,
                [-1, input_shape[1] * input_shape[2], self._embedding_dim])

        self.variable_def()  # set all variable

        # the _w is already qunatized: for each row, each idx(latent variable digit) have its own value to pass, value pf _w is quantized embd ouput

        def dist_fn(tensor_apart):
            dist = (tf.reduce_sum(tensor_apart**2, 1, keepdims=True) -
                    2 * tf.matmul(tensor_apart, self._w) +
                    tf.reduce_sum(self._w**2, 0, keepdims=True)
                    )  # different shape: tf.add broadcast
            return dist

        distances = tf.map_fn(dist_fn, flat_inputs)

        # print("distances:", distances)
        # distance.shape = [b,H*W,num_embeddings]
        encoding_indices = tf.argmin(
            distances, 2
        )  # (batchsize,(1=>index)), find the _w which ressemble the inpus the most
        # print("encoding_indices:", encoding_indices)# (b,h*w)
        encodings = tf.one_hot(encoding_indices, self._num_embeddings)

        self.loop_assign_moving_avg(encodings, flat_inputs)

        # self._w = self._w * (self.gamma) + (1 - self.gamma) * ()

        # encodings = tf.reshape(encodings, [-1, input_shape[1], input_shape[2], self._num_embeddings])
        # print("encodings(after):", encodings)
        encoding_indices = tf.reshape(encoding_indices, tf.shape(inputs)[:-1])
        # print("encoding_indices(after):", encoding_indices)

        quantized_embd_out = self.quantize(
            encoding_indices
        )  # Actually, this quantized method find the value from corespond econding_idx from w
        # print("quantized_embd_out:", quantized_embd_out)
        # print("inputs:", inputs)

        e_latent_loss = tf.reduce_mean((tf.stop_gradient(quantized_embd_out) -
                                        inputs)**2)  # embedding loss
        # q_latent_loss = tf.reduce_mean((tf.stop_gradient(inputs) - quantized_embd_out) ** 2)

        VQ_loss = self.commit_loss_coef * e_latent_loss

        quantized_embd_out = inputs + tf.stop_gradient(
            quantized_embd_out -
            inputs)  # in order to pass value to decoder???

        # print("quantized_embd_out(after):", quantized_embd_out)

        avg_probs = tf.reduce_mean(encodings, 0)

        perplexity = tf.exp(-tf.reduce_sum(avg_probs *
                                           tf.log(avg_probs + 1e-10)))

        # encodings_holder = tf.placeholder(tf.float32,
        #                                   [None, input_shape[1] * input_shape[2], self._num_embeddings])
        # flat_inputs_holder = tf.placeholder(tf.float32,
        #                                     [None, input_shape[1] * input_shape[2], self._embedding_dim])

        # assign_moving_avg_op = self.loop_assign_moving_avg(encodings_holder, flat_inputs_holder)
        assign_moving_avg_op = self.loop_assign_moving_avg(
            encodings, flat_inputs)

        return {
            'quantized_embd_out': quantized_embd_out,
            'VQ_loss': VQ_loss,
            'perplexity': perplexity,
            'encodings': encodings,
            'encoding_indices': encoding_indices,
            'assign_moving_avg_op': assign_moving_avg_op
        }
Exemplo n.º 55
0
 def get_least_likely_prediction(self, x):
     probs = self.get_probs(x)
     preds = tf.argmin(probs, axis=1)
     return preds
Exemplo n.º 56
0
shape = lambda x, as_tensor=False: _tf.shape(x) if as_tensor else tuple(x.shape
                                                                        )
get_num_dims = lambda x, as_tensor=False: _tf.shape(_tf.shape(x))[
    0] if as_tensor else int(_tf.shape(_tf.shape(x)))
minimum = _tf.minimum
maximum = _tf.maximum
clip = _tf.clip_by_value
# noinspection PyShadowingBuiltins
round = _tf.round
floormod = lambda x, y: x % y
floor = _tf.floor
ceil = _tf.math.ceil
# noinspection PyShadowingBuiltins
abs = _tf.abs
argmax = lambda x, axis=None: _tf.reshape(_tf.argmax(x, axis), get_num_dims(x))
argmin = lambda x, axis=None: _tf.reshape(_tf.argmin(x, axis), get_num_dims(x))


def cast(x, dtype_str):
    dtype_val = _tf.__dict__[dtype_str]
    return _tf.cast(x, dtype_val)


# noinspection PyShadowingNames
def arange(stop, start=0, step=1, dtype_str=None, dev_str=None):
    dtype = _tf.__dict__[dtype_str] if dtype_str else dtype_str
    if dev_str:
        with _tf.device('/' + dev_str.upper()):
            return _tf.range(start, stop, delta=step, dtype=dtype)
    else:
        return _tf.range(start, stop, delta=step, dtype=dtype)
Exemplo n.º 57
0
    def _fit(self, X):
        """Learn cluster centroids from training data.

        Called in self.fit

        """
        self.iterations_ = 0
        n_samples = X.shape[0]
        n_features = X.shape[1]

        # initialize centroids
        idx = np.random.choice(n_samples, self.k, replace=False)
        self.centroids_ = X[idx]

        self.g_train = tf.Graph()

        with self.g_train.as_default():
            tf_X = tf.placeholder(shape=(n_samples, n_features),
                                  dtype=self.dtype,
                                  name='X_data')
            tf_centroids = tf.placeholder(shape=(self.k, n_features),
                                          dtype=self.dtype,
                                          name='centroids')

            distance_matrices = []
            for idx in range(self.k):
                centroid = tf.slice(tf_centroids,
                                    begin=[idx, 0],
                                    size=[1, n_features])
                euclid_dist = tf.sqrt(
                    tf.reduce_sum(tf.pow(tf.sub(tf_X, centroid), 2),
                                  reduction_indices=1))
                distance_matrices.append(euclid_dist)
            distance_matrices = tf.reshape(tf.concat(0, distance_matrices),
                                           shape=(self.k, n_samples))

            assignments = tf.argmin(distance_matrices, 0)

            centroids = []
            for clust in range(self.k):
                indices = tf.reshape(tf.where(tf.equal(assignments, clust)),
                                     shape=(1, -1))
                cluster_samples = tf.gather(params=tf_X, indices=indices)
                centroid = tf.reduce_mean(cluster_samples,
                                          reduction_indices=[1])
                centroids.append(centroid)
            centroids = tf.concat(0, centroids)

            train_init = tf.initialize_all_variables()

        tf.reset_default_graph()

        sess = tf.Session(graph=self.g_train)
        with sess:
            sess.run(train_init)
            self.init_time_ = time()
            for _ in range(self.max_iter):

                idx, new_centroids = sess.run([assignments, centroids],
                                              feed_dict={
                                                  tf_X: X,
                                                  tf_centroids: self.centroids_
                                              })

                if (self.centroids_ == new_centroids).all():
                    break
                else:
                    self.centroids_ = new_centroids

                self.iterations_ += 1

                if self.print_progress:
                    self._print_progress(iteration=self.iterations_,
                                         n_iter=self.max_iter)

            self.clusters_ = {
                i: np.arange(n_samples)[idx == i]
                for i in range(self.k)
            }

        return self
Exemplo n.º 58
0
    def __init__(self, config):
        ### Initialize setting
        print("initializing")
        np.set_printoptions(precision=4)
        self.stage = config['stage']
        self.device = config['device']
        self.output_dim = config['output_dim']
        self.n_class = config['label_dim']

        self.subspace_num = config['n_subspace']
        self.subcenter_num = config['n_subcenter']
        self.code_batch_size = config['code_batch_size']
        self.cq_lambda = config['cq_lambda']
        self.max_iter_update_Cb = config['max_iter_update_Cb']
        self.max_iter_update_b = config['max_iter_update_b']
        #self.centers_device = config['centers_device']

        self.batch_size = config['batch_size']
        self.max_iter = config['max_iter']
        self.img_model = config['img_model']
        self.loss_type = config['loss_type']
        self.console_log = (config['console_log'] == 1)
        self.learning_rate = config['learning_rate']
        self.learning_rate_decay_factor = config['learning_rate_decay_factor']
        self.decay_step = config['decay_step']

        self.margin_param = config['margin_param']
        self.wordvec_dict = config['wordvec_dict']
        self.part_ids_dict = config['part_ids_dict']
        self.partlabel = config['partlabel']
        ### Format as 'path/to/save/dir/lr_{$0}_output_dim{$1}_iter_{$2}'
        self.save_dir = config['save_dir'] + self.loss_type + '_lr_' + str(
            self.learning_rate) + '_cqlambda_' + str(
                self.cq_lambda) + '_subspace_' + str(
                    self.subspace_num) + '_margin_' + str(
                        self.margin_param) + '_partlabel_' + str(
                            self.partlabel) + '_iter_' + str(
                                self.max_iter) + '_' + str(
                                    self.output_dim) + '_.npz'

        ### Setup session
        print("launching session")
        configProto = tf.ConfigProto()
        configProto.gpu_options.allow_growth = True
        configProto.allow_soft_placement = True
        self.sess = tf.Session(config=configProto)
        self.model_weights = config['model_weights']

        ### Create variables and placeholders

        with tf.device(self.device):
            self.img = tf.placeholder(tf.float32,
                                      [self.batch_size, 256, 256, 3])
            self.img_label = tf.placeholder(tf.float32,
                                            [self.batch_size, self.n_class])

            self.img_last_layer, self.img_output, self.C = \
                self.load_model(self.model_weights)

            ### Centers shared in different modalities (image & text)
            ### Binary codes for different modalities (image & text)
            self.img_output_all = tf.placeholder(tf.float32,
                                                 [None, self.output_dim])
            self.img_b_all = tf.placeholder(
                tf.float32, [None, self.subspace_num * self.subcenter_num])

            self.b_img = tf.placeholder(
                tf.float32, [None, self.subspace_num * self.subcenter_num])
            self.ICM_m = tf.placeholder(tf.int32, [])
            self.ICM_b_m = tf.placeholder(tf.float32,
                                          [None, self.subcenter_num])
            self.ICM_b_all = tf.placeholder(
                tf.float32, [None, self.subcenter_num * self.subspace_num])
            self.ICM_X = tf.placeholder(
                tf.float32, [self.code_batch_size, self.output_dim])
            self.ICM_C_m = tf.slice(self.C,
                                    [self.ICM_m * self.subcenter_num, 0],
                                    [self.subcenter_num, self.output_dim])
            self.ICM_X_residual = tf.add(
                tf.sub(self.ICM_X, tf.matmul(self.ICM_b_all, self.C)),
                tf.matmul(self.ICM_b_m, self.ICM_C_m))
            ICM_X_expand = tf.expand_dims(self.ICM_X_residual, 1)
            ICM_C_m_expand = tf.expand_dims(self.ICM_C_m, 0)
            # N*sc*D  *  D*n
            word_dict = tf.constant(np.loadtxt(self.wordvec_dict),
                                    dtype=tf.float32)
            ICM_word_dict = tf.reshape(
                tf.matmul(
                    tf.reshape(tf.sub(ICM_X_expand, ICM_C_m_expand), [
                        self.code_batch_size * self.subcenter_num,
                        self.output_dim
                    ]), tf.transpose(word_dict)),
                [self.code_batch_size, self.subcenter_num, self.n_class])
            ICM_sum_squares = tf.reduce_sum(tf.square(ICM_word_dict),
                                            reduction_indices=2)
            ICM_best_centers = tf.argmin(ICM_sum_squares, 1)
            self.ICM_best_centers_one_hot = tf.one_hot(ICM_best_centers,
                                                       self.subcenter_num,
                                                       dtype=tf.float32)

            self.global_step = tf.Variable(0, trainable=False)
            self.train_op = self.apply_loss_function(self.global_step)
            self.sess.run(tf.initialize_all_variables())
        return
Exemplo n.º 59
0
# [kx1, ky1, kz1]
# [kx2, ky2, kz2]
# [kx3, ky3, kz3]
# [kx4, ky4, kz4]
#  ...  ...  ...
tile_centroids = tf.tile(centroids, [NUM_SAMPLES, 1])
tile_centroids = tf.reshape(tile_centroids, [NUM_SAMPLES * NUM_CLUSTERS, DIM])

# Multiply element-wise and sum the rows
tile_sum = tf.reduce_sum(tf.square(tile_samples - tile_centroids), 1)

# Reshape to align each cluster calcualtion
reshape_sum = tf.reshape(tile_sum, [NUM_SAMPLES, NUM_CLUSTERS])

# Determine new cluster assignments
cluster_assignments = tf.argmin(reshape_sum, axis=1)

# Sum datapoints based on cluster assignments
# Then, divide by count to calculate new centroids
# Source: https://www.tensorflow.org/versions/r0.12/api_docs/python/math_ops/segmentation
new_centroids = tf.unsorted_segment_sum(samples, cluster_assignments, NUM_CLUSTERS)
count_cluster_assignments = tf.unsorted_segment_sum(tf.ones([NUM_SAMPLES, DIM]), cluster_assignments, NUM_CLUSTERS)
new_centroids = new_centroids / count_cluster_assignments

# Assign the new centroids
assign_new_centroids = tf.assign(centroids, new_centroids)

# Run the clustring algorithm 
init = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init)
Exemplo n.º 60
0
import numpy as np
import tensorflow as tf

from tensorflow.examples.tutorials.mnist import input_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_digits_pl = tf.placeholder("float", [784])

l1_distance = tf.abs(tf.add(training_digits_pl, tf.negative(test_digits_pl)))
distance = tf.reduce_sum(l1_distance, axis = 1)
pred = tf.argmin(distance, 0)

accuracy = 0.0

init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    
    for i in range(len(test_digits)):
        nn_index = sess.run(pred, feed_dict = {training_digits_pl: training_digits, test_digits_pl: test_digits[i, :]})
        
        print("test", i, "prediction :", np.argmax(training_labels[nn_index]), "true label :", np.argmax(test_labels[i]))
        
        if np.argmax(training_labels[nn_index]) == np.argmax(test_labels[i]):
            accuracy += 1.0/len(test_digits)