tensorFlow.js出現(xiàn)于2018年,2019年發(fā)布第一個(gè)穩(wěn)定版本 1.0版本和悦,2020年5月發(fā)布2.0版本,2021年發(fā)布3.0版本
張量和操作
1. 張量(tensor)
tensorFlow.js中數(shù)據(jù)的中央單元為tf.tensor:一組形狀為一維或多維數(shù)組的值渠缕。tf.tensor與多維數(shù)組非常相似鸽素。tf.tensor包含一下屬性
- rank:定義張量包含的維度
- shape:定義數(shù)據(jù)每個(gè)維度的大小
- dtype:定義張量的數(shù)據(jù)類型(默認(rèn)為float32,還有bool亦鳞,int32,complex64和字符串?dāng)?shù)據(jù)類型)
使用tf.tensor()方法從數(shù)組創(chuàng)建tf.tensor
// Create a rank-2 tensor (matrix) matrix tensor from a multidimensional array.
const a = tf.tensor([[1, 2], [3, 4]]);
console.log('shape:', a.shape);
a.print();
// Or you can create a tensor from a flat array and specify a shape.
const shape = [2, 2];
const b = tf.tensor([1, 2, 3, 4], shape);
console.log('shape:', b.shape);
b.print();
const a = tf.tensor([[1, 2], [3, 4]], [2, 2], 'int32');
console.log('shape:', a.shape);
console.log('dtype', a.dtype);
a.print();
更改張量的形狀
tf.Tensor 中元素的數(shù)量是其形狀大小的乘積馍忽。由于通常可以有多個(gè)具有相同大小的形狀燕差,因此將 tf.Tensor 重塑為具有相同大小的其他形狀通常非常實(shí)用遭笋。這可以通過(guò) reshape() 方法實(shí)現(xiàn):
const a = tf.tensor([[1, 2], [3, 4]]);
console.log('a shape:', a.shape);
a.print();
const b = a.reshape([4, 1]);
console.log('b shape:', b.shape);
b.print();
從張量中獲取值
可以使用 Tensor.array() 或 Tensor.data() 方法從 tf.Tensor 中獲取值:
const a = tf.tensor([[1, 2], [3, 4]]);
// Returns the multi dimensional array of values.
a.array().then(array => console.log(array));
// Returns the flattened data that backs the tensor.
a.data().then(data => console.log(data));
同步獲取
const a = tf.tensor([[1, 2], [3, 4]]);
// Returns the multi dimensional array of values.
console.log(a.arraySync());
// Returns the flattened data that backs the tensor.
console.log(a.dataSync());
2. 運(yùn)算
張量可用于存儲(chǔ)數(shù)據(jù),而運(yùn)算則可以用于操作改數(shù)據(jù)徒探。TensorFlow.js 還提供了可對(duì)張量執(zhí)行的適用于線性代數(shù)和機(jī)器學(xué)習(xí)的多種運(yùn)算
對(duì) tf.Tensor 中的所有元素執(zhí)行 x2 計(jì)算:
const x = tf.tensor([1, 2, 3, 4]);
const y = x.square(); // equivalent to tf.square(x)
y.print();
對(duì)兩個(gè) tf.Tensor 的元素執(zhí)行逐元素相加:
const a = tf.tensor([1, 2, 3, 4]);
const b = tf.tensor([10, 20, 30, 40]);
const y = a.add(b); // equivalent to tf.add(a, b)
y.print();
由于張量是不可變的瓦呼,因此這些運(yùn)算不會(huì)改變其值。相反测暗,return 運(yùn)算總會(huì)返回新的 tf.Tensor央串。
- tf.add() (加法)
- tf.sub() (減法)
- tf.mul() (乘法)
- tf.div() (除法)
3. 內(nèi)存
使用WebGL后端時(shí),必須顯示管理tf.Tensor內(nèi)存(即使 tf.Tensor 超出范圍也不足以釋放其內(nèi)存)碗啄。
要銷毀 tf.Tensor 的內(nèi)存蹋辅,您可以使用 dispose() 方法或 tf.dispose():
const a = tf.tensor([[1, 2], [3, 4]]);
a.dispose(); // Equivalent to tf.dispose(a)
模型
模型和層
在機(jī)器學(xué)習(xí)中,模型是一個(gè)帶有可學(xué)習(xí)參數(shù)的函數(shù)挫掏,可將輸入映射至輸出。通過(guò)在數(shù)據(jù)上訓(xùn)練模型獲得最佳參數(shù)秩命。訓(xùn)練好的模型可以提供從輸入到所需輸出的準(zhǔn)確映射尉共。
可以通過(guò)兩種方式創(chuàng)建機(jī)器學(xué)習(xí)模型:
- 使用Layers API(使用層構(gòu)建模型)
- 使用Core API(借助低級(jí)運(yùn)算,例如tf.matMul()弃锐、tf.add()等)
1袄友、使用Layers API創(chuàng)建模型
Layers API模型分為:序貫?zāi)P秃秃瘮?shù)模式
序貫?zāi)P?/h5>
最常見(jiàn)的模型是 <a data-md-type="link">Sequential</a> 模型,序貫?zāi)P褪菍拥木€性堆疊霹菊。您可以通過(guò)將層列表傳遞到 <a data-md-type="link">sequential()</a> 函數(shù)來(lái)創(chuàng)建 Sequential 模型:
訓(xùn)練模型
在TensorFlow.js 中剧蚣,可以通過(guò)兩種方式訓(xùn)練機(jī)器學(xué)習(xí)模型:
- 使用 Layers API 與 <a data-md-type="link">LayersModel.fit()</a> 或 <a data-md-type="link">LayersModel.fitDataset()</a>支竹。
- 使用 Core API 與 <a data-md-type="link">Optimizer.minimize()</a>。
簡(jiǎn)介
機(jī)器學(xué)習(xí)模型是一種具有可學(xué)習(xí)參數(shù)的函數(shù)鸠按,可將輸入映射到所需輸出礼搁。基于數(shù)據(jù)訓(xùn)練模型可以獲得最佳參數(shù)目尖。
訓(xùn)練模型涉及到的步驟:
- 獲取一批次數(shù)據(jù)來(lái)訓(xùn)練模型
- 讓模型做出預(yù)測(cè)
- 將該預(yù)測(cè)與真實(shí)值進(jìn)行對(duì)比
- 確定每個(gè)參數(shù)的更改幅度馒吴,使模型在未來(lái)能夠針對(duì)改批次數(shù)據(jù)做出更好的預(yù)測(cè)