Tensorflow 自定义高斯激活函数

本文介绍了如何在 TensorFlow 中自定义高斯激活函数,该函数的形式为 f(x) = exp( - (x^2) / (sigma^2) ),其中 sigma = 0.5。通过创建自定义函数,可以将这种特殊的激活函数应用到特定的机器学习项目中。
摘要由CSDN通过智能技术生成

Gaussian non-linear activation function

tensorflow自带了几个常用的激活函数,但是最近手头的项目,激活函数用的既不是relu,也不是sigmod,而是一个很生僻的函数,高斯激活函数(Gaussian activation function)。

自定义高斯函数形式:f(x) = exp( - (x^2) / (sigma^2) )

实验中的 sigma = 0.5

 

自定义激活函数代码如下

新建文件gaussian_activation.py

#-*- encoding:utf-8 -*-
#!/usr/local/env python

import numpy as np
import tensorflow as tf
import math
from tensorflow.python.framework import ops

def gaussian(x):
    return math.exp(- (x*x) / (0.25))

def gaussian_grad(x):
    return (-8) * x * math.exp(- (x*x) / (0.25))

gaussian_np = np.vectorize(gaussian)
gaussian_grad_np = np.vectorize(gaussian_grad)

gaussian_np_32 = lambda x: gaussian_np(x).astype(np.float32)
gaussian_grad_np_32 = lambda x: gaussian_grad_np(x).astype(np.float32)

def gaussian_grad_tf(x, name=None):
    with ops.name_scope(name, "
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值