-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHW5-myNN.R
152 lines (113 loc) · 3.65 KB
/
HW5-myNN.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
library(data.table) # allows us to use function fread,
# which quickly reads data from csv files
accuracy <- function(p, y)
{
return(mean((p > 0.5) == (y == 1)))
}
train <- function(X, Y, Xtest, Ytest, m=100, num_iterations=2000,
learning_rate=1e-1)
{
n = dim(X)[1]
p = dim(X)[2]+1
ntest = dim(Xtest)[1]
X1 = cbind(rep(1, n), X)
Xtest1 = cbind(rep(1, ntest), Xtest)
sigma = .1;
alpha = matrix(rnorm(p*m)*sigma, nrow=p)
beta = matrix(rnorm(m+1)*sigma, nrow=m+1)
accuracies1 = NULL
accuracies2= NULL
for (it in 1:num_iterations)
{
Z= 1/(1+exp(-(X1%*%alpha)))
Z1 = cbind(rep(1,n),Z)
pr = 1/(1+exp(-(Z1%*%beta)))
dbeta = matrix(rep(1,n), nrow=1)%*%(matrix(Y-pr, n, m+1)*Z1)/n
beta = beta + learning_rate*t(dbeta)
#Training Accuracies
accuracies1 = append(accuracies1,accuracy(pr, Y))
P= 1/(1+exp(-(Xtest1%*%alpha)))
P1 = cbind(rep(1,n),P)
predict_test = 1/(1+exp(-(P1%*%beta)))
#Testing Accuracies
accuracies2 = append(accuracies2,accuracy(predict_test, Ytest))
}
for (k in 1:m)
{
da = (Y-pr)*beta[k+1]*Z[ ,k]+(1-Z[ ,k])
dalpha = (matrix(rep(1,n), nrow=1)%*%(matrix(da,n,p)*X1))/n
alpha[ ,k] = alpha[ ,k] + learning_rate*t(dalpha)
}
return (list(beta, alpha, accuracies1, accuracies2))
}
# load data
load_digits <- function(subset=NULL, normalize=TRUE) {
#Load digits and labels from digits.csv.
#Args:
#subset: A subset of digit from 0 to 9 to return.
#If not specified, all digits will be returned.
#normalize: Whether to normalize data values to between 0 and 1.
#Returns:
#digits: Digits data matrix of the subset specified.
#The shape is (n, p), where
#n is the number of examples,
#p is the dimension of features.
#labels: Labels of the digits in an (n, ) array.
#Each of label[i] is the label for data[i, :]
# load digits.csv, adopted from sklearn.
df <- fread("Downloads/digits.csv")
df <- as.matrix(df)
## only keep the numbers we want.
if (length(subset)>0) {
c <- dim(df)[2]
l_col <- df[,c]
index = NULL
for (i in 1:length(subset)){
number = subset[i]
index = c(index,which(l_col == number))
}
sort(index)
df = df[index,]
}
# convert to arrays.
digits = df[,-1]
labels = df[,c]
# Normalize digit values to 0 and 1.
if (normalize == TRUE) {
digits = digits - min(digits)
digits = digits/max(digits)}
# Change the labels to 0 and 1.
for (i in 1:length(subset)) {
labels[labels == subset[i]] = i-1
}
return(list(digits, labels))
}
split_samples <- function(digits,labels) {
# Split the data into a training set (70%) and a testing set (30%).
num_samples <- dim(digits)[1]
num_training <- round(num_samples*0.7)
indices = sample(1:num_samples, size = num_samples)
training_idx <- indices[1:num_training]
testing_idx <- indices[-(1:num_training)]
return (list(digits[training_idx,], labels[training_idx],
digits[testing_idx,], labels[testing_idx]))
}
#====================================
# Load digits and labels.
result = load_digits(subset=c(3, 5), normalize=TRUE)
digits = result[[1]]
labels = result[[2]]
result = split_samples(digits,labels)
training_digits = result[[1]]
training_labels = result[[2]]
testing_digits = result[[3]]
testing_labels = result[[4]]
# print dimensions
length(training_digits)
length(testing_digits)
# Train a net and display training accuracy.
final_results=train(training_digits, training_labels, testing_digits, testing_labels)
training_accuracy= final_results[[3]]
training_accuracy
testing_accuracy = final_results[[4]]
testing_accuracy