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

Updates Flux and Optimisers dependencies #12

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Version [1.0.3] - 2024-12-19

## Version [1.0.2] - 2024-11-13

### Added
Expand Down
6 changes: 3 additions & 3 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "EnergySamplers"
uuid = "f446124b-5d5e-4171-a6dd-a1d99768d3ce"
authors = ["Patrick Altmeyer and contributors"]
version = "1.0.2"
version = "1.0.3"

[deps]
CategoricalArrays = "324d7699-5711-5eae-9e2f-1d82baa6b597"
Expand All @@ -16,9 +16,9 @@ Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
Aqua = "0.8"
CategoricalArrays = "0.10"
Distributions = "0.25"
Flux = "0.14"
Flux = "0.14, 0.15, 0.16"
MLUtils = "0.4"
Optimisers = "0.3"
Optimisers = "0.3, 0.4"
StatsBase = "0.33, 0.34"
Tables = "1.12"
Test = "1"
Expand Down
22 changes: 11 additions & 11 deletions docs/src/_intro.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ for feature in numerics
rescale!(test[!, feature], μ, σ; obsdim=1)
end

prep_X(x) = gpu(Matrix(x)')
prep_y(y) = gpu(reshape(y, 1, :))
prep_X(x) = Matrix(x)'
prep_y(y) = reshape(y, 1, :)
train_X, test_X = prep_X.((train[:, features], test[:, features]))
train_y, test_y = prep_y.((train[:, target], test[:, target]))
train_set = Flux.DataLoader((train_X, train_y); batchsize=100, shuffle=false)
Expand All @@ -68,22 +68,22 @@ Finally, we create a small helper function that runs the training loop for a giv
function train_logreg(; steps::Int=1000, opt=Flux.Descent(2))
Random.seed!(1)

paramvec(θ) = reduce(hcat, cpu(θ))
model = gpu(Dense(length(features), 1, sigmoid))
paramvec(θ) = reduce(hcat, θ)
model = Dense(length(features), 1, sigmoid)
θ = Flux.params(model)
θ₀ = paramvec(θ)

predict(x; thres=0.5) = model(x) .> thres
accuracy(x, y) = mean(cpu(predict(x)) .== cpu(y))
accuracy(x, y) = mean(predict(x) .== y)

loss(yhat, y) = Flux.binarycrossentropy(yhat, y)
avg_loss(yhat, y) = mean(loss(yhat, y))
trainloss() = avg_loss(model(train_X), train_y)
testloss() = avg_loss(model(test_X), test_y)

trainlosses = [cpu(trainloss()); zeros(steps)]
testlosses = [cpu(testloss()); zeros(steps)]
weights = [cpu(θ₀); zeros(steps, length(θ₀))]
trainlosses = [trainloss(); zeros(steps)]
testlosses = [testloss(); zeros(steps)]
weights = [θ₀; zeros(steps, length(θ₀))]

opt_state = Flux.setup(opt, model)

Expand All @@ -102,9 +102,9 @@ function train_logreg(; steps::Int=1000, opt=Flux.Descent(2))
end

# Bookkeeping
weights[t + 1, :] = cpu(paramvec(θ))
trainlosses[t + 1] = cpu(trainloss())
testlosses[t + 1] = cpu(testloss())
weights[t + 1, :] = paramvec(θ)
trainlosses[t + 1] = trainloss()
testlosses[t + 1] = testloss()
end

println("Final parameters are $(paramvec(θ))")
Expand Down
Loading