Professional Documents
Culture Documents
class
The code begins by importing the necessary libraries and setting up the required modules
from PyTorch. It also imports the dataset manipulation library, NumPy, and the
visualization library, Matplotlib.
Next, a function called train_model is defined, which serves as the main training loop for
the model. It takes the model, loss criterion, optimizer, learning rate scheduler, and the
number of epochs as input.
Inside the train_model function, the current time is recorded, and variables are initialized
to keep track of the best model weights and accuracy. The function then iterates over the
specified number of epochs.
For each epoch, the code enters a loop that iterates over the training and testing phases. In
the training phase, the learning rate scheduler is updated, and the model is set to training
mode. In the testing phase, the model is set to evaluation mode.
Within each phase loop, the code calculates the running loss and the number of correct
predictions. It processes the data in batches, performs forward and backward passes,
updates the model's weights during the training phase, and calculates the loss and
accuracy metrics.
At the end of each epoch, the code prints the loss and accuracy for both the training and
testing phases. If the accuracy in the testing phase is better than the previous best
accuracy, the model's weights are saved as the best weights so far.
After completing the training loop, the total time taken for training and the best accuracy
achieved are printed.
Next, the code sets up data transformations for the training and testing datasets using
PyTorch's transforms module. The data directory and image datasets are defined, and data
loaders are created to load the images in batches for training and testing.
The sizes of the datasets and the class names are determined.
A sample image from the training dataset is visualized using Matplotlib.
The code then loads the pre-trained AlexNet model from the torchvision library. The last
fully connected layer of the model is replaced with a new linear layer that matches the
number of classes in the custom dataset.
If a GPU is available, the model is moved to the GPU for accelerated computation.
The loss criterion is set to cross-entropy, and the optimizer is initialized with stochastic
gradient descent (SGD) using specified learning rate and momentum values.
A learning rate scheduler is also defined to adjust the learning rate during training based
on a specified step size and gamma value.
Finally, the train_model function is called with the defined model, criterion, optimizer,
scheduler, and the number of epochs to start the training process.
Dataset: 5 classes (air plane, bike, car, helicopter, pipeline)
Train
- Airplane
- Bike
- Car
- Helicopter
- Pipeline
Test
- Air plane
- Bike
- Car
- Helicopter
- Pipeline
HW 5 Write a program inplementing kNN algorithm solving a freely chosen
problem
The code begins by importing the necessary libraries, including NumPy for numerical
operations, scikit-learn for dataset loading, model selection, and evaluation, and
matplotlib for data visualization. The StandardScaler from scikit-learn is also imported to
standardize the pixel values of the dataset.
The MNIST dataset is loaded using the load_digits function from scikit-learn, and the
features (X) and labels (y) are assigned accordingly. The dataset is then split into training
and testing sets using the train_test_split function, with 20% of the data reserved for
testing.
To ensure fair comparison, the feature data is standardized using the StandardScaler class.
Standardization scales the features to have zero mean and unit variance, which can
improve the performance of certain algorithms.
A kNN classifier with k=5 neighbors is created using the KNeighborsClassifier class
from scikit-learn. The classifier is then trained on the standardized training data using
the fit method.
Next, the trained model is used to make predictions on the standardized test data using
the predict method. The predicted labels (y_pred) are obtained.
To evaluate the performance of the model, the classification_report function from scikit-
learn is used, which provides metrics such as precision, recall, F1-score, and support for
each class. The classification report is printed to assess the model's accuracy and
performance on the test set.
To visualize a random digit from the test set, a random index is generated
using np.random.randint, and the corresponding digit image, predicted label, and true
label are extracted. The digit image is reshaped to its original 8x8 size and displayed
using plt.imshow with a grayscale color map. The title of the plot includes the predicted
label and the true label for comparison.
HW1 Write a program implementing perceptron learning algorithm
The code starts by defining the training data, where the input features are represented by
matrix X and the corresponding target labels are stored in vector Y. The weights (w) and
bias (b) are initialized randomly.
The learning rate (learning_rate) and the number of iterations (num_iterations) are set to
control the training process. The perceptron learning algorithm is executed using a nested
loop structure. The outer loop iterates over the specified number of iterations, while the
inner loop iterates over each training example.
For each training example, the predicted output (prediction) is calculated based on the
current weights and bias. If the predicted output is greater than zero, it is assigned as class
1; otherwise, it is assigned as class 0.
The weights and bias are updated using the perceptron learning rule, which calculates the
error between the predicted output and the target label. The weights are adjusted in the
direction of the input features, scaled by the learning rate, and multiplied by the error.
The bias is updated similarly but without the input features.
After the training loop completes, a scatter plot is created to visualize the training data.
The data points belonging to class 1 are plotted in red, while those belonging to class 0
are plotted in blue. The decision boundary, represented by a straight line, is plotted based
on the learned weights and bias.
To test the perceptron, a test dataset is defined, and the trained model is applied to
classify each test example. The predicted output is calculated, and based on its sign, the
corresponding class label is printed.