Skip to content

Instantly share code, notes, and snippets.

@CosmosKey
Created October 20, 2024 19:13
Show Gist options
  • Save CosmosKey/0b7dc2eb19e6bccc8b5cc647d7ea7d1c to your computer and use it in GitHub Desktop.
Save CosmosKey/0b7dc2eb19e6bccc8b5cc647d7ea7d1c to your computer and use it in GitHub Desktop.
Neural net training and prediction sample code
# ChatGPT generated code.
# TIO.RUN example: https://tinyurl.com/33erc3yu
# Define fixed weights and biases for reproducibility
# Weights for Input Layer to Hidden Layer (W1)
# W1 is a 4x4 matrix
$W1 = @(
@(0.2, -0.5, 0.1, 2.0), # Weights for Hidden Neuron 1
@(1.5, 1.3, -1.2, 0.7), # Weights for Hidden Neuron 2
@(0.3, 0.8, -0.6, -0.1), # Weights for Hidden Neuron 3
@(-1.0, 2.0, 1.0, -0.5) # Weights for Hidden Neuron 4
)
# Biases for Hidden Layer (b1)
$b1 = @(0.5, -1.5, 2.0, 0.0)
# Weights for Hidden Layer to Output Layer (W2)
# W2 is a 1x4 matrix (since there is 1 output neuron and 4 hidden neurons)
$W2 = @(1.2, -0.7, 0.3, 0.8)
# Bias for Output Layer (b2)
$b2 = 1.0
# Sigmoid activation function
function Sigmoid {
param (
[double]$z
)
return 1 / (1 + [math]::Exp(-$z))
}
# Derivative of sigmoid function
function Sigmoid-Derivative {
param (
[double]$sigmoid
)
return $sigmoid * (1 - $sigmoid)
}
# Define the simplified Breast Cancer Wisconsin dataset
# Each entry has: Clump Thickness, Uniformity of Cell Size, Uniformity of Cell Shape, Marginal Adhesion, Label (1=Malignant, 0=Benign)
$dataset = @(
@{Features = @(5, 1, 1, 1); Label = 1},
@{Features = @(4, 1, 1, 1); Label = 0},
@{Features = @(6, 4, 4, 5); Label = 1},
@{Features = @(4, 1, 1, 1); Label = 0},
@{Features = @(5, 2, 2, 2); Label = 1},
@{Features = @(3, 1, 1, 1); Label = 0},
@{Features = @(7, 4, 4, 6); Label = 1},
@{Features = @(2, 1, 1, 1); Label = 0},
@{Features = @(4, 3, 3, 3); Label = 1},
@{Features = @(3, 1, 1, 1); Label = 0},
@{Features = @(5, 2, 2, 2); Label = 1},
@{Features = @(4, 1, 1, 1); Label = 0},
@{Features = @(6, 4, 4, 5); Label = 1},
@{Features = @(4, 1, 1, 1); Label = 0},
@{Features = @(5, 2, 2, 2); Label = 1},
@{Features = @(3, 1, 1, 1); Label = 0},
@{Features = @(7, 4, 4, 6); Label = 1},
@{Features = @(2, 1, 1, 1); Label = 0},
@{Features = @(4, 3, 3, 3); Label = 1},
@{Features = @(3, 1, 1, 1); Label = 0},
@{Features = @(5, 1, 1, 2); Label = 1},
@{Features = @(4, 1, 1, 1); Label = 0},
@{Features = @(6, 5, 4, 5); Label = 1},
@{Features = @(4, 1, 1, 1); Label = 0},
@{Features = @(5, 2, 2, 2); Label = 1},
@{Features = @(3, 1, 1, 1); Label = 0},
@{Features = @(7, 4, 4, 6); Label = 1},
@{Features = @(2, 1, 1, 1); Label = 0},
@{Features = @(4, 3, 3, 3); Label = 1},
@{Features = @(3, 1, 1, 1); Label = 0},
@{Features = @(5, 1, 1, 1); Label = 1},
@{Features = @(4, 1, 1, 1); Label = 0},
@{Features = @(6, 4, 4, 5); Label = 1},
@{Features = @(4, 1, 1, 1); Label = 0},
@{Features = @(5, 2, 2, 2); Label = 1},
@{Features = @(3, 1, 1, 1); Label = 0},
@{Features = @(7, 4, 4, 6); Label = 1},
@{Features = @(2, 1, 1, 1); Label = 0},
@{Features = @(4, 3, 3, 3); Label = 1},
@{Features = @(3, 1, 1, 1); Label = 0},
@{Features = @(5, 2, 2, 2); Label = 1},
@{Features = @(4, 1, 1, 1); Label = 0},
@{Features = @(6, 4, 4, 5); Label = 1},
@{Features = @(4, 1, 1, 1); Label = 0},
@{Features = @(5, 2, 2, 2); Label = 1},
@{Features = @(3, 1, 1, 1); Label = 0},
@{Features = @(7, 4, 4, 6); Label = 1},
@{Features = @(2, 1, 1, 1); Label = 0},
@{Features = @(4, 3, 3, 3); Label = 1},
@{Features = @(3, 1, 1, 1); Label = 0},
@{Features = @(5, 1, 1, 1); Label = 1},
@{Features = @(4, 1, 1, 1); Label = 0},
@{Features = @(6, 4, 4, 5); Label = 1},
@{Features = @(4, 1, 1, 1); Label = 0},
@{Features = @(5, 2, 2, 2); Label = 1},
@{Features = @(3, 1, 1, 1); Label = 0},
@{Features = @(7, 4, 4, 6); Label = 1},
@{Features = @(2, 1, 1, 1); Label = 0},
@{Features = @(4, 3, 3, 3); Label = 1},
@{Features = @(3, 1, 1, 1); Label = 0},
@{Features = @(5, 1, 1, 1); Label = 1},
@{Features = @(4, 1, 1, 1); Label = 0},
@{Features = @(6, 4, 4, 5); Label = 1},
@{Features = @(4, 1, 1, 1); Label = 0},
@{Features = @(5, 2, 2, 2); Label = 1},
@{Features = @(3, 1, 1, 1); Label = 0},
@{Features = @(7, 4, 4, 6); Label = 1},
@{Features = @(2, 1, 1, 1); Label = 0},
@{Features = @(4, 3, 3, 3); Label = 1},
@{Features = @(3, 1, 1, 1); Label = 0},
@{Features = @(5, 1, 1, 1); Label = 1},
@{Features = @(4, 1, 1, 1); Label = 0},
@{Features = @(6, 4, 4, 5); Label = 1},
@{Features = @(4, 1, 1, 1); Label = 0},
@{Features = @(5, 2, 2, 2); Label = 1},
@{Features = @(3, 1, 1, 1); Label = 0},
@{Features = @(7, 4, 4, 6); Label = 1},
@{Features = @(2, 1, 1, 1); Label = 0},
@{Features = @(4, 3, 3, 3); Label = 1},
@{Features = @(3, 1, 1, 1); Label = 0},
@{Features = @(5, 1, 1, 1); Label = 1},
@{Features = @(4, 1, 1, 1); Label = 0},
@{Features = @(6, 4, 4, 5); Label = 1},
@{Features = @(4, 1, 1, 1); Label = 0},
@{Features = @(5, 2, 2, 2); Label = 1},
@{Features = @(3, 1, 1, 1); Label = 0},
@{Features = @(7, 4, 4, 6); Label = 1},
@{Features = @(2, 1, 1, 1); Label = 0},
@{Features = @(4, 3, 3, 3); Label = 1},
@{Features = @(3, 1, 1, 1); Label = 0},
@{Features = @(5, 1, 1, 1); Label = 1},
@{Features = @(4, 1, 1, 1); Label = 0},
@{Features = @(6, 4, 4, 5); Label = 1},
@{Features = @(4, 1, 1, 1); Label = 0},
@{Features = @(5, 2, 2, 2); Label = 1},
@{Features = @(3, 1, 1, 1); Label = 0},
@{Features = @(7, 4, 4, 6); Label = 1},
@{Features = @(2, 1, 1, 1); Label = 0},
@{Features = @(4, 3, 3, 3); Label = 1},
@{Features = @(3, 1, 1, 1); Label = 0},
@{Features = @(5, 1, 1, 1); Label = 1},
@{Features = @(4, 1, 1, 1); Label = 0},
@{Features = @(6, 4, 4, 5); Label = 1},
@{Features = @(4, 1, 1, 1); Label = 0},
@{Features = @(5, 2, 2, 2); Label = 1},
@{Features = @(3, 1, 1, 1); Label = 0},
@{Features = @(7, 4, 4, 6); Label = 1},
@{Features = @(2, 1, 1, 1); Label = 0},
@{Features = @(4, 3, 3, 3); Label = 1},
@{Features = @(3, 1, 1, 1); Label = 0},
@{Features = @(5, 1, 1, 1); Label = 1},
@{Features = @(4, 1, 1, 1); Label = 0},
@{Features = @(6, 4, 4, 5); Label = 1},
@{Features = @(4, 1, 1, 1); Label = 0},
@{Features = @(5, 2, 2, 2); Label = 1},
@{Features = @(3, 1, 1, 1); Label = 0},
@{Features = @(7, 4, 4, 6); Label = 1},
@{Features = @(2, 1, 1, 1); Label = 0},
@{Features = @(4, 3, 3, 3); Label = 1},
@{Features = @(3, 1, 1, 1); Label = 0},
@{Features = @(5, 1, 1, 1); Label = 1},
@{Features = @(4, 1, 1, 1); Label = 0},
@{Features = @(6, 4, 4, 5); Label = 1},
@{Features = @(4, 1, 1, 1); Label = 0},
@{Features = @(5, 2, 2, 2); Label = 1},
@{Features = @(3, 1, 1, 1); Label = 0},
@{Features = @(7, 4, 4, 6); Label = 1},
@{Features = @(2, 1, 1, 1); Label = 0},
@{Features = @(4, 3, 3, 3); Label = 1},
@{Features = @(3, 1, 1, 1); Label = 0},
@{Features = @(5, 1, 1, 1); Label = 1},
@{Features = @(4, 1, 1, 1); Label = 0},
@{Features = @(6, 4, 4, 5); Label = 1},
@{Features = @(4, 1, 1, 1); Label = 0},
@{Features = @(5, 2, 2, 2); Label = 1},
@{Features = @(3, 1, 1, 1); Label = 0},
@{Features = @(7, 4, 4, 6); Label = 1},
@{Features = @(2, 1, 1, 1); Label = 0},
@{Features = @(4, 3, 3, 3); Label = 1},
@{Features = @(3, 1, 1, 1); Label = 0},
@{Features = @(5, 1, 1, 1); Label = 1},
@{Features = @(4, 1, 1, 1); Label = 0},
@{Features = @(6, 4, 4, 5); Label = 1},
@{Features = @(4, 1, 1, 1); Label = 0},
@{Features = @(5, 2, 2, 2); Label = 1},
@{Features = @(3, 1, 1, 1); Label = 0},
@{Features = @(7, 4, 4, 6); Label = 1},
@{Features = @(2, 1, 1, 1); Label = 0},
@{Features = @(4, 3, 3, 3); Label = 1},
@{Features = @(3, 1, 1, 1); Label = 0}
)
# $dataset = $dataset | Select -first 20
# Shuffle the dataset to ensure randomness
$shuffled = $dataset | Get-Random -Count $dataset.Count
Write-Host "Dataset count: $($dataset.Count)"
# Split the dataset into 80% training and 20% testing
$trainSize = [math]::Floor(0.8 * $shuffled.Count)
$trainingSet = $shuffled[0..($trainSize - 1)]
$testSet = $shuffled[$trainSize..($shuffled.Count - 1)]
Write-Output "Training Set ($($trainingSet.Count) examples):"
$trainingSet | ForEach-Object { "$($_.Features) , Label: $($_.Label)" }
Write-Output "`nTest Set ($($testSet.Count) examples):"
$testSet | ForEach-Object { "$($_.Features) , Label: $($_.Label)" }
# Hyperparameters
$learningRate = 0.1
$epochs = 200
# Training loop
for ($epoch = 1; $epoch -le $epochs; $epoch++) {
foreach ($data in $trainingSet) {
# Forward Pass - Hidden Layer
$z1 = @()
for ($i = 0; $i -lt 4; $i++) {
$sum = 0
for ($j = 0; $j -lt 4; $j++) {
$sum += $W1[$i][$j] * $data.Features[$j]
}
$sum += $b1[$i]
$z1 += $sum
}
# Activation for Hidden Layer
$a1 = @()
foreach ($z in $z1) {
$a1 += Sigmoid $z
}
# Forward Pass - Output Layer
$z2 = 0
for ($i = 0; $i -lt 4; $i++) {
$z2 += $W2[$i] * $a1[$i]
}
$z2 += $b2
$a2 = Sigmoid $z2
# Compute Error
$y = $data.Label
$predictionError = $y - $a2
# Backward Pass - Output Layer
$delta2 = $predictionError * (Sigmoid-Derivative $a2)
# Backward Pass - Hidden Layer
$delta1 = @(0, 0, 0, 0)
for ($i = 0; $i -lt 4; $i++) {
$delta1[$i] = $delta2 * $W2[$i] * (Sigmoid-Derivative $a1[$i])
}
# Update Weights and Biases - Output Layer
for ($i = 0; $i -lt 4; $i++) {
$W2[$i] += $learningRate * $delta2 * $a1[$i]
}
$b2 += $learningRate * $delta2
# Update Weights and Biases - Hidden Layer
for ($i = 0; $i -lt 4; $i++) {
for ($j = 0; $j -lt 4; $j++) {
$W1[$i][$j] += $learningRate * $delta1[$i] * $data.Features[$j]
}
$b1[$i] += $learningRate * $delta1[$i]
}
}
# Optional: Display loss every 10 epochs
if ($epoch % 10 -eq 0) {
$loss = 0
foreach ($data in $trainingSet) {
# Forward Pass - Hidden Layer
$z1 = @()
for ($i = 0; $i -lt 4; $i++) {
$sum = 0
for ($j = 0; $j -lt 4; $j++) {
$sum += $W1[$i][$j] * $data.Features[$j]
}
$sum += $b1[$i]
$z1 += $sum
}
# Activation for Hidden Layer
$a1 = @()
foreach ($z in $z1) {
$a1 += Sigmoid $z
}
# Forward Pass - Output Layer
$z2 = 0
for ($i = 0; $i -lt 4; $i++) {
$z2 += $W2[$i] * $a1[$i]
}
$z2 += $b2
$a2 = Sigmoid $z2
# Calculate Loss (Mean Squared Error)
$loss += [math]::Pow(($data.Label - $a2), 2)
}
$avgLoss = $loss / $trainingSet.Count
Write-Output "Epoch $epoch - Loss: $([math]::Round($avgLoss, 4))"
}
}
# Prediction on Test Set
$correct = 0
$total = $testSet.Count
Write-Output "`nPredictions on Test Set:"
foreach ($data in $testSet) {
# Forward Pass - Hidden Layer
$z1 = @()
for ($i = 0; $i -lt 4; $i++) {
$sum = 0
for ($j = 0; $j -lt 4; $j++) {
$sum += $W1[$i][$j] * $data.Features[$j]
}
$sum += $b1[$i]
$z1 += $sum
}
# Activation for Hidden Layer
$a1 = @()
foreach ($z in $z1) {
$a1 += Sigmoid $z
}
# Forward Pass - Output Layer
$z2 = 0
for ($i = 0; $i -lt 4; $i++) {
$z2 += $W2[$i] * $a1[$i]
}
$z2 += $b2
$a2 = Sigmoid $z2
# Classification (Threshold = 0.5)
$prediction = [int]($a2 -ge 0.5)
# Compare with Actual Label
if ($prediction -eq $data.Label) {
$correct++
}
Write-Output "Predicted: $prediction, Actual: $($data.Label)"
}
# Calculate Accuracy
$accuracy = ($correct / $total) * 100
Write-Output "`nAccuracy: $([math]::Round($accuracy, 2))%"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment