0 Replies
      Latest reply on Mar 21, 2017 9:05 AM by ferasOS
      ferasOS Level 1 Level 1 (0 points)

        I have setup a Neural Network using (Keras with a Tensorflow backend).I have exported the weights and have 84 weights and one bias. The NN has 84 inputs and one output.

         

        I am trying to use the steps (by converting the code to Swift from Objective-C) in https://www.bignerdranch.com/blog/use-tensorflow-and-bnns-to-add-machine-learning-to-your-mac-or-ios-app/

         

        My current code looks like this:

         

            //: Playground - noun: a place where people can play

            import Accelerate

         

            //// //// CREATE MODEL ///

            var OUT_COUNT = 1

            var IN_COUNT  = 84

            var inVectorDescriptor =  BNNSVectorDescriptor(size: IN_COUNT, data_type: BNNSDataTypeFloat32, data_scale: 0.0, data_bias: 0.0)

            var outVectorDescriptor =  BNNSVectorDescriptor(size: OUT_COUNT,   data_type: BNNSDataTypeFloat32, data_scale: 0.0, data_bias: 0.0)

            let weightsPath = Bundle.main.url(forResource: "weights", withExtension: "data")

            let biasesPath = Bundle.main.url(forResource: "biases", withExtension: "data")

            let activation = BNNSActivation(function: BNNSActivationFunctionIdentity, alpha: 0, beta: 0)

            var weightsVector = Array<Float>(repeating:0.0,  count:IN_COUNT*OUT_COUNT)

            var biasesVector = Array<Float>(repeating:0.0, count:OUT_COUNT)

         

            // reading biases

            let biasesText = try String(contentsOf: biasesPath!, encoding:    String.Encoding.utf8)

            var biasesTextArr2 = biasesText.characters.split{$0 == ","}.map(String.init)

            let floatsArrayB = biasesTextArr2.flatMap{ Float($0) }

            biasesVector = floatsArrayB

         

            // reading weights

            let weightsText = try String(contentsOf: weightsPath!, encoding:     String.Encoding.utf8)

            let weightsTextArr = weightsText.characters.split{$0 ==   ","}.map(String.init)

            let floatsArrayW = weightsTextArr.flatMap{ Float($0) }

            weightsVector = floatsArrayW

         

            var weightsVectorBNNS = BNNSLayerData(data: &weightsVector, data_type:    BNNSDataTypeFloat32, data_scale: 0.0, data_bias: 0.0, data_table: nil)

            var biasesVectorBNNS = BNNSLayerData(data: biasesVector, data_type: BNNSDataTypeFloat32, data_scale: 0.0, data_bias: 0.0, data_table: nil)

            var parameters = BNNSFullyConnectedLayerParameters(in_size: IN_COUNT, out_size: OUT_COUNT, weights: weightsVectorBNNS, bias: biasesVectorBNNS, activation:activation)

            let filter = BNNSFilterCreateFullyConnectedLayer(&inVectorDescriptor, &outVectorDescriptor, &parameters, nil)

         

        and then to make a prediction:

         

            let  buffer = ....// data to be tested

            var  output = Array<Float>(repeating:0.0, count:OUT_COUNT)

            let success = BNNSFilterApply(filter, buffer, &output);

         

            /// softmax

            let outputSM = output.map { 1/(1 + exp($0)) }

         

            for (index,value) in output.enumerated() {

             output[index] = log(1 + exp(value))

            }

         

            print(outputSM)

            print(output)

         

        The data in biases and weights are in csv format. The result shown is a nan and that is not what is supposed to come out. I should be getting a value between 0 and 1.

         

        Hope someone can help.

         

        Best,

         

        Feras A.