/
tf_utils.py
107 lines (79 loc) · 4.18 KB
/
tf_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import numpy as np
import tensorflow as tf
import math
def random_mini_batches_GCN(X, Y, L, mini_batch_size, seed):
m = X.shape[0]
mini_batches = []
np.random.seed(seed)
permutation = list(np.random.permutation(m))
shuffled_X = X[permutation, :]
shuffled_Y = Y[permutation, :].reshape((m, Y.shape[1]))
shuffled_L1 = L[permutation, :].reshape((L.shape[0], L.shape[1]), order = "F")
shuffled_L = shuffled_L1[:, permutation].reshape((L.shape[0], L.shape[1]), order = "F")
num_complete_minibatches = math.floor(m / mini_batch_size)
for k in range(0, num_complete_minibatches):
mini_batch_X = shuffled_X[k * mini_batch_size : k * mini_batch_size + mini_batch_size, :]
mini_batch_Y = shuffled_Y[k * mini_batch_size : k * mini_batch_size + mini_batch_size, :]
mini_batch_L = shuffled_L[k * mini_batch_size : k * mini_batch_size + mini_batch_size, k * mini_batch_size : k * mini_batch_size + mini_batch_size]
mini_batch = (mini_batch_X, mini_batch_Y, mini_batch_L)
mini_batches.append(mini_batch)
mini_batch = (X, Y, L)
mini_batches.append(mini_batch)
return mini_batches
def random_mini_batches_GCN1(X, X1, Y, L, mini_batch_size, seed):
m = X.shape[0]
mini_batches = []
np.random.seed(seed)
permutation = list(np.random.permutation(m))
shuffled_X = X[permutation, :]
shuffled_X1 = X1[permutation, :]
shuffled_Y = Y[permutation, :].reshape((m, Y.shape[1]))
shuffled_L1 = L[permutation, :].reshape((L.shape[0], L.shape[1]), order = "F")
shuffled_L = shuffled_L1[:, permutation].reshape((L.shape[0], L.shape[1]), order = "F")
num_complete_minibatches = math.floor(m / mini_batch_size)
for k in range(0, num_complete_minibatches):
mini_batch_X = shuffled_X[k * mini_batch_size : k * mini_batch_size + mini_batch_size, :]
mini_batch_X1 = shuffled_X1[k * mini_batch_size : k * mini_batch_size + mini_batch_size, :]
mini_batch_Y = shuffled_Y[k * mini_batch_size : k * mini_batch_size + mini_batch_size, :]
mini_batch_L = shuffled_L[k * mini_batch_size : k * mini_batch_size + mini_batch_size, k * mini_batch_size : k * mini_batch_size + mini_batch_size]
mini_batch = (mini_batch_X, mini_batch_X1, mini_batch_Y, mini_batch_L)
mini_batches.append(mini_batch)
mini_batch = (X, X1, Y, L)
mini_batches.append(mini_batch)
return mini_batches
def random_mini_batches(X1, X2, Y, mini_batch_size, seed):
m = X1.shape[0]
m1 = X2.shape[0]
mini_batches = []
np.random.seed(seed)
permutation = list(np.random.permutation(m))
shuffled_X1 = X1[permutation, :]
shuffled_Y = Y[permutation, :].reshape((m, Y.shape[1]))
permutation1 = list(np.random.permutation(m1))
shuffled_X2 = X2[permutation1, :]
num_complete_minibatches = math.floor(m1/mini_batch_size)
mini_batch_X1 = shuffled_X1
mini_batch_Y = shuffled_Y
for k in range(0, num_complete_minibatches):
mini_batch_X2 = shuffled_X2[k * mini_batch_size : k * mini_batch_size + mini_batch_size, :]
mini_batch = (mini_batch_X1, mini_batch_X2, mini_batch_Y)
mini_batches.append(mini_batch)
return mini_batches
def random_mini_batches_single(X1, Y, mini_batch_size, seed):
m = X1.shape[0]
mini_batches = []
np.random.seed(seed)
permutation = list(np.random.permutation(m))
shuffled_X1 = X1[permutation, :]
#shuffled_X2 = X2[permutation, :]
shuffled_Y = Y[permutation, :].reshape((m, Y.shape[1]))
num_complete_minibatches = math.floor(m/mini_batch_size)
for k in range(0, num_complete_minibatches):
mini_batch_X1 = shuffled_X1[k * mini_batch_size : k * mini_batch_size + mini_batch_size, :]
mini_batch_Y = shuffled_Y[k * mini_batch_size : k * mini_batch_size + mini_batch_size, :]
mini_batch = (mini_batch_X1, mini_batch_Y)
mini_batches.append(mini_batch)
return mini_batches
def convert_to_one_hot(Y, C):
Y = np.eye(C)[Y.reshape(-1)].T
return Y