我正在尝试使用Excel VBA编写一个基本的人工神经网络。我特别参考了一个例子:
https://www.analyticsvidhya.com/blog/2017/05/neural-network-from-scratch-in-python-and-r/
我基于文章中的Python示例来编写代码(代码示例位于文章的底部)。不幸的是,我没有Python的使用权限,所以我试图用VBA来完成这个任务。我已经尽力将代码转换成适合Excel的格式,但是我遇到了一个问题,我不确定如何解决:
这是我的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) * LearnRateNextDebug.Print OutputEnd SubFunction Sigmoid_Activation(X) As Variant Sigmoid_Activation = 1 / (1 + Application.WorksheetFunction.Power(-X)) End FunctionFunction Derivatives_Sigmoid(X) As Double Derivatives_Sigmoid = X * (1 - X) End FunctionPublic Function ArrayLen(arr As Variant) As Integer ArrayLen = UBound(arr) - LBound(arr) + 1 End Function
我的问题是:
在反向传播部分,我在这一行得到了类型不匹配的错误:
E = Y – Output
我猜这是因为在VBA中没有内置的函数来从数组(Y)中减去一个Double类型的值(Output)。尽管Output被声明为变体类型,但我认为它将包含一个浮点值。我不确定这是冲突的原因。似乎在Python中可以这样做,这可能就是为什么它被用于科学计算的原因。
无论如何,最好的解决方法是什么?
回答:
不幸的是,VBA不支持直接对数组进行操作。相反,你必须使用循环
如果你真的想用VBA来做,可以看看这个CodeReview帖子。
如果你想使用Excel,但不能接受VBA的限制,有几种Python工具可以让你使用Python来编程Excel。快速搜索一下你会找到很多,包括https://www.xlwings.org/,它一直在我要尝试的列表上已经太久了。