Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/461.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript React.js&;tensorflow.js网页可以';t在控制台和html页面上打印结果值 从“React”导入React; 导入“/App.css”; 从“@tensorflow/tfjs”导入*作为tf 函数App(){ 常数x=[28,23,25,25,26,25,27,28,32,30,35,32,34,30,29] 常数y=[23,25,25,26,25,27,28,32,30,35,32,34,30,29,26] var-res; const model=tf.sequential(); add(tf.layers.dense({units:1,inputShape:[1]})); compile({loss:'meanSquaredError',优化器:tf.train.sgd(0.001)}); 常数xs=tf.tensor2d(x[15,1]) 常数ys=tf.tensor2d(y[15,1]) fit(xs,ys,{epochs:1})。然后(()=>{ res=tf.round(model.predict(tf.tensor2d([50],[1,1])) 警报(res) }) console.log(res) 返回( 结果:{res} ); } 导出默认应用程序;_Javascript_Reactjs_Tensorflow - Fatal编程技术网

Javascript React.js&;tensorflow.js网页可以';t在控制台和html页面上打印结果值 从“React”导入React; 导入“/App.css”; 从“@tensorflow/tfjs”导入*作为tf 函数App(){ 常数x=[28,23,25,25,26,25,27,28,32,30,35,32,34,30,29] 常数y=[23,25,25,26,25,27,28,32,30,35,32,34,30,29,26] var-res; const model=tf.sequential(); add(tf.layers.dense({units:1,inputShape:[1]})); compile({loss:'meanSquaredError',优化器:tf.train.sgd(0.001)}); 常数xs=tf.tensor2d(x[15,1]) 常数ys=tf.tensor2d(y[15,1]) fit(xs,ys,{epochs:1})。然后(()=>{ res=tf.round(model.predict(tf.tensor2d([50],[1,1])) 警报(res) }) console.log(res) 返回( 结果:{res} ); } 导出默认应用程序;

Javascript React.js&;tensorflow.js网页可以';t在控制台和html页面上打印结果值 从“React”导入React; 导入“/App.css”; 从“@tensorflow/tfjs”导入*作为tf 函数App(){ 常数x=[28,23,25,25,26,25,27,28,32,30,35,32,34,30,29] 常数y=[23,25,25,26,25,27,28,32,30,35,32,34,30,29,26] var-res; const model=tf.sequential(); add(tf.layers.dense({units:1,inputShape:[1]})); compile({loss:'meanSquaredError',优化器:tf.train.sgd(0.001)}); 常数xs=tf.tensor2d(x[15,1]) 常数ys=tf.tensor2d(y[15,1]) fit(xs,ys,{epochs:1})。然后(()=>{ res=tf.round(model.predict(tf.tensor2d([50],[1,1])) 警报(res) }) console.log(res) 返回( 结果:{res} ); } 导出默认应用程序;,javascript,reactjs,tensorflow,Javascript,Reactjs,Tensorflow,我想在react制作的网页上打印tensorflow预测结果。 第19行警报(res)正常,但第22行控制台。日志(res)打印“未定义” 第26行结果:{res}不打印res值。 怎么了?正如我们在这一行中看到的model.fit(xs,ys,{epochs:1})。然后(()=>{,fit返回一个 这意味着代码是异步的,回调将在console.log之后执行 您可以将函数转换为异步函数,以生成所需的代码: import React from 'react'; import './App.cs

我想在react制作的网页上打印tensorflow预测结果。 第19行警报(res)正常,但第22行控制台。日志(res)打印“未定义” 第26行结果:{res}不打印res值。
怎么了?

正如我们在这一行中看到的
model.fit(xs,ys,{epochs:1})。然后(()=>{
fit
返回一个

这意味着代码是异步的,回调将在
console.log
之后执行

您可以将函数转换为异步函数,以生成所需的代码:

import React from 'react';
import './App.css';
import * as tf from '@tensorflow/tfjs'

function App() {
  const x = [28, 23, 25, 25, 26, 25, 27, 28, 32, 30, 35, 32, 34, 30, 29]
  const y = [23, 25, 25, 26, 25, 27, 28, 32, 30, 35, 32, 34, 30, 29, 26]
  var res;
    
  const model = tf.sequential();
  model.add(tf.layers.dense({units: 1, inputShape: [1]}));
  model.compile({loss: 'meanSquaredError', optimizer: tf.train.sgd(0.001)});

  const xs = tf.tensor2d(x, [15, 1])
    const ys = tf.tensor2d(y, [15, 1])

  model.fit(xs, ys, { epochs: 1 }).then(() => {
    res = tf.round(model.predict(tf.tensor2d([50], [1, 1])))
    alert(res)
  })
  
  console.log(res)

  return (
    <div>
      <h1>Result: {res} </h1>
    </div>
  );
}

export default App;
从“React”导入React
导入“./App.css”
从“@tensorflow/tfjs”导入*作为tf
异步函数App(){
常数x=[28,23,25,25,26,25,27,28,32,30,35,32,34,30,29]
常数y=[23,25,25,26,25,27,28,32,30,35,32,34,30,29,26]
const model=tf.sequential()
add(tf.layers.dense({units:1,inputShape:[1]}))
compile({loss:'meanSquaredError',优化器:tf.train.sgd(0.001)})
常数xs=tf.tensor2d(x[15,1])
常数ys=tf.tensor2d(y[15,1])
wait model.fit(xs,ys,{epochs:1})
常数res=tf.round(model.predict(tf.tensor2d([50],[1,1]))
console.log(res)
返回(
结果:{res}
)
}
导出默认应用程序

Model.fit是一个承诺,因此console.log en return在Model.fit完成之前执行注意:我从未使用过
tensorflow.js
,因此我不知道您的代码中是否有其他错误
import React from 'react'
import './App.css'
import * as tf from '@tensorflow/tfjs'

async function App() {
  const x = [28, 23, 25, 25, 26, 25, 27, 28, 32, 30, 35, 32, 34, 30, 29]
  const y = [23, 25, 25, 26, 25, 27, 28, 32, 30, 35, 32, 34, 30, 29, 26]
    
  const model = tf.sequential()
  model.add(tf.layers.dense({units: 1, inputShape: [1]}))
  model.compile({loss: 'meanSquaredError', optimizer: tf.train.sgd(0.001)})

  const xs = tf.tensor2d(x, [15, 1])
  const ys = tf.tensor2d(y, [15, 1])

  await model.fit(xs, ys, { epochs: 1 })
  const res = tf.round(model.predict(tf.tensor2d([50], [1, 1])))
  
  console.log(res)

  return (
    <div>
      <h1>Result: {res} </h1>
    </div>
  )
}

export default App