0

我开始为神经网络编写代码。
训练数据集位于 Excel 工作表中 - 两个输入和两个输出。
我正在努力将数据导入 VBA,以便可以训练网络。

训练部分代码

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
4

1 回答 1

2

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

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

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

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

然后在给定数据范围的情况下填充它,比如说“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
于 2013-07-29T20:08:26.663 回答