Tensorflow.js是一個開放源代碼庫,用於在瀏覽器或節點環境中運行機器學習模型和深度學習神經網絡。 tf.ones()函數用於創建一個新的張量,其中所有元素都設置為1。
用法:
tf.ones(shape, dtype, name)
參數:
- shape:它采用張量的形狀。它可以是多維數組或int32
- dtype:它采用我們要插入的1的數據類型。默認情況下將其設置為float32,但是也可以將其設置為int32。它是一個可選參數。
- name:它采用了我們正在執行的操作的名稱。默認情況下為“無”。它是一個可選參數。
返回值:它返回具有相同數據類型的張量。
範例1:在此示例中,我們將創建一個二維張量,其所有值均設置為整數數據類型的1。
的JavaScript
// Import tensorflow
import tensorflow as tf
// Get a Tensor
val = tf.ones([10, 10], tf.int32)
// Print the Tensor
print(val)
輸出:
tf.Tensor( [[1 1 1 1 1 1 1 1 1 1] [1 1 1 1 1 1 1 1 1 1] [1 1 1 1 1 1 1 1 1 1] [1 1 1 1 1 1 1 1 1 1] [1 1 1 1 1 1 1 1 1 1] [1 1 1 1 1 1 1 1 1 1] [1 1 1 1 1 1 1 1 1 1] [1 1 1 1 1 1 1 1 1 1] [1 1 1 1 1 1 1 1 1 1] [1 1 1 1 1 1 1 1 1 1]], shape=(10, 10), dtype=int32)
範例2:在此示例中,我們將創建一個一維張量,其所有值都設置為float數據類型的1。
的JavaScript
// Import tensorflow
import tensorflow as tf
// Get a Tensor
val = tf.ones(5, tf.float32)
// Print a Tensor
print(val)
輸出:
tf.Tensor([1. 1. 1. 1. 1.], shape=(5,), dtype=float32)
相關用法
- 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()用法及代碼示例
注:本文由純淨天空篩選整理自pratikraut0000大神的英文原創作品 Tensorflow.js tf.ones() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。