Skip to content

Commit

Permalink
some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
a-mhamdi committed Dec 21, 2024
1 parent 45b4adb commit 527e0d2
Show file tree
Hide file tree
Showing 9 changed files with 432 additions and 3,841 deletions.
168 changes: 141 additions & 27 deletions Codes/Julia/Part-1/Jupyter/classifier-ann.ipynb

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
### A Pluto.jl notebook ###
# v0.20.3

#> [frontmatter]
#> title = "Binary Classifier using ANN"
#> tags = ["ann", "flux", "julialang"]
#> date = "2024-12-21"
#> description = "Customer Churn Modelling"
#>
#> [[frontmatter.author]]
#> name = "A. Mhamdi"
#> url = "https://a-mhamdi.github.io/jlai/"

using Markdown
using InteractiveUtils

Expand Down Expand Up @@ -58,7 +68,7 @@ md"Import the required librairies"
md"Hyperparameters tuning"

# ╔═╡ 486ac35e-5a6e-4f7d-8bfd-3d00782eeffb
md"Load data for csv file"
md"Load data from csv file"

# ╔═╡ 0d137d2c-db4f-4ec1-8290-9f621f956ee4
df = CSV.read("../../Datasets/Churn_Modelling.csv", DataFrame)
Expand All @@ -67,8 +77,10 @@ df = CSV.read("../../Datasets/Churn_Modelling.csv", DataFrame)
md"Choose the target vector `y`"

# ╔═╡ 601b31ca-be6f-4b7d-bbb3-72c5dc4760f9
ydf = select(df, :Exited)
#coerce!(ydf, :Exited => Multiclass)
begin
ydf = select(df, :Exited)
# coerce!(ydf, :Exited => OrderedFactor)
end

# ╔═╡ 27c7a2c0-ebd7-41bb-8a89-8a5d2c8d1300
y_ = ydf.Exited
Expand Down Expand Up @@ -121,7 +133,7 @@ end
md"Design the architecture of the classifier, denoted hereafter by `clf`"

# ╔═╡ e2e6cc50-d02c-4bc9-8095-479207efa7bb
clf = Chain(
mdl = Chain(
Dense( 11 => 8, relu ),
Dense( 8 => 8, relu ),
Dense( 8 => 8, relu ),
Expand All @@ -132,10 +144,10 @@ clf = Chain(
md"Permute dims: ROW => features and COL => observation"

# ╔═╡ b0bdb22e-8196-4046-ad6a-317d1ef2d879
X = X_'; # permutedims(X)
X = permutedims(X_)

# ╔═╡ 2d134795-76f9-4807-92fa-203192d34b48
y = y_'; # permutedims(y)
# ╔═╡ 567a79d9-a0a3-4142-864e-207d3f011895
y = permutedims(y_)

# ╔═╡ e2df135a-8ce2-462a-8a25-402de6c5cd26
md"Optimizers and data loader"
Expand All @@ -147,10 +159,7 @@ md"Optimizers and data loader"
opt = Flux.Adam(η);

# ╔═╡ 090e9957-7f97-49f7-89b5-efe39b8f94f3
state = Flux.setup(opt, clf);

# ╔═╡ 01d71ad5-e013-4dc1-bd31-0521a9e9655b
η_, epochs_, batchsize_ = .001, 1_00, 64
state = Flux.setup(opt, mdl);

# ╔═╡ 19752951-1314-4df4-98d2-407d4a8c6e72
@bind batchsize Slider(2:8:128, default=32)
Expand All @@ -162,18 +171,18 @@ loader = Flux.DataLoader((X, y); batchsize=batchsize, shuffle=true);
md"**Training phase**"

# ╔═╡ f83759e2-eed2-46cc-95fc-c974e6af1f04
@bind epochs Slider(1:2:32, default=4)
@bind epochs Slider(1:8:128, default=64)

# ╔═╡ 1d0f0991-6709-4d1b-b316-3a4cf52cfdb7
begin
vec_loss = []
@showprogress for _ in 1:epochs
for (X, y) in loader
loss, grads = Flux.withgradient(clf) do mdl
ŷ_ = mdl(X);
Flux.Losses.logitbinarycrossentropy(ŷ_, y);
@showprogress for epoch in 1:epochs
for (mb_X, mb_y) in loader
loss, grads = Flux.withgradient(mdl) do m
mb_ŷ = m(mb_X);
Flux.logitbinarycrossentropy(mb_ŷ, mb_y);
end
Flux.update!(state, clf, grads[1]); # Upd `W` and `b`
Flux.update!(state, mdl, grads[1]); # Upd `W` and `b`
push!(vec_loss, loss); # Log `loss` to the vector `vec_loss`
end
end
Expand All @@ -189,46 +198,86 @@ plot(vec_loss, label="Loss")
extrema(vec_loss)

# ╔═╡ f567f2ec-225c-4ab2-a9d7-e0833b972868
md"Some metrics"
md"**Some metrics**"

# ╔═╡ 710d5efb-3c20-4655-a3cd-2490a80133c1
ŷ_ = clf(X) |> σ;
ŷ_ = mdl(X) |> σ;

# ╔═╡ 1da45b0e-c430-4f35-8a6a-2a6a2bf0afc6
= (ŷ_ .≥ .5);

# ╔═╡ c8d291ea-8505-4304-9bb5-6177f01a5c43
md"Basic way to compute the accuracy"
md"_Basic way to compute the accuracy_"

# ╔═╡ 086e0922-608a-4449-86e4-e86b32d20f41
accuracy = mean( ŷ .== y )

# ╔═╡ b71d3fc7-29be-49f9-994a-7371d03e81fc
md"Confusion Matrix"
md"_Confusion Matrix_"

# ╔═╡ de87da99-5d60-4b60-b1bd-c89d18ba6bbd
displayed_cm = MLJ.ConfusionMatrix(levels=[1, 0])(ŷ, y)
displayed_cm = MLJ.ConfusionMatrix(levels=[0, 1])(ŷ, y)

# ╔═╡ b578d4cb-eca6-4098-91c9-148ab6dbf52e
cm = ConfusionMatrices.matrix(displayed_cm)

# ╔═╡ 6cff22b5-1e50-4d8b-ab3c-1b92ba1063c6
md"Other metrics"
md"_Classification metrics_"

# ╔═╡ a9ba36ff-0321-4cb9-a7af-0cbef12554ce
TP, TN, FP, FN = cm[2, 2], cm[1, 1], cm[2, 1], cm[1, 2];

# ╔═╡ e3487d37-edb3-46a2-909f-4e294626bae1
md"_Accuracy_"

# ╔═╡ 72c1d033-42a9-4ad4-814b-4d5c1d0d375b
accuracy_ = (TP+TN)/(TP+TN+FP+FN) # MLJ.accuracy(cm)
accuracy_ = (TP+TN)/(TP+TN+FP+FN)

# ╔═╡ 0bb0694a-d4e2-4c15-a24d-d073a9bc514c
MLJ.accuracy(ŷ, y)

# ╔═╡ 0fb71566-73ee-4096-98b1-bfb7917f494d
md"_True Negative Rate_"

# ╔═╡ ba2efbb6-0029-4fee-b216-d537bd32b60e
precision_ = TP/(TP+FP) # MLJ.precision(cm)
true_negative_rate_ = TN/(TN+FP)

# ╔═╡ f45cc9db-03aa-46e4-be87-1af83a3b958d
MLJ.true_negative_rate(ŷ, y)

# ╔═╡ 862a1e4c-0e68-468d-9149-94158926285a
md"_True Positive Rate_"

# ╔═╡ 5053c71a-9236-4723-8dd3-d2fe2180c357
recall_ = TP/(TP+FN) # MLJ.recall(cm)
true_positive_rate_ = TP/(TP+FN)

# ╔═╡ 8883aedb-7e8e-482d-9225-5ba3fd6794b1
MLJ.true_positive_rate(ŷ, y)

# ╔═╡ 25b29473-507a-4d7f-9a5e-8267e7acbdb6
md"_f1-score_"

# ╔═╡ f46fcd55-6630-4ac3-b86b-316db204bbdf
begin
precision_ = TP/(TP+FP)
recall_ = TP/(TP+FN)
f1score_ = 2/(1/precision_ + 1/recall_)
end

# ╔═╡ b6dd27d1-5f67-4707-ac74-f31852957add
f1score_ = 2/(1/precision_ + 1/recall_) # MLJ.f1score(cm)
# ╔═╡ 9748255a-b13f-4cfe-b9a7-400a15ead1f5
MLJ.f1score(ŷ, y)

# ╔═╡ 90100f84-e006-4915-9e82-c849c3d91351
html"""
<style>
main {
margin: 0 auto;
max-width: 2000px;
padding-left: max(160px, 10%);
padding-right: max(160px, 10%);
}
</style>
"""

# ╔═╡ Cell order:
# ╠═891eda37-95d5-46dd-a642-79e68e7bb322
Expand Down Expand Up @@ -259,13 +308,12 @@ f1score_ = 2/(1/precision_ + 1/recall_) # MLJ.f1score(cm)
# ╠═e2e6cc50-d02c-4bc9-8095-479207efa7bb
# ╠═9f737b1c-fe5a-4ec9-a47e-a05c44c15a35
# ╠═b0bdb22e-8196-4046-ad6a-317d1ef2d879
# ╠═2d134795-76f9-4807-92fa-203192d34b48
# ╠═567a79d9-a0a3-4142-864e-207d3f011895
# ╠═e2df135a-8ce2-462a-8a25-402de6c5cd26
# ╠═d0a6e9c5-1a39-4edc-bb67-56b95d045052
# ╠═55ecb9c4-8339-4690-84da-25407fdf4219
# ╠═2dddfc11-9907-4e5a-b949-ef1f4e27dfac
# ╠═090e9957-7f97-49f7-89b5-efe39b8f94f3
# ╠═01d71ad5-e013-4dc1-bd31-0521a9e9655b
# ╠═19752951-1314-4df4-98d2-407d4a8c6e72
# ╠═1b2c6fe5-ec88-4a54-88d0-f3bde759d8da
# ╠═348b2e02-a9c4-4ac7-b34a-641a218ed69d
Expand All @@ -286,7 +334,16 @@ f1score_ = 2/(1/precision_ + 1/recall_) # MLJ.f1score(cm)
# ╠═b578d4cb-eca6-4098-91c9-148ab6dbf52e
# ╠═6cff22b5-1e50-4d8b-ab3c-1b92ba1063c6
# ╠═a9ba36ff-0321-4cb9-a7af-0cbef12554ce
# ╠═e3487d37-edb3-46a2-909f-4e294626bae1
# ╠═72c1d033-42a9-4ad4-814b-4d5c1d0d375b
# ╠═0bb0694a-d4e2-4c15-a24d-d073a9bc514c
# ╠═0fb71566-73ee-4096-98b1-bfb7917f494d
# ╠═ba2efbb6-0029-4fee-b216-d537bd32b60e
# ╠═f45cc9db-03aa-46e4-be87-1af83a3b958d
# ╠═862a1e4c-0e68-468d-9149-94158926285a
# ╠═5053c71a-9236-4723-8dd3-d2fe2180c357
# ╠═b6dd27d1-5f67-4707-ac74-f31852957add
# ╠═8883aedb-7e8e-482d-9225-5ba3fd6794b1
# ╠═25b29473-507a-4d7f-9a5e-8267e7acbdb6
# ╠═f46fcd55-6630-4ac3-b86b-316db204bbdf
# ╠═9748255a-b13f-4cfe-b9a7-400a15ead1f5
# ╟─90100f84-e006-4915-9e82-c849c3d91351
23 changes: 23 additions & 0 deletions Codes/Julia/Part-1/Pluto/selection-process.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
### A Pluto.jl notebook ###
# v0.20.3

#> [frontmatter]
#> title = "Selection Process"
#> tags = ["fuzzy", "julialang"]
#> date = "2024-12-21"
#> description = "Fuzzy Decision Maker"
#>
#> [[frontmatter.author]]
#> name = "A. Mhamdi"
#> url = "https://a-mhamdi.github.io/jlai/#"

using Markdown
using InteractiveUtils

Expand Down Expand Up @@ -184,6 +194,18 @@ test_in = [9., 5.]
# ╔═╡ 3be1e889-8d70-4620-be13-0c0b3b59ee2e
eval_fis(fis, test_in)

# ╔═╡ 416fbb5d-e96f-4f80-98bd-68bbe820fd76
html"""
<style>
main {
margin: 0 auto;
max-width: 2000px;
padding-left: max(160px, 10%);
padding-right: max(160px, 10%);
}
</style>
"""

# ╔═╡ Cell order:
# ╠═65c4f4fb-cfa6-42b2-b74d-a160e3ea920f
# ╠═76c90b07-36d5-4ab8-95f3-1cbb02d5477d
Expand Down Expand Up @@ -219,3 +241,4 @@ eval_fis(fis, test_in)
# ╠═9089e155-dbba-490c-9ddf-5f3a76270cbe
# ╠═bbc2a4c3-7095-437f-b59d-c99bfb46759e
# ╠═3be1e889-8d70-4620-be13-0c0b3b59ee2e
# ╟─416fbb5d-e96f-4f80-98bd-68bbe820fd76
23 changes: 23 additions & 0 deletions Codes/Julia/Part-1/Pluto/tipper.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
### A Pluto.jl notebook ###
# v0.20.3

#> [frontmatter]
#> title = "Tipper Problem"
#> tags = ["Fuzzy", "julialang"]
#> date = "2024-12-21"
#> description = "Fuzzy Inference System"
#>
#> [[frontmatter.author]]
#> name = "A. Mhamdi"
#> url = "https://a-mhamdi.github.io/jlai/#"

using Markdown
using InteractiveUtils

Expand Down Expand Up @@ -118,6 +128,18 @@ fis = FISMamdani([food, service], tip, rules)
# ╔═╡ 411ae0e5-4c42-4e4e-a9dd-99ed072ed85a
eval_fis(fis, [9., 8.])

# ╔═╡ 9e7f7930-7ebb-4cd3-b299-b09d0a817f13
html"""
<style>
main {
margin: 0 auto;
max-width: 2000px;
padding-left: max(160px, 10%);
padding-right: max(160px, 10%);
}
</style>
"""

# ╔═╡ Cell order:
# ╠═264261bf-4e21-4510-a106-4854cbb30c1c
# ╠═ec6eaa3e-9c69-4dc1-bc86-ae44b1977d93
Expand Down Expand Up @@ -150,3 +172,4 @@ eval_fis(fis, [9., 8.])
# ╠═d46fa358-3f45-4958-a540-cf8b8337bb7b
# ╠═dfe56972-c831-4e73-b19d-dda42bbb45c8
# ╠═411ae0e5-4c42-4e4e-a9dd-99ed072ed85a
# ╟─9e7f7930-7ebb-4cd3-b299-b09d0a817f13
29 changes: 26 additions & 3 deletions Codes/Julia/Part-1/Pluto/xor-gate.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
### A Pluto.jl notebook ###
# v0.20.3

#> [frontmatter]
#> title = "XOR GATE"
#> date = "2024-12-20"
#> tags = ["xor", "ann", "julialan", "pluto"]
#> description = "Build a typical `XOR` gate using artificial neural net."
#>
#> [[frontmatter.author]]
#> name = "A. Mhamdi"
#> url = "https://a-mhamdi.github.io/jlai/"

using Markdown
using InteractiveUtils

Expand Down Expand Up @@ -74,7 +84,7 @@ md"`mdl` is the model to be built"

# ╔═╡ 0acd627b-cf06-464b-98e1-acf65fbe867e
mdl = Chain(Dense( 2 => 4, tanh ),
Dense( 4 => 4, tanh ),
Dense( 4 => 4, tanh ),
Dense( 4 => 1, σ ),
)

Expand All @@ -88,7 +98,7 @@ y_raw = mdl(X)
md"`opt` designates the optimizer"

# ╔═╡ a7a2cf85-1a98-4408-ae68-faa3ad078474
@bind η Slider(.0001:0.01:.1, default=.001)
@bind η Slider(.001:0.001:.01, default=.003)

# ╔═╡ 185b5a65-904a-402d-9c4d-10cb917704f0
opt = Adam(η)
Expand All @@ -103,7 +113,7 @@ state = Flux.setup(opt, mdl)
md"**TRAINING PHASE**"

# ╔═╡ f4d384bb-9dfe-4839-99ca-60af08c1f475
@bind epochs Slider(1:2:64, default=4)
@bind epochs Slider(1:2:16, default=4)

# ╔═╡ fec114ea-9fd1-44a1-8512-854eef73cc1f
begin
Expand Down Expand Up @@ -159,6 +169,18 @@ md"Plot of both ground truth and results after training"
# ╔═╡ 7491441d-19aa-49aa-a692-628d73a96611
plot(sc1, sc3, layout=(1,2), size=(512,512))

# ╔═╡ 6e6c6526-c067-4c66-8cf0-9ffe339ebf0a
html"""
<style>
main {
margin: 0 auto;
max-width: 2000px;
padding-left: max(160px, 10%);
padding-right: max(160px, 10%);
}
</style>
"""

# ╔═╡ Cell order:
# ╠═3d94a9b8-c4ac-446f-b3c4-fec5be847f18
# ╠═067f56ad-4a5a-4272-aaa3-4a4bc00fcb4c
Expand Down Expand Up @@ -197,3 +219,4 @@ plot(sc1, sc3, layout=(1,2), size=(512,512))
# ╠═189fbddb-1208-40e3-83d7-afe29b7c4dc1
# ╠═65404f3d-4dd5-43bf-a90f-71a34182cfa8
# ╠═7491441d-19aa-49aa-a692-628d73a96611
# ╟─6e6c6526-c067-4c66-8cf0-9ffe339ebf0a
Loading

0 comments on commit 527e0d2

Please sign in to comment.