You are on page 1of 25

HW 17 AlexNet transfer learning, at least 5 classes with minimum 20 images per

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.

HW 13 Why local connectivity & shared weights?


Convolutional neural networks (CNNs) are a type of artificial intelligence that is
particularly well-suited for tasks like image recognition and object detection.
CNNs achieve this success by employing two key concepts: local connectivity and
shared weights. Local connectivity means that each neuron in a CNN is only
connected to a small subset of the input data, while shared weights means that the
same weights are used to calculate the output of each neuron in a feature map.
These two concepts together allow CNNs to efficiently capture spatial patterns and
translational invariance in data.

Fully connected neuron network


Traditional Neuron Network

 The weight matrix A is N by M so that the network is “fully connected”.


 All nodes on adjacent layers are fully connected with each other
 Can be seen as with M “kernels” which has N dimensions each
 Many parameters; suffer severe overfitting

Locally connected neuron network

 Output is based only on the “receptive field” of size P, so weight matrix W is P by


M (P<N)
 M kernels each with dimension P
 Less parameters to train, less overfit
Convolutional Neuron Network ( shared-weight local)
 Shared weight local connected neural network
 Weight matrix W is P by 1 (since we can write P as a Matrix as well)
 Even fewer parameters to train
 Can simultaneously train many maps to extract more features
HW 9 Write a programe implementing Q learning algorithm solving a freely
chosen problem (except the problem used as example in the lecture)
Implements the Q-learning algorithm for maximizing benefits in an energy market.
The answer "15" is a potential result of running the provided Q-learning algorithm for
energy market benefit maximization. The specific value of the answer depends on
various factors, including the defined environment parameters (number of generators,
consumers, production costs, and demand prices), training parameters (learning rate,
discount factor, and exploration rate), and the number of training episodes.
The code begins by defining the environment parameters, including the number of
generators and consumers, and their respective production costs and demand prices. It
then initializes the Q-table, which holds the expected rewards for each state-action pair.
The training loop consists of multiple episodes, where each episode represents an
iteration of the Q-learning algorithm. In each episode, the code initializes a random
state and iterates through a maximum number of steps. For each step, the code selects
an action based on either exploration (random action) or exploitation (greedy action
selection using the Q-table). The chosen action is then applied to the environment, and
the agent receives a reward.
After each action, the Q-table is updated using the Q-learning formula, which considers
the current state, action, reward, and the estimated maximum future reward. The
learning rate and discount factor control the weight given to the new information and
the importance of future rewards, respectively.
The training loop continues until the episode is terminated, either by reaching the
maximum number of steps or by a termination condition specific to the environment. If
an error occurs due to an out-of-bounds state, the episode is terminated and the next
episode begins.
Once the training is complete, the code evaluates the trained agent by running it in the
environment for a certain number of trials. Similar to the training loop, the agent selects
actions based on the Q-table and observes rewards. The results are printed at the end,
showing the learned Q-table.
The code also includes a function to step the environment, which determines the
reward, next state, and termination condition based on the current state and action. It
handles buying from generators and selling to consumers, considering the given prices
and costs. An error is thrown if the state is out of bounds.
Overall, the code implements the Q-learning algorithm for the energy market, training
an agent to maximize benefits by learning the optimal buying and selling strategies
based on the given prices and costs.

You might also like