Question: This is for my Machine Learning Class. We have to explain the below code in English words and give explanation for it. Please give an
This is for my Machine Learning Class. We have to explain the below code in English words and give explanation for it. Please give an explanation for the below code giving as much detail as possible as it relates to Machine Learning. This code is written in Python and the supporting question is below also.
The question I wrote the code from is as follows (supporting question):
https://colab.research.google.com (you can run the code here if you would like to test it out).
- Download one of the datasets below and use a linear perceptron and an MLP with one hidden layer for classification. Compare the classification accuracies (a linear perceptron code is also available on bboard).
Dataset 1: Brodatz texure Images 1.2.01 through 1.2.13 are histogram equalized versions of 1.1.01 through 1.1.13 and are marked with "H.E." in the list at the web address: http://sipi.usc.edu/database/database.php?volume=textures
You need to randomly pick 32x32 windows from these images (say 50 windows for training and 50 for test for each texture image)
Dataset 2: IMDB Movie reviews sentiment classification dataset at https://keras.io/datasets/
Code:
%reset import math import numpy as np import tensorflow as tf import tensorflow.keras as keras from tensorflow.keras.utils import to_categorical from tensorflow.keras import models from tensorflow.keras import layers from tensorflow.keras.datasets import imdb
(training_data, training_targets), (testing_data, testing_targets) = imdb.load_data(num_words=10000)
def vectorize(sequences, dimension = 10000): results = np.zeros((len(sequences), dimension)) for i, sequence in enumerate(sequences): results[i, sequence] = 1 return results
train_x = vectorize(training_data) train_x = np.insert(train_x, 0, 1, axis=1);
test_x = vectorize(testing_data) test_x = np.insert(test_x, 0, 1, axis=1);
train_y = training_targets
test_y = testing_targets
Once deleted, variables cannot be recovered. Proceed (y/[n])? y
print(train_y) def sigmoid(x): return 1 / (1 + math.exp(-x))
[1 0 0 ... 0 1 0]
N=25000 d=10000 eta=0.001 rng = np.random.RandomState(1) w = rng.randn(1, d+1)/100; dwold=np.zeros(w.shape)
for epoch in range(40): numcorr=0 dw=np.zeros(w.shape) for t in range(N): r=train_y[t] x=train_x[t,:] y=sigmoid(np.dot(x,w.T)) if (y>0.5 and r==1) or (y<=0.5 and r==0) : numcorr=numcorr+1 delta=r-y dw=x*delta # dw = 0.9*dwold + 0.1*dw # dwold=dw w=w*0.9999+eta*dw; print(numcorr/N) Output of numcorr:
0.80448 0.83164 0.832 0.832 0.832 0.832 0.832 0.832 0.832 0.832 0.832 0.832 0.832 0.832 0.832 0.832 0.832 0.832 0.832
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
