javascripttensorflowtensorflow.jsevaluatetfjs-node

Problem at using the data from tf.data.csv in model.evaluate method at Tensorflow.js


How I can use returned values of 'tf.data.csv' in the evaluation method 'evaluate()' in Tensorflow.js?

I wanted to train a simple model on TFJS. First I read the data from a CSV file. Then I trained the model and finally I calculated the loss and accuracy. But I could not to measure accuracy on the test dataset which is imported by 'tf.data.csv'.

<html>
<head></head>
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest"></script>
    <script lang="js">
        
        async function run(){
            const trainingUrl = 'wdbc-train.csv';
            const trainingData = tf.data.csv(trainingUrl, {
                columnConfigs: {
                    diagnosis:{
                        isLabel: true
                    }
                }
             });
            const numOfFeatures = (await trainingData.columnNames()).length - 1;
            const numOfSamples= 455

            const convertedData =
                  trainingData.map(({xs, ys}) => {
                      const labels = [
                            ys.diagnosis == 1 ? 1 : 0
                             ] 
                      return{ xs: Object.values(xs), ys: Object.values(labels)};
                  }).batch(20);
                  
            const testingUrl = 'wdbc-test.csv';
            
           
            const testingData = tf.data.csv(testingUrl, {
                 columnConfigs: {
                    diagnosis:{
                        isLabel: true
                    }
                }
                
                              
            });
            
       
            const convertedTestingData = // YOUR CODE HERE
                  testingData.map(({xs, ys}) => {
                      const labels = [
                            ys.diagnosis == 1 ? 1 : 0
                             ] 
                      return{ xs: Object.values(xs), ys: Object.values(labels)};
                  }).batch(10);
       
            const numOfTestFeatures = (await testingData.columnNames()).length - 1;
            const a =testingData.toArray()
            console.log(a)
          
            const model = tf.sequential();
            model.add(tf.layers.dense({inputShape: [numOfFeatures], activation: "relu", units: 10}));
            model.add(tf.layers.dense({inputShape: 10 , activation: "relu", units: 10}));
           
           model.add(tf.layers.dense({activation: "sigmoid", units: 1}));                
           model.compile({loss: "binaryCrossentropy", optimizer: tf.train.rmsprop(0.05),metrics: "accuracy"});
             await model.fitDataset(convertedData, 
                             {epochs:2,
                              callbacks:{
                                  onEpochEnd: async(epoch, logs) =>{
                                      console.log("Epoch: " + epoch + " Loss: " + logs.loss );
                                                 

                                  }
                              }});
            
             const result = model.evaluateDataset(convertedData,{batchSize: 10});
            console.log("Accuracy: " + result);
           
            await model.save('downloads://my_model');

        }
        run();
    </script>
<body>
</body>
</html>


Solution

  • tf.data.csv returns a tf.data.Dataset. The evaluate method expect a tensor or an array of tensor. If you would like to evaluate a tf.data.Dataset, the method evaluateDataset can be used instead.

    evaluateDataset returns a promise.

    const data = await model.evaluateDataset(testingData) 
    // data can be a tf.Scalar or an array of tf.Scalar