Skip to content

Commit 71c7c80

Browse files
Bump Nx to 0.13 (#358)
1 parent d6fdc37 commit 71c7c80

43 files changed

Lines changed: 486 additions & 455 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/workflows/ci.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ jobs:
1414
- elixir: "1.19.4"
1515
otp: "27.3"
1616
lint: true
17-
- elixir: "1.15.8"
18-
otp: "26.1"
17+
- elixir: "1.17.3"
18+
otp: "26.2"
1919
steps:
2020
- uses: actions/checkout@v2
2121

lib/scholar/covariance/ledoit_wolf.ex

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,19 @@ defmodule Scholar.Covariance.LedoitWolf do
5454
#Nx.Tensor<
5555
f32[2][2]
5656
[
57-
[0.3557686507701874, 0.17340737581253052],
58-
[0.17340737581253052, 1.0300586223602295]
57+
[0.35576865, 0.17340738],
58+
[0.17340738, 1.0300586]
5959
]
6060
>
6161
iex> model.shrinkage
6262
#Nx.Tensor<
6363
f32
64-
0.15034137666225433
64+
0.15034138
6565
>
6666
iex> model.location
6767
#Nx.Tensor<
6868
f32[2]
69-
[0.17184630036354065, 0.3276958167552948]
69+
[0.1718463, 0.32769582]
7070
>
7171
7272
iex> key = Nx.Random.key(0)
@@ -76,20 +76,20 @@ defmodule Scholar.Covariance.LedoitWolf do
7676
#Nx.Tensor<
7777
f32[3][3]
7878
[
79-
[2.5945029258728027, 1.5078359842300415, 1.1623677015304565],
80-
[1.5078359842300415, 2.106797456741333, 1.1812156438827515],
81-
[1.1623677015304565, 1.1812156438827515, 1.4606266021728516]
79+
[2.594503, 1.507836, 1.1623677],
80+
[1.507836, 2.1067975, 1.1812156],
81+
[1.1623677, 1.1812156, 1.4606266]
8282
]
8383
>
8484
iex> model.shrinkage
8585
#Nx.Tensor<
8686
f32
87-
0.1908363401889801
87+
0.19083634
8888
>
8989
iex> model.location
9090
#Nx.Tensor<
9191
f32[3]
92-
[1.1228725910186768, 0.5419300198554993, 0.8678852319717407]
92+
[1.1228726, 0.54193, 0.86788523]
9393
>
9494
9595
iex> key = Nx.Random.key(0)
@@ -99,9 +99,9 @@ defmodule Scholar.Covariance.LedoitWolf do
9999
#Nx.Tensor<
100100
f32[3][3]
101101
[
102-
[3.8574986457824707, 2.2048025131225586, 2.1504499912261963],
103-
[2.2048025131225586, 2.4572863578796387, 1.7215262651443481],
104-
[2.1504499912261963, 1.7215262651443481, 2.154898166656494]
102+
[3.8574986, 2.2048025, 2.15045],
103+
[2.2048025, 2.4572864, 1.7215263],
104+
[2.15045, 1.7215263, 2.1548982]
105105
]
106106
>
107107
"""

lib/scholar/covariance/shrunk_covariance.ex

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,14 @@ defmodule Scholar.Covariance.ShrunkCovariance do
5151
#Nx.Tensor<
5252
f32[2][2]
5353
[
54-
[0.7721845507621765, 0.19141492247581482],
55-
[0.19141492247581482, 0.33952537178993225]
54+
[0.77218455, 0.19141492],
55+
[0.19141492, 0.33952537]
5656
]
5757
>
5858
iex> model.location
5959
#Nx.Tensor<
6060
f32[2]
61-
[0.18202415108680725, -0.09216632694005966]
61+
[0.18202415, -0.09216633]
6262
>
6363
iex> key = Nx.Random.key(0)
6464
iex> {x, _new_key} = Nx.Random.multivariate_normal(key, Nx.tensor([0.0, 0.0]), Nx.tensor([[0.8, 0.3], [0.2, 0.4]]), shape: {10}, type: :f32)
@@ -67,14 +67,14 @@ defmodule Scholar.Covariance.ShrunkCovariance do
6767
#Nx.Tensor<
6868
f32[2][2]
6969
[
70-
[0.7000747323036194, 0.1276099532842636],
71-
[0.1276099532842636, 0.41163527965545654]
70+
[0.70007473, 0.12760995],
71+
[0.12760995, 0.41163528]
7272
]
7373
>
7474
iex> model.location
7575
#Nx.Tensor<
7676
f32[2]
77-
[0.18202415108680725, -0.09216632694005966]
77+
[0.18202415, -0.09216633]
7878
>
7979
"""
8080

lib/scholar/cross_decomposition/pls_svd.ex

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -90,15 +90,15 @@ defmodule Scholar.CrossDecomposition.PLSSVD do
9090
iex> model.y_std
9191
#Nx.Tensor<
9292
f32[2]
93-
[5.467098712921143, 5.661198616027832]
93+
[5.4670987, 5.6611986]
9494
>
9595
iex> model.x_weights
9696
#Nx.Tensor<
9797
f32[3][2]
9898
[
99-
[0.521888256072998, -0.11256571859121323],
100-
[0.6170258522033691, 0.7342619299888611],
101-
[0.5889922380447388, -0.6694686412811279]
99+
[0.5218878, -0.11256802],
100+
[0.6170291, 0.7342591],
101+
[0.58898926, -0.66947126]
102102
]
103103
>
104104
"""
@@ -115,7 +115,15 @@ defmodule Scholar.CrossDecomposition.PLSSVD do
115115

116116
c = Nx.dot(x, [0], y, [0])
117117

118-
{u, _s, vt} = Nx.LinAlg.svd(c, full_matrices?: false)
118+
# full_matrices?: true on purpose: the false path forms the Gram matrix
119+
# CᵀC to avoid the larger QR/Halley iteration, which squares the
120+
# condition number and can cost the smaller singular vectors real
121+
# precision (Nx.LinAlg.svd's own docs call this out). C here is the
122+
# cross-covariance of two already small, already centered and scaled
123+
# blocks, so its singular values are routinely far apart, exactly the
124+
# regime that trade-off hurts. true costs more but keeps x_weights and
125+
# y_weights properly orthonormal.
126+
{u, _s, vt} = Nx.LinAlg.svd(c, full_matrices?: true)
119127
u = Nx.slice_along_axis(u, 0, num_components, axis: 1)
120128
vt = Nx.slice_along_axis(vt, 0, num_components, axis: 0)
121129
{u, vt} = Scholar.Decomposition.Utils.flip_svd(u, vt)
@@ -171,20 +179,20 @@ defmodule Scholar.CrossDecomposition.PLSSVD do
171179
#Nx.Tensor<
172180
f32[4][2]
173181
[
174-
[-1.397004246711731, -0.10283949971199036],
175-
[-1.1967883110046387, 0.17159013450145721],
176-
[0.5603229403495789, -0.10849219560623169],
177-
[2.0334696769714355, 0.039741579443216324]
182+
[-1.3970047, -0.10283327],
183+
[-1.1967875, 0.17159548],
184+
[0.56032246, -0.10849468],
185+
[2.0334697, 0.039732467]
178186
]
179187
>
180188
iex> y
181189
#Nx.Tensor<
182190
f32[4][2]
183191
[
184-
[-1.2260178327560425, -0.019306711852550507],
185-
[-0.9602956175804138, 0.04015407711267471],
186-
[0.3249155580997467, -0.04311027377843857],
187-
[1.8613981008529663, 0.022262824699282646]
192+
[-1.226018, -0.019301286],
193+
[-0.9602954, 0.040158324],
194+
[0.32491535, -0.043111708],
195+
[1.8613981, 0.022254586]
188196
]
189197
>
190198
@@ -252,20 +260,20 @@ defmodule Scholar.CrossDecomposition.PLSSVD do
252260
#Nx.Tensor<
253261
f32[4][2]
254262
[
255-
[-1.397004246711731, -0.10283949971199036],
256-
[-1.1967883110046387, 0.17159013450145721],
257-
[0.5603229403495789, -0.10849219560623169],
258-
[2.0334696769714355, 0.039741579443216324]
263+
[-1.3970047, -0.10283327],
264+
[-1.1967875, 0.17159548],
265+
[0.56032246, -0.10849468],
266+
[2.0334697, 0.039732467]
259267
]
260268
>
261269
iex> y
262270
#Nx.Tensor<
263271
f32[4][2]
264272
[
265-
[-1.2260178327560425, -0.019306711852550507],
266-
[-0.9602956175804138, 0.04015407711267471],
267-
[0.3249155580997467, -0.04311027377843857],
268-
[1.8613981008529663, 0.022262824699282646]
273+
[-1.226018, -0.019301286],
274+
[-0.9602954, 0.040158324],
275+
[0.32491535, -0.043111708],
276+
[1.8613981, 0.022254586]
269277
]
270278
>
271279

lib/scholar/decomposition/kernel_pca.ex

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ defmodule Scholar.Decomposition.KernelPCA do
9595
iex> x = Nx.tensor([[0.5, 0.2, 0.8], [1.0, 0.5, 0.2], [0.3, 1.0, 0.7], [0.9, 0.1, 1.0]])
9696
iex> kpca = Scholar.Decomposition.KernelPCA.fit(x, num_components: 2, kernel: :rbf)
9797
iex> kpca.eigenvalues
98-
Nx.tensor([0.3644833564758301, 0.2607457637786865])
98+
Nx.tensor([0.3644834, 0.26074564])
9999
"""
100100
deftransform fit(x, opts \\ []) do
101101
opts = NimbleOptions.validate!(opts, @opts_schema)
@@ -164,7 +164,7 @@ defmodule Scholar.Decomposition.KernelPCA do
164164
iex> Scholar.Decomposition.KernelPCA.transform(kpca, Nx.tensor([[0.5, 0.5, 0.5]]))
165165
Nx.tensor(
166166
[
167-
[0.12500187754631042, 0.02909737080335617]
167+
[0.12497942, 0.029221743]
168168
]
169169
)
170170
"""
@@ -216,10 +216,10 @@ defmodule Scholar.Decomposition.KernelPCA do
216216
iex> Scholar.Decomposition.KernelPCA.fit_transform(x, num_components: 2, kernel: :rbf)
217217
Nx.tensor(
218218
[
219-
[-0.13561572134494781, -0.16519643366336823],
220-
[0.021601030603051186, 0.44114425778388977],
221-
[0.4687873423099518, -0.1576462835073471],
222-
[-0.3547726571559906, -0.1183016300201416]
219+
[-0.13543777, -0.16535977],
220+
[0.021126157, 0.44115686],
221+
[0.4689568, -0.15726776],
222+
[-0.3546452, -0.1185294]
223223
]
224224
)
225225
"""

lib/scholar/decomposition/pca.ex

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,12 @@ defmodule Scholar.Decomposition.PCA do
100100
iex> pca.components
101101
Nx.tensor(
102102
[
103-
[0.36182016134262085, -0.08202514797449112, 0.8565111756324768, 0.3588128685951233],
104-
[0.6585038900375366, 0.7275884747505188, -0.17632202804088593, -0.07679986208677292]
103+
[0.36158934, -0.082268484, 0.8565728, 0.35884288],
104+
[0.65654594, 0.72970587, -0.17576799, -0.074712664]
105105
]
106106
)
107107
iex> pca.singular_values
108-
Nx.tensor([25.089859008789062, 6.007821559906006])
108+
Nx.tensor([25.089865, 6.007852])
109109
"""
110110
deftransform fit(x, opts \\ []) do
111111
opts = NimbleOptions.validate!(opts, @opts_schema)
@@ -210,12 +210,12 @@ defmodule Scholar.Decomposition.PCA do
210210
iex> pca.components
211211
Nx.tensor(
212212
[
213-
[-0.33354005217552185, 0.1048964187502861, -0.8618107080105579, -0.3674643635749817],
214-
[-0.5862125754356384, -0.7916879057884216, 0.15874788165092468, -0.06621300429105759]
213+
[-0.3331483, 0.105326004, -0.86190677, -0.36747244],
214+
[-0.58288467, -0.79399943, 0.15797591, -0.06967129]
215215
]
216216
)
217217
iex> pca.singular_values
218-
Nx.tensor([77.05782028025969, 10.137848854064941])
218+
Nx.tensor([77.05691, 10.158366])
219219
"""
220220
deftransform incremental_fit(batches, opts) do
221221
opts = NimbleOptions.validate!(opts, @opts_schema)
@@ -242,12 +242,12 @@ defmodule Scholar.Decomposition.PCA do
242242
iex> pca.components
243243
Nx.tensor(
244244
[
245-
[-0.3229745328426361, 0.09587063640356064, -0.8628664612770081, -0.37677285075187683],
246-
[-0.6786625981330872, -0.7167785167694092, 0.14237160980701447, 0.07332050055265427]
245+
[-0.32289746, 0.095940694, -0.8628819, -0.37678596],
246+
[-0.67544055, -0.7201917, 0.141901, 0.070487075]
247247
]
248248
)
249249
iex> pca.singular_values
250-
Nx.tensor([166.141845703125, 6.078948020935059])
250+
Nx.tensor([166.14182, 6.078579])
251251
"""
252252
deftransform partial_fit(model, x) do
253253
if Nx.rank(x) != 2 do
@@ -373,9 +373,9 @@ defmodule Scholar.Decomposition.PCA do
373373
iex> Scholar.Decomposition.PCA.transform(pca, x_transform)
374374
Nx.tensor(
375375
[
376-
[-1.4739344120025635, -0.48932668566703796],
377-
[0.28113049268722534, 0.2337251454591751],
378-
[2.0361955165863037, 0.9567767977714539]
376+
[-1.4737695, -0.49078038],
377+
[0.28105056, 0.23384926],
378+
[2.0358706, 0.9584787]
379379
]
380380
)
381381
"""
@@ -442,13 +442,12 @@ defmodule Scholar.Decomposition.PCA do
442442
iex> Scholar.Decomposition.PCA.fit_transform(x, num_components: 2)
443443
Nx.tensor(
444444
[
445-
[0.16441848874092102, 0.028548287227749825],
446-
[-0.32804328203201294, 0.20709986984729767],
447-
[-0.3284338414669037, -0.08318747580051422],
448-
[-0.42237386107444763, -0.0735677033662796],
449-
[0.17480169236660004, -0.11189625412225723],
450-
[0.7396301627159119, 0.03300142288208008
451-
]
445+
[0.16341472, -0.017230444],
446+
[-0.33249766, 0.1893504],
447+
[-0.32686582, -0.10110473],
448+
[-0.42023695, -0.005525116],
449+
[0.1768687, -0.14014886],
450+
[0.7393165, 0.074657336]
452451
]
453452
)
454453
"""

lib/scholar/decomposition/truncated_svd.ex

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,14 @@ defmodule Scholar.Decomposition.TruncatedSVD do
8181
#Nx.Tensor<
8282
f32[2][2]
8383
[
84-
[0.6871105432510376, 0.7265529036521912],
85-
[0.7265529036521912, -0.6871105432510376]
84+
[0.68631727, 0.72730225],
85+
[0.72730225, -0.68631727]
8686
]
8787
>
8888
iex> tsvd.singular_values
8989
#Nx.Tensor<
9090
f32[2]
91-
[7.528080940246582, 0.7601959705352783]
91+
[7.5280886, 0.7601881]
9292
>
9393
9494
"""
@@ -119,10 +119,10 @@ defmodule Scholar.Decomposition.TruncatedSVD do
119119
f32[5][2]
120120
[
121121
[0.0, 0.0],
122-
[0.6871105432510376, 0.7265529036521912],
123-
[1.413663387298584, 0.039442360401153564],
124-
[4.240990161895752, 0.1183270812034607],
125-
[6.017930030822754, -0.18578583002090454]
122+
[0.68631727, 0.72730225],
123+
[1.4136195, 0.04098499],
124+
[4.2408586, 0.122954965],
125+
[6.0181293, -0.17921868]
126126
]
127127
>
128128
iex> key = Nx.Random.key(0)
@@ -131,11 +131,11 @@ defmodule Scholar.Decomposition.TruncatedSVD do
131131
#Nx.Tensor<
132132
f32[5][2]
133133
[
134-
[1.9478826522827148, 2.260593891143799],
135-
[2.481153964996338, 1.906071662902832],
136-
[3.023407220840454, 1.352442979812622],
137-
[5.174456596374512, -0.46385863423347473],
138-
[6.521108150482178, -1.6488237380981445]
134+
[1.9499695, 2.258082],
135+
[2.4829183, 1.9048187],
136+
[3.024655, 1.3492495],
137+
[5.1740265, -0.46841517],
138+
[6.5195804, -1.655032]
139139
]
140140
>
141141
"""

lib/scholar/interpolation/bezier_spline.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,13 @@ defmodule Scholar.Interpolation.BezierSpline do
4343
[
4444
[
4545
[0.0, 2.0],
46-
[0.3333331048488617, 1.0333333015441895],
47-
[0.6666665077209473, 0.06666667759418488],
46+
[0.33333334, 1.0333333],
47+
[0.66666675, 0.06666667],
4848
[1.0, 0.0]
4949
],
5050
[
5151
[1.0, 0.0],
52-
[1.3333334922790527, -0.06666667759418488],
52+
[1.3333333, -0.06666667],
5353
[1.6666665077209473, 0.7666666507720947],
5454
[2.0, 1.0]
5555
],

0 commit comments

Comments
 (0)