-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_GCN.py
More file actions
177 lines (120 loc) · 3.31 KB
/
test_GCN.py
File metadata and controls
177 lines (120 loc) · 3.31 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
from model import *
import torch
import gzip
import pickle
import os
import random
import torch
import torch.optim as optim
from model import GCN
import matplotlib.pyplot as plt
import numpy as np
import argparse
torch.manual_seed(0)
parser = argparse.ArgumentParser()
parser.add_argument('-size','--size',type=int,default=1500)#radius
parser.add_argument('-L','--L',type=int,default=5)#radius
args = parser.parse_args()
#inference
size=args.size
L=args.L
if torch.cuda.is_available():
device = "cuda"
elif torch.backends.mps.is_available():
device = "mps"
else:
device = "cpu"
def max_product(M,p):
m,n=M.shape
P=torch.tile(p.unsqueeze(1), (1, m)).to(device)
PM=torch.mul(M.to(device),torch.t(P).to(device))
q=torch.max(PM,dim=1).values
return q
#dateset seting:
print('==> Building model..')
net=GCN(2,2,64).to(device)
model_name=f"./pretrain/best_GCN_size{size}_num100_L5.pth"
net.load_state_dict(torch.load(model_name,map_location=torch.device(device)))
net.eval()
#optimizer
criterion = nn.MSELoss()
optimizer = optim.Adam(net.parameters(), lr=0.001)
optimizer.zero_grad()
def test(size,task):
R_primal=[]
R_dual=[]
train_loss=0
if task==1:# test set
training_data_len=100
pointer=100
else: # train set
training_data_len=100
pointer=0
for iter in range(training_data_len):
iter=iter+pointer
with open(f'./instance/size_{size}/LPinstance_{size}_{iter}.pkl', 'rb') as f:
data_list = pickle.load(f)
# print(f"{iter}------------------------")
A=torch.tensor(data_list[0],dtype=torch.float32).to(device)
M, N = A.shape
x = torch.zeros(size=(N, 2))
y = torch.zeros(size=(M, 2))
x = torch.as_tensor(x, dtype=torch.float32).to(device)
y = torch.as_tensor(y, dtype=torch.float32).to(device)
pred_primal, pred_dual = net(A, x, y)
primal=torch.tensor(data_list[1],dtype=torch.float32).to(device)
dual=torch.tensor(data_list[2],dtype=torch.float32).to(device)
x_dot=resortation_1(A,pred_primal)
y_dot=resortation_2(A,pred_dual)
if(y_dot==None):
continue
if (x_dot == None):
continue
rp=abs(torch.sum(x_dot)-torch.sum(primal))/torch.sum(primal)
rd=abs(torch.sum(y_dot)-torch.sum(dual))/torch.sum(dual)
R_primal.append(rp)
R_dual.append(rd)
print(torch.mean(torch.tensor(R_primal)))
# print("------------------------")
print(torch.mean(torch.tensor(R_dual)))
R_primal=torch.mean(torch.tensor(R_primal))
R_dual=torch.mean(torch.tensor(R_dual))
return R_primal,R_dual
def resortation_1(A,x):
M,N=A.shape
x=x.squeeze(1)
ones=torch.ones(N).to(device)
zeros=torch.zeros(M).to(device)
x=torch.max(zeros,torch.min(ones,x))
for i in range(M):
term=torch.sum(A[i]*x)
if term >=1:
nx=torch.where(A[i]!=0)
x[nx]=x[nx]/term
return x
def resortation_2(A,y):
M,N=A.shape
ones=torch.ones(M).to(device)
y=y.squeeze(1)
eps=1e-5
y=torch.max(ones*eps,torch.min(ones,y))
for j in range(N):
term=torch.sum(torch.t(A)[j]*y)
if term <=1:
nx=torch.where(torch.t(A)[j]!=0)
y[nx]=y[nx]/term
return y
print("test performance--------------")
task=1
testdata=test(size,task)
#TrainSet
print("training performance--------------")
task=0
testdata=test(size,task)
# quit()
# X = np.array([i for i in range(T)])
# plt.plot(X,epoch_loss_list ,color="g",label='Training Loss')
# plt.xlabel('epoch')
# plt.ylabel('epoch loss')
# plt.legend()
# plt.show()