HW8-Python代写
时间:2022-12-05
2022/12/4 20:44 hw8
localhost:8888/lab 1/2
HW8
The successive halving hyperparameter search technique is as follows:
1. Specify a hyperparameter grid.
2. Sample a set of hyperparameter combinations from this grid.
3. Train models for $T$ epochs under each hyperparameter combination.
4. Assess the validation losses for each combination.
5. Stop training for the half of combinations which have the worst validation losses.
6. Continue 2–4 until only one combination remains.
Complete the code to implement this procedure for the hidden_layer_sizes of
sklearn 's MLPClassifier as applied to the iris dataset. Print out the selected
hidden_layer_sizes when you are done.
Steps 0–1
Search Space: [(1,), (1, 1), (1, 1, 1, 1), (1, 1, 1, 1, 1, 1, 1, 1), (2,), (2, 2),
(2, 2, 2, 2), (2, 2, 2, 2, 2, 2, 2, 2), (4,), (4, 4), (4, 4, 4, 4), (4, 4, 4, 4, 4,
4, 4, 4), (8,), (8, 8), (8, 8, 8, 8), (8, 8, 8, 8, 8, 8, 8, 8)]
The Random Sample: [(8, 8, 8, 8, 8, 8, 8, 8), (1,), (2, 2, 2, 2, 2, 2, 2, 2), (1, 1,
1, 1), (8, 8), (8, 8, 8, 8), (2, 2, 2, 2), (4, 4)]
Steps 2―5
To train an MLPClassifier for an epoch, use the partial_fit(X, y, classes=None)
method. Make sure to read the description of the classes parameter in the
documentation.
The loss to evlauate on the validation set is sklearn.metrics.log_loss .
In [49]: import numpy as np
from sklearn import datasets
from sklearn.neural_network import MLPClassifier
import random
from sklearn.metrics import log_loss

# The training and validation sets.
iris = datasets.load_iris()
X = iris.data
y = iris.target
X_train, X_val, y_train, y_val = train_test_split(
X, y, test_size=0.3, shuffle=True, stratify = y)
In [54]: # Step 0: Specify the grid.
grid = [(2**k,)*2**l for k in np.arange(0, 4) for l in np.arange(0,4)]
print("Search Space:", grid)

# Step 1: Sample.
random.seed(553)
hyper = random.sample(grid, 8)
print("The Random Sample:", hyper)
2022/12/4 20:44 hw8
localhost:8888/lab 2/2
Take $T=100$ as the number of epochs for step 2.
In [ ]: # Steps 2―5:
T = 100 # Number of epochs for step 2.

# YOUR CODE HERE:
essay、essay代写