Tensorflow.js tf.layers.add()函数
Tensorflow.js 是谷歌开发的一个开源库,用于在浏览器或节点环境中运行机器学习模型和深度学习神经网络。它还可以帮助开发人员用 JavaScript 语言开发 ML 模型,并且可以直接在浏览器或 Node.js 中使用 ML。
当提供多个数组作为输入时, tf.layers.add()函数用于计算每个元素的加法。
语法:
tf.layers.add().apply([input1, input2,input3,......,inputn])
参数:
- inputShape:它是一个可选参数,用于创建输入层,它采用数字和空值等值。
- batchInputShape:它是一个可选参数,用于在主层之前创建输入层,它采用数字和空值等值。
- batchSize:它是一个可选参数,用于制作 batchInputShape,它只接受数字。
- dtype:可选参数,代表数据类型。默认情况下,它具有“float32”并且还支持其他值,例如“int32”、“bool”等。
- name:它是一个可选参数,用于定义层的名称,它接受字符串。
- trainable :它是一个可选参数,用于确定提供的输入层是否更新。它接受布尔值。
- weights:它拥有层的起始权重。它也是一个可选参数。
- inputDType:它是用于输入数据类型的可选参数。与 dtype 一样,它也支持其所有值。
返回值:返回Add。
示例 1:
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
const input1 = tf.input({shape: [90.5, 3]});
const input2 = tf.input({shape: [90.5, 3]});
const addLayer = tf.layers.add();
const sum = addLayer.apply([input1, input2]);
console.log(JSON.stringify(sum.shape));
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
const input1 = tf.input({shape: [127.65, 56]});
const input2 = tf.input({shape: [127.65, 56]});
const input3 = tf.input({shape: [127.65, 56]});
const input4 = tf.input({shape: [127.65, 56]});
const input5 = tf.input({shape: [127.65, 56]});
const input6 = tf.input({shape: [127.65, 56]});
const input7 = tf.input({shape: [127.65, 56]});
const input8 = tf.input({shape: [127.65, 56]});
const addLayer = tf.layers.add();
const sum = addLayer.apply([input1, input2, input3,
input4, input5, input6, input7, input8]);
console.log(JSON.stringify(sum.shape));
输出:
[null,90.5,3]
示例 2:
Javascript
// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
const input1 = tf.input({shape: [127.65, 56]});
const input2 = tf.input({shape: [127.65, 56]});
const input3 = tf.input({shape: [127.65, 56]});
const input4 = tf.input({shape: [127.65, 56]});
const input5 = tf.input({shape: [127.65, 56]});
const input6 = tf.input({shape: [127.65, 56]});
const input7 = tf.input({shape: [127.65, 56]});
const input8 = tf.input({shape: [127.65, 56]});
const addLayer = tf.layers.add();
const sum = addLayer.apply([input1, input2, input3,
input4, input5, input6, input7, input8]);
console.log(JSON.stringify(sum.shape));
输出:
[null,127.65,56]
参考: https://js.tensorflow.org/api/latest/#layers.add