Results 1 to 5 of 5

Thread: Neural networks in VBA

Threaded View

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2012
    Posts
    7

    Neural networks in VBA

    If you guys have some experience from Pytorch or other major deep learning frameworks, and have some time to spare, give my simple neural network project (maybe the first one in VBA?) a test run. Big plus if you can test it with OpenBLAS dll which you put inside the same folder as the Excel workbook and edit the dll path in BlasFunctions module -- OpenBLAS provides prebuilt binary packages here https://sourceforge.net/projects/ope...files/v0.3.27/:

    https://github.com/personalityson/VBANN

    Code:
    Public Sub SetupAndTrainSequential()
        Const MODEL_NAME As String = "MySequentialModel"
        Dim lBatchSize As Long
        Dim lNumEpochs As Long
        Dim lInputSize As Long
        Dim lLabelSize As Long
        Dim oFullSet As TensorDataset
        Dim oTrainingSet As SubsetDataset
        Dim oTrainingLoader As DataLoader
        Dim oTestSet As SubsetDataset
        Dim oTestLoader As DataLoader
        Dim oModel As Sequential
    
        lInputSize = 8
        lLabelSize = 1
        lBatchSize = 16
        lNumEpochs = 40
    
        'Prepare training data
        Set oFullSet = ImportDatasetFromWorksheet(ThisWorkbook, "Concrete", Array(lInputSize, lLabelSize), True, False)
        RandomSplit oFullSet, 0.8, oTrainingSet, oTestSet
        Set oTrainingLoader = DataLoader(oTrainingSet, lBatchSize)
        Set oTestLoader = DataLoader(oTestSet, lBatchSize)
        
        'Setup and train
        Set oModel = Sequential(L2Loss(), SGDM())
        oModel.Add InputNormalizationLayer(oTrainingLoader)
        oModel.Add FullyConnectedLayer(lInputSize, 32)
        oModel.Add LeakyReLULayer()
        oModel.Add FullyConnectedLayer(32, 16)
        oModel.Add LeakyReLULayer()
        oModel.Add FullyConnectedLayer(16, 8)
        oModel.Add LeakyReLULayer()
        oModel.Add FullyConnectedLayer(8, lLabelSize)
        oModel.Fit oTrainingLoader, oTestLoader, lNumEpochs
    
        'Compute test loss
        MsgBox oModel.Loss(oTestLoader)
    
        'Save to worksheet
        Serialize MODEL_NAME, oModel
    
        'Load from worksheet
        Set oModel = Unserialize(MODEL_NAME)
    
        'Compute test loss again with unserialized model
        MsgBox oModel.Loss(oTestLoader)
    
        Beep
    End Sub
    
    Public Sub SetupAndTrainXGBoost()
        Const MODEL_NAME As String = "MyXGBoostModel"
        Dim lInputSize As Long
        Dim lLabelSize As Long
        Dim lNumRounds As Long
        Dim lMaxDepth As Long
        Dim dblLearningRate As Double
        Dim X As Tensor
        Dim T As Tensor
        Dim oFullSet As TensorDataset
        Dim oTrainingSet As SubsetDataset
        Dim oTestSet As SubsetDataset
        Dim oModel As XGBoost
    
        lInputSize = 8
        lLabelSize = 1
        dblLearningRate = 0.1
        lMaxDepth = 6
        lNumRounds = 100
    
        'Prepare training data
        Set oFullSet = ImportDatasetFromWorksheet(ThisWorkbook, "Concrete", Array(lInputSize, lLabelSize), True, True)
        RandomSplit oFullSet, 0.8, oTrainingSet, oTestSet
        Set X = oTrainingSet.Cache.Tensor(1)
        Set T = oTrainingSet.Cache.Tensor(2)
    
        'Setup and train
        Set oModel = XGBoost(L2Loss(), dblLearningRate, lMaxDepth)
        oModel.Fit X, T, lNumRounds
    
        'Compute test loss
        Set X = oTestSet.Cache.Tensor(1)
        Set T = oTestSet.Cache.Tensor(2)
        MsgBox oModel.Loss(X, T)
    
        'Save to worksheet
        Serialize MODEL_NAME, oModel
    
        'Load from worksheet
        Set oModel = Unserialize(MODEL_NAME)
    
        'Compute test loss again with unserialized model
        MsgBox oModel.Loss(X, T)
    
        Beep
    End Sub
    Thanks
    Last edited by drgs; Aug 2nd, 2025 at 04:36 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width