excel - 在 Excel 中编写 ANN : VBA Type Mismatch Error

标签 excel vba machine-learning neural-network backpropagation

所以我尝试使用 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) * 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

我的问题是:

在反向传播下的部分。我收到类型不匹配错误:

E = Y – Output

我想这是因为在 VBA 中没有内置函数用于从数组 (Y) 中减去 Double 类型值 (输出)。尽管输出被声明为变体,但我相信它将保存的值将包含浮点值。我不确定这是否是冲突的原因。不过,您似乎可以在 Python 中执行此操作,这可能就是它用于科学计算的原因。

无论如何,解决这个问题的最佳方法是什么?

最佳答案

遗憾的是,VBA 不支持对数组进行直接操作。相反,你必须循环:(

如果你真的想用 VBA 来做,你可以看看这个 CodeReview post

如果您想使用 Excel 来完成此操作,但无法接受 VBA 的限制,有一些 Python 工具允许使用 Python 对 Excel 进行编程。快速搜索将为您提供大量内容,包括 https://www.xlwings.org/这已经在我的要尝试的事情 list 上太久了。

关于excel - 在 Excel 中编写 ANN : VBA Type Mismatch Error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53499503/

相关文章:

machine-learning - 神经网络模型中隐藏层的数量

vba - 在 excel 中,只想对某些值求和(不像 SUMIF 那样简单)?

javascript - 对象不支持 IE11 中的属性或方法 'getElementsById'

vba - 使用 CURRENTREGION 函数通过 VBA 使用现有数据创建表

VBA 类及其自身集合

machine-learning - 准确度随训练数据大小的变化而波动

python - 考虑 Pandas 中的几个属性,删除重复项

vba - 获取承载当前运行的 VBA 代码的工作簿的名称?

excel - 如何在Grails excel-export/apache-POI插件中设置背景颜色?

vba - 使用 VBA-Macros 抓取源代码