vba Excel VBA人工神经网络训练代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16992436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 21:32:09  来源:igfitidea点击:

Excel VBA Artificial Neural Network training code

excel-vbaneural-networkexcel-2010vbaexcel

提问by Chane

Ok so I have started to write a code for a neural network on excel vba. The training data set is in a excel sheet - two inputs and two outputs. I am struggling to import the data into vba so that the network can be trained....

好的,所以我已经开始在 excel vba 上为神经网络编写代码。训练数据集在一个 excel 表中 - 两个输入和两个输出。我正在努力将数据导入 vba 以便可以训练网络....

Here is my code for the training part

这是我的训练部分代码

`Private Sub Train_Click()
alpha = 0.3
mu = 0.8
n = 4
m = 4
Dim I, J, K As Integer
Dim TrainError As Double
Dim TrainingData As String
NumCases = 123
For J = 0 To NumCases - 1
For I = 0 To m
X1(J, I) = Sheets("Sheet1").Cells(I, J).String
Next I
targval(J) = X1(J, n)
Next J

Call Init(n, m)
J = 0
Do Until J = 1000 And TrainError = 0
For I = 0 To NumCases - 1
For K = 0 To n - 1
InputNeuron(K) = X1(I, K)
Next
Call HiddenInput(n, m)
Call HiddenTransfer(m)
Call OutputInput(m)
Call OutputTransfer
Call UpdateOut(I, m)
Call UpdateHidden(n, m)
TrainError = TrainError + (targval(I) - oout) ^ 2
Next I
TrainError = Sqrt(TrainError / NumCases)
If TrainError < 0.01 Then
Exit Do
End If
J = J + 1
Loop
End Sub

Can anyone help??

有人可以帮忙吗??

回答by hiuller

If your problem is to import the data, I presume the issue is inside the first loop:

如果您的问题是导入数据,我认为问题出在第一个循环中:

X1(J, I) = Sheets("Sheet1").Cells(I, J).String

If so, you could just try to use a method that reads your data given a Range. You declare an array to hold the input layer:

如果是这样,您可以尝试使用一种方法来读取给定Range. 您声明一个数组来保存输入层:

Public inputv() As Double   '// the input layer data

and then populate it given the range of the data, lets say "C19:C27", with this method:

然后在给定数据范围的情况下填充它,让我们说“C19:C27”,使用以下方法:

Private Sub loadInput(ByRef r As Range)

    Dim s As Range
    Set s = r.Resize(1, 1)

    // this way you can even evaluate the number of trainning examples, m
    m = r.Rows.Count


    ReDim inputv(0 To m - 1, 0 To n - 1)
    Dim i, j As Integer

    For i = 0 To m - 1
        For j = 0 To n - 1
            inputv(i, j) = s(i + 1, j + 1).Value
        Next j
    Next i

End Sub