import torch
import numpy as np
import matplotlib.pyplot as plt
import sys
sys.path.append("路径")
import d2lzh_pytorch as d2l
'''
1. relu
2. sigmoid
3. tanh
'''
'''
----------------------------------------------------------------------ReLU函数:只选正数,负数归零
'''
def xyplot(x_vals, y_vals, name):
d2l.set_figsize(figsize=(5, 2.5))
d2l.plt.plot(x_vals.detach().numpy(), y_vals.detach().numpy(), label=name)
d2l.plt.xlabel('x')
d2l.plt.ylabel(name + '(x)')
d2l.plt.legend()
d2l.plt.savefig('路径')
x = torch.arange(-8.0, 8.0, 1, requires_grad=True)
y = x.relu()
xyplot(x, y, 'relu')
y.sum().backward()
xyplot(x, x.grad, 'grad of relu')
'''
------------------------------------------------------------------sigmoid 激活函数:可以将元素的值变换到0-1之间
'''
y = x.sigmoid()
xyplot(x, y, 'sigmoid')
x.grad.zero_()
y.sum().backward()
xyplot(x, x.grad, 'grad of sigmoid')
'''
-----------------------------------------------tanh函数:将元素变换到-1到1之间
'''
y = x.tanh()
xyplot(x, y, 'tanh')
x.grad.zero_()
y.sum().backward()
xyplot(x, x.grad, 'grad of tanh')