Tensorflow.js是Google開發的開源庫,用於在瀏覽器或節點環境中運行機器學習模型和深度學習神經網絡。
.scalar()函數用於創建張量均值的標量類型。定標器是zero-dimension數組,也稱為0級張量。使用.scalar()函數創建標量。
用法:
t.scalar( value, dataType )
參數:
- value:標量的值。該值可以是數字,字符串,Uint8Array [],布爾值。
- dataType [可選]:值的數據類型。它可以是int32,float32,bool,complex64或字符串。
返回值:它返回張量對象。
創建一個標量:在此示例中,我們將創建一個新的標量,表示僅一個值的張量。
範例1:
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Value of a scalar
var value = 12
// Creating the value of a scalar
var tens = tf.scalar(value)
Printing the scalar
tens.print();
輸出:
Tensor 12
創建特定數據類型的標量:在此示例中,我們將創建特定數據類型的標量。請注意,數據類型隻能是int32,float32,bool,complex64或string。
範例2:
Javascript
// Importing the tennsorflow.js library
import * as tf from "@tensorflow/tfjs"
// Creating a scalar using int value
var int_tensor = tf.scalar(12, 'int32')
int_tensor.print()
// Creating a scalar using string value
var str_tensor = tf.scalar("GFG", "string")
str_tensor.print()
// Creating a scalar using float value
var float_tensor = tf.scalar(12.6, "float32")
float_tensor.print();
// Creating a scalar using bool value
var bool_tensor1 = tf.scalar(true, "bool")
bool_tensor1.print()
// Creating a scalar using bool(0 and 1) type
var bool_tensor2 = tf.scalar(0, "bool")
bool_tensor2.print()
輸出:
Tensor 12 Tensor GFG Tensor 12.600000381469727 Tensor true Tensor false
注意:您也可以使用tf.tensor()函數創建標量。讓我們看一個例子
使用tf.tensor()創建標量:
範例3:
Javascript
// Importing the tennsorflow.js library
import * as tf from "@tensorflow/tfjs"
// Creating a scalar using int tf.tensor()
var tens = tf.tensor(12, [], "int32")
tens.print()
在這裏,我們為函數的第二個參數提供了一個空數組,因為我們正在創建一個標量,並且標量是rank-0張量。
輸出:
Tensor 12
相關用法
- PHP imagecreatetruecolor()用法及代碼示例
- p5.js year()用法及代碼示例
- d3.js d3.utcTuesdays()用法及代碼示例
- PHP ImagickDraw getTextAlignment()用法及代碼示例
- PHP Ds\Sequence last()用法及代碼示例
- 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()用法及代碼示例
- PHP SimpleXMLElement children()用法及代碼示例
- p5.js removeElements()用法及代碼示例
- PHP Imagick adaptiveSharpenImage()用法及代碼示例
注:本文由純淨天空篩選整理自_saurabh_jaiswal大神的英文原創作品 Tensorflow.js tf.scalar() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。