Skip to content

Commit a64c80e

Browse files
committed
Fix type system reports
1 parent daf2f6b commit a64c80e

3 files changed

Lines changed: 13 additions & 12 deletions

File tree

lib/scholar/interpolation/cubic_spline.ex

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -241,15 +241,7 @@ defmodule Scholar.Interpolation.CubicSpline do
241241

242242
defnp predict_n(%__MODULE__{x: x, coefficients: coefficients}, target_x, opts) do
243243
original_shape = Nx.shape(target_x)
244-
245-
target_x =
246-
case target_x do
247-
{} ->
248-
Nx.new_axis(target_x, 0)
249-
250-
_ ->
251-
Nx.flatten(target_x)
252-
end
244+
target_x = Nx.flatten(target_x)
253245

254246
idx_selector = Nx.new_axis(target_x, 1) > Nx.new_axis(x, 0)
255247

lib/scholar/shared.ex

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,12 @@ defmodule Scholar.Shared do
5555
is_nil(weights) ->
5656
Nx.tensor(1.0, type: type)
5757

58-
Nx.is_tensor(weights) and Nx.shape(weights) in [{}, {num_samples}] ->
59-
weights |> Nx.broadcast({num_samples}) |> Nx.as_type(type)
60-
6158
is_list(weights) and length(weights) == num_samples ->
6259
Nx.tensor(weights, type: type)
6360

61+
Nx.is_tensor(weights) and Nx.shape(weights) in [{}, {num_samples}] ->
62+
weights |> Nx.broadcast({num_samples}) |> Nx.as_type(type)
63+
6464
true ->
6565
raise ArgumentError,
6666
"invalid value for :weights option: expected list or tensor of positive numbers of size #{num_samples}, got: #{inspect(weights)}"

test/scholar/interpolation/cubic_spline_test.exs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,15 @@ defmodule Scholar.Interpolation.CubicSplineTest do
206206
end
207207
end
208208

209+
test "predict/2 supports scalar tensors with jit_apply" do
210+
model = CubicSpline.fit(Nx.iota({3}), Nx.iota({3}))
211+
212+
prediction =
213+
Nx.Defn.jit_apply(&CubicSpline.predict/3, [model, Nx.tensor(1.0), []])
214+
215+
assert prediction == Nx.tensor(1.0)
216+
end
217+
209218
test "not sorted x" do
210219
x = Nx.tensor([3, 2, 4, 1, 0])
211220
y = Nx.tensor([-10, 3, -1, 2, 1])

0 commit comments

Comments
 (0)