import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from IPython.display import HTML
# Gradient of the function
def grad_f(x):
...
# Heavy Ball method implementation
def heavy_ball_method(alpha, beta, x0, num_iterations):
x = np.zeros(num_iterations + 1)
x_prev = x0
x_curr = x0 # Initialize x[1] same as x[0] to start the algorithm
for i in range(num_iterations):
x[i] = x_curr
x_new = x_curr - alpha * grad_f(x_curr) + beta * (x_curr - x_prev)
x_prev = x_curr
x_curr = x_new
x[num_iterations] = x_curr
return x
# Parameters
L = ...
mu = ...
alpha_star = ...
beta_star = ...
x0 = ...
num_iterations = 30
# Generate the trajectory of the method
trajectory = heavy_ball_method(alpha_star, beta_star, x0, num_iterations)
# Setup the figure and axes for the animation
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(7, 3.5))
fig.suptitle("Heavy ball method with optimal hyperparameters α* β*")
# Function for updating the animation
def update(i):
ax1.clear()
ax2.clear()
# Plot f(x) and trajectory
x_vals = np.linspace(-4, 4, 100)
f_vals = np.piecewise(x_vals, [x_vals < 1, (x_vals >= 1) & (x_vals < 2), x_vals >= 2],
[lambda x: 12.5 * x**2, lambda x: .5 * x**2 + 24 * x - 12, lambda x: 12.5 * x**2 - 24 * x + 36])
ax1.plot(x_vals, f_vals, 'b-')
ax1.plot(trajectory[:i], [12.5 * x**2 if x < 1 else .5 * x**2 + 24 * x - 12 if x < 2 else 12.5 * x**2 - 24 * x + 36 for x in trajectory[:i]], 'ro-')
# Add vertical dashed lines at x=1 and x=2 on the left subplot
ax1.axvline(x=1, color='navy', linestyle='--')
ax1.axvline(x=2, color='navy', linestyle='--')
# Plot function value from iteration
f_trajectory = [None for x in trajectory]
f_trajectory[:i] = [12.5 * x**2 if x < 1 else .5 * x**2 + 24 * x - 12 if x < 2 else 12.5 * x**2 - 24 * x + 36 for x in trajectory[:i]]
ax2.plot(range(len(trajectory)), f_trajectory, 'ro-')
ax2.set_xlim(0, len(trajectory))
ax2.set_ylim(min(f_vals), max(f_vals))
# Add horizontal dashed lines at f(1) and f(2) on the right subplot
f_1 = 12.5 * 1.0**2
f_2 = .5 * 2.**2 + 24 * 2. - 12
ax2.axhline(y=f_1, color='navy', linestyle='--')
ax2.axhline(y=f_2, color='navy', linestyle='--')
# ax1.set_title("Function f(x) and Trajectory")
ax1.set_xlabel("x")
ax1.set_ylabel("f(x)")
ax1.grid(linestyle=":")
# ax2.set_title("Function Value from Iteration")
ax2.set_xlabel("Iteration")
ax2.set_ylabel("f(x)")
ax2.grid(linestyle=":")
plt.tight_layout()
# Create the animation
ani = animation.FuncAnimation(fig, update, frames=num_iterations, repeat=False, interval=100)
HTML(ani.to_jshtml())1 задача
2 задача
2.2
import requests
from sklearn.datasets import load_svmlight_file
# URL of the file to download
url = 'https://cu25.fmin.xyz/files/mushrooms.txt'
# Download the file and save it locally
response = requests.get(url)
dataset = 'mushrooms.txt'
# Ensure the request was successful
if response.status_code == 200:
with open(dataset, 'wb') as f:
f.write(response.content)
# Load the dataset from the downloaded file
data = load_svmlight_file(dataset)
A, b = data[0].toarray(), data[1]
n, d = A.shape
print("Data loaded successfully.")
print(f"Number of samples: {n}, Number of features: {d}")
else:
print(f"Failed to download the file. Status code: {response.status_code}")2.3
from sklearn.model_selection import train_test_split
# Split the data into training and test sets
A_train, A_test, b_train, b_test = train_test_split(A, b, test_size=0.2, random_state=214)2.9
def backtracking_L(f, grad, x, h, L0, rho, maxiter=100):
L = L0
fx = f(x)
gradx = grad(x)
iter = 0
while iter < maxiter:
y = x - 1 / L * h
if f(y) <= fx - 1 / L * gradx.dot(h) + 1 / (2 * L) * h.dot(h):
break
else:
L = L * rho
iter += 1
return L