-
Notifications
You must be signed in to change notification settings - Fork 2
/
MLP.R
86 lines (71 loc) · 4.73 KB
/
MLP.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
#********************** MLP Network construction function *********************#
##Arguments
# train_dataset: the 60 % of the data to train the network
# validate_dataset: the 40 % of the data to test the network
# predictor_order: the no. of supplied past historical data (sample input range: 3 <-> 10)
# learning_rate : the learning rate to train the network (sample input range : 1 <-> 0.05)
# Return Values
# Predicted Value and Error Results
MLP <- function(train_dataset,validate_dataset,non_normalize, predictor_order,
hidden_neurons, learning_rate,learning_func, weights){
require("neuralnet")
# Training
if(predictor_order == 3){
exchange_model <- neuralnet(oneDayAhead ~ firstDay + secondDay + thirdDay,
learningrate = learning_rate, data = train_dataset,
hidden = hidden_neurons, act.fct=learning_func,
startweights = weights)
}
if(predictor_order == 4){
exchange_model <- neuralnet(oneDayAhead ~ firstDay + secondDay + thirdDay + fourthDay,
learningrate = learning_rate,data = train_dataset, hidden = hidden_neurons,
act.fct=learning_func, startweights = weights)
}
if(predictor_order == 5){
exchange_model <- neuralnet(oneDayAhead ~ firstDay + secondDay + thirdDay + fourthDay +
FifthDay, learningrate = learning_rate,
data = train_dataset, hidden = hidden_neurons,
act.fct=learning_func,startweights = weights)
}
if(predictor_order == 6){
exchange_model <- neuralnet(oneDayAhead ~ firstDay + secondDay + thirdDay + fourthDay +
FifthDay + SixthDay, learningrate = learning_rate,
data = train_dataset, hidden = hidden_neurons,
act.fct=learning_func, startweights = weights)
}
if(predictor_order == 7){
exchange_model <- neuralnet(oneDayAhead ~ firstDay + secondDay + thirdDay + fourthDay +
FifthDay + SixthDay + SeventhDay ,
learningrate = learning_rate, data = train_dataset,
hidden = hidden_neurons, act.fct=learning_func,
startweights = weights)
}
if(predictor_order == 8){
exchange_model <- neuralnet(oneDayAhead ~ firstDay + secondDay + thirdDay + fourthDay +
FifthDay + SixthDay + SeventhDay + eighthDay,
learningrate = learning_rate, data = train_dataset, hidden = hidden_neurons,
act.fct=learning_func,startweights = weights)
}
if(predictor_order == 9){
exchange_model <- neuralnet(oneDayAhead ~ firstDay + secondDay + thirdDay + fourthDay +
FifthDay + SixthDay + SeventhDay+eighthDay+ninethDay,
learningrate = learning_rate, data = train_dataset,
hidden = hidden_neurons, act.fct=learning_func,
startweights = weights)
}
if(predictor_order == 10){
exchange_model <- neuralnet(oneDayAhead ~ firstDay + secondDay + thirdDay + fourthDay +
FifthDay + SixthDay + SeventhDay + eighthDay + ninethDay+tenthDay,
learningrate = learning_rate, data = train_dataset,
hidden = hidden_neurons, act.fct=learning_func,
startweights = weights)
}
# Testing and Error Result
model_results <- neuralnet::compute(exchange_model, validate_dataset[1:predictor_order])
predicted_oneDayhead <- model_results$net.result
predict_value <- denormalized(predicted_oneDayhead,non_normalize)
actual <- denormalized(validate_dataset[,predictor_order+1],non_normalize)
error <- actual - predict_value
final_result <- list(predict_value,error,exchange_model)
return(final_result)
}