此深度神经网络模型可以计算0到9的加法,准确率达到惊人的100%。排行榜第一名,独孤求败(hhhh
可惜Acwing没np这个模块
线性回归版本link
线性回归减法器link
import numpy as np
class SingleLayerMLP:
def __init__(self):
#self.W = np.ones((10, 1)) #np.random.randn(14, 1) # 14 inputs to 1 output node
#self.W = np.arange(20).reshape((20, 1)) 0, 1...19
base_array = np.arange(10) ## 这里可以改进一下 10改成10**8即可AC acwing第一题。
self.W = np.tile(base_array, (2, 1)).reshape(-1, 1)
self.b = np.zeros(1)
def relu(self, x):
return np.maximum(0, x)
def relu_derivative(self, x):
return np.where(x > 0, 1, 0)
def forward(self, x):
self.z = np.dot(x, self.W) + self.b
self.a = self.relu(self.z)
return self.a
def backward(self, x, y, output, learning_rate):
output_error = output - y
output_delta = output_error * self.relu_derivative(self.z)
self.W -= learning_rate * np.dot(x.T, output_delta)
self.b -= learning_rate * np.sum(output_delta, axis=0)
def train(self, X, y, epochs, learning_rate):
for epoch in range(epochs):
output = self.forward(X)
self.backward(X, y, output, learning_rate)
if epoch % 100 == 0:
loss = np.mean((output - y) ** 2)
print(f"Epoch {epoch}, Loss: {loss}")
def predict(self, x):
return self.forward(x)
# One-hot encode inputs
def one_hot_encode(a, b):
x = np.zeros(20) #这里可以改进一下 20改成2*10**8即可AC acwing第一题。
x[a] = 1
x[b+10] = 1
''' 由于只是做加法,所以操作符不用了
if op == '+':
x[10] = 1
elif op == '-':
x[11] = 1
elif op == '*':
x[12] = 1
elif op == '/':
x[13] = 1
'''
return x
# Generate training data 由于一步到位所以训练数据也不需要了
def generate_data(num_samples=100, max_value=10):
data = []
for _ in range(num_samples):
a = np.random.randint(0, max_value)
b = np.random.randint(0, max_value)
op = np.random.choice(['+']) # , '-', '*', '/'
if op == '+':
result = a + b
elif op == '-':
result = a - b
elif op == '*':
result = a * b
elif op == '/':
if b == 0:
continue # Avoid division by zero
result = a / b
data.append((a, b, op, result))
return data
# 如果想用一点点学习参数可以用这个生成0,0,0到9,9,18的数据对。
def generate_addition_pairs(max_value=10):
data = []
for a in range(max_value):
for b in range(max_value):
#op = '+'
result = a + b
data.append((a, b, result))
return data
# Prepare training data
data = generate_addition_pairs() #generate_data()
X = np.array([one_hot_encode(a, b) for a, b, _ in data])
y = np.array([result for _, _, result in data]).reshape(-1, 1)
# Define and train the model 由于一眼法一步到位就不训练了
mlp_model = SingleLayerMLP()
#mlp_model.train(X, y, epochs=1, learning_rate=0.001)
# Example input
a = 1
b = 2
# op = '+' 不需要了
# One-hot encode the input
encoded_input = one_hot_encode(a, b).reshape(1, -1)
print("编码的输入向量Encoded Input:")
print(encoded_input)
# Feed the input to the model 将A和B输入模型
predicted_output = mlp_model.predict(encoded_input)
print(f"输入Input: {a} {'+'} {b} = \n输出Predicted Output: {predicted_output[0][0]}")
编码的输入向量Encoded Input:
[[0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0.]]
输入Input: 1 + 2 =
输出Predicted Output: 3.0