-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.py
More file actions
81 lines (59 loc) · 1.79 KB
/
example.py
File metadata and controls
81 lines (59 loc) · 1.79 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
import torch
from torch.autograd import Variable
import poly
import numpy as np
import time
def uni_test():
dtype = torch.DoubleTensor
x = torch.linspace(-1, 1, 10000).type(dtype)
order = 300
vand = np.polynomial.legendre.legvander(x.data.numpy(), order)
# print("vand done", vand.shape)
# def func():
# ptorch = Legendre(order)
vand_torch = poly.legendre(x, order)
print("torch done")
difference = np.linalg.norm(vand - vand_torch.data.numpy())
print("difference = ", difference)
# checking to make sure can run multiple times
vand_torch = poly.legendre(x, order)
vand_torch = poly.legendre(x, order)
difference = np.linalg.norm(vand - vand_torch.data.numpy())
print("difference = ", difference)
assert difference < 1e-5, "pytorch and numpy.legendre not same"
def uni_time():
dtype = torch.DoubleTensor
N = 10000
xnump = np.linspace(-1, 1, N)
x = Variable(torch.linspace(-1, 1, N).type(dtype))
order = 400
def fnump():
vand = np.polynomial.legendre.legvander(xnump, order)
return vand
def ftorch():
vand_torch = poly.legendre(x, order)
return vand_torch
print("fnump ")
start = time.clock()
for ii in range(100):
fnump()
end = time.clock()
print("Elapsed time = ", end - start)
print("ftorch ")
start = time.clock()
for ii in range(100):
ftorch()
end = time.clock()
print("Elapsed time = ", end - start)
if __name__ == "__main__":
uni_test()
uni_time()
dim = 20
order = 3
N = 40
x = Variable(torch.rand(N, dim)*2.0 - 1.0)
# poly = MultiLegendre(dim, order)
# print("Number of unknowns = ", poly.num)
# print(poly(x))
# for param in poly.parameters():
# print(param)