tensorflow.js是否有办法输入-1作为形状值之一的形状

时间:2018-09-24 17:15:51

标签: javascript arrays numpy matrix tensorflow.js

在numpy中,您可以输入一个值为-1的形状,该形状可以做到:

np.arange(9)
>>> array([0,1,2,3,4,5,6,7,8])

np.arange(9).reshape((3,-1))
>>> array([[0,1,2],
           [3,4,5],
           [6,7,8]])

它将推断出剩余的形状。 (3,3)。

但是当我这样做时在tensorflow.js中:

const data = tf.tensor([0,0, 127, 255], [2,-1]);

它抛出一个错误。用tf.js可以做到这一点吗?

1 个答案:

答案 0 :(得分:1)

使用reshape时,您与tensorflow.js的行为相同。但是在创建张量时它不起作用。请参阅以下内容:

const x = tf.tensor1d([1, 2, 3, 4]);
x.reshape([2, -1]).print();
<html>
  <head>
    <!-- Load TensorFlow.js -->
    <script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.12.0"> </script>
  </head>

  <body>
  </body>
</html>