开始使用 Tensorflow JS 模型和训练。太阳能发电预测

Getting started using Tensorflow JS models and training. Prediction of solar power production

我想问一个可能非常菜鸟的问题,关于我在 TensorFlow.js 的第一步。我想创建一个模型,可以根据一天中的云层百分比和温度预测每日的太阳能发电量。

这个问题我有三个数组:

const production = [["0.00","0.00","0.00","0.00","0.00","0.03","0.20","0.42","0.85","1.51","1.58","1.46","1.68","1.68","0.51","0.24","0.14","0.05","0.00","0.00","0.00","0.00","0.00","0.00"],["0.00","0.00","0.00","0.00","0.00","0.01","0.12","0.29","0.81","1.42","1.62","2.09","2.26","1.77","0.44","0.20","0.11","0.04","0.00","0.00","0.00","0.00","0.00","0.00"]]
const clouds = [["0.90","0.90","0.90","0.90","0.80","0.75","0.75","0.75","0.72","0.27","0.34","0.58","0.35","0.20","0.20","0.20","0.20","0.17","0.20","0.20","0.20","0.20","0.20","0.01"],["0.74","0.20","0.22","0.39","0.79","0.75","0.75","0.75","0.50","0.40","0.40","0.40","0.40","0.40","0.26","0.30","0.20","0.20","0.20","0.33","0.77","0.90","0.90","0.90"]]
const temp = [["15.50","15.22","14.65","14.35","13.84","14.46","15.97","17.08","18.30","19.51","20.39","21.00","21.60","21.94","21.94","21.63","20.89","19.32","17.58","16.40","15.63","14.86","14.16","13.64"],["14.98","14.97","14.89","14.51","14.47","15.14","15.83","16.69","17.89","19.10","20.06","20.84","21.46","21.91","21.58","20.99","19.75","18.06","16.75","15.74","15.49","15.26","15.29","15.45"]]

我想使用 Tensorflow.js 来预测当天的产量,当我询问天气时 API 每小时的云层百分比和温度的一天。将结果作为 [clouds] [temp] 放入我的模型中,并从模​​型 [production] 中接收一个新数组。每个数组由 24 个数字组成,代表一天中的小时数。我认为它是顺序的,因为通常下面的等式应该有效:[production] = a + b*[clouds] + c*[temp]

我使用 Boston housing example 进行了很多试验,但我似乎无法将我的参数与该模型相匹配。如果有人能在示例中适合我的测量值,那就太好了。

目前我正在使用这个 class:

import * as tf from '@tensorflow/tfjs';

/**
* Linear model class
*/
export default class LinearModel {
  /**
 * Train model
 */
  async trainModel(temp, clouds, production){
    const layers = tf.layers.dense({
      units: 24, // Dimensionality of the output space
      inputShape: [24], // Only one param
    });
    const lossAndOptimizer = {
      loss: 'meanSquaredError',
      optimizer: 'sgd', // Stochastic gradient descent
    };

    this.linearModel = tf.sequential();
    this.linearModel.add(layers); // Add the layer
    this.linearModel.compile(lossAndOptimizer);

    // Start the model training!
    await this.linearModel.fit(
      [tf.tensor2d(temp), tf.tensor2d(clouds)],
      tf.tensor2d(production),
    );
  }
}

调用是这样的:

const model = new LinearModel()
await model.trainModel(temp, clouds, production)

正如您可能注意到的那样,代码暂时失败了。它与我价值观的 2d-3d 维度有关。但是我真的没有找到线索。

错误日志:

ValueError: Error when checking model input: the Array of Tensors that you are passing to your model is not the size the model expected. Expected to see 1 Tensor(s), but instead got the following list of Tensor(s): Tensor
    [['13.14', '12.70', '12.09', ..., '16.22', '16.11', '15.96'],
     ['15.82', '15.84', '15.73', ..., '15.34', '14.31', '13.30'],
     ['12.83', '12.36', '11.86', ..., '14.29', '13.87', '13.66'],
     ['13.62', '13.67', '13.68', ..., '14.89', '14.42', '14.05'],
     ['13.95', '14.00', '14.14', ..., '16.27', '16.24', '16.20'],
     ['16.23', '16.14', '16.00', ..., '15.39', '15.10', '14.82'],
     ['14.98', '14.97', '14.89', ..., '15.26', '15.29', '15.45'],
     ['15.41', '15.35', '15.23', ..., '15.91', '15.65', '15.69'],
     ['15.50', '15.22', '14.65', ..., '14.86', '14.16', '13.64'],
     ['13.23', '12.88', '12.55', ..., '16.76', '16.10', '15.50'],
     ['14.83', '14.22', '13.59', ..., '15.49', '14.84', '14.20'],
     ['13.70', '13.23', '12.89', ..., '19.00', '19.00', '19.00'],
     ['19.00', '19.00', '19.00', ..., '16.09', '15.43', '14.77'],
     ['14.55', '14.09', '19.00', ..., '18.50', '17.10', '16.29'],
     ['15.78', '15.13', '14.75', ..., '19.00', '17.05', '16.83'],
     ['16.55', '16.06', '15.70', ..., '19.00', '18.49', '18.50'],
     ['18.22', '18.27', '18.08', ..., '16.93', '16.27', '16.00'],
     ['15.98', '16.12', '16.19', ..., '19.00', '19.00', '19.00'],
     ['19.00', '19.00', '19.00', ..., '19.00', '19.00', '19.00'],
     ['19.00', '19.00', '19.00', ..., '13.16', '18.64', '17.90']],Tensor
    [['0.22', '0.28', '0.17', ..., '0.79', '0.90', '0.80'],
     ['0.86', '0.87', '0.75', ..., '0.27', '0.20', '0.20'],
     ['0.20', '0.20', '0.20', ..., '0.20', '0.20', '0.20'],
     ['0.20', '0.20', '0.20', ..., '0.66', '0.20', '0.13'],
     ['0.78', '0.29', '0.46', ..., '0.90', '0.82', '0.90'],
     ['0.90', '0.84', '0.90', ..., '0.25', '0.53', '0.90'],
     ['0.74', '0.20', '0.22', ..., '0.90', '0.90', '0.90'],
     ['0.90', '0.90', '0.90', ..., '0.44', '0.68', '0.90'],
     ['0.90', '0.90', '0.90', ..., '0.20', '0.20', '0.01'],
     ['0.06', '0.06', '0.06', ..., '0.20', '0.20', '0.20'],
     ['0.20', '0.20', '0.25', ..., '0.03', '0.02', '0.04'],
     ['0.10', '0.10', '0.09', ..., '0.20', '0.20', '0.20'],
     ['0.20', '0.20', '0.20', ..., '0.20', '0.55', '0.59'],
     ['0.85', '0.69', '0.20', ..., '0.20', '0.20', '0.20'],
     ['0.20', '0.19', '0.27', ..., '0.20', '0.20', '0.20'],
     ['0.20', '0.20', '0.20', ..., '0.20', '0.97', '0.96'],
     ['0.20', '0.20', '0.20', ..., '0.20', '0.20', '0.90'],
     ['0.90', '0.90', '0.90', ..., '0.20', '0.20', '0.20'],
     ['0.20', '0.20', '0.20', ..., '0.20', '0.20', '0.20'],
     ['0.20', '0.20', '0.20', ..., '0.20', '0.54', '0.20']]
    at new ValueError (/Users/hacor/regression-test/node_modules/@tensorflow/tfjs-layers/dist/tf-layers.node.js:9827:28)
    at standardizeInputData (/Users/hacor/regression-test/node_modules/@tensorflow/tfjs-layers/dist/tf-layers.node.js:18254:19)
    at LayersModel.standardizeUserDataXY (/Users/hacor/regression-test/node_modules/@tensorflow/tfjs-layers/dist/tf-layers.node.js:19125:13)
    at LayersModel.<anonymous> (/Users/hacor/regression-test/node_modules/@tensorflow/tfjs-layers/dist/tf-layers.node.js:19147:35)
    at step (/Users/hacor/regression-test/node_modules/@tensorflow/tfjs-layers/dist/tf-layers.node.js:9745:23)
    at Object.next (/Users/hacor/regression-test/node_modules/@tensorflow/tfjs-layers/dist/tf-layers.node.js:9726:53)
    at /Users/hacor/regression-test/node_modules/@tensorflow/tfjs-layers/dist/tf-layers.node.js:9719:71
    at new Promise (<anonymous>)
    at __awaiter (/Users/hacor/regression-test/node_modules/@tensorflow/tfjs-layers/dist/tf-layers.node.js:9715:12)
    at LayersModel.standardizeUserData (/Users/hacor/regression-test/node_modules/@tensorflow/tfjs-layers/dist/tf-layers.node.js:19142:16)

如有任何帮助,我们将不胜感激!

此致,

哈科尔

输入始终是一个张量,而您要发送两个 - 您需要将 temp, clouds 组合成一个张量

有多种方法可以做到这一点,但很可能您希望将每个方法都作为一个单独的维度,所以只需将它们堆叠起来即可:

const inputs = tf.stack([temp, clouds)]);
trainModel(inputs, production)
...

顺便说一句,你的模型除了一个损失函数外没有任何层可以做任何事情 - 所以它不会做太多
看看 https://github.com/vladmandic/stocks/blob/main/src/model.js 看看我指的是什么。