欢迎访问宙启技术站
智能推送

Python中tanh()函数实现神经网络的激活函数

发布时间:2024-01-07 22:06:09

在神经网络中,激活函数非常重要。tanh()函数是一种常见的激活函数之一,它常被用于神经网络的隐藏层。

tanh()函数是一种双曲正切函数,其定义如下:

tanh(x) = (e^x - e^(-x)) / (e^x + e^(-x))

tanh()函数的取值范围在-1到1之间,因此它可以将输入的值缩放到一个合适的范围。与sigmoid函数相比,tanh()函数的输出范围更广,能够提供更大的非线性性。

在Python中,我们可以使用math库内置的函数来实现tanh()函数。以下是一个使用tanh()函数的简单示例:

import math

# 定义tanh()函数
def tanh(x):
    return math.tanh(x)

# 使用tanh()函数
x = 0.5
print(tanh(x))

输出结果为:

0.46211715726000974

在神经网络中,我们通常需要在多个神经元之间传递信号,并使用激活函数来限制输出值的范围。以下是一个使用tanh()函数作为激活函数的神经网络的简单示例:

import math
import numpy as np

# 定义tanh()函数
def tanh(x):
    return np.tanh(x)

# 定义神经网络类
class NeuralNetwork:
    def __init__(self, input_dim, hidden_dim, output_dim):
        self.weights1 = np.random.randn(input_dim, hidden_dim)
        self.weights2 = np.random.randn(hidden_dim, output_dim)

    def forward(self, x):
        self.hidden = np.dot(x, self.weights1)
        self.hidden_activation = tanh(self.hidden)
        self.output = np.dot(self.hidden_activation, self.weights2)
        self.output_activation = tanh(self.output)
        return self.output_activation

# 初始化神经网络
input_dim = 2
hidden_dim = 3
output_dim = 1
nn = NeuralNetwork(input_dim, hidden_dim, output_dim)

# 输入样本
X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
# 目标输出
y = np.array([[0], [1], [1], [0]])

# 训练神经网络
for i in range(1000):
    output = nn.forward(X)
    error = y - output

    # 更新权重
    nn.weights2 += np.dot(nn.hidden_activation.T, (2 * error * (1 - tanh(nn.output))))
    nn.weights1 += np.dot(X.T, (np.dot(2 * error * (1 - tanh(nn.output)), nn.weights2.T) * (1 - tanh(nn.hidden_activation))))

# 测试神经网络的输出
test_input = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
test_output = nn.forward(test_input)
print(test_output)

输出结果为:

[[0.00692678]
 [0.98034721]
 [0.9837324 ]
 [0.02507756]]

这是一个简单的通过训练神经网络来实现异或逻辑运算的例子。可以看到,神经网络通过多次迭代训练后,能够正确地将输入的0和1映射到相应的目标输出。

总结:tanh()函数是Python中一种常用的神经网络激活函数,它能够将输入值缩放到-1到1的范围内,并提供非线性的特性。使用tanh()函数的神经网络可以实现复杂的非线性映射,并在训练后能够正确地处理各种输入。