Skip to content

Instantly share code, notes, and snippets.

@romstad

romstad/mlp.jl Secret

Created February 4, 2021 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romstad/0e6c9070e34e952a53c2e3f59b1d7bf1 to your computer and use it in GitHub Desktop.
Save romstad/0e6c9070e34e952a53c2e3f59b1d7bf1 to your computer and use it in GitHub Desktop.
Flux MLP for binary classification (doesn't work), based on https://github.com/FluxML/model-zoo/blob/master/vision/mnist/mlp.jl
using Flux, Statistics
using Flux.Data: DataLoader
using Flux: onehotbatch, onecold, throttle, @epochs
using Flux.Losses: logitbinarycrossentropy
using Base.Iterators: repeated
using Parameters: @with_kw
using CUDA
using MLDatasets
if has_cuda() # Check if CUDA is available
@info "CUDA is on"
CUDA.allowscalar(false)
end
@with_kw mutable struct Args
η::Float64 = 3e-4 # learning rate
batchsize::Int = 1024 # batch size
epochs::Int = 10 # number of epochs
device::Function = gpu # set as gpu, if gpu available
end
function getdata(args)
ENV["DATADEPS_ALWAYS_ACCEPT"] = "true"
# Loading Dataset
xtrain, ytrain = MLDatasets.MNIST.traindata(Float32)
xtest, ytest = MLDatasets.MNIST.testdata(Float32)
# Reshape Data in order to flatten each image into a linear array
xtrain = Flux.flatten(xtrain)
xtest = Flux.flatten(xtest)
# Change all non-zero labels to 1
ytrain = [y == 0 ? 0 : 1 for y ∈ ytrain]
ytest = [y == 0 ? 0 : 1 for y ∈ ytest]
# Batching
train_data = DataLoader(xtrain, ytrain, batchsize=args.batchsize, shuffle=true)
test_data = DataLoader(xtest, ytest, batchsize=args.batchsize)
return train_data, test_data
end
function build_model(; imgsize=(28,28,1))
return Chain(
Dense(prod(imgsize), 32, relu),
Dense(32, 1)
)
end
function loss_all(dataloader, model)
l = 0f0
for (x,y) in dataloader
l += logitbinarycrossentropy(model(x), y)
end
l/length(dataloader)
end
function accuracy(data_loader, model)
acc = 0
for (x,y) in data_loader
values = cpu(model(x))
guesses = [v[1] > 0.5 ? 1 : 0 for v ∈ values]
acc += sum(guesses .== cpu(y)) * 1 / size(x, 2)
end
acc/length(data_loader)
end
function train(; kws...)
# Initializing Model parameters
args = Args(; kws...)
# Load Data
train_data,test_data = getdata(args)
# Construct model
m = build_model()
train_data = args.device.(train_data)
test_data = args.device.(test_data)
m = args.device(m)
loss(x,y) = logitbinarycrossentropy(m(x), y)
## Training
evalcb = () -> @show(loss_all(train_data, m))
opt = ADAM(args.η)
@epochs args.epochs Flux.train!(loss, params(m), train_data, opt, cb = evalcb)
@show accuracy(train_data, m)
@show accuracy(test_data, m)
end
cd(@__DIR__)
train()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment