Tensorflow.js是Google开发的开源库,用于在浏览器或节点环境中运行机器学习模型和深度学习神经网络。
.tensor4d()函数用于创建一个新的4维张量,其参数为值,形状和数据类型。
用法:
tf.tensor4d(value, shape, dataType)
Parameters: 该函数接受以下三个参数。
- value:张量的值,它可以是嵌套的数字数组,平面数组或TypedArray。
- shape:采用张量的形状。如果未提供张量,则张量将从其值推断其形状。它是一个可选参数。
- datatype:它可以是‘float32’或‘int32’或‘bool’或‘complex64’或‘string’值。它是一个可选参数。
返回值:它返回相同数据类型的张量。返回的张量将始终为4维。
注意:也可以使用tf.tensor()函数实现4d张量函数,但是使用tf.tensor4d()可以使代码易于理解和阅读。
范例1:在这里,我们正在创建一个4d张量并进行打印。为了创建4d张量,我们使用.tensor4d()函数,并使用.print()函数打印张量。在这里,我们会将4d数组(即嵌套数组)传递给value参数。
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs";
// Create the tensor
let example1 = tf.tensor4d([[
[[1, 3], [2, 8]],
[[3, 9], [4, 2]]
]]);
// Print the tensor
example1.print()
输出:
Tensor [[[[1 , 3], [20, 8]], [[3 , 9], [4 , 2]]]]
范例2:在此示例中,我们在此处创建张量,并在其中传递平面数组并指定张量的shape参数。我们将在这里看到shape参数的用法。
Javascript
// Import the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Define the value of the tensor
const value = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
// Specify the shape of the tensor
const shape = [1, 2, 6, 1];
// Create the tensor
let example2 = tf.tensor4d(value, shape);
// Print the tensor
example2.print();
输出:
Tensor [[[[1 ], [2 ], [3 ], [4 ], [5 ], [6 ]], [[7 ], [8 ], [9 ], [10], [11], [12]]]]
在上面的示例中,我们创建了一个尺寸为1 x 2 x 6 x 1的张量。
范例3:在这里,在此示例中,我们将通过指定值,形状和数据类型来创建张量。我们将创建张量,其中所有值均为字符串数据类型。
Javascript
// Import the tensorflow.js library
import * as tf from "@tensorflow/tfjs";
// Define the value of the tensor
const value = ["C", "C++", "Java", "Python",
"PHP", "JS", "SQL", "React"];
// Specify the shape of the tensor
const shape = [1, 2, 4, 1];
// Create the tensor
let example3 = tf.tensor4d(value, shape);
// Print the tensor
example3.print();
输出:
Tensor [[[['C' ], ['C++' ], ['Java' ], ['Python']], [['PHP' ], ['JS' ], ['SQL' ], ['React' ]]]]
相关用法
- PHP imagecreatetruecolor()用法及代码示例
- p5.js year()用法及代码示例
- d3.js d3.utcTuesdays()用法及代码示例
- PHP ImagickDraw getTextAlignment()用法及代码示例
- PHP Ds\Sequence last()用法及代码示例
- PHP Imagick floodFillPaintImage()用法及代码示例
- PHP array_udiff_uassoc()用法及代码示例
- PHP geoip_continent_code_by_name()用法及代码示例
- d3.js d3.map.set()用法及代码示例
- PHP GmagickPixel setcolor()用法及代码示例
- PHP opendir()用法及代码示例
- PHP cal_to_jd()用法及代码示例
- d3.js d3.bisectLeft()用法及代码示例
- PHP stream_get_transports()用法及代码示例
- PHP Ds\Deque pop()用法及代码示例
注:本文由纯净天空筛选整理自gfgking大神的英文原创作品 Tensorflow.js tf.tensor4d() Function。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。