How do I reduce memory allocation in my ODE in Julia? - performance

I am using a struct in julia called "Droplet" which has fields for certain parameters of a particle such as x,y,z location, radius, and speed in the x,y,z directions. These are type specified as Float64 values:
module ParticlePoints
mutable struct Droplet
radius::Float64
osmolarity::Float64
x::Float64
y::Float64
z::Float64
xvelocity::Float64
yvelocity::Float64
zvelocity::Float64
xacceleration::Float64
yacceleration::Float64
zacceleration::Float64
searchradius::Float64
end # End the struct
I import the module into the ODE file and populate it each timestep. The droplet fields are given by indices from the vector u. Using view() doesn't work for the assignment because view gives me a sub-array. Is there any way to just get the float 64 value out of the view command, or a way to get the float 64 value out of the indices without consuming memory? Regardless, it is consuming a huge amount of memory for the following code, including large memory consumption for the line where I just specify droplet.searchradius as "10.":
for i in 1:dropletcount
# display(typeof(droplets[i]))
droplets[i].radius = getindex(u,i,1)::Float64
droplets[i].osmolarity = getindex(u, i+dropletcount)::Float64
droplets[i].x = getindex(u, Int(i+2*dropletcount))::Float64
droplets[i].y = getindex(u, Int(i+3*dropletcount))::Float64
droplets[i].z = getindex(u, Int(i+4*dropletcount))::Float64
droplets[i].xvelocity = getindex(u, Int(i+5*dropletcount))::Float64
droplets[i].yvelocity = getindex(u, Int(i+6*dropletcount))::Float64
droplets[i].zvelocity = getindex(u, Int(i+7*dropletcount))::Float64
droplets[i].searchradius = 10.
# display(typeof(droplets[i]))
end # End the for loop
The struct droplet is stored in a seperate module called particle points and is imported at the start of the module as such:
module MyDropletVectorJComponents
include("myparticlepoints.jl")
# include("myquadtreequery.jl")
using Interpolations
using LinearAlgebra
using RegionTrees
using StaticArrays: SVector
import RegionTrees: AbstractRefinery, needs_refinement, refine_data, adaptivesampling!
using .ParticlePoints
using DifferentialEquations
# The exports of this particular module
# export mydropfun!, myOvitoPrint!
export dropvecj!
I do not know why assigning a float64 value to this struct field which is type specified as float64 is causing such large memory allocations. The "droplets" you see is a vector that is called outside the ODE function and then imported in using the parameters p:
# Create the vector of droplets that will be fed into the main ODE
droplets::Vector{Droplet} = Droplet[] # Change this to a vector that needs to be imported in
for i in 1:outerdropletcount
droplet::Droplet = Droplet(0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.) #< ----- > THE FORMATTING FOR THE DROPLET
push!(droplets, droplet)
end # End the for loop
# typeof(droplets)
# display(droplets)
# Create the current residual that makes the osmolarity activate after the droplets settle
# normvelocity = zeros(1,1)
p = [outerdropletcount, k, L, γ, D, boolean_osm, dradii, dosmolarities, dvelocities, ρ, accelerationvec, droplets, xyzaccel, flux]
prob = ODEProblem(mydropfun!, ICvector, tspan, p)
sol = solve(prob, Tsit5(), reltol=1e-8, saveat = 0.1)
Additionally, it should be specified that the file containing the struct is in a separate file but in the same folder/directory as the ODE. Also the ODE and the function that lays out some of the parameters like the aforementioned "droplets" vector are in the same file. Any help would be deeply appreciated, and I am happy to provide more information on request.
EDIT: Here is a full MWE of the code. I reduced as much as I could. This should run:
module MyOvitoPrint
export myovitoprint
function myovitoprint(sol, dropletcount)
# This function will recast the solution of our ordinary differential equation into a printed file that can be visualized in ovito.
V = convert(Array,sol) # Convert the ODE solution into an array that can be worked on
V = dropdims(V, dims = 2) # Dimension 2 is a singleton and should be eliminated
totalsteps::Int64 = size(V,2)
# Create the empty string vector that will be used to create the printout
myprintout = Array{String}(undef, (5+dropletcount)*totalsteps, 1)
# Initialize the timestep
timestep::Float64 = 0.
stepindex::Int64 = 1
for i in 1: 5+dropletcount: (5+dropletcount)*totalsteps
myprintout[i] = "ITEM: TIMESTEP"
myprintout[i+1] = "$timestep"
myprintout[i+2] = "ITEM: NUMBER OF ATOMS"
myprintout[i+3] = "$dropletcount"
myprintout[i+4] = "ITEM: ATOMS x y z radius osmolarity"
for j in 1:dropletcount
# myprintout[i+4+j] = "$x $y $z $radius $osmolarity"
x = V[2*dropletcount+j, stepindex]
y = V[3*dropletcount+j, stepindex]
z = V[4*dropletcount+j, stepindex]
radius = V[j, stepindex]
osmolarity = V[dropletcount + j, stepindex]
myprintout[i+4+j] = "$x $y $z $radius $(osmolarity)"
end # End sub-loop for droplets
stepindex += 1
timestep += 0.1
end # End loop
# Now print the .xyz file
outfile = "E:/Research Scripts and Functions/OVITO_Files/visual_model.ovito"
f = open(outfile, "w")
for i in eachindex(myprintout)
println(f, myprintout[i])
end
close(f)
end # Function end
end # End module MyOvitoPrint
module DropletFields
export Droplet, TreeCellData
mutable struct Droplet
radius::Float64
osmolarity::Float64
x::Float64
y::Float64
z::Float64
xvelocity::Float64
yvelocity::Float64
zvelocity::Float64
xacceleration::Float64
yacceleration::Float64
zacceleration::Float64
searchradius::Float64
end # End the struct
mutable struct TreeCellData
list::Vector{Droplet}
endindex::Int64
end # End the struct
end # End module DropletFields
module NateDropOde
# Imports -----
using ..DropletFields
using RegionTrees
using StaticArrays: SVector
import RegionTrees: AbstractRefinery, needs_refinement, refine_data, adaptivesampling!
using DifferentialEquations
# Exports -----
export natedropode
function natedropode(du, u, p, t)
# Put in the parameters that are needed, very important line of code: #=========================================================================
dropletcount, k, L, γ, D, boolean_osm, dradii, dosmolarities, dvelocities, ρ, accelerationvec, droplets, xyzaccel, flux = p;
maxradius = 0.
dropletcount = dropletcount
maxradius = maximum(view(u,1:dropletcount))
normvelocity = 0.
ℷ = abs.(view(u, 5*dropletcount+1:8*dropletcount))
ℸ = view(u, 5*dropletcount+1:8*dropletcount)
Ζ = view(u, 5*dropletcount+1:8*dropletcount)
broadcast(abs, ℸ)
ℶ = view(u, 5*dropletcount+1:8*dropletcount).^2
ℵ= sum(ℶ)
ℵ = sqrt(ℵ)
if (t > 0.1 && normvelocity/dropletcount <= 1e-4)
boolean_osm[1] = 1.0
end # End conditional
for i in 1:dropletcount
# display(typeof(droplets[i]))
droplets[i].radius = getindex(u,i,1)::Float64
droplets[i].osmolarity = getindex(u, i+dropletcount)::Float64
droplets[i].x = getindex(u, Int(i+2*dropletcount))::Float64
droplets[i].y = getindex(u, Int(i+3*dropletcount))::Float64
droplets[i].z = getindex(u, Int(i+4*dropletcount))::Float64
droplets[i].xvelocity = getindex(u, Int(i+5*dropletcount))::Float64
droplets[i].yvelocity = getindex(u, Int(i+6*dropletcount))::Float64
droplets[i].zvelocity = getindex(u, Int(i+7*dropletcount))::Float64
droplets[i].searchradius = 10.
# display(typeof(droplets[i]))
end # End the for loop
xvec = view(u, Int(2*dropletcount+1):Int(3*dropletcount))
xorigin::Float64 = minimum(xvec) - 0.1
yvec = view(u, Int(3*dropletcount+1):Int(4*dropletcount))
yorigin::Float64 = minimum(yvec) - 0.1
zvec = view(u, Int(4*dropletcount+1):Int(5*dropletcount))
zorigin::Float64 = minimum(zvec) - 0.1
xwidth::Float64 = (maximum(xvec) - xorigin) * 1.1
ywidth::Float64 = (maximum(yvec) - yorigin) * 1.1
zwidth::Float64 = (maximum(zvec) - zorigin) * 1.1
r = MyRefinery(0.0)
asaf = TreeCellData(droplets, dropletcount)
root::Cell = Cell(SVector(xorigin, yorigin, zorigin), SVector(xwidth, ywidth, zwidth), asaf)
myquadtree = adaptivesampling!(root, r)
for i in 1:dropletcount
fill!(xyzaccel, 0.)
flux[1] = 0.
jvec = 0.
query(myquadtree, droplets[i], xyzaccel, flux, L, k, boolean_osm, D, jvec, accelerationvec)
myvolume = 4/3 * pi * droplets[i].radius^3
dvelocities[i] = (xyzaccel[1] - γ * droplets[i].xvelocity) / (ρ * myvolume)
dvelocities[i+dropletcount] = (xyzaccel[2] - γ * droplets[i].yvelocity) / (ρ * myvolume)
dvelocities[i+2*dropletcount] = (xyzaccel[3] - γ * droplets[i].zvelocity) / (ρ * myvolume)
mymoles = droplets[i].osmolarity * myvolume
myvolume = myvolume + flux[1]
dradii[i] = (3/(4*pi) * myvolume)^(1/3) - droplets[i].radius
dosmolarities[i] = mymoles/myvolume - droplets[i].osmolarity
end # End the for loop
du[1:dropletcount] = dradii
du[dropletcount+1 : 2*dropletcount] = dosmolarities
du[2*dropletcount+1 : 5*dropletcount] = view(u, 5*dropletcount+1:8*dropletcount)
du[5*dropletcount+1 : 8*dropletcount] = dvelocities
end # End the main function
struct MyRefinery <: AbstractRefinery
tolerance::Int64
end # End struct
function needs_refinement(r::MyRefinery, cell)
myindex::Int64 = 0
for i in 1:cell.data.endindex
# Adress an edge case where the point sits directly in top of the line of division
edgecorrectionx = 0.
edgecorrectiony = 0.
edgecorrectionz = 0.
edgecasex1::Bool = cell.data.list[i].x == cell.boundary.origin[1]
edgecasex2::Bool = cell.data.list[i].x == cell.boundary.origin[1] + cell.boundary.widths[1]
edgecasey1::Bool = cell.data.list[i].y == cell.boundary.origin[2]
edgecasey2::Bool = cell.data.list[i].y == cell.boundary.origin[2] + cell.boundary.widths[2]
edgecasez1::Bool = cell.data.list[i].z == cell.boundary.origin[3]
edgecasez2::Bool = cell.data.list[i].z == cell.boundary.origin[3] + cell.boundary.widths[3]
edgecasex1 ? edgecorrectionx::Float64 += (cell.boundary.widths[1]+10)^(-1/cell.boundary.widths[1]) : nothing
edgecasex2 ? edgecorrectionx::Float64 += (cell.boundary.widths[1]+10)^(-1/cell.boundary.widths[1]) : nothing
edgecasey1 ? edgecorrectiony::Float64 += (cell.boundary.widths[2]+10)^(-1/cell.boundary.widths[2]) : nothing
edgecasey2 ? edgecorrectiony::Float64 += (cell.boundary.widths[2]+10)^(-1/cell.boundary.widths[2]) : nothing
edgecasez1 ? edgecorrectionz::Float64 += (cell.boundary.widths[3]+10)^(-1/cell.boundary.widths[3]) : nothing
edgecasez2 ? edgecorrectionz::Float64 += (cell.boundary.widths[3]+10)^(-1/cell.boundary.widths[3]) : nothing
case1 = cell.boundary.origin[1] < cell.data.list[i].x + edgecorrectionx < cell.boundary.origin[1] + cell.boundary.widths[1]
case2 = cell.boundary.origin[2] < cell.data.list[i].y + edgecorrectiony < cell.boundary.origin[2] + cell.boundary.widths[2]
case3 = cell.boundary.origin[3] < cell.data.list[i].z + edgecorrectionz < cell.boundary.origin[3] + cell.boundary.widths[3]
if case1 && case2 && case3 == true
myindex += Int(1)
cell.data.list[myindex] = cell.data.list[i]
end # end conditional
end # End the for loop
cell.data.endindex = myindex
myindex == r.tolerance
end # End function
function refine_data(r::MyRefinery, cell::Cell, indices)
cell.data
end # End function
function intersects(cell, mydroplet)
case1x = mydroplet.x - mydroplet.searchradius + 2 * mydroplet.searchradius <= cell.boundary.origin[1]
case2x = mydroplet.x - mydroplet.searchradius >= cell.boundary.origin[1] + cell.boundary.widths[1]
casex = case1x || case2x
case1y = mydroplet.y - mydroplet.searchradius + 2 * mydroplet.searchradius <= cell.boundary.origin[2]
case2y = mydroplet.y - mydroplet.searchradius >= cell.boundary.origin[2] + cell.boundary.widths[2]
casey = case1y || case2y
case1z = mydroplet.z - mydroplet.searchradius + 2 * mydroplet.searchradius <= cell.boundary.origin[3]
case2z = mydroplet.z - mydroplet.searchradius >= cell.boundary.origin[3] + cell.boundary.widths[3]
casez = case1z || case2z
!(casex || casey || casez)
end # End function
function contains(point, mydroplet)
xdist = mydroplet.x - point.x
ydist = mydroplet.y - point.y
zdist = mydroplet.z - point.z
sqrt(xdist^2 + ydist^2 + zdist^2) <= mydroplet.radius+point.radius && (sqrt(xdist^2 + ydist^2 + zdist^2) != 0)
end # End function
function query(cell, mydroplet, xyzaccel, flux, L, k, boolean_osm, D, jvec, accelerationvec)
if intersects(cell, mydroplet) == false
return nothing
else
if isleaf(cell)
for i in 1 : length(cell.data.list)
contains(cell.data.list[i], mydroplet) ? xyzaccel .+= mygetaccelerationandflux(cell.data.list[i], mydroplet, L, k, boolean_osm, D, jvec, accelerationvec)[1] : nothing
contains(cell.data.list[i], mydroplet) ? flux .+= mygetaccelerationandflux(cell.data.list[i], mydroplet, L, k, boolean_osm, D, jvec, accelerationvec)[2] : nothing
end # End the for loop
end # End the conditional
if !isleaf(cell)
query(cell[1,1,1], mydroplet, xyzaccel, flux, L, k, boolean_osm, D, jvec, accelerationvec)
query(cell[1,1,2], mydroplet, xyzaccel, flux, L, k, boolean_osm, D, jvec, accelerationvec)
query(cell[1,2,1], mydroplet, xyzaccel, flux, L, k, boolean_osm, D, jvec, accelerationvec)
query(cell[1,2,2], mydroplet, xyzaccel, flux, L, k, boolean_osm, D, jvec, accelerationvec)
query(cell[2,2,2], mydroplet, xyzaccel, flux, L, k, boolean_osm, D, jvec, accelerationvec)
query(cell[2,2,1], mydroplet, xyzaccel, flux, L, k, boolean_osm, D, jvec, accelerationvec)
query(cell[2,1,2], mydroplet, xyzaccel, flux, L, k, boolean_osm, D, jvec, accelerationvec)
query(cell[2,1,1], mydroplet, xyzaccel, flux, L, k, boolean_osm, D, jvec, accelerationvec)
end # End conditional
end # End conditional
# Perform the final calculations
return xyzaccel
return flux
end # End function
function mygetaccelerationandflux(fardrop::Droplet, neardrop::Droplet, L, k, boolean_osm, D, jvec, accelerationvec)#::Matrix{Float64}
xdist = fardrop.x - neardrop.x
ydist = fardrop.y - neardrop.y
zdist = fardrop.z - neardrop.z
directionx = xdist/sqrt(xdist^2 + ydist^2 + zdist^2)
directiony = ydist/sqrt(xdist^2 + ydist^2 + zdist^2)
directionz = zdist/sqrt(xdist^2 + ydist^2 + zdist^2)
mybilayerdist = L * (neardrop.radius + fardrop.radius)
accelerationvec[1] = directionx * -k * (abs(mybilayerdist * directionx) - abs(xdist))
accelerationvec[2] = directiony * -k * (abs(mybilayerdist * directiony) - abs(ydist))
accelerationvec[3] = directionz * -k * (abs(mybilayerdist * directionz) - abs(zdist))
# =================== The osmolarity part of the code ================================= #
intersectcase1 = neardrop.radius + fardrop.radius - sqrt(xdist^2 + ydist^2 + zdist^2) >= 0
intersectcase2 = sqrt(xdist^2 + ydist^2 + zdist^2) + neardrop.radius - fardrop.radius >= 0
intersectcase3 = sqrt(xdist^2 + ydist^2 + zdist^2) + fardrop.radius - neardrop.radius >= 0
# jvec = 0
if boolean_osm[1] >= 1.0
if (intersectcase1 && intersectcase2 && intersectcase3)
component1 = (sqrt(xdist^2 + ydist^2 + zdist^2)^2 - fardrop.radius^2 + neardrop.radius^2)^2
component2 = 4 * sqrt(xdist^2 + ydist^2 + zdist^2)^2 * neardrop.radius^2
component3 = component2 - component1
component4 = 1/(2*sqrt(xdist^2 + ydist^2 + zdist^2)) * sqrt(component3)
mycircular_area = pi * component4^2
osmdiff = neardrop.osmolarity - fardrop.osmolarity
jvec = D * mycircular_area * osmdiff
end # End the conditional
end # End the conditional
return accelerationvec, jvec
end # End function
end # End module
module NateOdeManager
using ..NateDropOde
using ..DropletFields
using ..MyOvitoPrint
using DifferentialEquations
ICmatrix = [3 0.1 3 0 0 0 0 0; 3 1. 9 0 0 0 0 0; 3 0.4 14 0 0 0 0 0]
ρ = 0.2 # The density of the droplet, used to calculate the mass of each droplet within the system as a function of radius. To wit Eqn = mass(rho,volume(radius))
k = 1000. # The spring force between each droplet
L = 0.8 # The natural length proportional to the radii that droplets will settle at
γ = 30. # The simplified version of both fluid response and spring damping in the system
timeend = 250. # The time at which the ODE will end
D = 2. *10^-2 # The rate at which diffusion will happen in the system
boolean_osm = [0.0] # The "on" switch for when the diffusion in the sytem will activate
tstepextract = Int(timeend * 10)
ICvector = reshape(ICmatrix, :, 1);
dropletcount = size(ICmatrix,1)
tspan = (0.0, timeend);
dradii = zeros(dropletcount,1)
dosmolarities = zeros(dropletcount,1)
dvelocities = zeros(3*dropletcount,1)
accelerationvec = zeros(3,1)
xyzaccel = zeros(3,1)
flux = zeros(1,1)
# Create the vector of droplets that will be fed into the main ODE
droplets = Droplet[] # Change this to a vector that needs to be imported in
for i in 1:dropletcount
droplet = Droplet(0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.) #< ----- > THE FORMATTING FOR THE DROPLET
push!(droplets, droplet)
end # End the for loop
p = [dropletcount, k, L, γ, D, boolean_osm, dradii, dosmolarities, dvelocities, ρ, accelerationvec, droplets, xyzaccel, flux]
prob = ODEProblem(natedropode, ICvector, tspan, p)
sol = solve(prob, Tsit5(),saveat = 0.1, dt = 0.1)
myovitoprint(sol, dropletcount)
end # End module NateOdeManager

Related

Monte Carlo program throws a method error in Julia

I am running this code but it shows a method error. Can someone please help me?
Code:
function lsmc_am_put(S, K, r, σ, t, N, P)
Δt = t / N
R = exp(r * Δt)
T = typeof(S * exp(-σ^2 * Δt / 2 + σ * √Δt * 0.1) / R)
X = Array{T}(N+1, P)
for p = 1:P
X[1, p] = x = S
for n = 1:N
x *= R * exp(-σ^2 * Δt / 2 + σ * √Δt * randn())
X[n+1, p] = x
end
end
V = [max(K - x, 0) / R for x in X[N+1, :]]
for n = N-1:-1:1
I = V .!= 0
A = [x^d for d = 0:3, x in X[n+1, :]]
β = A[:, I]' \ V[I]
cV = A' * β
for p = 1:P
ev = max(K - X[n+1, p], 0)
if I[p] && cV[p] < ev
V[p] = ev / R
else
V[p] /= R
end
end
end
return max(mean(V), K - S)
end
lsmc_am_put(100, 90, 0.05, 0.3, 180/365, 1000, 10000)
error:
MethodError: no method matching (Array{Float64})(::Int64, ::Int64)
Closest candidates are:
(Array{T})(::LinearAlgebra.UniformScaling, ::Integer, ::Integer) where T at /Volumes/Julia-1.8.3/Julia-1.8.app/Contents/Resources/julia/share/julia/stdlib/v1.8/LinearAlgebra/src/uniformscaling.jl:508
(Array{T})(::Nothing, ::Any...) where T at baseext.jl:45
(Array{T})(::UndefInitializer, ::Int64) where T at boot.jl:473
...
Stacktrace:
[1] lsmc_am_put(S::Int64, K::Int64, r::Float64, σ::Float64, t::Float64, N::Int64, P::Int64)
# Main ./REPL[39]:5
[2] top-level scope
# REPL[40]:1
I tried this code and I was expecting a numeric answer but this error came up. I tried to look it up on google but I found nothing that matches my situation.
The error occurs where you wrote X = Array{T}(N+1, P). Instead, use one of the following approaches if you need a Vector:
julia> Array{Float64, 1}([1,2,3])
3-element Vector{Float64}:
1.0
2.0
3.0
julia> Vector{Float64}([1, 2, 3])
3-element Vector{Float64}:
1.0
2.0
3.0
And in your case, you should write X = Array{T,1}([N+1, P]) or X = Vector{T}([N+1, P]). But since there's such a X[1, p] = x = S expression in your code, I guess you mean to initialize a 2D array and update its elements through the algorithm. For this, you can define X like the following:
X = zeros(Float64, N+1, P)
# Or
X = Array{Float64, 2}(undef, N+1, P)
So, I tried the following in your code:
# I just changed the definition of `X` in your code like the following
X = Array{T, 2}(undef, N+1, P)
#And the result of the code was:
julia> lsmc_am_put(100, 90, 0.05, 0.3, 180/365, 1000, 10000)
3.329213731484463

aligning polygon around the edges

so ive tried to align polygons to achieve something like this:
but i get something like this:
local function signedArea(p, q, r)
local cross = (q.y - p.y) * (r.x - q.x)
- (q.x - p.x) * (r.y - q.y)
return cross
end
local function isCCW(p, q, r) return signedArea(p, q, r) < 0 end
local function jarvis_gift_wrapping_algorithm(points)
local numPoints = #points
if numPoints < 3 then return end
local leftMostPointIndex = 1
for i = 1, numPoints do
if points[i].x < points[leftMostPointIndex].x then
leftMostPointIndex = i
end
end
local p = leftMostPointIndex
local hull = {}
repeat
q = points[p + 1] and p + 1 or 1
for i = 1, numPoints, 1 do
if isCCW(points[p], points[i], points[q]) then q = i end
end
table.insert(hull, points[q])
p = q
until (p == leftMostPointIndex)
return hull
end
and its basically this: https://github.com/kennyledet/Algorithm-Implementations/blob/master/Convex_hull/Lua/Yonaba/convex_hull.lua
if anyone has any idea or know any algorithms that might help me to achieve that i would really appreciate that

Why do the weights of my iterative reweighted least square algorithm for logistic regression always end up with NaN?

I'm trying to code a iterative reweighted least square algorithm for logistic regression for face recognition (images represented as a 19x19 greyscale image) but the weights always end up with NaN.
w_new = zeros(361,1);
for i = 1:35 % 100-fold cross-validation of 3480 samples
[ phi, t, ~, ~ ] = removeRows100FoldCV(i, trainx, traint);
t( t == -1 ) = 0;
while(true)
w_old = w_new;
y = computeYs(w_old, phi);
R = generateR(y);
w_new = w_old - inv(phi' * R * phi) * phi' * (y - t);
if onlyMarginalChangesInW(w_new, w_old) == true
break;
end
end
end
The target vector t is originally 1 or -1 depending of the image representing a face or not.
Computation of y:
function [ y ] = computeYs( w, phi )
y = zeros(size(phi,1), 1);
for i = 1:size(phi,1)
a = w' * phi(i,:)';
y(i) = 1/(1+exp(-a));
end
end
Generation of R:
function [ R ] = generateR( y )
R = zeros(size(y));
for i = 1:size(R,1)
R(i,i) = y(i) * (1 - y(i));
end
end
And the break condition trigger:
function [ result ] = onlyMarginalChangesInW( w_new, w_old )
result = true;
for i = 1:size(w_new)
if (w_new(i) / w_old(i) > 0.01)
result = false;
break;
end
end
end
The NaN result occurs in your inv(phi' * R * phi). Did you check your phi? Try cond(phi) to check whether it is very large. That may cause that inverse operation provides a huge elements.
By the way, I am trying to understand why it is not w_new = w_old - inv(phi' * R * phi) * phi' * R * (y - t); in your iterative reweighted least square algorithm implementation?

How to accelerate matlab code?

I'm using matlab to implement a multilayer neural network. In the code I represent
the value of each node AS netValue{k}
the weight between layer k and k + 1 AS weight{k}
etc.
Since these data is three-dimensional, I have to use cell to hold a 2-D matrix to enable matrix multiply.
So it becomes really really slow to train the model, which I expect to have resulted from the usage of cell.
Can anyone tell me how to accelerate this code? Thanks
clc;
close all;
clear all;
input = [-2 : 0.4 : 2;-2:0.4:2];
ican = 4;
depth = 4; % total layer - 1, by convension
[featureNum , sampleNum] = size(input);
levelNum(1) = featureNum;
levelNum(2) = 5;
levelNum(3) = 5;
levelNum(4) = 5;
levelNum(5) = 2;
weight = cell(0);
for k = 1 : depth
weight{k} = rand(levelNum(k+1), levelNum(k)) - 2 * rand(levelNum(k+1) , levelNum(k));
threshold{k} = rand(levelNum(k+1) , 1) - 2 * rand(levelNum(k+1) , 1);
end
runCount = 0;
sumMSE = 1; % init MSE
minError = 1e-5;
afa = 0.1; % step of "gradient ascendence"
% training loop
while(runCount < 100000 & sumMSE > minError)
sumMSE = 0; % sum of MSE
for i = 1 : sampleNum % sample loop
netValue{1} = input(:,i);
for k = 2 : depth
netValue{k} = weight{k-1} * netValue{k-1} + threshold{k-1}; %calculate each layer
netValue{k} = 1 ./ (1 + exp(-netValue{k})); %apply logistic function
end
netValue{depth+1} = weight{depth} * netValue{depth} + threshold{depth}; %output layer
e = 1 + sin((pi / 4) * ican * netValue{1}) - netValue{depth + 1}; %calc error
assistS{depth} = diag(ones(size(netValue{depth+1})));
s{depth} = -2 * assistS{depth} * e;
for k = depth - 1 : -1 : 1
assistS{k} = diag((1-netValue{k+1}).*netValue{k+1});
s{k} = assistS{k} * weight{k+1}' * s{k+1};
end
for k = 1 : depth
weight{k} = weight{k} - afa * s{k} * netValue{k}';
threshold{k} = threshold{k} - afa * s{k};
end
sumMSE = sumMSE + e' * e;
end
sumMSE = sqrt(sumMSE) / sampleNum;
runCount = runCount + 1;
end
x = [-2 : 0.1 : 2;-2:0.1:2];
y = zeros(size(x));
z = 1 + sin((pi / 4) * ican .* x);
% test
for i = 1 : length(x)
netValue{1} = x(:,i);
for k = 2 : depth
netValue{k} = weight{k-1} * netValue{k-1} + threshold{k-1};
netValue{k} = 1 ./ ( 1 + exp(-netValue{k}));
end
y(:, i) = weight{depth} * netValue{depth} + threshold{depth};
end
plot(x(1,:) , y(1,:) , 'r');
hold on;
plot(x(1,:) , z(1,:) , 'g');
hold off;
Have you used the profiler to find out what functions are actually slowing down your code? It shows what lines take the most time to execute.

How to modify the color of images to remove vibrancy?

How I can change the colors from this:
into this:
I generated the output image using Gimp with the input image as layer one, and the background color from the image as layer two, in the Layers panel I selected the mode "colors"
I want preserve the background color, but want the colors be shades of brown.
Any ideas to do this with ChunkyPNG? Or should I use ImageMagick with color lookup tables?
Thanks for your ideas.
I found the one from Linuxios the most helpful Gimp layer modes
require "json"
require "httpclient"
require "chunky_png"
module ChunkyPNG::Color
def h(value)
r,g,b = r(value).to_f / MAX, g(value).to_f / MAX, b(value).to_f / MAX
min, max = [r,g,b].minmax
return 0 if max == min
result = case max
when r then (g - b) / (max - min) + (g < b ? 6 : 0)
when g then (b - r) / (max - min) + 2
when b then (r - g) / (max - min) + 4
end
result * 60
end
def s(value)
min, max = [r(value), g(value), b(value)].minmax.map { |value| value.to_f / MAX }
max == 0 ? 0 : (max - min) / max
end
def v(value)
[r(value), g(value), b(value)].max.to_f / MAX
end
def hsv(h, s, v)
h = h.to_f / 360
i = (h * 6).floor
f = h * 6 - i
p = v * (1 - s)
q = v * (1 - f * s)
t = v * (1 - (1 - f) * s)
case i % 6
when 0 then r, g, b = v, t, p
when 1 then r, g, b = q, v, p
when 2 then r, g, b = p, v, t
when 3 then r, g, b = p, q, v
when 4 then r, g, b = t, p, v
when 5 then r, g, b = v, p, q
end
rgb *[r,g,b].map {|value| (value * 255).round }
end
end
module CoderWall
module_function
def badges(username)
url = URI.escape("https://coderwall.com/#{username}.json")
response = HTTPClient.get(url)
json = JSON.parse(response.body)
urls = json['badges'].map {|b| b['badge']}
brown = ChunkyPNG::Color.from_hex("#bf8a30")
hue = ChunkyPNG::Color.h(brown)
saturation = ChunkyPNG::Color.s(brown)
value = ChunkyPNG::Color.v(brown)
urls.each do |url|
matches = url.match /(\w+)\-/
response = HTTPClient.get(url)
filename = "./tmp/coderwall/"+matches[1]+".png"
File.open(filename,"w") {|f| f.write(response.body)}
image = ChunkyPNG::Image.from_file(filename)
image.pixels.map! do |pixel|
v = ChunkyPNG::Color.v(pixel)
unless ChunkyPNG::Color.a(pixel) > 0
v = value
end
ChunkyPNG::Color.hsv(hue,saturation, v)
end
image.save(filename.gsub(/\.png/,"_brown.png"), :fast_rgba)
end
end
end
badges = CoderWall.badges("astropanic")
That above code consists from some snippets found around the web.
Here is the result: Stop doing new year resolutions. Make the change now !

Resources