← Back to archive
Programming Notes

Modeling the Five Elements' Relationships and Reverse-Solving with Backpropagation

术数编程
import numpy as np

# Generation and control relationship matrix
matrix = np.array([
    [0, 1, 0, 0, -1],
    [-1, 0, 1, 0, 0],
    [0, -1, 0, 1, 0],
    [0, 0, -1, 0, 1],
    [1, 0, 0, -1, 0]
])

# Interaction strength between each element
weights = np.random.randn(5, 5)

# Define the model function
def model(theta):
    # Convert the one-dimensional parameters into a two-dimensional matrix
    w1 = theta[:25].reshape(5, 5)
    b1 = theta[25:30]
    # Compute the interaction strength between each element
    hidden = np.matmul(matrix, w1) + b1
    # Apply a nonlinear transformation with the sigmoid function
    output = 1 / (1 + np.exp(-hidden))
    return output

# Define the loss function
def loss(theta, data):
    X, y = data
    y_pred = model(theta)
    return np.mean((y - y_pred) ** 2)

# Define the backpropagation function
def backprop(theta, data):
    X, y = data
    # Convert the one-dimensional parameters into a two-dimensional matrix
    w1 = theta[:25].reshape(5, 5)
    b1 = theta[25:30]
    # Compute the interaction strength between each element
    hidden = np.matmul(matrix, w1) + b1
    # Apply a nonlinear transformation with the sigmoid function
    output = 1 / (1 + np.exp(-hidden))
    # Compute the error
    error = y - output
    # Compute the gradients
    d_output = error * output * (1 - output)
    d_hidden = np.matmul(d_output, w1.T)
    dw1 = np.matmul(matrix.T, d_hidden * hidden * (1 - hidden))
    db1 = np.sum(d_hidden * hidden * (1 - hidden), axis=0)
    d_theta = np.concatenate((dw1.ravel(), db1))
    return d_theta

# Generate training data
X = np.random.randn(100, 5)
y = model(weights)

# Adjust parameters with the BP algorithm
theta = np.concatenate((weights.ravel(), np.zeros(5)))
lr = 0.1
for i in range(1000):
    d_theta = backprop(theta, (X, y))
    theta -= lr * d

Written by Master Sanfu on April 2, 2023. Please credit the source if you share.

Translation Notice: This English version was translated with AI assistance. Specialized, historical, religious, or culturally sensitive terms may contain nuances, inaccuracies, or debatable wording. In case of ambiguity or discrepancy, the original Chinese text shall prevail.