有没有办法 tensorflow.js 输入一个形状 -1 作为形状值之一
is there a way with tensorflow.js to input a shape with -1 as one of the shape value
在 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 是否有等效项?
在使用 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>
在 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 是否有等效项?
在使用 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>