Nội dung text [Practice] Single-Input Neural Networks (without Activation Function)
THỰC HÀNH MẠNG NEURON TUYẾN TÍNH —------------------------------------------------------------------------------------------------------------------------ MẠNG NEURON: - Số input: 01 - Số layer: 01. - Số neuron / layer: 01 - Hàm kích hoạt: Không - Dữ liệu: tuyến tính —------------------------------------------------------------------------------------------------------------------------ import numpy as np class SingleNeuron: def __init__(self, w_init=0.5, b_init=0.0): self.w = w_init self.b = b_init def forward(self, x): return self.w * x + self.b def train(self, x_train, y_train, learning_rate=0.1, epochs=5): for epoch in range(epochs): total_error = 0 for x, y in zip(x_train, y_train): y_pred = self.forward(x) error = y_pred - y dw = error * x db = error self.w -= learning_rate * dw self.b -= learning_rate * db total_error += error ** 2 loss = total_error / len(y_train) if (epoch+1) % 1 == 0: print(f"Epoch {epoch+1}/{epochs}, Loss: {loss:.4f}") def predict(self, x): return self.forward(x)