Skip to content

Commit

Permalink
Create Flux.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
Stalin-143 authored Dec 6, 2024
1 parent 57830c9 commit 91d2b20
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions Flux.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using Flux
using Statistics

# Generate some example data (y = 2x + 1)
x = rand(100) # Random input data
y = 2 * x .+ 1 .+ 0.1 * randn(100) # Output data with some noise

# Define the model: A simple linear layer with one input and one output
model = Chain(
Dense(1, 1) # Dense layer with 1 input and 1 output
)

# Loss function: Mean Squared Error (MSE)
loss(x, y) = Flux.mse(model(x), y)

# Training loop
optimizer = ADAM() # Optimizer choice (Adam)

# Prepare the data (reshape to column vectors)
x_data = reshape(x, 100, 1)
y_data = reshape(y, 100, 1)

# Train the model
for epoch in 1:1000
Flux.train!(loss, params(model), [(x_data, y_data)], optimizer)
if epoch % 100 == 0
println("Epoch $epoch, Loss: $(loss(x_data, y_data))")
end
end

# After training, you can use the model to predict
new_x = [0.5, 1.5, 2.5] # New input data
predictions = model(reshape(new_x, length(new_x), 1))

println("Predictions for new data: $predictions")

0 comments on commit 91d2b20

Please sign in to comment.