我希望通过Angular2的"synaptic.js"库将机器学习包含在项目中。我已经通过命令安装了这个库
npm install synaptic --save我想运行以下自定义javascript文件(myJsFile.js):
function myFunction() {
var A = new Layer(5);
var B = new Layer(2);
A.project(B);
var learningRate = .3;
for (var i = 0; i < 20000; i++)
{
// when A activates [1, 0, 1, 0, 1]
A.activate([1,0,1,0,1]);
// train B to activate [0,0]
B.activate();
B.propagate(learningRate, [0,0]);
}
// test it
A.activate([1,0,1,0,1]);
console.log(B.activate()); // [0.004606949693864496,0.004606763721459169]
}在我的index.html文件中,我包含了以下代码:
<script src="js/myJsFile.js"></script>在我的app.component.ts文件中
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { AuthenticationService } from '../../Services/authentication.service';
declare var myFunction: any;
@Component({
moduleId: module.id,
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: [ './app.component.css' ]
})
export class AppComponent implements OnInit {
// some code here
// I call f() function inside the app.component.html file in order to run the myFunction() javascript function.
f() {
new myFunction();
}例如,如果警告()函数位于myJsFile.js文件中,则代码将无任何问题地运行。但在我的例子中,我有以下错误:
app.component.html:9错误ReferenceError:层未定义
这意味着我必须在某个地方导入突触库。github (https://github.com/cazala/synaptic)中的使用部分指出了以下几点:
var synaptic = require('synaptic'); // this line is not needed in the browser
var Neuron = synaptic.Neuron,
Layer = synaptic.Layer,
Network = synaptic.Network,
Trainer = synaptic.Trainer,
Architect = synaptic.Architect;如果我将上述代码导入到我的myJsFile.js中,则会得到以下错误:
myJsFile.js:1未定义ReferenceError: require未定义
我应该在哪里导入突触库以及如何导入??
我还找到并安装了synaptic.d.ts文件,但我不知道如何使用它。有谁可以帮我??
非常感谢您的时间!
发布于 2017-09-03 18:26:59
您所举的示例可能是使用Node,对于使用角2+,您需要使用导入语法。
例如:
import { Neuron, Layer, Network, Trainer, Architect} from 'synaptic';那么你应该能够引用它们。
此外,我建议将您的myJsFile.js文件设置为ts文件,并以相同的方式导入。如果所有东西都是在js中导入的,而不是在html中导入的话,情况就不那么复杂了。
https://stackoverflow.com/questions/46026361
复制相似问题