Tensorflow.js tf.Tensor 类
Tensorflow.js 是谷歌开发的一个开源库,用于在浏览器或节点环境中运行机器学习模型和深度学习神经网络。
一个tf.Tensor对象表示一个不可变的多维数字数组,它具有形状和数据类型。张量是 TensorFlow.js 的核心数据结构,它们是将向量和矩阵推广到潜在的更高维度。
句法:
Tensor(value);
属性:该类具有以下属性:
- rank:它定义了张量包含的维数。
- shape:定义数据每个维度的大小。
- dtype:它定义了张量的数据类型。
返回值:它返回一个带有提供值的张量对象。
下面的示例演示了 Tensor 类及其各种方法。
示例 1:在此示例中,我们将创建一个 Tensor 类并查看 print() 方法的示例。此方法用于打印张量类。
Javascript
// Importing the tensorflow library
import * as tf from "@tensorflow/tfjs"
// Creating Tensor with values
let c = tf.tensor([1, 2, 3, 4])
// Using the print() method of Tensor class
c.print();
Javascript
// Importing tensorflow library
import * as tf from "@tensorflow/tfjs"
// Creating Tensor class with value and [4, 1] shape
const a = tf.tensor([1, 2, 3, 4],[4,1]);
// Using the clone() method on a Tensor
let b = a.clone();
// Printing the clone Tensor
b.print();
Javascript
// Importing tensorflow library
import * as tf from "@tensorflow/tfjs"
// Creating tensor
const a = tf.tensor([1, 2, 3, 4]);
// Using toStirng() method in Tensor class
let b = a.toString(true);
console.log(b);
Javascript
// Importing tensorflow library
import * as tf from "@tensorflow/tfjs"
// Creating tensor
const a = tf.tensor([1, 2, 3, 4]);
// Using data method on Tensor class
let b = a.data();
b.then((x)=>console.log(x),
(b)=>console.log("Error while copying"));
Javascript
// Importing tensorflow library
import * as tf from "@tensorflow/tfjs"
// Creating tensor
const a = tf.tensor([1, 2, 3, 4]);
// Using the dataSync() method
let b = a.dataSync();
console.log(b);
Javascript
// Importing tensorflow library
import * as tf from "@tensorflow/tfjs"
// Creating tensor
const a = tf.tensor([1, 2, 3, 4]);
// Using the buffer() method on Tensor class
let b = a.buffer();
// Printing result of Promise
b.then((x)=>console.log(x),
(b)=>console.log("Error while copying") );
Javascript
// Importing tensorflow library
import * as tf from "@tensorflow/tfjs"
// Creating tensor
const a = tf.tensor([1, 2, 3, 4]);
// Using bufferSync method on Tensor class
let b = a.bufferSync();
console.log(b);
Javascript
// Importing tensorflow library
import * as tf from "@tensorflow/tfjs"
// Creating tensor
const a = tf.tensor([1, 2, 3, 4]);
// Using the array() method on Tensor class
let b = a.array();
// Printing result of Promise
b.then((x)=>console.log(x),
(b)=>console.log("Error while copying"));
Javascript
// Importing tensorflow library
import * as tf from "@tensorflow/tfjs"
// Creating tensor
const a = tf.tensor([1, 2, 3, 4]);
// Using the arraySync() method on Tensor class
let b = a.arraySync();
console.log(b);
Javascript
// Importing tensorflow library
import * as tf from "@tensorflow/tfjs"
// Creating tensor
const b = tf.tensor([1, 2, 3, 4]);
// Using the dispose() method on Tensor class
b.dispose();
b.print();
输出:
Tensor
[[1, 2],
[3, 4]]
示例 2:在此示例中,我们将看到 Tensor 类的 clone() 方法。 clone() 方法用于复制现有的 Tensor 类。
Javascript
// Importing tensorflow library
import * as tf from "@tensorflow/tfjs"
// Creating Tensor class with value and [4, 1] shape
const a = tf.tensor([1, 2, 3, 4],[4,1]);
// Using the clone() method on a Tensor
let b = a.clone();
// Printing the clone Tensor
b.print();
输出:
Tensor[[1],
[2],
[3],
[4]]
示例 3:在此示例中,我们使用了 Tensor 类的 toString() 方法。该方法用于以人类可读的形式制作张量类数据。
Javascript
// Importing tensorflow library
import * as tf from "@tensorflow/tfjs"
// Creating tensor
const a = tf.tensor([1, 2, 3, 4]);
// Using toStirng() method in Tensor class
let b = a.toString(true);
console.log(b);
示例 4:在此示例中,我们将看到 Tensor 类的 data() 方法。它返回一个 Promise,它在解析中返回张量的值。
Javascript
// Importing tensorflow library
import * as tf from "@tensorflow/tfjs"
// Creating tensor
const a = tf.tensor([1, 2, 3, 4]);
// Using data method on Tensor class
let b = a.data();
b.then((x)=>console.log(x),
(b)=>console.log("Error while copying"));
输出:
1, 2, 3, 4
示例 5:在此示例中,我们将使用 Tensor 类的 dataSync() 方法。此方法复制张量类的值并返回它们。
Javascript
// Importing tensorflow library
import * as tf from "@tensorflow/tfjs"
// Creating tensor
const a = tf.tensor([1, 2, 3, 4]);
// Using the dataSync() method
let b = a.dataSync();
console.log(b);
输出:
1, 2, 3, 4
示例 6:在此示例中,我们将使用 Tensor 类的 buffer() 方法。它返回 tf.TensorBuffer 的 promise,它保存着底层数据的数据。
Javascript
// Importing tensorflow library
import * as tf from "@tensorflow/tfjs"
// Creating tensor
const a = tf.tensor([1, 2, 3, 4]);
// Using the buffer() method on Tensor class
let b = a.buffer();
// Printing result of Promise
b.then((x)=>console.log(x),
(b)=>console.log("Error while copying") );
输出:
TensorBuffer {
dtype:"float32",
shape:(1) [4],
size:4,
values:1,2,3,4,
strides:(0) [ ]
}
示例 7:在此示例中,我们将使用 bufferSync() 方法。它返回一个保存底层数据的 tf.TensorBuffer。
Javascript
// Importing tensorflow library
import * as tf from "@tensorflow/tfjs"
// Creating tensor
const a = tf.tensor([1, 2, 3, 4]);
// Using bufferSync method on Tensor class
let b = a.bufferSync();
console.log(b);
输出:
TensorBuffer {
dtype:"float32",
shape:(1) [4],
size:4,
values:1,2,3,4,
strides:(0) []
}
示例 8:在此示例中,我们将使用 Tensor 类的 array() 方法。它以嵌套数组的形式返回张量数据的 Promise。
Javascript
// Importing tensorflow library
import * as tf from "@tensorflow/tfjs"
// Creating tensor
const a = tf.tensor([1, 2, 3, 4]);
// Using the array() method on Tensor class
let b = a.array();
// Printing result of Promise
b.then((x)=>console.log(x),
(b)=>console.log("Error while copying"));
输出:
[1, 2, 3, 4]
例 9:在这个例子中,我们将使用 Tensor 类的 arraySync() 方法。它以嵌套形式返回张量数据。
Javascript
// Importing tensorflow library
import * as tf from "@tensorflow/tfjs"
// Creating tensor
const a = tf.tensor([1, 2, 3, 4]);
// Using the arraySync() method on Tensor class
let b = a.arraySync();
console.log(b);
输出:
[1, 2, 3, 4]
示例 10:在本示例中,我们将使用 Tensor 类的 dispose() 方法。它从内存中处理 tf.Tensor。
Javascript
// Importing tensorflow library
import * as tf from "@tensorflow/tfjs"
// Creating tensor
const b = tf.tensor([1, 2, 3, 4]);
// Using the dispose() method on Tensor class
b.dispose();
b.print();
输出:
Tensor is disposed.