ML with Flux
Build and train deep learning models with Flux.jl and explore traditional machine learning pipelines with MLJ.jl.
What is Flux.jl?
Flux.jl is Julia's premier deep learning framework. Unlike PyTorch or TensorFlow, Flux is written entirely in Julia, which means you can inspect, customize, and differentiate through any Julia code - no C++ internals to work around.
Building a Neural Network
using Flux # Define a simple feedforward network model = Chain( Dense(784, 128, relu), # Input layer Dropout(0.2), # Regularization Dense(128, 64, relu), # Hidden layer Dense(64, 10), # Output layer (10 classes) softmax ) # Check model structure println(model) println("Parameters: ", sum(length, Flux.params(model)))
Training Loop
using Flux: train!, onehotbatch, crossentropy using Flux.Data: DataLoader # Prepare data X_train = rand(Float32, 784, 1000) # 1000 samples y_train = onehotbatch(rand(0:9, 1000), 0:9) # DataLoader for batching loader = DataLoader((X_train, y_train), batchsize=32, shuffle=true) # Loss function and optimizer loss(x, y) = crossentropy(model(x), y) opt = Adam(0.001) # Training loop for epoch in 1:20 for (x, y) in loader grads = gradient(() -> loss(x, y), Flux.params(model)) Flux.Optimise.update!(opt, Flux.params(model), grads) end println("Epoch $epoch, Loss: $(loss(X_train, y_train))") end
GPU Acceleration
using CUDA # Move model and data to GPU model_gpu = model |> gpu X_gpu = X_train |> gpu y_gpu = y_train |> gpu # Training on GPU is the same code! loss_gpu(x, y) = crossentropy(model_gpu(x), y) # ... same training loop, just with GPU data # Move back to CPU for inference model_cpu = model_gpu |> cpu
MLJ.jl - Traditional Machine Learning
MLJ.jl provides a unified interface for traditional ML models, similar to scikit-learn:
using MLJ # Load a model Tree = @load DecisionTreeClassifier pkg=DecisionTree # Prepare data X, y = @load_iris train, test = partition(eachindex(y), 0.7, shuffle=true) # Create and train model tree = Tree(max_depth=5) mach = machine(tree, X, y) fit!(mach, rows=train) # Predict and evaluate y_pred = predict_mode(mach, rows=test) accuracy = sum(y_pred .== y[test]) / length(test) println("Accuracy: $accuracy") # Cross-validation evaluate!(mach, resampling=CV(nfolds=5), measure=accuracy)
Ready to Go Deeper?
Live instructor-led courses from our partners. Affiliate disclosure.
AI & ML Courses - 30% Off
Live instructor-led AI, machine learning, data science, and cloud courses for working professionals. Use code Limited30 at checkout.
EdurekaDataCamp - AI & Data Science
Hands-on Python, machine learning, and AI courses with interactive exercises and real projects.
DataCampedX - Top AI Courses
University-level AI courses from MIT, Harvard, Stanford. Earn certificates that employers recognize.
edX