# classification - logistic regression
# Generate random data for x_training with k=4 features and n=100 data points
x_training = np.random.rand(100, 4)
# Generate random data for y_training with 1 feature and n=100 data points
y_training = np.random.randint(2, size=100)
# iterative reweight least squre 방법을 사용하여 구하기
# Add a column of ones to x_training for the bias term (w0)
X = np.c_[np.ones(x_training.shape[0]), x_training]
# Set the number of iterations
num_iterations = 100
# Initialize the weights
w = np.zeros(X.shape[1])
# Perform iterative reweighted least squares
for i in range(num_iterations):
# Calculate the predictions
y_pred = 1 / (1 + np.exp(-X @ w))
# Calculate the weights
weights = y_pred * (1 - y_pred)
# Calculate the Hessian matrix
hessian = X.T @ (weights[:, np.newaxis] * X)
# conjugate gradient 를 사용하여 구하기
# Add a column of ones to x_training for the bias term (w0)
X = np.c_[np.ones(x_training.shape[0]), x_training]
# Initialize the weights
w = np.zeros(X.shape[1])
# Set the number of iterations
num_iterations = 100
# Set the tolerance
tol = 1e-6
# Perform conjugate gradient
for i in range(num_iterations):
# Calculate the predictions
y_pred = 1 / (1 + np.exp(-X @ w))
# Calculate the gradient
gradient = X.T @ (y_pred - y_training)
# Calculate the Hessian matrix
H = X.T @ (y_pred * (1 - y_pred) * X)
# Calculate the search direction
if i == 0:
d = -gradient
else:
beta = np.dot(gradient, gradient) / np.dot(gradient_old, gradient_old)
d = -gradient + beta * d
# Calculate the step size
alpha = -np.dot(gradient, d) / np.dot(d, H @ d)
# Update the weights
w = w + alpha * d
# Check for convergence
if np.linalg.norm(gradient) < tol:
break
# Store the gradient for the next iteration
gradient_old = gradient
# Newton's method 를 이용하여 구하기
# Initialize the weights
w = np.zeros(X.shape[1])
# Set the number of iterations
num_iterations = 100
# Set the tolerance
tol = 1e-6
# Perform Newton's method
for i in range(num_iterations):
# Calculate the predictions
y_pred = 1 / (1 + np.exp(-X @ w))
# Calculate the gradient
gradient = X.T @ (y_pred - y_training)
# Calculate the Hessian matrix
H = X.T @ (y_pred * (1 - y_pred) * X)
# Calculate the update
update = np.linalg.solve(H, -gradient)
# Update the weights
w = w + update
# Check for convergence
if np.linalg.norm(gradient) < tol:
break
# Calculate the predictions
y_pred = 1 / (1 + np.exp(-X @ w))
# Convert probabilities to binary predictions
y_pred_binary = (y_pred > 0.5).astype(int)
# AND gate 를 로지스틱 회귀모델로 학습하기
# Define the input data
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
# Define the output data
y = np.array([0, 0, 0, 1])
# Add a column of ones to X for the bias term
X = np.c_[np.ones(X.shape[0]), X]
# Initialize the weights
w = np.zeros(X.shape[1])
# Set the learning rate
learning_rate = 0.1
# Set the number of iterations
num_iterations = 1000
# Perform gradient descent
for i in range(num_iterations):
# Calculate the predictions
y_pred = 1 / (1 + np.exp(-X @ w))
# Calculate the error
error = y_pred - y
# Calculate the gradient
gradient = X.T @ error / len(y)
# Update the weights
w = w - learning_rate * gradient
# Calculate the predictions
y_pred = 1 / (1 + np.exp(-X @ w))
# Print the predictions
print(y_pred)
# Define the validation data
X_val = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
# Define the output data
y_val = np.array([0, 0, 0, 1])
# Add a column of ones to X_val for the bias term
X_val = np.c_[np.ones(X_val.shape[0]), X_val]
# Calculate the predictions for the validation data
y_pred_val = 1 / (1 + np.exp(-X_val @ w))
# Convert probabilities to binary predictions
y_pred_binary = (y_pred_val > 0.5).astype(int)
# Compare the predictions to the actual values
print(y_pred_binary == y_val)
# 뉴럴 네트워크
# 1개의 입력층, 1개의 은닉층, 1개의 출력층
import numpy as np
# Define the sigmoid activation function
def sigmoid(x):
return 1 / (1 + np.exp(-x))
# Define the derivative of the sigmoid function
def sigmoid_derivative(x):
return x * (1 - x)
# Define the neural network class
class NeuralNetwork:
def __init__(self, input_size, hidden_size, output_size):
# Initialize the weights
self.weights1 = np.random.randn(input_size, hidden_size)
self.weights2 = np.random.randn(hidden_size, output_size)
def forward(self, X):
# Calculate the output of the hidden layer
self.hidden_layer_output = sigmoid(np.dot(X, self.weights1))
# Calculate the output of the output layer
self.output = sigmoid(np.dot(self.hidden_layer_output, self.weights2))
return self.output
def backward(self, X, y, output):
# Calculate the error in the output layer
self.output_error = y - output
# Calculate the derivative of the output layer
self.output_delta = self.output_error * sigmoid_derivative(output)
# Calculate the error in the hidden layer
self.hidden_layer_error = self.output_delta.dot(self.weights2.T)
# Calculate the derivative of the hidden layer
self.hidden_layer_delta = self.hidden_layer_error * sigmoid_derivative(self.hidden_layer_output)
# Update the weights
self.weights2 += self.hidden_layer_output.T.dot(self.output_delta)
self.weights1 += X.T.dot(self.hidden_layer_delta)
def train(self, X, y, num_iterations):
for i in range(num_iterations):
# Perform forward propagation
output = self.forward(X)
# Perform backward propagation
self.backward(X, y, output)
# Define the input data
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
# Define the output data
y = np.array([[0], [1], [1], [0]])
# Create a neural network with 2 input neurons, 2 hidden neurons, and 1 output neuron
nn = NeuralNetwork(2, 2, 1)
# Train the neural network
nn.train(X, y, 10000)
# Define the validation data
X_val = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
# Predict the output for the validation data
y_pred = nn.forward(X_val)
# Print the predictions
print(y_pred)
# Convert probabilities to binary predictions
y_pred_binary = (y_pred > 0.5).astype(int)
# Compare the predictions to the actual values
print(y_pred_binary == y)
댓글 영역
획득법
① NFT 발행
작성한 게시물을 NFT로 발행하면 일주일 동안 사용할 수 있습니다. (최초 1회)
② NFT 구매
다른 이용자의 NFT를 구매하면 한 달 동안 사용할 수 있습니다. (구매 시마다 갱신)
사용법
디시콘에서지갑연결시 바로 사용 가능합니다.