|
1 | 1 | module RCLIBSVMExt |
2 | | -using ReservoirComputing |
| 2 | + |
3 | 3 | using LIBSVM |
| 4 | +using ReservoirComputing: |
| 5 | + SVMReadout, addreadout!, ReservoirChain |
| 6 | +import ReservoirComputing: train |
4 | 7 |
|
5 | | -function ReservoirComputing.train(svr::LIBSVM.AbstractSVR, |
6 | | - states::AbstractArray, target::AbstractArray) |
7 | | - out_size = size(target, 1) |
8 | | - output_matrix = [] |
| 8 | +function train(svr::LIBSVM.AbstractSVR, |
| 9 | + states::AbstractArray, target::AbstractArray) |
| 10 | + @assert size(states, 2) == size(target, 2) "states and target must share columns." |
| 11 | + perm_states = permutedims(states) |
| 12 | + size_target = size(target, 1) |
9 | 13 |
|
10 | | - if out_size == 1 |
11 | | - output_matrix = LIBSVM.fit!(svr, states', vec(target)) |
| 14 | + if size_target == 1 |
| 15 | + vec_target = vec(target) |
| 16 | + model = LIBSVM.fit!(svr, perm_states, vec_target) |
| 17 | + return model |
12 | 18 | else |
13 | | - for i in 1:out_size |
14 | | - push!(output_matrix, LIBSVM.fit!(svr, states', target[i, :])) |
| 19 | + models = Vector{Any}(undef, size_target) |
| 20 | + for (idx, row_target) in enumerate(eachrow(target)) |
| 21 | + models[idx] = LIBSVM.fit!(svr, perm_states, row_target) |
15 | 22 | end |
| 23 | + return models |
16 | 24 | end |
17 | | - |
18 | | - return OutputLayer(svr, output_matrix, out_size, target[:, end]) |
19 | 25 | end |
20 | 26 |
|
21 | | -function ReservoirComputing.get_prediction(training_method::LIBSVM.AbstractSVR, |
22 | | - output_layer::AbstractArray, x::AbstractArray) |
23 | | - out = zeros(output_layer.out_size) |
| 27 | +_has_models(ps) = (ps isa NamedTuple) && (:models in propertynames(ps)) |
| 28 | + |
| 29 | +function (svmro::SVMReadout)(inp::AbstractArray, ps, st::NamedTuple) |
| 30 | + if !_has_models(ps) |
| 31 | + return inp, st |
| 32 | + end |
| 33 | + models = getfield(ps, :models) |
| 34 | + |
| 35 | + vec_like = false |
| 36 | + if ndims(inp) == 1 |
| 37 | + reshaped_inp = reshape(inp, 1, :) |
| 38 | + num_imp = 1 |
| 39 | + vec_like = true |
| 40 | + elseif ndims(inp) == 2 |
| 41 | + if size(inp, 2) == 1 |
| 42 | + reshaped_inp = reshape(vec(inp), 1, :) |
| 43 | + num_inp = 1 |
| 44 | + vec_like = true |
| 45 | + else |
| 46 | + reshaped_inp = permutedims(inp) |
| 47 | + num_imp = size(reshaped_inp, 1) |
| 48 | + end |
| 49 | + else |
| 50 | + throw(ArgumentError("SVMReadout expects 1D or 2D input; got size $(size(inp))")) |
| 51 | + end |
24 | 52 |
|
25 | | - for i in 1:(output_layer.out_size) |
26 | | - x_new = reshape(x, 1, length(x)) |
27 | | - out[i] = LIBSVM.predict(output_layer.output_matrix[i], x_new)[1] |
| 53 | + if models isa AbstractVector |
| 54 | + out_data = Array{float(eltype(reshaped_inp))}(undef, svmro.out_dims, num_imp) |
| 55 | + @inbounds for i in 1:svmro.out_dims |
| 56 | + single_out = LIBSVM.predict(models[i], reshaped_inp) |
| 57 | + out_data[i, :] = single_out |
| 58 | + end |
| 59 | + else |
| 60 | + single_out = LIBSVM.predict(models, reshaped_inp) |
| 61 | + out_data = reshape(single_out, 1, :) |
28 | 62 | end |
29 | 63 |
|
30 | | - return out |
| 64 | + if vec_like |
| 65 | + return vec(out_data), st |
| 66 | + else |
| 67 | + return out_data, st |
| 68 | + end |
31 | 69 | end |
32 | 70 |
|
33 | | -end #module |
| 71 | +end # module |
0 commit comments