home *** CD-ROM | disk | FTP | other *** search
/ Robotics & Artificial Int…3 (Professional Edition) / Robotics & Artificial Intelligence Tools 2003 (Professional Edition).iso / neural network tool and application / nsinstall.exe / data1.cab / CustomSolutionWizard_Files / ProjectShells / VBShell.bas < prev    next >
Encoding:
BASIC Source File  |  2002-03-08  |  12.1 KB  |  248 lines

  1. Attribute VB_Name = "NNTrainAndGetResponse"
  2. Option Explicit
  3.  
  4. Public resetNetwork As Boolean
  5.  
  6. Function TrainNetwork() As Variant
  7.  
  8.     'DESCRIPTION: This subroutine illustrates how the DLL generated by the Custom
  9.     'Solution Wizard can be used for training in a Visual Basic application.  The
  10.     'subroutine trains the network DLL saving the best weights to a specified file for
  11.     'later use.  Step by step instructions are given on how to use the generated DLL
  12.     'for this purpose.  Read through comments carefully considering all of the IMPLEMENTATION
  13.     'NOTES as you get to them.
  14.  
  15.     'NOTE: This subroutine only applies to users of the Developers level of the Custom
  16.     'Solution Wizard.  The Developers level allows the user to generate learning DLLs,
  17.     'whereas all other levels can only generate recall DLLs.
  18.  
  19.     'Step 1: Create a new neural network object of the NSLearningNetwork type.
  20.     '-------------------------------------------------------------------------
  21.     Dim nn As New NSLearningNetwork
  22.  
  23.     'Step 2: Set the pathName of the generated network DLL.
  24.     '------------------------------------------------------
  25.     'IMPLEMENTATION NOTES: The DLL_PATH_NAME below is defined in the Globals module as the
  26.     'path to the newly generated DLL.
  27.     'MORE INFO: The call to dllPathName will fail if you do not have the Developers
  28.     'level of the Custom Solution Wizard or if you generated the DLL using a recall
  29.     'NeuroSolutions  breadboard.  If either of these cases apply, you will only be able
  30.     'to create NSRecallNetwork objects (which are not trainable).
  31.     nn.dllPathName = DLL_PATH_NAME
  32.  
  33.     'Step 3: Load the initial network weights. (Optional)
  34.     '----------------------------------------------------
  35.     'IMPLEMENTATION NOTES: The WEIGHTS_PATH_NAME below is defined in the Globals module as
  36.     'the path to the weights file which was saved when the DLL was generated.
  37.     'MORE INFO: This step is not necessary.  If this step is not performed, the
  38.     'network will simply start with random initial weights.
  39.     nn.loadWeights WEIGHTS_PATH_NAME
  40.     
  41.     'Resets the network (randomizes the network weights, etc.) if the user has so indicated
  42.     'via the "Reset Network before Training" check box on the VBShellDlg form
  43.     If resetNetwork = True Then
  44.         nn.resetNetwork
  45.     End If
  46.     
  47.     'Step 4: Define the input data.
  48.     '------------------------------
  49.     'IMPLEMENTATION NOTES: The following loads the training input data from your initial
  50.     'network configuration into the array inputData.  The training input data was saved
  51.     'to a file during DLL generation.  The location of this file is specified in
  52.     'the Globals module.  There are a number of ways to load your inputData, including
  53.     'hard-coding, user input, and file access.  This implementation reads the data from
  54.     'a text file using DAO (other file types supported by DAO can also be used - see your
  55.     'Visual Basic documentation or consult the Microsoft Developer Network for more
  56.     'information on DAO).  This approach maximizes flexibility for both data access and
  57.     'manipulation.
  58.     'MORE INFO: Note that the number of inputs in the input data must match the number of inputs
  59.     'expected by the generated DLL.  Also, there can be any number of exemplars in the
  60.     'input data as long as the number of exemplars in the input data matches the number
  61.     'of exemplars in the desired data.
  62.     Dim inputData As Variant
  63.     inputData = createDataArrayFromFile(DATA_PATH, INPUT_FILE_NAME)
  64.     
  65.     'Step 5: Send the input data to the network DLL.
  66.     '-----------------------------------------------
  67.     nn.inputData = inputData
  68.  
  69.     'Step 6: Define the desired data.
  70.     '--------------------------------
  71.     'IMPLEMENTATION NOTES: The following loads the training desired data from your initial
  72.     'network configuration into the array desiredData.  The training desired data was saved
  73.     'to a file during DLL generation.  The location of this file is specified in
  74.     'the Globals module.  There are a number of ways to load your desiredData, including
  75.     'hard-coding, user input, and file access.  This implementation reads the data from
  76.     'a text file using DAO (other file types supported by DAO can also be used - see your
  77.     'Visual Basic documentation or consult the Microsoft Developer Network for more
  78.     'information on DAO).  This approach maximizes flexibility for both data access and
  79.     'manipulation.
  80.     'MORE INFO: Note that the number of outputs in the desired data must match the number of outputs
  81.     'expected by the generated DLL.  Also, there can be any number of exemplars in the
  82.     'desired data as long as the number of exemplars in the desired data matches the number
  83.     'of exemplars in the input data.
  84.     Dim desiredData As Variant
  85.     desiredData = createDataArrayFromFile(DATA_PATH, DESIRED_FILE_NAME)
  86.     
  87.     'Step 7: Send the desired data to the network DLL.
  88.     '-------------------------------------------------
  89.     nn.desiredData = desiredData
  90.     
  91.     'Step 8: Enable the automatic saving of the best weights. (Optional)
  92.     '-------------------------------------------------------------------
  93.     'MORE INFO: This step is not necessary.  The default value of this property is
  94.     'always False.  Setting it to True will cause the best network weights to be saved
  95.     'during training.  If this property is set to True, be sure to set the
  96.     'bestWeightsPathName to a valid pathName (See Step 9).
  97.     nn.saveBestWeightsEnabled = True
  98.     
  99.     'Step 9: Set the pathName for saving the best weights. (Optional)
  100.     '----------------------------------------------------------------
  101.     'IMPLEMENTATION NOTES: The BEST_WEIGHTS_PATH_NAME below is defined in the
  102.     'Globals module.
  103.     'MORE INFO: The specified pathName will be used for saving the best weights during
  104.     'training.  This bestWeightsPathName property must be set to a valid pathName
  105.     'if saveBestWeightsEnabled has been set to True.  Otherwise, setting this property
  106.     'is optional.
  107.     nn.bestWeightsPathName = BEST_WEIGHTS_PATH_NAME
  108.     
  109.     'Step 10: Train the network.
  110.     '---------------------------
  111.     'IMPLEMENTATION NOTES: The NUMBER_OF_EPOCHS is determined from the initial network configuration
  112.     'and is specified in the Globals module.
  113.     nn.train NUMBER_OF_EPOCHS
  114.     
  115.     'Step 11: Get the best cost achieved during training.
  116.     '----------------------------------------------------
  117.     Dim bestCost As Single
  118.     bestCost = nn.bestCost
  119.     TrainNetwork = bestCost
  120.     
  121.     'Step 12: Release the neural network object.
  122.     '-------------------------------------------
  123.     Set nn = Nothing
  124.     
  125. End Function
  126.  
  127. Function GetNetworkResponse() As Variant
  128.  
  129.     'DESCRIPTION: This function illustrates how the DLL generated by the Custom
  130.     'Solution Wizard can be used for getting the network response (output) in a Visual
  131.     'Basic application.  Step by step instructions are given on how to use the
  132.     'generated DLL for this purpose.  Read through comments carefully considering all
  133.     'of the IMPLEMENTATION NOTES as you get to them.  This function returns the network
  134.     'response.
  135.  
  136.     'NOTE: This subroutine applies to users of any level of the Custom Solution Wizard.
  137.     
  138.     'Step 1: Create a new neural network object of the NSRecallNetwork type.
  139.     '-----------------------------------------------------------------------
  140.     'MORE INFO: You could create an NSLearningNetwork object network instead, if you
  141.     'have the Developers level of the Custom Solution Wizard and used it along with a
  142.     'NeuroSolutions breadboard capable of learning to generate the DLL.
  143.     Dim nn As New NSRecallNetwork
  144.  
  145.     'Step 2: Set the pathName of the generated network DLL.
  146.     '------------------------------------------------------
  147.     'IMPLEMENTATION NOTES: The DLL_PATH_NAME below is defined in the Globals module as the
  148.     'path to the newly generated DLL.
  149.     nn.dllPathName = DLL_PATH_NAME
  150.  
  151.    'Step 3: Load the saved network weights.
  152.     '---------------------------------------
  153.     'IMPLEMENTATION NOTES: The BEST_WEIGHTS_PATH_NAME below is defined in the Globals module.
  154.     'The initial best weights file is an exact copy of the weights file that was saved
  155.     'when the DLL was generated. However, the best weights file will change with each
  156.     'run of the TrainNetwork function if the network is reset before training.
  157.     nn.loadWeights BEST_WEIGHTS_PATH_NAME
  158.     
  159.     'Step 4: Define the input data.
  160.     '------------------------------
  161.     'IMPLEMENTATION NOTES: The following loads the training input data from your initial
  162.     'network configuration into the array inputData.  The training input data was saved
  163.     'to a file during DLL generation.  The location of this file is specified in
  164.     'the Globals module.  There are a number of ways to load your inputData, including
  165.     'hard-coding, user input, and file access.  This implementation reads the data from
  166.     'a text file using DAO (other file types supported by DAO can also be used - see your
  167.     'Visual Basic documentation or consult the Microsoft Developer Network for more
  168.     'information on DAO).  This approach maximizes flexibility for both data access and
  169.     'manipulation.
  170.     'MORE INFO: Note that the number of inputs in the input data must match the number of inputs
  171.     'expected by the generated DLL.
  172.     Dim inputData As Variant
  173.     inputData = createDataArrayFromFile(DATA_PATH, INPUT_FILE_NAME)
  174.     
  175.     'Step 5: Send the input data to the network DLL.
  176.     '-----------------------------------------------
  177.     nn.inputData = inputData
  178.     
  179.     'Step 6: Get the network response (output).
  180.     '------------------------------------------
  181.     'MORE INFO: The network response is assigned to the return value of GetNetworkResponse
  182.     'function.
  183.     GetNetworkResponse = nn.getResponse
  184.     
  185.     'Step 7: Release the neural network object.
  186.     '------------------------------------------
  187.     Set nn = Nothing
  188.  
  189. End Function
  190.  
  191. Function createDataArrayFromFile(filePath As String, fileName As String) As Variant
  192.  
  193.     'The following describes general information about the use of this function.
  194.     'Usually, no further action is required. However, this information is
  195.     'provided to explain how use this function on a different computer or with
  196.     'a different dataset.
  197.     
  198.     'In order to use this function, you must have Microsoft DAO 3.6 / Jet 4.0 installed
  199.     'on your machine (installed during the Custom Solution Wizard installation if
  200.     'not already present).
  201.     
  202.     'You must also have a reference to the Microsoft DAO 3.6 Object Library.
  203.     'To add this reference, within Visual Basic, goto to Project menu, click
  204.     'References, then place a check next to the Microsoft DAO 3.6 Object Library
  205.     'item in the resulting list box. Note: This reference has already been added for
  206.     'this project.
  207.  
  208.     'Furthermore, use of this function requires that you have created a schema.ini
  209.     'file defining the format of your data file and placed this file in the same
  210.     'directory as your data file (already done for the training data taken from
  211.     'the breadboard used to create the DLL).
  212.     
  213.     'Consider for example the following tab delimited XOr input data:
  214.     'x   y
  215.     '0   0
  216.     '0   1
  217.     '1   0
  218.     '1   1
  219.     
  220.     'If this XOr input data were placed within a text file named "XOr.txt", a "schema.ini"
  221.     'file (placed in the same directory as the "XOr.txt" file) with the following
  222.     'information could be used to correctly read this file:
  223.     
  224.     '[XOr.txt]
  225.     'ColNameHeader = True
  226.     'Format = TabDelimited
  227.     'MaxScanRows = 0
  228.     'CharacterSet = ANSI
  229.     
  230.     'For more information on using schema.ini files see the Reading Data from Text
  231.     'Files topic in the Custom Solution Wizard help.
  232.  
  233.     Dim dbs As Database
  234.     Set dbs = OpenDatabase(filePath, False, True, "Text;")
  235.  
  236.     Dim rst As Recordset
  237.     Set rst = dbs.OpenRecordset(fileName, dbOpenSnapshot)
  238.     rst.MoveLast
  239.     Dim numberOfRecords As Long
  240.     numberOfRecords = rst.RecordCount
  241.     rst.MoveFirst
  242.     createDataArrayFromFile = rst.GetRows(numberOfRecords)
  243.     
  244.     rst.Close
  245.     dbs.Close
  246.  
  247. End Function
  248.