question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Hi i want to use BrightWire to implement the following simple interface.

The problem is that the library is very strongly coupled with data source.

 interface IBaseTrainableProbabilisticMulticategoryModel
    {
        //Construct with  NumClasses and NumFeatures

        int[] NumClasses { get; }
        int NumFeatures { get; }
        /// <summary>
        /// Train a batch 
        /// </summary>
        /// <param name="batchData"> [N,numfeatures]</param>
        /// <param name="batchLabels"> [N,subclasses] = clas id</param>
        /// <param name="weights"> [N,subclasses] = weight for subclass. used to balance classes. weights each data point contribution to the loss for each subclass</param>
        /// <returns>Loss</returns>
        float Train(float[][] batchData, int[][] batchLabels, float[][] weights);

        /// <summary>
        /// predict a batch
        /// </summary>
        /// <returns>[subclasses,N,probability distribution]</returns>
        float[][][] Predict(float[][] batchData);
    }

Is there a lower level api where i can implement a feed forward neural network model , that holds its state(weights) , can be serializable , and is always ready for training and prediction.

This is easily achievable with CNTK and Tenroflow. Their problem is that they only suppoort 64 bit.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jdermodycommented, Sep 21, 2021

I see your point - the current design is that everything is created through indirection and the classes themselves are not public from the assembly. The drawback of this is that the usage is less obvious.

For example in BrightWire.Net4 you create a data table builder like this:

var dataTableBuilder = BrightWireProvider.CreateDataTableBuilder();
dataTableBuilder.AddColumn(ColumnType.Float, "capital costs");
dataTableBuilder.AddColumn(ColumnType.Float, "labour costs");
dataTableBuilder.AddColumn(ColumnType.Float, "energy costs");
dataTableBuilder.AddColumn(ColumnType.Float, "output", true);

In BrightWire (,net core) you do it like this:

var context = new BrightDataContext();
var dataTableBuilder = context.BuildTable();
dataTableBuilder.AddColumn(BrightDataType.Float, "capital costs");
dataTableBuilder.AddColumn(BrightDataType.Float, "labour costs");
dataTableBuilder.AddColumn(BrightDataType.Float, "energy costs");
dataTableBuilder.AddColumn(BrightDataType.Float, "output").SetTarget(true);

BrightWire (.net core) is a more consistent in that everything is available through the context, which acts as an extension point via extension methods in other assemblies. I suppose this approach is one of framework rather than library, but perhaps the better design is to be both.

0reactions
PatriceDargentoncommented, Sep 25, 2021

Ok, I found out how it works, thank you very much! Here it is with BrightWire.Net4 in VB .NET:

Public Sub TestXOR()

    Dim lap = BrightWireProvider.CreateLinearAlgebra
    ' Some training data that the network will learn. The XOR pattern looks like:
    ' 0 0 => 0
    ' 1 0 => 1
    ' 0 1 => 1
    ' 1 1 => 0
    Dim builder = BrightWireProvider.CreateDataTableBuilder()
    builder.AddColumn(ColumnType.Float, "X")
    builder.AddColumn(ColumnType.Float, "Y")
    builder.AddColumn(ColumnType.Float, "XOR", True)
    builder.Add(0.0!, 0.0!, 0.0!)
    builder.Add(1.0!, 0.0!, 1.0!)
    builder.Add(0.0!, 1.0!, 1.0!)
    builder.Add(1.0!, 1.0!, 0.0!)
    Dim data = builder
    Dim dataTable = builder.Build()

    ' create the graph
    Dim graph = New GraphFactory(lap)
    Dim errorMetric = graph.ErrorMetric.CrossEntropy
    graph.CurrentPropertySet.Use(graph.GradientDescent.RmsProp)
    ' and gaussian weight initialisation
    graph.CurrentPropertySet.Use(graph.WeightInitialisation.Gaussian)
    ' create the engine
    Dim testData = graph.CreateDataSource(dataTable)

    Dim engine = graph.CreateTrainingEngine(testData, learningRate:=0.1!, batchSize:=4)
    ' create the network
    Const HIDDEN_LAYER_SIZE As Integer = 6
    With graph.Connect(engine)
        ' create a feed forward layer with sigmoid activation
        .AddFeedForward(HIDDEN_LAYER_SIZE).Add(graph.SigmoidActivation)
        ' create a second feed forward layer with sigmoid activation
        .AddFeedForward(engine.DataSource.OutputSize).Add(graph.SigmoidActivation)
        ' calculate the error and backpropagate the error signal
        .AddBackpropagation(errorMetric)
    End With

    ' train the network
    Dim executionContext = graph.CreateExecutionContext
    Dim i = 0
    Do While i < 1000
        engine.Train(executionContext)
        If i Mod 100 = 0 Then engine.Test(testData, errorMetric)
        i += 1
    Loop

    engine.Test(testData, errorMetric)
    ' create a new network to execute the learned network
    Dim networkGraph = engine.Graph
    Dim executionEngine = graph.CreateEngine(networkGraph)
    Dim output = executionEngine.Execute(testData)
    Debug.WriteLine("Average output = " &
        output.Average((Function(o) o.CalculateError(errorMetric))))

    ' print the values that have been learned
    For Each item In output
        For Each index In item.MiniBatchSequence.MiniBatch.Rows
            Dim row = dataTable.GetRow(index)
            Dim result = item.Output(index)
            Dim rOutput! = result.Data(0)
            Debug.WriteLine(
                row.GetField(Of Single)(0) & " XOR " &
                row.GetField(Of Single)(1) & " = " & rOutput.ToString("0.00000"))
        Next
    Next

End Sub
Read more comments on GitHub >

github_iconTop Results From Across the Web

What is the difference between a high-level and low- ...
And a low-level API is a very close-to-the-metal API that is more verbose so to speak or more complicated. I had it backwards...
Read more >
The different types of APIs - Linx
Low -level APIs are much more detailed and specific due to a low level of abstraction. Low-level APIs allow for finer control over...
Read more >
Low-Level vs High-Level API Solutions for Shopping Cart ...
Well-elaborated low-level APIs are more powerful and detailed, though more intricate in use. They are meant for vendors with experienced ...
Read more >
Low-Level API Architecture
The low-level API is the collection of basic classes that implement SNMP according to v1, v2c, and v3 standards. The low-level API consists...
Read more >
Lower Level APIs Overview
Learnosity does have certain lower level APIs, which are APIs that are used as part of the Author, Items and Reports APIs, providing...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found