Exemplo n.º 1
0
 def test_Grid2d():
     G = graphs.Grid2d(shape=(3, 2))
     self.assertEqual([G.h, G.w], [3, 2])
     G = graphs.Grid2d(shape=(3, ))
     self.assertEqual([G.h, G.w], [3, 3])
     G = graphs.Grid2d(shape=3)
     self.assertEqual([G.h, G.w], [3, 3])
Exemplo n.º 2
0
    def __init__(self, height, width, reward_location, walls_location, obstacles_location, initial_state=None,
                 obstacles_transition_probability=.2):
        """Initialize GridMazeDomain.

        Parameters
        ----------
        height: int
            Height of the grid, default it
        width: int
            Width of the grid
        walls_location: np.array
            Locations of the inaccessible states
        obstacles_location: np.array
            Locations of the states with difficult access
        obstacles_transition_probability: float
            Transition probability to an obstacle state must be in the range
        [0, 1]"""

        if obstacles_transition_probability < 0 or obstacles_transition_probability > 1:
            raise ValueError('obstacles_transition_probability must be in range [0, 1]')

        self.width = width
        self.height = height
        self.num_states = int(height*width)

        self.reward_location = reward_location

        self.initial_state = initial_state

        self.transition_probabilities = np.ones(self.num_states)

        self.transition_probabilities[obstacles_location] = obstacles_transition_probability

        self.transition_probabilities[walls_location] = 0.

        self.graph = graphs.Grid2d(N1=height, N2=width)

        self.weighted_graph = graphs.Grid2d(N1=height, N2=width)

        for obstacle in obstacles_location:
            self.weighted_graph.W[obstacle, :] *= obstacles_transition_probability
            self.weighted_graph.W[:, obstacle] *= obstacles_transition_probability

        for wall in walls_location:
            self.weighted_graph.W[wall, :] = 0.
            self.weighted_graph.W[:, wall] = 0.

        self.weighted_graph.Ne = sparse.tril(self.weighted_graph.W).nnz

        self._state = self._init_random_state()
Exemplo n.º 3
0
 def test_grid2d_diagonals(self):
     value = 0.5
     G = graphs.Grid2d(6, 7, diagonal=value)
     self.assertEqual(G.W[2, 8], value)
     self.assertEqual(G.W[9, 1], value)
     self.assertEqual(G.W[9, 3], value)
     self.assertEqual(G.W[2, 14], 0.0)
     self.assertEqual(G.W[17, 1], 0.0)
     self.assertEqual(G.W[9, 16], 1.0)
     self.assertEqual(G.W[20, 27], 1.0)
Exemplo n.º 4
0
    def test_localize(self):
        G = graphs.Grid2d(20)
        G.compute_fourier_basis()
        g = filters.Heat(G, 100)

        # Localize signal at node by filtering Kronecker delta.
        NODE = 10
        s1 = g.localize(NODE, method='exact')

        # Should be equal to a row / column of the filtering operator.
        gL = G.U.dot(np.diag(g.evaluate(G.e)[0]).dot(G.U.T))
        s2 = np.sqrt(G.N) * gL[NODE, :]
        np.testing.assert_allclose(s1, s2)

        # That is actually a row / column of the analysis operator.
        F = g.compute_frame(method='exact')
        np.testing.assert_allclose(F, gL)
Exemplo n.º 5
0
 def test_grid2d(self):
     graphs.Grid2d(3, 2)
     graphs.Grid2d(3)
    # LOAD DATASET
    ############################################################################
    if P['dataset'] == 'synth':
        X, y = make_blobs(n_samples=100,
                          centers=5,
                          n_features=4,
                          random_state=None)  # 6
        X = X.astype(np.float32)
        A = kneighbors_graph(X, n_neighbors=25, mode='distance').todense()
        A = np.asarray(A)
        A = np.maximum(A, A.T)
        A = sp.csr_matrix(A, dtype=np.float32)
        n_clust = y.max() + 1
    elif P['dataset'] == 'cloud':
        G = graphs.Grid2d(
            N1=15, N2=10
        )  # Community(N=150, seed=0) #SwissRoll(N=400, seed=0) #Ring(N=100) #TwoMoons()  #Cube(nb_pts=500)  #Bunny()
        X = G.coords.astype(np.float32)
        A = G.W
        y = np.ones(X.shape[0])  # X[:,0] + X[:,1]
        n_clust = 5
    else:
        A, X, _, _, _, _, _, _, y_ohe = citation.load_data(P['dataset'])
        y = np.argmax(y_ohe, axis=-1)
        X = X.todense()
        n_clust = y.max() + 1

    # Sort IDs
    if P['dataset'] != 'cloud':
        ids = np.argsort(y)
        y = y[ids]
Exemplo n.º 7
0
newpath = 'C:/Kaige_Research/Graph Learning/graph_learning_code/results/linear_signal_results/' + str(
    timeRun) + '/'
if not os.path.exists(newpath):
    os.makedirs(newpath)

node_num = 100
item_num = 1000
noise_scale = 0.1
dimension = 2
alpha = 1
beta = 0.2
theta = 0.1
step_size = 0.5
item_features = np.random.uniform(size=(item_num, dimension))

G = graphs.Grid2d(N1=10, N2=10)
fig, axes = plt.subplots(1, 2)
_ = axes[0].spy(G.W)
G.plot(ax=axes[1])

adj = G.W.toarray()
pos = G.coords
node_features = pos.copy()
clear_signal = np.dot(item_features, node_features.T)

noisy_signal = clear_signal.copy()
noisy_signal2 = clear_signal.copy()

for i in range(item_num):
    mask = choice(list(range(node_num)), size=10)
    noisy_signal[i, mask] = 0
Exemplo n.º 8
0
upsampling_from_mask_op = Lambda(upsampling_from_mask)
upsampling_from_matrix_op = Lambda(upsampling_from_matrix)

# HYPERPARAMS
ITER = 10000
ACTIV = 'tanh'
dataset = 'grid'
gnn_channels = 32
es_patience = 1000

# LOAD DATASET
if dataset == 'ring':
    G = graphs.Ring(N=200)
elif dataset == 'grid':
    G = graphs.Grid2d(N1=30, N2=30)
X = G.coords.astype(np.float32)
A = G.W
y = np.zeros(X.shape[0])  # X[:,0] + X[:,1]
n_classes = np.unique(y).shape[0]
n_feat = X.shape[-1]
n_nodes = A.shape[0]

# MODEL DEFINITION
X_in = Input(
    tensor=tf.placeholder(tf.float32, shape=(None, n_feat), name='X_in'))
A_in = Input(tensor=tf.sparse_placeholder(tf.float32, shape=(None, None)),
             name='A_in')
I_in = Input(
    tensor=tf.placeholder(tf.int32, shape=(None, ), name='segment_ids_in'))
X_target = Input(
Exemplo n.º 9
0
 def test_Grid2d():
     G = graphs.Grid2d()
     needed_attributes_testing(G)
Exemplo n.º 10
0
 def test_Grid2d():
     G = graphs.Grid2d()