Tensorflow.js tf.data.Dataset 类 .prefetch() 方法
Tensorflow.js是谷歌开发的一个开源库,用于在浏览器或节点环境中运行机器学习模型和深度学习神经网络。
tf.data.Dataset 类 .prefetch()函数用于生成从给定数据集中预取指定元素的数据集。
句法:
prefetch (bufferSize)
参数:此函数接受一个参数,如下所示:
- bufferSize:它是一个整数值,指定要预取的元素个数。
返回值:它返回元素的数据集。
示例 1:
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Calling the .prefetch() function over
// the specified dataset of some elements
const a = tf.data.array([5, 10, 15, 20]).prefetch(4);
// Getting the dataset of prefetched elements
await a.forEachAsync(a => console.log(a));
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Specifying a dataset of some elements
const a = tf.data.array(["a", "b", "c", "d", "e"]);
// Calling the .prefetch() function over
// the above dataset along with the
// batch of size 2
const b = a.batch(2)
const c = b.prefetch(2)
// Getting the dataset of prefetched elements
await c.forEachAsync(c => console.log(c));
输出:
5
10
15
20
示例 2:
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Specifying a dataset of some elements
const a = tf.data.array(["a", "b", "c", "d", "e"]);
// Calling the .prefetch() function over
// the above dataset along with the
// batch of size 2
const b = a.batch(2)
const c = b.prefetch(2)
// Getting the dataset of prefetched elements
await c.forEachAsync(c => console.log(c));
输出:
Tensor
['a', 'b']
Tensor
['c', 'd']
Tensor
['e']
参考: https://js.tensorflow.org/api/latest/#tf.data.Dataset.prefetch