|
1 | 1 | """ |
2 | 2 |
|
3 | | -.. _l-example-dot-profile: |
| 3 | +.. _l-onnx-array-api-example: |
4 | 4 |
|
5 | 5 | Compares implementations for a Piecewise Linear |
6 | 6 | =============================================== |
7 | 7 |
|
8 | | -A pieceise linear function is implemented and trained |
9 | | -following the tutorial :epkg:`Custom C++ and Cuda Extensions`. |
| 8 | +First example. |
10 | 9 |
|
11 | 10 | .. contents:: |
12 | 11 | :local: |
13 | 12 |
|
14 | | -Piecewise linear regression |
15 | | -+++++++++++++++++++++++++++ |
| 13 | +One function |
| 14 | +++++++++++++ |
16 | 15 | """ |
17 | | -import time |
18 | | -import pandas |
19 | | -import matplotlib.pyplot as plt |
20 | | -import torch |
21 | | -from td3a_cpp_deep.fcts.piecewise_linear import ( |
22 | | - PiecewiseLinearFunction, |
23 | | - PiecewiseLinearFunctionC, |
24 | | - PiecewiseLinearFunctionCBetter) |
25 | | - |
26 | | - |
27 | | -def train_piecewise_linear(x, y, device, cls, |
28 | | - max_iter=400, learning_rate=1e-4): |
29 | | - |
30 | | - alpha_pos = torch.tensor([1], dtype=torch.float32).to(device) |
31 | | - alpha_neg = torch.tensor([0.5], dtype=torch.float32).to(device) |
32 | | - alpha_pos.requires_grad_() |
33 | | - alpha_neg.requires_grad_() |
34 | | - |
35 | | - losses = [] |
36 | | - fct = cls.apply |
37 | | - |
38 | | - for t in range(max_iter): |
39 | | - |
40 | | - y_pred = fct(x, alpha_neg, alpha_pos) |
41 | | - loss = (y_pred - y).pow(2).sum() |
42 | | - loss.backward() |
43 | | - losses.append(loss) |
44 | | - |
45 | | - with torch.no_grad(): |
46 | | - alpha_pos -= learning_rate * alpha_pos.grad |
47 | | - alpha_neg -= learning_rate * alpha_neg.grad |
48 | | - |
49 | | - # Manually zero the gradients after updating weights |
50 | | - alpha_pos.grad.zero_() |
51 | | - alpha_neg.grad.zero_() |
52 | | - |
53 | | - return losses, alpha_neg, alpha_pos |
54 | | - |
55 | | - |
56 | | -################################ |
57 | | -# Python implementation |
58 | | -# +++++++++++++++++++++ |
59 | | - |
60 | | -device = 'cuda:0' if torch.cuda.is_available() else 'cpu' |
61 | | -print("device:", device) |
62 | | -x = torch.randn(100, 1, dtype=torch.float32) |
63 | | -y = x * 0.2 + (x > 0).to(torch.float32) * x * 1.5 + torch.randn(100, 1) / 5 |
64 | | -x = x.to(device).requires_grad_() |
65 | | -y = y.to(device).requires_grad_() |
66 | | - |
67 | | -begin = time.perf_counter() |
68 | | -losses, alpha_neg, alpha_pos = train_piecewise_linear( |
69 | | - x, y, device, PiecewiseLinearFunction) |
70 | | -end = time.perf_counter() |
71 | | -print(f"duration={end - begin}, alpha_neg={alpha_neg} " |
72 | | - f"alpha_pos={alpha_pos}") |
73 | | - |
74 | | -################################ |
75 | | -# C++ implementation |
76 | | -# ++++++++++++++++++ |
77 | | - |
78 | | -begin = time.perf_counter() |
79 | | -losses, alpha_neg, alpha_pos = train_piecewise_linear( |
80 | | - x, y, device, PiecewiseLinearFunctionC) |
81 | | -end = time.perf_counter() |
82 | | -print(f"duration={end - begin}, alpha_neg={alpha_neg} " |
83 | | - f"alpha_pos={alpha_pos}") |
84 | | - |
85 | | -################################ |
86 | | -# C++ implementation, second try |
87 | | -# ++++++++++++++++++++++++++++++ |
88 | | - |
89 | | -begin = time.perf_counter() |
90 | | -losses, alpha_neg, alpha_pos = train_piecewise_linear( |
91 | | - x, y, device, PiecewiseLinearFunctionCBetter) |
92 | | -end = time.perf_counter() |
93 | | -print(f"duration={end - begin}, alpha_neg={alpha_neg} " |
94 | | - f"alpha_pos={alpha_pos}") |
95 | | - |
96 | | -################################# |
97 | | -# The C++ implementation is very close to the python code. |
98 | | -# The second implementation in C++ is faster because |
99 | | -# it reuses created tensors. |
100 | | - |
101 | | -################################## |
102 | | -# Graphs |
103 | | -# ++++++ |
104 | | - |
105 | | -df = pandas.DataFrame() |
106 | | -df['x'] = x.cpu().detach().numpy().ravel() |
107 | | -df['y'] = y.cpu().detach().numpy().ravel() |
108 | | -df['yp'] = PiecewiseLinearFunction.apply( |
109 | | - x, alpha_neg, alpha_pos).cpu().detach().numpy() |
110 | | - |
111 | | -fig, ax = plt.subplots(1, 2, figsize=(10, 4)) |
112 | | -df.plot.scatter(x="x", y='y', label="y", color="blue", ax=ax[0]) |
113 | | -df.plot.scatter(x="x", y='yp', ax=ax[0], label="yp", color="orange") |
114 | | -ax[1].plot([float(lo.detach()) for lo in losses], label="loss") |
115 | | -ax[1].legend() |
116 | | - |
117 | | - |
118 | | -# plt.show() |
0 commit comments