Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

segmented_sum #32

Closed
pevnak opened this issue May 16, 2017 · 5 comments
Closed

segmented_sum #32

pevnak opened this issue May 16, 2017 · 5 comments

Comments

@pevnak
Copy link
Contributor

pevnak commented May 16, 2017

Hello,
is there a simple way, how I can use segmented_sum in the model? I would like to use Flux to implement multi-instance learning problems as described in
Using Neural Network Formalism to Solve Multiple-Instance Problems.

Thank you very much for help.
Tomas

@MikeInnes
Copy link
Member

You may want to mention it to @alha02 who's implementing a lot of tf ops in #28. It's pretty easy to do. I should add some docs for this.

@ghost
Copy link

ghost commented May 24, 2017

@pevnak I added segment_sum op to my TODO-list in #28

@pevnak
Copy link
Contributor Author

pevnak commented May 27, 2017

Thanks a lot. I am curious how flux will look like with it.

@pevnak
Copy link
Contributor Author

pevnak commented Sep 17, 2017

For those interested in, it can be easily added as multiplication with sparse vector:

function sparse_segmented_mean(x,segments::Vector{UnitRange{Int64}})
s = zeros(eltype(x),size(x,2),length(segments))
for (i,segment) in enumerate(segments)
s[segment,i]=1./length(segment)
end
x*sparse(s)
end

or slightly faster version

function fast_segmented_mean(x,segments::Vector{UnitRange{Int64}})
rowval = collect(1:size(x,2))
nzval = zeros(eltype(x),size(x,2))
colptr = zeros(Int,length(segments)+1)
colptr[end] = size(x,2)+1
for (i,segment) in enumerate(segments)
colptr[i] = segment.start
nzval[segment] = 1./length(segment)
end
m = size(x,2)
n = length(segments)
x*SparseMatrixCSC(m,n,colptr,rowval,nzval)
end

@pevnak pevnak closed this as completed Sep 17, 2017
@MikeInnes
Copy link
Member

FWIW, while I don't know your use case exactly, this seems like a fairly strange construction to me. Often TensorFlow has things like this because it can't handle things like variable-length lists of tensors. So in Flux it might be possible to just use an array of matrices, and map the appropriate function over that list.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants