1

如何在 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>

4

1 回答 1

1

tf.data.csv返回一个tf.data.Dataset。该evaluate方法需要一个tensor或一个数组tensor。如果您想评估 a ,可以使用tf.data.Dataset该方法。evaluateDataset

evaluateDataset返回一个承诺。

const data = await model.evaluateDataset(testingData) 
// data can be a tf.Scalar or an array of tf.Scalar
于 2020-01-16T10:16:34.293 回答