绘制x和y张量

时间:2018-09-21 17:48:19

标签: tensorflow.js

给出这两个张量 x=tf.tensor([1,2,3,4])y=tf.tensor([4,6,7,8]) 我想绘制y=f(x)。是否有任何可视化工具作为张量板?

1 个答案:

答案 0 :(得分:1)

您可以使用图表工具,例如chartjs,d3.js,...

(async function train(){
const x = tf.tensor([1, 2, 3, 4]);
const y = tf.tensor([2, 4, 6, 8]);
const data1 = await x.data()
const data2 = await y.data()
 new Chart(document.getElementById("myChart"), {
      type: 'scatter',
      data: {
        datasets: [{
          label: "x",
          data: data1,
          pointBackgroundColor: "red",
          borderColor: "red"
        }, {
          label: "y",
          data: data2,
          pointBackgroundColor: "green",
          borderColor: "green"
        }]
      },
      options: {
        responsive: true,
        showLines: false,
        legend: {
          labels: {
            usePointStyle: true
          }
        }
      }
    });
})
<html>
  <head>
    <!-- Load TensorFlow.js -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/tensorflow/0.12.4/tf.js"> </script>
    <!-- Load chart.js -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.7.2/Chart.min.js"></script>
  </head>

  <body>
    <canvas id="myChart"></canvas>
  </body>
</html>