📜  Tensorflow.js tf.tile()函数

📅  最后修改于: 2022-05-13 01:56:25.958000             🧑  作者: Mango

Tensorflow.js tf.tile()函数

TensorFlow.js 是一个用于 JavaScript 机器学习的库。它帮助开发人员使用 JavaScript 开发 ML 模型,并直接在浏览器或 Node.js 中使用 ML。

tf.tile()函数用于通过重复 reps 给出的次数来创建张量。

句法:

tf.tile(x, reps)

注意:这个函数通过复制输入的次数来创建一个新的张量。例如,通过 [3] 平铺 [1, 2, 3, 4] 会产生 [1, 2, 3, 4,1, 2, 3, 4,1, 2, 3, 4]。

参数:该函数接受以下两个参数。

  • x:传递给瓦片的张量。它可以是tf.Tensor 、 TypedArray 或 Array。
  • reps:此参数定义每个维度的复制次数。

返回:返回tf.Tensor对象。

示例 1:

Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Creating a tensor1d
const a = tf.tensor1d([1, 2, 3, 4]);
  
// Creating the tensor with the help of tile()
const x = a.tile([2]);
  
// Printing the tensor
x.print();


Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Creating a tensor2d
const a = tf.tensor2d([1, 2, 3, 4, 5, 6],[2, 3]);
  
// Creating the tensor with the help of tile()
const x = a.tile([1,2]);
  
// Printing the tensor
x.print();


Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Creating a tensor2d
const a = tf.tensor2d([1, 2, 3, 4, 5, 6],[2, 3]);
  
// Creating the tensor with the help of tile()
const x = a.tile([3,2]);
  
// Printing the tensor
x.print();


输出:

Tensor
   [1, 2, 3, 4, 1, 2, 3, 4]

示例 2:

Javascript

// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Creating a tensor2d
const a = tf.tensor2d([1, 2, 3, 4, 5, 6],[2, 3]);
  
// Creating the tensor with the help of tile()
const x = a.tile([1,2]);
  
// Printing the tensor
x.print();

输出:

Tensor
   [[1, 2, 3, 1, 2, 3],
    [4, 5, 6, 4, 5, 6]]

示例 3:

Javascript

// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Creating a tensor2d
const a = tf.tensor2d([1, 2, 3, 4, 5, 6],[2, 3]);
  
// Creating the tensor with the help of tile()
const x = a.tile([3,2]);
  
// Printing the tensor
x.print();

输出:

Tensor
   [[1, 2, 3, 1, 2, 3],
    [4, 5, 6, 4, 5, 6],
    [1, 2, 3, 1, 2, 3],
    [4, 5, 6, 4, 5, 6],
    [1, 2, 3, 1, 2, 3],
    [4, 5, 6, 4, 5, 6]]

参考: https://js.tensorflow.org/api/latest/#tile