Skip to content

Commit

Permalink
campatibility with v0.4-dev Triangular, remove iceil()
Browse files Browse the repository at this point in the history
  • Loading branch information
davidavdav committed Mar 5, 2015
1 parent 822fd0b commit 3853410
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 13 deletions.
1 change: 1 addition & 0 deletions REQUIRE
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ Clustering
Distributions
PDMats
HDF5
Compat
2 changes: 2 additions & 0 deletions src/GaussianMixtures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ using Distributions
using PDMats
using Clustering
using HDF5, JLD
using Compat

include("compat.jl")
include("gmmtypes.jl")
include("bayestypes.jl")

Expand Down
4 changes: 2 additions & 2 deletions src/bayes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ Base.copy(vg::VGMM) = VGMM(vg.n, vg.d, copy(vg.π), copy(vg.α), copy(vg.β),
copy(vg.m), copy(vg.ν), copy(vg.W), copy(vg.hist))

## W[k] really is chol(W_k, :U), use precision() to get it back
precision(c::Triangular) = c' * c
mylogdet(c::Triangular) = 2sum(log(diag(c)))
precision(c::AbstractTriangular) = c' * c
mylogdet(c::AbstractTriangular) = 2sum(log(diag(c)))
mylogdet(m::Matrix) = logdet(m)

## sharpen VGMM to a GMM
Expand Down
12 changes: 8 additions & 4 deletions src/compat.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
## bugs in v0.3 and compatibility
if VERSION < v"0.4.0-dev"
Base.copy{T,A,uplo}(t::Triangular{T,A,uplo}) = Triangular(copy(t.data), uplo)
# Base.copy{T,A,uplo}(t::Triangular{T,A,uplo}) = Triangular(copy(t.data), uplo)
typealias AbstractTriangular Triangular
typealias UpperTriangular{T,M} Triangular{T,M,:U,false}
else
import Base.LinAlg.AbstractTriangular
end

## this we need for xμTΛxμ!
Base.A_mul_Bc!(A::StridedMatrix{Float64}, B::Triangular{Float32}) = A_mul_Bc!(A, convert(AbstractMatrix{Float64}, B))
Base.A_mul_Bc!(A::Matrix{Float32}, B::Triangular{Float64}) = A_mul_Bc!(A, convert(AbstractMatrix{Float32}, B))
#Base.A_mul_Bc!(A::StridedMatrix{Float64}, B::AbstractTriangular{Float32}) = A_mul_Bc!(A, convert(AbstractMatrix{Float64}, B))
#Base.A_mul_Bc!(A::Matrix{Float32}, B::AbstractTriangular{Float64}) = A_mul_Bc!(A, convert(AbstractMatrix{Float32}, B))
## this for diagstats
Base.BLAS.gemm!(a::Char, b::Char, alpha::Float64, A::Matrix{Float32}, B::Matrix{Float64}, beta::Float64, C::Matrix{Float64}) = Base.BLAS.gemm!(a, b, alpha, float64(A), B, beta, C)
#Base.BLAS.gemm!(a::Char, b::Char, alpha::Float64, A::Matrix{Float32}, B::Matrix{Float64}, beta::Float64, C::Matrix{Float64}) = Base.BLAS.gemm!(a, b, alpha, float64(A), B, beta, C)
4 changes: 2 additions & 2 deletions src/gmms.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ end
Base.eltype{T}(gmm::GMM{T}) = T

## switch between full covariance and inverse cholesky decomposition representations.
covar{T}(ci::Triangular{T}) = (c = inv(ci); c * c')
covar{T}(ci::AbstractTriangular{T}) = (c = inv(ci); c * c')
cholinv{T}::Matrix{T}) = chol(inv(cholfact(0.5+Σ'))), :U)

kind{T}(g::GMM{T,DiagCov{T}}) = :diag
Expand Down Expand Up @@ -64,7 +64,7 @@ function Base.full{T}(gmm::GMM{T})
if kind(gmm) == :full
return gmm
end
Σ = convert(FullCov{T}, [Triangular(diagm(vec(1./√gmm.Σ[i,:])), :U, false) for i=1:gmm.n])
Σ = convert(FullCov{T}, [UpperTriangular(diagm(vec(1./√gmm.Σ[i,:]))) for i=1:gmm.n])
new = GMM(copy(gmm.w), copy(gmm.μ), Σ, copy(gmm.hist), gmm.nx)
addhist!(new, "Converted to full covariance")
end
Expand Down
2 changes: 1 addition & 1 deletion src/gmmtypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ abstract GaussianMixture{T,CT}
## Full covariance is represented by inverse cholesky of the covariance matrix,
## i.e., Σ^-1 = ci * ci'
typealias DiagCov{T} Matrix{T}
typealias FullCov{T} Vector{Triangular{T,Matrix{T},:U,false}}
typealias FullCov{T} Vector{UpperTriangular{T,Matrix{T}}}

## GMMs can be of type FLoat32 or Float64, and diagonal or full
type GMM{T<:FloatingPoint, CT<:Union(Matrix,Vector)} <: GaussianMixture{T,CT}
Expand Down
4 changes: 3 additions & 1 deletion src/nomodule.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ using Distributions
using PDMats
using Clustering
using HDF5, JLD
using Compat

include("compat.jl")
require("gmmtypes.jl")
require("bayestypes.jl")

Expand All @@ -17,5 +19,5 @@ include("rand.jl")
include("data.jl")
include("recognizer.jl")

include("bayes.jl") ## experimental, not in main module
include("bayes.jl")

2 changes: 1 addition & 1 deletion src/stats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ function stats{T<:FloatingPoint}(gmm::GMM, x::Matrix{T}; order::Int=2, parallel=
elseif kind(gmm) == :full
bytes = sizeof(T) * ((d + d^2 + 5nₓ + nₓ*d)ng + (2d + 2)nₓ)
end
blocks = iceil(bytes / (mem * (1<<30)))
blocks = @compat ceil(Integer, bytes / (mem * (1<<30)))
if parallel
blocks= min(nₓ, max(blocks, nworkers()))
end
Expand Down
4 changes: 2 additions & 2 deletions src/train.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function GMMk{T}(n::Int, x::DataOrMatrix{T}; kind=:diag, nInit::Int=50, nIter::I
yy = Matrix[]
for y in x
ny = size(y,1)
nsample = min(ny, iceil(nneeded / length(x)))
nsample = min(ny, @compat ceil(Integer, nneeded / length(x)))
push!(yy, y[sample(1:ny, nsample, replace=false),:])
end
xx = vcat(yy...)
Expand Down Expand Up @@ -265,7 +265,7 @@ end
## A function we see more often... Λ is in chol(inv(Σ)) form
## compute Δ_i = (x_i - μ)' Λ (x_i - μ)
## Note: the return type of Δ should be the promote_type of x and μ/ciΣ
function xμTΛxμ!::Matrix, x::Matrix, μ::Matrix, ciΣ::Triangular)
function xμTΛxμ!::Matrix, x::Matrix, μ::Matrix, ciΣ::UpperTriangular)
# broadcast!(-, Δ, x, μ) # size: nₓ × d, add ops: nₓ * d
(nₓ, d) = size(x)
@inbounds for j = 1:d
Expand Down

0 comments on commit 3853410

Please sign in to comment.