H2_AndresAlcivar
H2_AndresAlcivar
H2_AndresAlcivar
SOONGSIL UNIVERSITY
1. GRADIENT_1D
import numpy as np
import matplotlib.pylab as plt
2. GRADIENT_2D
import numpy as np
import matplotlib.pylab as plt
from mpl_toolkits.mplot3d import Axes3D
x[idx] = tmp_val - h
fxh2 = f(x) # Evaluate f at x-h
grad[idx] = (fxh1 - fxh2) / (2 * h) # Compute the partial
derivative
# Plotting
plt.figure()
plt.quiver(X, Y, -grad[0], -grad[1], angles="xy",
color="#666666") # Draw arrows showing gradient direction
plt.xlim([-2, 2])
plt.ylim([-2, 2])
plt.xlabel('x0') # Label for the x-axis
plt.ylabel('x1') # Label for the y-axis
plt.grid() # Add a grid to the plot
plt.draw() # Render the plot
plt.show() # Display the plot
ANDRES ALCIVAR
SOONGSIL UNIVERSITY
3. GRADIENT_METHOD
import numpy as np
import matplotlib.pylab as plt
from gradient_2d import numerical_gradient # Import the numerical
gradient function
# Set the learning rate and number of steps for the gradient descent
lr = 0.1
step_num = 20
x, x_history = gradient_descent(function_2, init_x, lr=lr,
step_num=step_num)
# Plotting setup
plt.plot([-5, 5], [0, 0], '--b') # Draw x-axis
plt.plot([0, 0], [-5, 5], '--b') # Draw y-axis
plt.plot(x_history[:, 0], x_history[:, 1], 'o') # Plot the history of x
positions
# Define the loss function which calculates the total loss for a
given input and true output
def loss(self, x, t):
z = self.predict(x) # Get the score vector by predicting
y = softmax(z) # Apply softmax to get probability distribution
loss = cross_entropy_error(y, t) # Calculate the cross-entropy
loss
return loss
# Test data
x = np.array([0.6, 0.9]) # Input data
t = np.array([0, 0, 1]) # True label (one-hot encoded)
# Compute the loss function for a given set of input and true
output
def loss(self, x, t):
y = self.predict(x)
return cross_entropy_error(y, t) # Return the cross-entropy
loss
batch_num = x.shape[0]
# Load the MNIST dataset with normalization and one-hot encoding of the
labels
(x_train, t_train), (x_test, t_test) = load_mnist(normalize=True,
one_hot_label=True)
# Calculate the number of iterations per epoch (full pass through the
training data)
iter_per_epoch = max(train_size / batch_size, 1)
# Training loop
for i in range(iters_num):
# Randomly select a mini-batch of samples
batch_mask = np.random.choice(train_size, batch_size)
x_batch = x_train[batch_mask]
t_batch = t_train[batch_mask]
# At the end of each epoch, evaluate and print the training and test
accuracy
if i % iter_per_epoch == 0:
train_acc = network.accuracy(x_train, t_train)
test_acc = network.accuracy(x_test, t_test)
train_acc_list.append(train_acc)
test_acc_list.append(test_acc)
print("train acc, test acc | " + str(train_acc) + ", " +
str(test_acc))