Прогноз (Predict) 5 значений продаж в соответствии с продажами в предыдущие месяцы с использованием Tensaflow. js в Angular - PullRequest
0 голосов
/ 03 августа 2020

Привет, я новичок в ML и Tensaflow. У меня есть массив, содержащий прошлые значения продаж, и мне нужно предсказать значения продаж на следующие пять месяцев. На данный момент я использовал регрессию, но это дает только одно значение прогноза продаж, как мне получить 5 значений прогноза. Пожалуйста, если есть лучший подход, укажите

@Component({
  selector: 'app-chart-window',
  templateUrl: './chart-window.component.html',
  styleUrls: ['./chart-window.component.css']
})
export class ChartWindowComponent implements OnInit {

  arr: Array<any> =[];
  linearModel: tf.Sequential;
  prediction: any;

  constructor(private salesInteractionService :SalesInteractionService) {}

В OnInit я получаю значения продаж из БД и сохраняю их в массиве (arr), а затем вызывает функцию training (), передавая arr в качестве параметра.

  ngOnInit() {

    this.salesInteractionService.getSalesChartInfo2().subscribe(results =>{
      results.sales.map(chart =>{

        this.arr.push(+chart.total)
      })
    });
    console.log(this.arr);

    this.train(this.arr);
  }

Это моя обучающая функция, здесь я определяю модель, которая ожидает 1 входную форму и выдает 1 единицу, но мне нужно получить 5 значений прогноза продаж.

async train(salesArray: Array<any> =[]): Promise<any> { 

console.log(salesArray);

this.linearModel = tf.sequential();     //define a model for linear regression.
this.linearModel.add(tf.layers.dense({units: 1, inputShape: [1]})); //here the output units should be 5

// prepare the model for training: pecify the loss and the optimizer.
this.linearModel.compile({loss: 'meanSquaredError', optimizer: 'sgd'});


    // training data, completely random stuff
    const xs = tf.tensor1d(salesArray);
    const ys = tf.tensor1d(salesArray);

    // train
    await this.linearModel.fit(xs, ys)

    console.log('model trained!')
  }

функция прогнозирования вызывается при нажатии кнопки

  public predict(val: number) { 

    const output = this.linearModel.predict(tf.tensor2d([val], [1, 1])) as any;
    this.prediction = Array.from(output.dataSync());

    console.log(this.prediction); //this gives only one sales prediction value
  }

}
...