首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在角6项目中使用@tensorflow/tfjs时出错

在角6项目中使用@tensorflow/tfjs时出错
EN

Stack Overflow用户
提问于 2018-07-06 19:19:23
回答 1查看 2.8K关注 0票数 0

我已经生成了一个没有其他依赖项的角6项目,该项目是超级干净的,唯一的依赖项是@tensorflow/tfjs

如果我在localhost上服务我的项目:4200,我得到的信息如下:

模块未找到:错误:无法解析C:\User\user\bla\bla中的“crypto”

问题是,我想要一个tensorflow只支持最近5个版本的特性,无论我从0.11.1和更高版本中选择了什么版本,当web开始打包代码时,总是无法编译或失败。

这是一个关于GitHub的tensorflow.js问题,我已经做了,但还没有解决方案。https://github.com/tensorflow/tfjs/issues/494

在这里可以找到一个活代码。

https://stackblitz.com/edit/angular-eu4cjy

这里也有代码示例

app.component.ts

代码语言:javascript
复制
import { Component, OnInit } from '@angular/core';
import * as tf from '@tensorflow/tfjs';


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



  // TRAINING DATA.
  x_train = tf.tensor2d([[0, 0], [0, 1], [1, 0], [1, 1]]);
  y_train = tf.tensor2d([[0], [1], [1], [0]]);

  // Defining a model.
  model: tf.Sequential;

  prediction: any;

  constructor() { }

  ngOnInit() {

  }

  async initModel() {

    this.model = tf.sequential();
    this.model.add(tf.layers.dense({ units: 8, inputShape: [2], activation: 'tanh' })); // input layer
    this.model.add(tf.layers.dense({ units: 1, activation: 'sigmoid' })); // output layer
    const optimizer = tf.train.sgd(0.01);
    this.model.compile({
      optimizer: optimizer,
      loss: 'binaryCrossentropy',
    });


    // Creating dataset
    const xs = tf.tensor2d([[0, 0], [0, 1], [1, 0], [1, 1]]);
    xs.print();
    const ys = tf.tensor2d([[0], [1], [1], [0]]);
    ys.print();
    // Train the model
    await this.model.fit(xs, ys, {
      batchSize: 1,
      epochs: 1500
    });

    const saveResults = await this.model.save('localstorage://my-model-1');

    const loadedModel = await tf.loadModel('localstorage://my-model-1');
    console.log('Prediction from loaded model:');
    // loadedModel.predict(tf.ones([1, 3])).print();



  }

  train() {
    this.initModel();
  }

  predict() {

    const xs = tf.tensor2d([[0, 0], [0, 1], [1, 0], [1, 1]]);

    this.prediction = this.model.predict(xs);
    console.log(this.prediction);

  }

}

package.json

代码语言:javascript
复制
{
  "name": "angular-template",
  "description": "",
  "homepage": "https://stackblitz.com/edit/angular-eu4cjy",
  "dependencies": {
    "@angular/animations": "^5.0.0",
    "@angular/common": "6.0.0",
    "@angular/compiler": "6.0.0",
    "@angular/core": "6.0.0",
    "@angular/forms": "6.0.0",
    "@angular/http": "^5.0.0",
    "@angular/platform-browser": "6.0.0",
    "@angular/platform-browser-dynamic": "6.0.0",
    "@angular/router": "6.0.0",
    "core-js": "2.5.5",
    "rxjs": "6.1.0",
    "zone.js": "0.8.26",
    "@tensorflow/tfjs": "0.12.0"
  },
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "devDependencies": {
    "@angular/cli": "1.6.7",
    "@angular/compiler-cli": "^5.0.0",
    "@angular/language-service": "^5.0.0",
    "@types/jasmine": "~2.5.53",
    "@types/jasminewd2": "~2.0.2",
    "@types/node": "~6.0.60",
    "codelyzer": "~3.0.1",
    "jasmine-core": "~2.6.2",
    "jasmine-spec-reporter": "~4.1.0",
    "karma": "~1.7.0",
    "karma-chrome-launcher": "~2.1.1",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^1.2.1",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.1.2",
    "ts-node": "~3.0.4",
    "tslint": "~5.3.2",
    "typescript": "~2.4.2"
  }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-07-11 20:16:46

Nick 的帮助下,解决方案是这样的。但在此之前,我希望问题是解决后角或tensorflow版本。

编辑此文件

代码语言:javascript
复制
`node_modules/@angular-devkit/build-angular/src/angular-cli-files/models/webpack-configs/browser.js' 

改变了正则表达式中的线条:

代码语言:javascript
复制
// old:
node: false,
// new:
node: { crypto: true, stream: true },
I found an issue that you should chime-in on to help fix this down the road: angular/angular-cli#10954

希望这能有所帮助!

我找到了新的解决方案10/4/2019

只需在package.json上添加以下行即可

代码语言:javascript
复制
{
  "scripts": { },
  "dependencies": { },
  "devDependencies": { },

  // ======================
  "browser": {
    "crypto": false
  }
  // ======================

}
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51216526

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档