|
13 | 13 | """ |
14 | 14 |
|
15 | 15 | # Author: Paul Krzakala <paul.krzakala@gmail.com> |
| 16 | +# Thibaut Germain <thibaut.germain.pro@gmail.com> |
16 | 17 | # License: MIT License |
17 | 18 |
|
18 | 19 | # sphinx_gallery_thumbnail_number = 1 |
|
75 | 76 | # This is simple but inefficient for large batches. |
76 | 77 | # |
77 | 78 | # Instead, you can use :func:`ot.batch.solve_batch`, which solves all |
78 | | -# problems in parallel. |
| 79 | +# problems in parallel. Several methods are available: ["sinkhorn", "log_sinkhorn"] |
| 80 | +# which solve regularized OT problems, and ["proximal"] which |
| 81 | +# solves regularized and unregularized OT problem using a proximal point scheme. |
| 82 | +# By default, the method is set to "auto" which automatically selects the appropriate |
| 83 | +# method based on the value of `reg`. If `reg` is None or 0, the proximal point method |
| 84 | +# is used. If `reg` is greater than 0, the Sinkhorn algorithm is used. |
| 85 | + |
| 86 | +max_iter = 10000 |
| 87 | +tol = 1e-4 |
| 88 | + |
| 89 | +# Classical OT problem |
| 90 | +## Naive approach |
| 91 | +results_values_list = [] |
| 92 | +for i in range(n_problems): |
| 93 | + res = ot.solve(M_list[i], max_iter=max_iter, tol=tol) |
| 94 | + results_values_list.append(res.value_linear) |
79 | 95 |
|
80 | | -reg = 1.0 |
81 | | -max_iter = 100 |
82 | | -tol = 1e-3 |
| 96 | +## Batched approach |
| 97 | +results_batch = ot.solve_batch(M=M_batch, max_iter=max_iter, tol=tol) |
| 98 | +results_values_batch = results_batch.value_linear |
83 | 99 |
|
84 | | -# Naive approach |
| 100 | +exact_validated = np.allclose( |
| 101 | + np.array(results_values_list), results_values_batch, atol=tol * 10 |
| 102 | +) |
| 103 | + |
| 104 | +# Entropic regularized OT problem |
| 105 | +## Naive approach |
| 106 | +reg = 1.0 |
85 | 107 | results_values_list = [] |
86 | 108 | for i in range(n_problems): |
87 | 109 | res = ot.solve(M_list[i], reg=reg, max_iter=max_iter, tol=tol, reg_type="entropy") |
88 | 110 | results_values_list.append(res.value_linear) |
89 | 111 |
|
90 | | -# Batched approach |
| 112 | +## Batched approach |
91 | 113 | results_batch = ot.solve_batch( |
92 | 114 | M=M_batch, reg=reg, max_iter=max_iter, tol=tol, reg_type="entropy" |
93 | 115 | ) |
94 | 116 | results_values_batch = results_batch.value_linear |
95 | 117 |
|
96 | | -assert np.allclose(np.array(results_values_list), results_values_batch, atol=tol * 10) |
| 118 | +entropic_validated = np.allclose( |
| 119 | + np.array(results_values_list), results_values_batch, atol=tol * 10 |
| 120 | +) |
| 121 | + |
| 122 | +print( |
| 123 | + f"Exact solve vs proximal batch close: {exact_validated} \nSinkhorn solve vs Sinkhorn solve_batch close: {entropic_validated}" |
| 124 | +) |
97 | 125 |
|
98 | 126 | ############################################################################# |
99 | 127 | # |
|
0 commit comments