Skip to main content
 首页 » 编程设计

python之如何在tensorflow中制作自定义激活函数

2024年12月31日31sxdcgaq8080

我需要制作一个tensorflow中没有的激活函数,我该怎么办?我见过这个链接, How to make a custom activation function with only Python in Tensorflow? 但我仍然不知道如何实现图片中的新型激活功能。 relu,leaky_relu and a new type of relu

请您参考如下方法:

我认为这个可以为您服务。我只使用了包含 tensorflow 的函数,他负责管理反向传播。

如果您使用 python 函数,则必须同时对前向和后向进行编程。但问题是当您必须将分段函数的函数掩码保存在“缓存”中时(我个人不知道它是如何完成的,如果知道的话会很有趣)。

import numpy as np 
import tensorflow as tf 
 
def new_relu(x, k=0.2): 
    part_1 = tf.to_float(tf.math.less_equal(0.0, x)) 
    part_2 = tf.to_float(tf.math.logical_and(tf.math.less_equal(-1.0, x), tf.math.less(x, 0.0))) 
    part_3 = tf.to_float(tf.math.less(x, -1.0)) 
    return part_1*x + part_2*x*k #+ part_3*0 
 
def new_relu_test(): 
 
    # create data 
    x = tf.random_normal([10])*10000 
    y = new_relu(x) 
    with tf.Session(): 
        diff = tf.test.compute_gradient_error(x, [10], y, [10]) 
        print(diff) 
 
    # use in dense 
    x = tf.placeholder(shape=[None, 3], dtype=tf.float32) 
    nn = tf.layers.dense(x, 3, activation=new_relu) 

编辑: 如果您希望第二个参数也是张量,则必须与输入大小相同。

import numpy as np 
import tensorflow as tf 
 
def new_relu(x, k=0.2): 
    part_1 = tf.to_float(tf.math.less_equal(0.0, x)) 
    part_2 = tf.to_float(tf.math.logical_and(tf.math.less_equal(-1.0, x), tf.math.less(x, 0.0))) 
    part_3 = tf.to_float(tf.math.less(x, -1.0)) 
    return part_1*x + part_2*x*k #+ part_3*0 
 
def new_relu_test(): 
    # create data 
    x = tf.random_normal([10])*10000 
    y = new_relu(x) 
    with tf.Session(): 
        diff = tf.test.compute_gradient_error(x, [10], y, [10]) 
        print(diff) 
 
    # use in dense 
    x = tf.placeholder(shape=[None, 3], dtype=tf.float32) 
    x_b = tf.placeholder(shape=[None], dtype=tf.float32) 
    nn_1 = tf.layers.dense(x, 3) 
    nn_2 = tf.layers.dense(x, 3) 
    nn = tf.layers.dense(nn_2, 1, activation=None) 
    new_r = new_relu(x, tf.tile(tf.expand_dims(x_b, -1), [1, 3])) 
 
    with tf.Session() as sess: 
        sess.run(tf.initializers.global_variables()) 
        sess.run(new_r, feed_dict={x: np.random.rand(100, 3), x_b: np.random.rand(100)}) 
 
new_relu_test() 

编辑 2:

使用 conv2d

import numpy as np 
import tensorflow as tf 
 
def new_relu(x, k=0.2): 
    part_1 = tf.to_float(tf.math.less_equal(0.0, x)) 
    part_2 = tf.to_float(tf.math.logical_and(tf.math.less_equal(-1.0, x), tf.math.less(x, 0.0))) 
    part_3 = tf.to_float(tf.math.less(x, -1.0)) 
    return part_1*x + part_2*x*k #+ part_3*0 
 
def new_relu_test(): 
    # create data 
    x = tf.random_normal([10])*10000 
    y = new_relu(x) 
    with tf.Session(): 
        diff = tf.test.compute_gradient_error(x, [10], y, [10]) 
        print(diff) 
 
    # use in dense 
    x = tf.placeholder(shape=[None, 28, 28, 3], dtype=tf.float32) 
 
    conv1_weights = tf.get_variable("weight",[3,3,3,32],initializer=tf.truncated_normal_initializer(stddev=0.1)) 
    conv1_biases = tf.get_variable("bias", [32], initializer=tf.constant_initializer(0.0)) 
    conv1 = tf.nn.conv2d(x, conv1_weights, strides=[1, 1, 1, 1], padding='SAME') 
    relu1 = new_relu(tf.nn.bias_add(conv1, conv1_biases)) 
 
    with tf.Session() as sess: 
        sess.run(tf.initializers.global_variables()) 
        sess.run(relu1, feed_dict={x: np.random.rand(100, 28, 28, 3)}) 
new_relu_test()