Newer
Older
tr <- mydata[1:25, ] # Training
te <- mydata[26:500, ] # Test
# Random initialization of the weights in the interval [-1, 1]
nn <- neuralnet(
formula ,
data = tr,
hidden = c(10),
startweights = winit
)
# Plot of the training data (black), test data (blue), and predictions (red)
plot(
tr,
cex = 2,
ylab = "sin(x)",
xlab = "x",
main = "NN with logistic activation function"
)
points(te, col = "blue", cex = 1)
points(te[, 1], predict(nn, te), col = "red", cex = 1)
grid()
legend(
"bottomleft",
legend = c("Training data", "Test data", "Predicted test data"),
col = c("black", "blue", "red"),
pch = 1,
pt.cex = c(2, 1, 1),
cex = 1
)
# ----2.----
}
h3 <- function(x) {
log(1 + exp(x))
}
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
nn_h1 <- neuralnet(
formula ,
data = tr,
hidden = c(10),
startweights = t(winit),
act.fct = h1
)
plot(tr,
cex = 2,
xlab = "x",
"sin(x)",
main = "NN with linear activation function (h1)")
points(te, col = "blue", cex = 1)
points(te[, 1], predict(nn_h1, te), col = "red", cex = 1)
grid()
legend(
"bottomleft",
legend = c("Training data", "Test data", "Predicted test data"),
col = c("black", "blue", "red"),
pch = 1,
pt.cex = c(2, 1, 1),
cex = 1
)
nn_h2 <- neuralnet(
formula ,
data = tr,
hidden = c(10),
startweights = t(winit),
act.fct = h2
)
plot(
tr,
cex = 2,
xlab = "x",
ylab = "sin(x)",
main = "NN with ReLU activation function (h2)"
)
points(te, col = "blue", cex = 1)
points(te[, 1], predict(nn_h2, te), col = "red", cex = 1)
grid()
legend(
"bottomleft",
legend = c("Training data", "Test data", "Predicted test data"),
col = c("black", "blue", "red"),
pch = 1,
pt.cex = c(2, 1, 1),
cex = 1
)
nn_h3 <- neuralnet(
formula ,
data = tr,
hidden = c(10),
startweights = t(winit),
act.fct = h3
)
plot(
tr,
cex = 2,
xlab = "x",
ylab = "sin(x)",
main = "NN with softplus activation function (h3)"
)
points(te, col = "blue", cex = 1)
points(te[, 1], predict(nn_h3, te), col = "red", cex = 1)
grid()
legend(
"bottomleft",
legend = c("Training data", "Test data", "Predicted test data"),
col = c("black", "blue", "red"),
pch = 1,
pt.cex = c(2, 1, 1),
cex = 1
)
# ----3.----
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
mydata1 <- data.frame(Var = Var1, Sin = sin(Var1))
pred1 <- predict(nn, mydata1)
plot(
mydata1,
cex = 2,
main = "NN with logistic activation",
ylim = c(-10, 2),
xlab = "x",
ylab = "sin(x)"
)
points(mydata1, col = "blue", cex = 1)
points(mydata1[, 1], pred1, col = "red", cex = 1)
grid()
legend(
"bottomleft",
legend = c("Training data", "Test data", "Predicted test data"),
col = c("black", "blue", "red"),
pch = 1,
pt.cex = c(2, 1, 1),
cex = 1
)
# ----4.----
pred1[order(pred1)][1]
mydata2 <- data.frame(Sin2 = sin(Var2), Var2)
startweights = winit,
threshold = 0.1
main = "NN with logistic activation function",
xlab = "sin(x)",
ylab = "x",
)
points(mydata2[, 1],
predict(nn2, mydata2),
col = "red",
cex = 1)
grid()
legend(
"bottomleft",
legend = c("Training data", "Predicted training data"),
col = c("blue", "red"),