菜单
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

TensorFlow 操作

  • 方形
  • 重塑形状

张量加法

您可以使用 tensorA.add(tensorB) 将两个张量相加

示例

const tensorA = tf.tensor([[1, 2], [3, 4], [5, 6]]);
const tensorB = tf.tensor([[1,-1], [2,-2], [3,-3]]);

// 张量加法
const tensorNew = tensorA.add(tensorB);

// 结果: [ [2, 1], [5, 2], [8, 3] ]

自己动手试一试 »


张量减法

您可以使用 tensorA.sub(tensorB) 来减去两个张量

示例

const tensorA = tf.tensor([[1, 2], [3, 4], [5, 6]]);
const tensorB = tf.tensor([[1,-1], [2,-2], [3,-3]]);

// 张量减法
const tensorNew = tensorA.sub(tensorB);

// 结果: [ [0, 3], [1, 6], [2, 9] ]

自己动手试一试 »



张量乘法

您可以使用 tensorA.mul(tensorB) 将两个张量相乘

示例

const tensorA = tf.tensor([1, 2, 3, 4]);
const tensorB = tf.tensor([4, 4, 2, 2]);

// 张量乘法
const tensorNew = tensorA.mul(tensorB);

// 结果: [ 4, 8, 6, 8 ]

自己动手试一试 »


张量除法

您可以使用 tensorA.div(tensorB) 来除以两个张量

示例

const tensorA = tf.tensor([2, 4, 6, 8]);
const tensorB = tf.tensor([1, 2, 2, 2]);

// 张量除法
const tensorNew = tensorA.div(tensorB);

// 结果: [ 2, 2, 3, 4 ]

自己动手试一试 »


张量平方

您可以使用 tensor.square() 来计算张量的平方

示例

const tensorA = tf.tensor([1, 2, 3, 4]);

// 张量平方
const tensorNew = tensorA.square();

// 结果 [ 1, 4, 9, 16 ]

自己动手试一试 »


张量重塑

张量中的元素数量是形状中各尺寸的乘积。

由于具有相同尺寸的形状可能不同,因此通常需要将张量重塑为具有相同尺寸的其他形状。

您可以使用 tensor.reshape() 来重塑张量

示例

const tensorA = tf.tensor([[1, 2], [3, 4]]);
const tensorB = tensorA.reshape([4, 1]);

// 结果: [ [1], [2], [3], [4] ]

自己动手试一试 »


×

联系销售

如果您想将 W3Schools 服务用于教育机构、团队或企业,请发送电子邮件给我们
sales@w3schools.com

报告错误

如果您想报告错误,或想提出建议,请发送电子邮件给我们
help@w3schools.com

W3Schools 经过优化,旨在方便学习和培训。示例可能经过简化,以提高阅读和学习体验。教程、参考资料和示例会不断审查,以避免错误,但我们无法保证所有内容的完全正确性。使用 W3Schools 即表示您已阅读并接受我们的使用条款Cookie 和隐私政策

版权所有 1999-2024 Refsnes Data。保留所有权利。W3Schools 由 W3.CSS 提供支持