在 Tensorflow.js 的 model.evaluate 方法中使用来自 tf.data.csv 的数据时出现问题

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

如何在 Tensorflow.js 中的评估方法 'evaluate()' 中使用 'tf.data.csv' 的返回值?

我想在 TFJS 上训练一个简单的模型。首先,我从 CSV 文件中读取数据。然后我训练了模型,最后我计算了损失和准确率。 但是我无法测量 '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>

tf.data.csvreturns一个tf.data.Datasetevaluate 方法需要 tensortensor 的数组。如果您想评估 tf.data.Dataset,可以改用 evaluateDataset 方法。

evaluateDatasetreturns一个承诺。

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