我具有以下用于神经网络FF和BP处理的训练数据集3823行x 65列(64 X输入和一个Y列作为输出,第65列)。
我输入了以下代码,但始终收到“ Y中的错误-输出:不一致的数组”。滚动到底部1/4以查看“问题区域”!
train<-as.matrix(train)
sum(is.na(train))
## Implementing Neural Network
# input matrix
X <- as.matrix(train[, -65], nrows=3823, ncol=64, byrow=TRUE) # X: all rows, all columns except col. 65
# output matrix
Y <- as.matrix(train[,65], nrows=3823, ncol=1, byrow=TRUE) # Y: all rows, only column 65
# Find number of missing values/check ranges (TRAINING data set)
sum(is.na(train))
#sigmoid function
sigmoid<-function(x){
1/(1+exp(-x))
}
# derivative of sigmoid function
derivatives_sigmoid<-function(x){
x*(1-x)
}
# variable initialization
epoch=1000 # epoch is the number of times all of the training vectors are used once to update the weights.
lr=0.1 # Learning Rate controls how much to change the model in response to the
# estimated error each time the model weights are updated.
inputlayer_neurons=ncol(X) # here ncol(x) = 64
output_neurons=10 # We have 10 digits to sort out as our output layer of 10 nodes
hiddenlayer_neurons=round((2/3)*(inputlayer_neurons +output_neurons), 0) # will try (2/3)(64+10)=50 neurons at this hidden layer
#weight and bias initialization
wh=matrix( rnorm(inputlayer_neurons*hiddenlayer_neurons,mean=0,sd=1), inputlayer_neurons, hiddenlayer_neurons) # weight in the hidden layer
bias_in=runif(hiddenlayer_neurons)
bias_in_temp=rep(bias_in, nrow(X))
bh=matrix(bias_in_temp, nrow = nrow(X), byrow = FALSE) # bias for the hidden layers
wout=matrix( rnorm(hiddenlayer_neurons%*%output_neurons,mean=0,sd=1), hiddenlayer_neurons, output_neurons) # resulting weight for output
bias_out=runif(output_neurons) # bias for output neurons
bias_out_temp=rep(bias_out,nrow(X))
bout=matrix(bias_out_temp,nrow = nrow(X),byrow = FALSE) # bias for output layer
# Feed-Forward Propagation
# FF: information moves only forward from the input nodes, through the hidden nodes (how ever many) and to the output nodes.
# There are no cycles or loops in the network
for(i in 1:epoch){
hidden_layer_input1= X%*%wh # Calculate hidden layer input
hidden_layer_input=hidden_layer_input1+bh # Calculate hidden layer input including bias
hidden_layer_activations=sigmoid(hidden_layer_input) # Perform non-linear transformation on hidden linear input
# Perform linear and non-linear transformation of hidden layer activation at output layer
output_layer_input1=hidden_layer_activations%*%wout
output_layer_input=output_layer_input1+bout
output = sigmoid(output_layer_input)
# Back Propagation
# to calculate a gradient that is needed in the calculation of the weights to be used in the network.
# The error is computed at the output and distributed backwards throughout the network’s layers.
E = Y - output # !! PROBLEM AREA !!
slope_output_layer=derivatives_sigmoid(output) # Slope (gradient) of Error at the output layer neurons
slope_hidden_layer=derivatives_sigmoid(hidden_layer_activations) # Slope (gradient) of hidden layer neurons
d_output=E%*%slope_output_layer # Change factor(delta) at output layer
Error_at_hidden_layer=d_output%*%t(wout) # Here, the error will propagate back into the network which means error at hidden layer.
d_hiddenlayer=Error_at_hidden_layer%*%slope_hidden_layer # change factor(delta) at hidden layer, multiply the error at hidden layer with slope of hidden layer activation
wout= wout + (t(hidden_layer_activations)%*%d_output)*lr # Update weights at the output and hidden layer: The weights in the network can be updated from the errors calculated for training.
# Update biases at the output and hidden layer: The biases in the network can be updated from the aggregated errors at that neuron.
bout= bout+rowSums(d_output)%*%lr
wh = wh +(t(X)%*%d_hiddenlayer)%*%lr
bh = bh + rowSums(d_hiddenlayer)%*%lr
}
output
由于我无法深入探究问题,因此我不知道它的真正外观。但是我假设它将是每个数字0到9的10个十进制数字(0.0000-1.0000)的列。
我试图使它们成为矩阵,数值等,但无济于事。火车数据集中没有空白或NA(我通过Excel过滤检查了每一列和每一行)。 请感谢您的帮助。
这是“火车”文件的数据结构
dput(head(train)) <- structure(c(0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 6L,
10L, 8L, 0L, 5L, 11L, 15L, 16L, 15L, 3L, 14L, 16L, 12L, 6L, 16L,
11L, 4L, 10L, 1L, 0L, 13L, 16L, 0L, 1L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0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L, 9L, 8L, 16L, 3L, 8L, 15L, 15L, 10L, 4L, 16L, 4L, 15L, 9L,
15L, 0L, 0L, 11L, 8L, 0L, 3L, 0L, 0L, 12L, 8L, 0L, 0L, 0L, 0L,
0L, 3L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 6L, 10L,
9L, 0L, 4L, 10L, 14L, 16L, 14L, 1L, 12L, 16L, 7L, 15L, 0L, 15L,
14L, 16L, 1L, 3L, 0L, 2L, 7L, 16L, 0L, 0L, 0L, 0L, 0L, 16L, 0L,
0L, 0L, 0L, 0L, 6L, 0L, 0L, 7L, 4L, 6L, 2L), .Dim = c(6L, 65L
), .Dimnames = list(NULL, c("V1", "V2", "V3", "V4", "V5", "V6",
"V7", "V8", "V9", "V10", "V11", "V12", "V13", "V14", "V15", "V16",
"V17", "V18", "V19", "V20", "V21", "V22", "V23", "V24", "V25",
"V26", "V27", "V28", "V29", "V30", "V31", "V32", "V33", "V34",
"V35", "V36", "V37", "V38", "V39", "V40", "V41", "V42", "V43",
"V44", "V45", "V46", "V47", "V48", "V49", "V50", "V51", "V52",
"V53", "V54", "V55", "V56", "V57", "V58", "V59", "V60", "V61",
"V62", "V63", "V64", "V65")))