[英]Writing an ANN in Excel: VBA Type Mismatch Error
So I'm trying to program a basic Artificial Neural Network using Excel VBA. 因此,我正在尝试使用Excel VBA对基本的人工神经网络进行编程。 I've been following one example in particular: 我一直在特别关注一个例子:
https://www.analyticsvidhya.com/blog/2017/05/neural-network-from-scratch-in-python-and-r/ https://www.analyticsvidhya.com/blog/2017/05/neural-network-from-scratch-in-python-and-r/
I've been basing the code off of the Python example in the article (code example is toward the bottom of the article). 我已经根据本文中的Python示例编写了代码(代码示例位于本文的底部)。 I don't have access to Python unfortunately so I'm trying to do this with VBA. 不幸的是,我无法使用Python,因此我正在尝试使用VBA进行此操作。 I've done my best to convert the code into a useable format for Excel, however there is an issue I'm encountering and I'm not sure how to solve it: 我已尽力将代码转换为Excel的可用格式,但是遇到了一个问题,我不确定如何解决:
Here is my VBA code: 这是我的VBA代码:
Sub ANN()
Dim X(1010, 1011, 101) As Integer 'Input
Dim Y(1, 1, 0) As Double 'output for comparison
Dim E(0, 0, 0) As Double 'Error
'Variable Initialization
Dim Epoch As Long
Dim LearnRate As Double
Dim InputLayer_Neurons() As Integer 'Number of Features in data set
ReDim InputLayer_Neurons(ArrayLen(X))
Dim HiddenLayer_Neurons As Integer
Dim Output_Neurons As Integer
Dim hidden_layer_input1 As Variant
Dim hidden_layer_input As Variant
Dim hiddenlayer_activations As Variant
Dim output_layer_input1 As Variant
Dim output_layer_input As Variant
Dim slope_output_layer As Variant
Dim slope_hidden_layer As Variant
Dim d_output As Variant
Dim Output As Variant
Dim Wh As Double 'Weight Hidden Layer
Dim Bh As Double 'Bias Hidden Layer
Dim Wout As Double 'Weight output Layer
Dim Bout As Double 'Bias Ouput layer
Dim i As Long
Epoch = 5000 'Training Iterations
LearnRate = 0.1 'Learning Rate
HiddeLayer_Neurons = 3 'Number of Neurons in Hidden Layer
Output_Neurons = 1 'Number of Neurons at output layer
'Weight & Bias Initialization
Wh = Application.WorksheetFunction.RandBetween(InputLayer_Neurons, HiddenLayer_Neurons)
Bh = Application.WorksheetFunction.RandBetween(1, HiddenLayer_Neurons)
Wout = Application.WorksheetFunction.RandBetween(HiddenLayer_Neurons, Output_Neurons)
Bout = Application.WorksheetFunction.RandBetween(1, Output_Neurons)
For i = 0 To Epoch
'Forward Propagation
hidden_layer_input1 = WorksheetFunction.MMult(X, Wh)
hidden_layer_input = hidden_layer_input1 + Bh
hiddenlayer_activations = Sigmoid_Activation(hidden_layer_input)
output_layer_input1 = WorksheetFunction.MMult(hiddenlayer_activations, Wout)
output_layer_input = output_layer_input1 + Bout
Output = Derivatives_Sigmoid(output_layer_input)
'Backpropagation
E = Y - Output
slope_output_layer = Derivatives_Sigmoid(Output)
slope_hidden_layer = Derivatives_Sigmoid(hiddenlayer_activations)
d_output = E * slope_output_layer
Error_at_hidden_layer = WorksheetFunction.MMult(d_output, Transpose(Wout))
d_hiddenlayer = Error_at_hidden_layer * slope_hidden_layer
Wout = Wout + WorksheetFunction.MMult(Transpose(hiddenlayer_activations), d_output) * LearnRate
Bout = Bout + WorksheetFunction.Sum(d_ouput) * LearnRate
Wh = Wh + WorksheetFunction.MMult(Transpose(X), d_hiddenlayer) * LearnRate
Bh = Bh + WorksheetFunction.Sum(d_hiddenlayer) * LearnRate
Next
Debug.Print Output
End Sub
Function Sigmoid_Activation(X) As Variant
Sigmoid_Activation = 1 / (1 + Application.WorksheetFunction.Power(-X))
End Function
Function Derivatives_Sigmoid(X) As Double
Derivatives_Sigmoid = X * (1 - X)
End Function
Public Function ArrayLen(arr As Variant) As Integer
ArrayLen = UBound(arr) - LBound(arr) + 1
End Function
my Issue is: 我的问题是:
In the section under Backpropagation. 在反向传播下的部分中。 I am getting a type mismatch error on the line: 我在行上收到类型不匹配错误:
E = Y – Output
I suppose this is because in VBA there is no built in function for subtracting a Double type value (Output) from an array (Y). 我想这是因为在VBA中没有用于从数组(Y)中减去Double类型值(Output)的内置函数。 Even though Output is declared as a variant, I believe the value it will hold will contain a floating point value. 即使Output被声明为变量,我相信它将持有的值将包含一个浮点值。 I'm not sure if this is the reason for the conflict. 我不确定这是否是冲突的原因。 It seems you can do this in Python however, which is probably why it is used for scientific calculations. 但是,您似乎可以在Python中执行此操作,这可能就是为什么将其用于科学计算的原因。
In any case, what would be the best way to resolve this? 无论如何,解决此问题的最佳方法是什么?
Unfortunately VBA does not support direct operations on arrays. 不幸的是,VBA不支持对阵列的直接操作。 Instead you have to loop :( 相反,您必须循环:(
If you really want to do it with VBA you could have a look at this CodeReview post . 如果您真的想使用VBA进行此操作,则可以查看此CodeReview帖子 。
If you want to do it using Excel, but cannot accpet the limitations of VBA, there are a few Python tools allowing to program Excel using Python. 如果您想使用Excel来执行此操作,但不能满足VBA的限制,则可以使用一些Python工具来使用Python编写Excel。 A quick search will get you plenty, including https://www.xlwings.org/ which has been on my list of things to try for much too long. 快速搜索将为您带来很多收益,包括https://www.xlwings.org/ ,这在我的清单上已经尝试了太久了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.