📜  Tensorflow.js tf.layers.activation()函数

📅  最后修改于: 2022-05-13 01:56:46.481000             🧑  作者: Mango

Tensorflow.js tf.layers.activation()函数

简介: Tensorflow.js 是谷歌开发的一个开源库,用于在浏览器或节点环境中运行机器学习模型和深度学习神经网络。 Tensorflow.js tf.layers.activation()函数用于将函数应用于我们输入层的所有元素。我们还可以将函数应用于具有密集层的输入数据。

句法:

tf.layers.activation(args);    

参数:以下是此函数接受的参数:

  • args:它是具有字段的对象类型:
    • 激活:它是应用于所有输入元素的函数的名称。
    • inputShape:模型输入层的形状。它用于创建输入层。
    • batchInputShape:用于输入层的制作。它为输入层中的样本定义了批次的形状。
    • batchSize:用于输入层的制作。它在输入层的构造中用作batchInputShape的补充。
    • dtype:定义图层的数据类型。它用于模型的第一层。
    • name:它声明的字符串是输入层的名称。
    • 可训练:它声明层是否可以通过函数训练。它是布尔数据类型。
    • 权重:张量是层的初始数据。
    • inputDType:图层中输入数据的数据类型。

返回:激活

以下是此函数的一些示例:

示例 1:在本示例中,我们将制作激活层并检查返回值。

Javascript
import * as tf from "@tensorflow/tfjs"
 
// Creating config for the activation layer
const config = {
    activation: 'sigmoid',
    inpurShape: 5,
    dtype: 'int32',
    name: 'activationLayer'
};
 
// Defining the activation layer
const activationLayer = tf.layers.activation(config);
 
// printing return of activation layer
console.log(activationLayer);


Javascript
import * as tf from "@tensorflow/tfjs"
 
// Configuration file for the activation layer
const geek_config = {
    activation: 'sigmoid',
    inpurShape: 5,
    dtype: 'int32',
    name: 'activationLayer'
};
 
const geek_activation = tf.layers.activation(geek_config);
const geek_inputLayer = tf.layers.dense({units: 1});
 
// Our Input layer for the model
const geek_input = tf.input({shape: [7]});
 
// Making structure for the model
const geek_output = geek_inputLayer.apply(geek_input);
const geek_result = geek_activation.apply(geek_output);
 
// Making Model from structure 
const config2 = {inputs: geek_input, outputs: geek_result}
const model = tf.model(config2);
 
// Collect both outputs and print separately.
const config3 = tf.randomUniform([4, 7])
const  geek_activationResult = model.predict(confg3);
geek_activationResult.print();


输出:

{
  "_callHook": null,
  "_addedWeightNames": [],
  "_stateful": false,
  "id": 38,
  "activityRegularizer": null,
  "inputSpec": null,
  "supportsMasking": true,
  "_trainableWeights": [],
  "_nonTrainableWeights": [],
  "_losses": [],
  "_updates": [],
  "_built": false,
  "inboundNodes": [],
  "outboundNodes": [],
  "name": "ActivationLayer",
  "trainable_": true,
  "initialWeights": null,
  "_refCount": null,
  "fastWeightInitDuringBuild": false,
  "activation": {}
}

示例 2:在此示例中,我们将使用一些配置创建我们的激活层,并使用激活层训练我们的输入数据。

Javascript

import * as tf from "@tensorflow/tfjs"
 
// Configuration file for the activation layer
const geek_config = {
    activation: 'sigmoid',
    inpurShape: 5,
    dtype: 'int32',
    name: 'activationLayer'
};
 
const geek_activation = tf.layers.activation(geek_config);
const geek_inputLayer = tf.layers.dense({units: 1});
 
// Our Input layer for the model
const geek_input = tf.input({shape: [7]});
 
// Making structure for the model
const geek_output = geek_inputLayer.apply(geek_input);
const geek_result = geek_activation.apply(geek_output);
 
// Making Model from structure 
const config2 = {inputs: geek_input, outputs: geek_result}
const model = tf.model(config2);
 
// Collect both outputs and print separately.
const config3 = tf.randomUniform([4, 7])
const  geek_activationResult = model.predict(confg3);
geek_activationResult.print();

输出:

Tensor
    [[0.4178988],
     [0.2027801],
     [0.2813435],
     [0.2546847]]

参考: https://js.tensorflow.org/api/latest/#layers.activation