Chane
Chane

Reputation: 43

Retrieve data from Excel sheet for Artificial Neural Network training

I started to write code for a neural network.
The training data set is in an Excel sheet - two inputs and two outputs.
I am struggling to import the data into VBA so the network can be trained.

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

Upvotes: 0

Views: 5947

Answers (1)

hiuller
hiuller

Reputation: 461

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:

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:

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

Upvotes: 2

Related Questions