Comments (4)
For a more concrete but less MWE example, here's what I'm doing right now:
@cast rank1res[k] := rank1filter(permutedims(specs[k, :, :]))
@cast sproj[k, n, c] := rank1res[k][1][c, n]
@cast vs[k, c] := rank1res[k][2][c]
@cast λs[k] := rank1res[k][3]
@cast q[k] := rank1res[k][4]
Though actually I'm not sure how I would get rid of the permutedims
stuff here if it was combined with multiple return values, so maybe this isn't a great idea after all.
from tensorcast.jl.
Have never thought about that.
Fundamentally this package wants to make one big array from the RHS, and then there's another set of procedures to reshape/slice as desired by the LHS, afterwards. What certainly ought to work (but doesn't) is this:
@cast vi[j,k] := findmax(x[:, k])[j]
And what sort-of works is the following awful hack, because the LHS parser never checks that (v, i)
isn't a symbol, and so just assigns to it like (v, i) = eachcol(rhs)
:
x = randn(10, 20);
Base.ndims(::Tuple) = 1
@cast (v, i)[j][k] := findmax(x[:, k])[j] lazy
v
from tensorcast.jl.
FWIW, behaviour on version 0.4 is that these work without tweaks, and should probably be added to the tests:
julia> x = rand(Int8, 3, 5)
3×5 Matrix{Int8}:
76 -128 57 -8 -52
-85 15 -33 -55 -20
-2 -2 -29 -23 -17
julia> @cast vi[j,k] := findmax(x[:, k])[j]
2×5 Matrix{Signed}:
76 15 57 -8 -17
1 2 1 1 3
julia> @cast (v, i)[j][k] := findmax(x[:, k])[j]
2-element Vector{SubArray{Signed, 1, LinearAlgebra.Transpose{Signed, Matrix{Signed}}, Tuple{Base.Slice{Base.OneTo{Int64}}, Int64}, false}}:
[76, 15, 57, -8, -17]
[1, 2, 1, 1, 3]
julia> v
5-element view(transpose(::Matrix{Signed}), :, 1) with eltype Signed:
76
15
57
-8
-17
julia> @cast (v, i)[j][k] |= findmax(x[:, k])[j] # less lazy
2-element Vector{Vector{Signed}}:
[76, 15, 57, -8, -17]
[1, 2, 1, 1, 3]
julia> @pretty @cast (v, i)[j][k] := findmax(x[:, k])[j]
begin
ndims(x) == 2 || throw(ArgumentError("expected a 2-tensor x[:, k]"))
local sandpiper = sliceview(x, (:, *))
local butterfly = @__dot__(findmax(sandpiper))
local raven = transmute(stack(butterfly), (2, 1))
(v, i) = sliceview(raven, (:, *))
end
Still not ideal in terms of the element types, I guess. With Float64:
julia> x = rand(3, 5);
julia> @cast vi[j,k] := findmax(x[:, k])[j]
2×5 Matrix{Real}:
0.853762 0.394276 0.739257 0.347495 0.804691
3 2 3 3 1
julia> @cast (v, i)[j][k] := findmax(x[:, k])[j];
julia> Int.(i)
5-element Vector{Int64}:
3
2
3
3
1
from tensorcast.jl.
On this topic, StructArrays allows for efficient broadcasting out to several arrays, as for instance here: https://github.com/JuliaDiff/ChainRules.jl/blob/main/src/unzipped.jl#L6
It might not be hard to allow the pattern @cast v[k], i[k] := stuff[k]
(no reduction, same indices on all both LHS arrays), implemented using something like this, or exactly with unzip_broadcast
.
Issue about making this work with GPU arrays: JuliaArrays/StructArrays.jl#150
from tensorcast.jl.
Related Issues (20)
- Smarter repeat?
- Use vector of indexes HOT 3
- World Age Issues with TensorCast calls from pyJulia HOT 3
- Exploit `einops` for docs / tests / advertising
- Slicing an array produces `Vector{<:SubArray}`, hence allocates HOT 4
- Indices which run over a shorter range than `axes(A,d)` HOT 9
- Extra singleton dimension appears during casting HOT 2
- cannot @cast on views of other @cast results HOT 3
- @cast for remaining n-1 dimensions, e.g, add noise to a tensor of column vectors HOT 1
- @cast works for hcat but not for [ ] HOT 1
- @cast on functions with tuples as lvalues HOT 3
- Concatenation / forced indexing HOT 1
- @cast into an SMatrix HOT 3
- Interpolation & scope HOT 1
- Hope to close the both side indices check HOT 2
- Performance of nested reductions HOT 1
- Expr -> Symbol MethodError when combining mapslices and reshapes HOT 1
- Support for AxisKeys
- array arguments in @cast indexing HOT 1
- Error in summation within @reduce HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tensorcast.jl.