Beispiel #1
0
    def optimize_S(self, W, F):
        Q = distance_matrix(F, metric='euclidean')
        P = (2 * W - self.gamma * Q) / (2 + 2 * self.alpha)

        S = np.zeros((self.num_nodes, self.num_nodes))
        for index in range(S.shape[0]):
            S[index] = best_simplex_projection(P[index])

        return S
Beispiel #2
0
    def __init__(self,
                 d_components=2,
                 initial_dims=30,
                 X_train=None,
                 random_state=0,
                 y_train=None,
                 c=0.5):

        self.d_components = d_components  # dimension of projection space
        self.initial_dims = initial_dims  # initial dimensions of data, before applying t-sne
        self.X_train = X_train
        self.y_train = y_train
        self.c = c
        D = distance_matrix(self.X_train)
        self.sigma = determine_sigma(D, self.c)
        self.random_state = random_state
        self.trained = False
Beispiel #3
0
    def __init__(self, mat, r=10, leaf_side=16):
        LinearOperator.__init__(self, dtype=mat.dtype, shape=mat.shape,
                                matvec=self._matvec,
                                rmatvec=self._rmatvec)
        self.mat = BlackBox(mat)
        self.r = r
        self.leaf_side = leaf_side
        self.leaf_size = leaf_side**2

        N = int(np.sqrt(self.mat.shape[0]))

        self.pattern = distance_matrix(N, format='coo')
        perm = hilbert_traverse(N)
        conjugate_sparse(self.pattern, perm)
        self.pattern = self.pattern.tocsr()
        self.mat.permutate(perm)
        self.root = hmat_node(self, tuple(zip((0, 0), self.mat.shape)))
        return
Beispiel #4
0
    def __init__(self, mat, r=10, leaf_side=16):
        LinearOperator.__init__(self,
                                dtype=mat.dtype,
                                shape=mat.shape,
                                matvec=self._matvec,
                                rmatvec=self._rmatvec)
        self.mat = BlackBox(mat)
        self.r = r
        self.leaf_side = leaf_side
        self.leaf_size = leaf_side**2

        N = int(np.sqrt(self.mat.shape[0]))

        self.pattern = distance_matrix(N, format='coo')
        perm = hilbert_traverse(N)
        conjugate_sparse(self.pattern, perm)
        self.pattern = self.pattern.tocsr()
        self.mat.permutate(perm)
        self.root = hmat_node(self, tuple(zip((0, 0), self.mat.shape)))
        return
Beispiel #5
0
def build_graph(config):
    batch_size = config['training']['batch_size']

    attr_query_ids = tf.placeholder(tf.int32,
                                    shape=(batch_size, ),
                                    name='attr_query_ids')
    correct_value_ids = tf.placeholder(tf.int32,
                                       shape=(batch_size, ),
                                       name='correct_value_ids')
    incorrect_value_ids = tf.placeholder(tf.int32,
                                         shape=(batch_size, ),
                                         name='incorrect_value_ids')

    # Embedding matrices.
    attr_embeddings = tf.get_variable(
        'attr_embeddings',
        dtype=tf.float32,
        shape=(config['data']['attr_vocab_size'],
               config['model']['embedding_size']),
        initializer=tf.random_uniform_initializer(-1.0 / 200, 1.0 / 200))
    value_embeddings = tf.get_variable(
        'value_embeddings',
        dtype=tf.float32,
        shape=(config['data']['value_vocab_size'],
               config['model']['embedding_size']),
        initializer=tf.random_uniform_initializer(-1.0 / 200, 1.0 / 200))

    # Used by model / loss function.
    attr_queries = tf.nn.embedding_lookup(attr_embeddings, attr_query_ids)
    correct_values = tf.nn.embedding_lookup(value_embeddings,
                                            correct_value_ids)
    incorrect_values = tf.nn.embedding_lookup(value_embeddings,
                                              incorrect_value_ids)

    distance_metric = config['model']['distance_metric']
    s = utils.distance(attr_queries, correct_values, distance_metric)
    s_prime = utils.distance(attr_queries, incorrect_values, distance_metric)
    loss = tf.maximum(0.0, 1 + s - s_prime)
    loss = tf.reduce_mean(loss)
    tf.losses.add_loss(loss)

    # Summaries
    unk = tf.equal(correct_value_ids, config['data']['value_vocab_size'] - 1)
    obs = tf.logical_not(unk)

    mean_loss, _ = tf.metrics.mean(loss,
                                   metrics_collections=['metrics'],
                                   updates_collections=['updates'],
                                   name='streaming_loss')
    tf.summary.scalar('loss', mean_loss)

    scores = utils.distance_matrix(attr_queries, value_embeddings,
                                   distance_metric)
    rank = utils.rank(scores, correct_value_ids)

    mrr, _ = tf.metrics.mean(1.0 / rank,
                             metrics_collections=['metrics'],
                             updates_collections=['updates'],
                             name='streaming_mrr')
    mrr_obs, _ = tf.metrics.mean(1.0 / tf.boolean_mask(rank, obs),
                                 metrics_collections=['metrics'],
                                 updates_collections=['updates'],
                                 name='streaming_mrr_obs')
    mrr_unk, _ = tf.metrics.mean(1.0 / tf.boolean_mask(rank, unk),
                                 metrics_collections=['metrics'],
                                 updates_collections=['updates'],
                                 name='streaming_mrr_unk')
    tf.summary.scalar('mean_reciprocal_rank', mrr)
    tf.summary.scalar('mean_reciprocal_rank_obs', mrr_obs)
    tf.summary.scalar('mean_reciprocal_rank_unk', mrr_unk)

    lt_1 = tf.cast(rank <= 1.0, dtype=tf.float32)
    lt_20 = tf.cast(rank <= 20.0, dtype=tf.float32)
    acc_at_1, _ = tf.metrics.mean(lt_1,
                                  metrics_collections=['metrics'],
                                  updates_collections=['updates'],
                                  name='streaming_acc_at_1')
    acc_at_1_obs, _ = tf.metrics.mean(tf.boolean_mask(lt_1, obs),
                                      metrics_collections=['metrics'],
                                      updates_collections=['updates'],
                                      name='streaming_acc_at_1_obs')
    acc_at_1_unk, _ = tf.metrics.mean(tf.boolean_mask(lt_1, unk),
                                      metrics_collections=['metrics'],
                                      updates_collections=['updates'],
                                      name='streaming_acc_at_1_unk')
    acc_at_20, _ = tf.metrics.mean(lt_20,
                                   metrics_collections=['metrics'],
                                   updates_collections=['updates'],
                                   name='streaming_acc_at_20')
    acc_at_20_obs, _ = tf.metrics.mean(tf.boolean_mask(lt_20, obs),
                                       metrics_collections=['metrics'],
                                       updates_collections=['updates'],
                                       name='streaming_acc_at_20_obs')
    acc_at_20_unk, _ = tf.metrics.mean(tf.boolean_mask(lt_20, unk),
                                       metrics_collections=['metrics'],
                                       updates_collections=['updates'],
                                       name='streaming_acc_at_20_unk')
    tf.summary.scalar('accuracy_at_1', acc_at_1)
    tf.summary.scalar('accuracy_at_1_obs', acc_at_1_obs)
    tf.summary.scalar('accuracy_at_1_unk', acc_at_1_unk)
    tf.summary.scalar('accuracy_at_20', acc_at_20)
    tf.summary.scalar('accuracy_at_20_obs', acc_at_20_obs)
    tf.summary.scalar('accuracy_at_20_unk', acc_at_20_unk)
Beispiel #6
0
def get_model(point_cloud, is_training):
    """ Classification TFN, input is BxNx3, output Bx10 """
    batch_size = point_cloud.get_shape()[0].value
    num_point = point_cloud.get_shape()[1].value
    end_points = {}

    # radial basis functions
    rbf_low = 0.0
    rbf_high = 3.5
    rbf_count = 4
    rbf_spacing = (rbf_high - rbf_low) / rbf_count
    centers = tf.cast(tf.lin_space(rbf_low, rbf_high, rbf_count), FLOAT_TYPE)

    # rij : [None, N, N, 3]
    rij = utils.difference_matrix(point_cloud)

    # dij : [None, N, N]
    dij = utils.distance_matrix(point_cloud)

    # rbf : [None, N, N, rbf_count]
    gamma = 1. / rbf_spacing
    rbf = tf.exp(-gamma * tf.square(tf.expand_dims(dij, axis=-1) - centers))

    # channels
    layer_dims = [1, 8, 8, 8]
    num_layers = len(layer_dims) - 1

    # embed : [None, N, layer1_dim, 1]
    with tf.variable_scope(None, "embed"):
        embed = layers.self_interaction_layer_without_biases(
            tf.ones(shape=(tf.shape(point_cloud)[0], num_point, 1, 1)),
            layer_dims[0])

    input_tensor_list = {0: [embed]}

    for layer, layer_dim in enumerate(layer_dims[1:]):
        with tf.variable_scope(None,
                               "layer" + str(layer),
                               values=[input_tensor_list]):
            input_tensor_list = layers.convolution(input_tensor_list, rbf, rij)
            input_tensor_list = layers.concatenation(input_tensor_list)
            input_tensor_list = layers.self_interaction(
                input_tensor_list, layer_dim)
            input_tensor_list = layers.nonlinearity(input_tensor_list)

    tfn_scalars = input_tensor_list[0][0]
    tfn_output_shape = tfn_scalars.get_shape().as_list()
    axis_to_squeeze = [i for i, e in enumerate(tfn_output_shape) if e == 1]
    tfn_output = tf.reduce_mean(tf.squeeze(tfn_scalars, axis=axis_to_squeeze),
                                axis=1)
    fully_connected_layer = tf.get_variable(
        "fully_connected_weights", [tfn_output_shape[-2], NUM_CLASSES],
        dtype=FLOAT_TYPE)
    output_biases = tf.get_variable("output_biases", [NUM_CLASSES],
                                    dtype=FLOAT_TYPE)

    # output : [None, NUM_CLASSES]
    output = tf.einsum("xy,hx->hy", fully_connected_layer,
                       tfn_output) + output_biases

    return output, end_points
Beispiel #7
0
  HEADER = '\033[95m'
  BLUE = '\033[94m'
  GREEN = '\033[92m'
  YELLOW = '\033[93m'
  RED = '\033[91m'
  NORMAL = '\033[0m'
  BOLD = '\033[1m'

def show(title, paths, dist):
    print "%s%s paths%s (Distance=%d):" % (bcolors.HEADER, title.title(), bcolors.NORMAL, dist)
    for path in sorted(paths):
        print "   ", path
    print

if __name__ == "__main__":
    distanceMatrix = utils.distance_matrix()

    print "%sDistance Matrix%s" % (bcolors.HEADER, bcolors.NORMAL)
    print "  ",str(distanceMatrix).replace("\n","\n   ")
    print

    comparisons = [
        ("shortest", bruteforce.shortestPaths),
        ("longest", bruteforce.longestPath),
    ]

    distLookup = {}
    for title, func in comparisons:
        paths = func(distanceMatrix)
        dist = utils.path_distance(distanceMatrix, paths[0])
        distLookup[title] = dist
Beispiel #8
0
def main():
    if os.path.exists('.\\logs'):
        shutil.rmtree('.\\logs')
    args = parse_args()
    wrap = WRAP
    train_years = [
        '2000', '2001', '2002', '2003', '2004', '2005', '2006', '2007', '2008',
        '2009', '2010', '2011', '2012', '2013'
    ][5:]
    dev_years = ['2011', '2012', '2013', '2014', '2015', '2016']  #, '2017']
    dmat = distance_matrix(
        'C:/Users/Sean/Documents/MATH_498/data/distance_matrix.txt',
        'C:/Users/Sean/Documents/MATH_498/data/country_codes.csv')
    modtype = 'state' + ('less' * (1 - args.state_model))
    scene = Scenario(modtype,
                     train_years=train_years,
                     dev_years=dev_years,
                     distance_matrix=dmat,
                     mlimit=True)
    model, inner = get_model('C:/Users/Sean/Documents/MATH_498/code/' +
                             modtype + '_weights.h5', (20, ),
                             wrap=wrap,
                             load=not args.train,
                             state=args.state_model)
    # Work on the loss function
    compile_args = dict(metrics=[r2_keras, r2_population],
                        loss=lambda x, y: weighted_mae(x, y),
                        optimizer=Adam(lr=0.001))
    fit_args = dict(epochs=200,
                    batch_size=64,
                    callbacks=[
                        TensorBoard(log_dir='.\\logs', histogram_freq=5),
                        EarlyStopping(patience=15, restore_best_weights=True)
                    ])
    scene.set_network(model,
                      compile_args,
                      fit_args,
                      noise=10000,
                      wrap=wrap,
                      train=args.train)
    state_model = inner if args.state_model else None
    if args.continent:
        continent_map = iso_to_continent()
        sub_states = [
            iso for iso in continent_map.index
            if continent_map[iso].lower() == args.continent.lower()
        ]
    else:
        sub_states = None
    scene.run(year='2000',
              timesteps=20,
              movement_function=na_gravity_distribution,
              view=args.view,
              state_model=state_model,
              sub_states=sub_states)
    model_json = scene.network.to_json()
    with open(
            'C:/Users/Sean/Documents/MATH_498/code/' + modtype + '_model.json',
            "w") as json_file:
        json.dump(model_json, json_file)
    scene.network.save_weights('C:/Users/Sean/Documents/MATH_498/code/' +
                               modtype + '_weights.h5')