ReferenceError:无法找到变量global

时间:2018-06-11 21:12:59

标签: javascript angular

我对angularjs很新,并试图让它与张量流一起工作。但是当我编译下面的代码时,我得到一个ReferenceError:找不到变量:global in minimal.js:49。我该如何解决?

import { Component, OnInit } from '@angular/core';



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

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  title = 'app';

  linearModel: tf.Sequential;
  prediction: any;

  ngOnInit(){
    this.trainNewModel();

  }

async trainNewModel() {
  //Define a model for linear regression.
  this.linearModel = tf.sequential();
  this.linearModel.add(tf.layers.dense({units: 1, inputShape: [1]}));

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

  // Training data, completely random stuff
  const xs = tf.tensor1d([1.59,2.08,9.98,8.41,6.21,4.75,7.96,5.63,2.43,7.61,1.37,5.44,3.57,1,6.92,9.25,3.83,8.73,8.19,8.03,4.75,6.72,9.22,5.82,3.1,5.56,4.52,5.61,6.44,4.59,4.76,8.95,2.31,9.44,7.51,8.29,1,6.24,9.68,6.83,3.92,5.31,7.86,8.54,2.87,4.67,1.02,6.19,2.07,6.6,1.97,6.13,1.63,5.04,0.58,5.03,2.79,0.66,7.74,1.19,2.98,3.63,4.99,7.71,1.31,6.49,1.23,2.17,5.28,9.66,2.59,1.55,5.75,6.4,8.91,5.1,1.49,7.1,0.47,7.16,8.74,0.37,5.22,4.29,8.22,4.38,6.07,6.96,2.91,1.2,3.33,0.45,2.4,2.08,5.78,4.6,8.47,9.6,5.89,1.45]);
  const ys = tf.tensor1d([9.68,2.84,0.94,7.84,8.63,1.07,8.24,0.25,8.22,4.61,8.44,9.43,4.81,7.27,4.63,3.28,7.17,4.22,1.41,6.82,9.24,9.85,5.35,7.63,8.43,8.98,6.24,4.27,2.12,6.91,2.48,2.3,2.39,2.2,5.29,6.88,1.82,4.9,3.26,1.36,6.58,2.18,2.25,4.04,1.45,7.17,1.63,8.03,8.15,4.07,3.92,7.4,4.35,6.1,0.32,6.76,3.94,0.12,0.57,7.01,5.96,8.07,0.97,0.88,2.31,7.1,0.22,6.83,7.28,3.65,5.14,5.46,6.64,7.9,6.98,9.45,6.77,1.09,7.28,4.15,7.25,2.27,2.69,0.42,4.75,1.18,9.05,6.2,3.88,9.36,3.71,8.19,5.44,5.07,4.81,0.11,6.59,3.64,2.38,5.51]);

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

  console.log('model trained!')
}

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

  }
}

0 个答案:

没有答案