Tensorflow.js是由Google开发的开源库,用于在浏览器或节点环境中运行机器学习模型以及深度学习神经网络。
.addLoss() 函数用于将损失附加到所述层。此外,损失可能取决于几个输入张量,例如操作损失取决于所述层的输入。
用法:
addLoss(losses)
参数:
- losses:它是声明的损失。它可以是RegularizerFn 或RegularizerFn[] 类型。
返回值:它返回void。
范例1:
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Creating a model
const model = tf.sequential();
// Adding a layer
model.add(tf.layers.dense({units:1, inputShape:[3]}));
// Defining input
const input = tf.tensor1d([1, 2, 3, 4]);
// Calling addLoss() method with its
// parameter
const res = model.layers[0].addLoss([tf.abs(input)]);
// Printing output
console.log(JSON.stringify(input));
model.layers[0].getWeights()[0].print();
输出:
{"kept":false,"isDisposedInternal":false,"shape":[4],"dtype":"float32", "size":4,"strides":[],"dataId":{"id":82},"id":124,"rankType":"1","scopeId":61} Tensor [[0.143441 ], [-0.58002 ], [-0.5836995]]
范例2:
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
// Creating a model
const model = tf.sequential();
// Adding layers
model.add(tf.layers.dense({units:1, inputShape:[3]}));
model.add(tf.layers.dense({units:4}));
model.add(tf.layers.dense({units:9, inputShape:[11]}));
// Defining inputs
const input1 = tf.tensor1d([0.5, 0.2, -33, null]);
const input2 = tf.tensor1d([0.33, 0.5, -1]);
const input3 = tf.tensor1d([1, 0.44]);
// Calling addLoss() method with its
// parameter
const res1 = model.layers[0].addLoss([tf.cos(input1)]);
const res2 = model.layers[0].addLoss([tf.sin(input2)]);
const res3 = model.layers[0].addLoss([tf.tan(input3)]);
// Printing outputs
console.log(JSON.stringify(input1));
console.log(JSON.stringify(input2));
console.log(JSON.stringify(input3));
model.layers[0].getWeights()[0].print();
输出:
{"kept":false,"isDisposedInternal":false,"shape":[4],"dtype":"float32", "size":4,"strides":[],"dataId":{"id":169},"id":261,"rankType":"1","scopeId":112} {"kept":false,"isDisposedInternal":false,"shape":[3],"dtype":"float32", "size":3,"strides":[],"dataId":{"id":170},"id":262,"rankType":"1","scopeId":112} {"kept":false,"isDisposedInternal":false,"shape":[2],"dtype":"float32", "size":2,"strides":[],"dataId":{"id":171},"id":263,"rankType":"1","scopeId":112} Tensor [[-0.0062826], [0.0883235 ], [-1.0633234]]
参考:https://js.tensorflow.org/api/latest/#tf.layers.Layer.addLoss
相关用法
- Lodash _.method()用法及代码示例
- Node.js Http2ServerRequest.method用法及代码示例
- Node.js http.IncomingMessage.method用法及代码示例
- Javascript dataView.getInt16()用法及代码示例
- Collect.js toArray()用法及代码示例
- Javascript RegExp toString()用法及代码示例
- Node.js URLSearchParams.has()用法及代码示例
- Node.js hmac.update()用法及代码示例
- HTML DOM isEqualNode()用法及代码示例
- JavaScript Date toLocaleTimeString()用法及代码示例
- Tensorflow.js tf.Tensor.buffer()用法及代码示例
- Node.js crypto.createHash()用法及代码示例
- Node.js process.send()用法及代码示例
- Node.js writeStream.clearLine()用法及代码示例
- HTML DOM History go()用法及代码示例
- Node.js fs.link()用法及代码示例
- Java ArrayList toArray()用法及代码示例
- JavaScript Math random()用法及代码示例
- JavaScript Math round()用法及代码示例
- Javascript toFixed()用法及代码示例
- Javascript toPrecision()用法及代码示例
注:本文由纯净天空筛选整理自nidhi1352singh大神的英文原创作品 Tensorflow.js tf.layers addLoss() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。