-
-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathnnmf.jl
46 lines (32 loc) · 1018 Bytes
/
nnmf.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# #
# ## Setting up the cluster
using Distributed
addprocs(4, exeflags=["--project=$(Base.active_project())"])
# ## Loading Dagger
@everywhere using Dagger
import Dagger: Computation, reduceblock
# ## Hierachical parallelism
#
# Each Julia worker is modeled as a `OSProc` which can have multiple `ThreadProc`
# assigned to it. Let's walk all workers and discover how many `ThreadProc`s there are.
p = sum(length(Dagger.get_processors(OSProc(id))) for id in workers())
# ## NNMF
function nnmf(X, W, H)
# H update
H = (H .* (W' * (X ./ (W * H))) ./ (sum(W; dims=1))')
# W update
W = (W .* ((X ./ (W * H)) * (H')) ./ (sum(H; dims=2)'))
# error estimate
(X - W * H, W, H)
end
# ## Sample data
ncol = 2000
nrow = 10000
nfeatures = 12
# Eagerly allocating
X = compute(rand(Blocks(nrow, ncol÷p), nrow, ncol))
W = compute(rand(Blocks(nrow, ncol÷p), nrow, nfeatures))
H = compute(rand(Blocks(nrow, ncol÷p), nfeatures, ncol))
# ## Run a NMF step
err, W, H = nnmf(X, W, H)
compute(err)