Giter Club home page Giter Club logo

Comments (4)

ssfrr avatar ssfrr commented on June 11, 2024

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.

mcabbott avatar mcabbott commented on June 11, 2024

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.

mcabbott avatar mcabbott commented on June 11, 2024

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.

mcabbott avatar mcabbott commented on June 11, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.