Artificial Intelligence
Simple Neural Network
neural_network.spdl
# Simple Neural Network in Spindle
# This demonstrates basic AI concepts using Spindle's capabilities
# Initialize weights for a simple XOR neural network
weights1 <- [0.5, 0.3, -0.2, 0.1, -0.4, 0.6, 0.2, -0.3]
weights2 <- [0.4, -0.5, 0.3, 0.1]
# Sigmoid activation function
PROCEDURE sigmoid(x) {
# Simple approximation of sigmoid function
IF (x > 0) {
result <- 1
} ELSE {
result <- 0
}
RETURN result
}
# Forward pass through the network
PROCEDURE forward_pass(input1, input2) {
# Hidden layer calculations
hidden1 <- (input1 * weights1[1]) + (input2 * weights1[2])
hidden2 <- (input1 * weights1[3]) + (input2 * weights1[4])
hidden3 <- (input1 * weights1[5]) + (input2 * weights1[6])
hidden4 <- (input1 * weights1[7]) + (input2 * weights1[8])
# Apply activation function
hidden1 <- sigmoid(hidden1)
hidden2 <- sigmoid(hidden2)
hidden3 <- sigmoid(hidden3)
hidden4 <- sigmoid(hidden4)
# Output layer
output <- (hidden1 * weights2[1]) + (hidden2 * weights2[2]) +
(hidden3 * weights2[3]) + (hidden4 * weights2[4])
output <- sigmoid(output)
RETURN output
}
# Test XOR problem
DISPLAY("XOR Neural Network Results:")
# Test case 1: 0 XOR 0 = 0
result1 <- forward_pass(0, 0)
DISPLAY("Input: [0, 0], Output: ")
DISPLAY(result1)
# Test case 2: 0 XOR 1 = 1
result2 <- forward_pass(0, 1)
DISPLAY("Input: [0, 1], Output: ")
DISPLAY(result2)
# Test case 3: 1 XOR 0 = 1
result3 <- forward_pass(1, 0)
DISPLAY("Input: [1, 0], Output: ")
DISPLAY(result3)
# Test case 4: 1 XOR 1 = 0
result4 <- forward_pass(1, 1)
DISPLAY("Input: [1, 1], Output: ")
DISPLAY(result4)