栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

深度学习中常用的函数

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

深度学习中常用的函数

下面介绍深度学习中常用的函数及其代码实现:

1.恒等函数
import numpy as np
import matplotlib.pyplot as plt


def identity_function(x):
    return x


x = np.arange(0.0, 5.0, 0.1)
y = identity_function(x)

plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('y')

plt.show()

2.阶跃函数
import numpy as np
import matplotlib.pyplot as plt


def step_function(x):
    return np.array(x > 0, dtype=np.int)



x = np.arange(-5.0, 5.0, 0.1)
y = step_fucntion(x)
plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('y')

plt.show()

3.sigmoid函数
import numpy as np
import matplotlib.pyplot as plt


def sigmoid(x):
    return 1 / (1 + np.exp(-x))


x = np.arange(-5.0, 5.0, 0.1)
y = sigmoid(x)


plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('y')

plt.show()

4. sigmoid_grad函数
import numpy as np
import matplotpli.pyplot as plt


def sigmoid(x):
    return 1 / (1 + np.exp(-x)


def sigmoid_grad(x):
    return (1.0 - sigmoid(x)) * sigmoid(x)


x = np.arange(-5.0, 5.0, 0.1)
y = sigmoid_grad(x)

plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('y')

plt.show()

5.relu函数
import numpy as np
import matplotplib.pyplot as plt


def relu(x):
    return np.maximum(0, x)


x = np.arange(-5.0, 5.0, 0.1)
y = relu(x)

plt.plot(x, y)
plt.xlabel('x')
plt.ylabel('y')

plt.show()

 6.relu_grad函数
import numpy as np


def relu_grad(x):
    grad = np.zeros(x)
    grad[x>=] = 1
    return grad
7.softmax函数
import numpy as np


def softmax(x):
    if x.ndim == 2:
        x = x.T
        x = x - np.max(x, axis=0)
        y = np.exp(x) / np.sum(np.exp(x), axis=0)
        return y.T
    
    x = x - np.max(x)
    return np.exp(x) / np.sum(np.exp(x))
8.mean_squared_error函数
import numpy as np


def mean_squared_error(y, x):
    return 0.5 * np.sum((y-t)**2)
9.cross_entropy_error函数
import numpy as np


def cross_entropy_error(y, t):
    if y.ndim == 1:
        t = t.reshape(1, t.size)
        y = y.reshape(1, y.size)
    
    if t.size == y.size:
        t = t.argmax(axis=1)
        
    batch_size = y.shape[0]
    
    return -np.sum(np.log(y[np.arange(batch_size), t] + 1e-7)) / batch_size

10.softmax_loss函数
import numpy as np


def softmax_loss(X, t):
    y = softmax(X)
    return cross_entropy_error(y, t)

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/875746.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号