AiSc 2

Pasted Nov 4, 2026, 3:46 PM1 file30 lines

Scan to open on your phone

Point a camera at the QR code - it opens this exact paste, no app needed.

https://codepastes.com/ai2
untitled
import numpy as np

# Inputs for AND gate and corresponding targets
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.array([0, 0, 0, 1])

# Initialize random weights, bias, and learning rate
weights = np.array([0.0, 0.0])
bias = 0.0
lr = 0.1

# Training loop over 10 epochs
for epoch in range(10):
    for inputs, target in zip(X, y):
        # Step function activation
        linear_combination = np.dot(inputs, weights) + bias
        prediction = 1 if linear_combination >= 0 else 0

        # Update weights and bias based on error
        error = target - prediction
        weights += lr * error * inputs
        bias += lr * error

# Test final trained output
print("Trained Weights:", weights, "Bias:", bias)
print("Predictions for AND gate:")

for inputs in X:
    pred = 1 if (np.dot(inputs, weights) + bias) >= 0 else 0
    print(f"Input: {inputs} -> Predicted Output: {pred}")

894 chars · codepastes.com