1.7.创建Tensor
1.7.1.Import from numpy
1.7.2.Import from List
1.7.3.set default type
1.7.4.rand/rand_like,randint
1.7.5.full()
1.7.6.arange()
1.7.7.linspace/logspace
1.7.7.1.torch.linspace()
1.7.7.2.torch.logspace()
1.7.8.ones/zeros/eye
1.7.8.1.ones
1.7.8.2.zeros
1.7.8.3.eye
1.7.9.torch.randperm()
1.7.10.sin
1.7.11.Sinh
1.7.12.Cosh
1.7.13.Tanh
1.7.14.cons
1.7.15.Tan
1.7.16.Asin
1.7.17.acos
1.7.18.atan
1.7.创建Tensor
1.7.1.Import from numpy
从numpy引入数据
# -*- coding: UTF-8 -*-import numpy as np
import torcha = np.array([2, 3.3])
print(torch.from_numpy(a))
"""
tensor([2.0000, 3.3000], dtype=torch.float64)
"""a = np.ones([2, 3])
print(torch.from_numpy(a))
"""
tensor([[1., 1., 1.],[1., 1., 1.]], dtype=torch.float64)
"""
1.7.2.Import from List
随机生成pytorch的数据。
import torchprint(torch.empty(1))
"""
tensor([-24217520.])
"""print(torch.Tensor(2, 3))
"""
tensor([[0., 0., 0.],[0., 0., 0.]])
"""print(torch.IntTensor(2, 3))
"""
tensor([[0, 0, 0],[0, 0, 0]], dtype=torch.int32)
"""print(torch.FloatTensor(2, 3))
"""
tensor([[0., 0., 0.],[0., 0., 0.]])
"""
1.7.3.set default type
设置pytorch的默认类型
import torch# torch中的默认类型是torch.FloatTensor类型
print(torch.tensor([1.2, 3]).type())
"""
torch.FloatTensor
"""# 将torch的默认值设置成torch.DoubleTensor
torch.set_default_tensor_type(torch.DoubleTensor)
print(torch.tensor([1.2, 3]).type())
"""
torch.DoubleTensor
"""
1.7.4.rand/rand_like,randint
torch.rand和torch.randn有什么区别?
一个是均匀分布,一个是标准正态分布。
torch.rand()
均匀分布
torch.rand(*sizes, out=None)–>Tensor
返回一个张量,包含了从区间[0, 1)的均匀分布中抽取的一组随机数。张量的形状由参数sizes定义。
参数:
sizes(int…) - 整数序列,定义了输出张量的形状
out(Tensor,optinal) - 结果张量
import torcht1 = torch.rand(2, 3)
print(t1, t1.type())
"""
tensor([[0.6835, 0.1949, 0.0010],[0.1842, 0.3441, 0.7061]]) torch.FloatTensor
"""
*randn(size, out=None, dtype=None)和randn_like(input, dtype=None)
标准正态分布
torch.randn(*sizes, out=None)–>Tensor
返回一个张量,包含了从标准正态分布(均值为0,方差1,即高斯白噪声)中抽取的一组随机数。张量的形状由参数sizes定义。
参数:
sizes(int…) - 整数序列,定义了输出张量的形状
out(Tensor, optinal) - 结果张量
import torch# randn(*size, out=None, dtype=None)
# randn_like(input, dtype=None)
t1 = torch.randn(2, 3)
t2 = torch.randn_like(t1)
print(t1, t1.type())
print(t2, t2.type())
torch.randint(), torch.randint_like()
import torch# randint(low=0, high, size, out=None, dtype=None)
# randint_like(input, low=0, high, dtype=None)
# 整数范围[low, high]
t1 = torch.randint(1,4,(2,3,2)) # 形状写入[2, 3, 2]也行
t2 = torch.randint_like(t1, 4)
print(t1)
"""
tensor([[[2, 2],[3, 1],[3, 2]],[[1, 1],[1, 3],[2, 2]]])
"""print(t2)
"""
tensor([[[2, 3],[1, 2],[0, 2]],[[3, 1],[1, 1],[0, 2]]])
"""
1.7.5.full()
PyTorch是由Facebook开发的开源机器学习库。它用于深度神经网络和自然语言处理。
功能torch.full()返回一个大小为fill_value的张量的张量。
用法:torch.ones(size, fill_value, out=None)
参数:
size: 定义输出张量形状的整数序列。
fill_value:用于填充输出张量的数字。
out(Tensor, optional) : 输出张量
返回类型:张量
# -*- coding: UTF-8 -*-import torch# Applying the full function and storing the resulting tensor in 'a'
# [3,4]为3行4列的输出结果,3为填充参数。
a = torch.full([3, 4], 3)
print("a = ", a)# [2, 5] 是要输出的2行5列的结果,3.5位填充参数。
b = torch.full([2, 5], 3.5)
print("b = ", b)
输出结果:
a = tensor([[3, 3, 3, 3],[3, 3, 3, 3],[3, 3, 3, 3]])
b = tensor([[3.5000, 3.5000, 3.5000, 3.5000, 3.5000],[3.5000, 3.5000, 3.5000, 3.5000, 3.5000]])
1.7.6.arange()
功能torch.arange()返回大小的一维张量(end - start)/ step,从间隔的值[start , end]从开始就采取共同的差异步骤。
用法:torch.arange(start=0, end, step=1, out=None)
参数:
start: 点集的起始值。默认值:0
end : 点集的最终值
step : 每对相邻点之间的间隙。默认值:1
out(Tensor, optional) : 输出张量
返回类型:张量
代码1:
# -*- coding: UTF-8 -*-import torch# Applying the arange function and storing the resulting tensor in 't'
a = torch.arange(3)
print("a = ", a)
"""
a = tensor([0, 1, 2])
"""b = torch.arange(1, 6)
print("b = ", b)
"""
b = tensor([1, 2, 3, 4, 5])
"""c = torch.arange(1, 5, 0.5)
print("c = ", c)
"""
c = tensor([1.0000, 1.5000, 2.0000, 2.5000, 3.0000, 3.5000, 4.0000, 4.5000])
"""
1.7.7.linspace/logspace
1.7.7.1.torch.linspace()
torch.linespace()返回一维步长张量,该步张量在起点和终点之间等距。
输出张量是尺寸步长的一维。
用法:torch.linspace(start, end, steps=100, out=None)
参数:
start: 点集的起始值。
end : 点集的最终值。
steps : 每对相邻点之间的间隙。默认值:100
out(Tensor, optional): 输出张量
返回类型:张量
代码1:
# -*- coding: UTF-8 -*-import torch# Applying the linspace function and storing the resulting tensor in 't'
a = torch.linspace(3, 10, 5)
print("a = ", a)b = torch.linspace(start=-10, end=10, steps=5)
print("b = ", b)
代码2:可视化
# -*- coding: UTF-8 -*-import torch
import numpy as np
import matplotlib.pyplot as plt# Applying the linspace function to get a tensor of size 15 with values from -5 to 5
a = torch.linspace(-5, 5, 15)
print(a)# Plotting
plt.plot(a.numpy(), np.zeros(a.numpy().shape), color = 'red', marker = "o")
plt.title("torch.linspace")
plt.xlabel("X")
plt.ylabel("Y")plt.show()
输出:
tensor([-5.0000, -4.2857, -3.5714, -2.8571, -2.1429, -1.4286, -0.7143, 0.0000,0.7143, 1.4286, 2.1429, 2.8571, 3.5714, 4.2857, 5.0000])
1.7.7.2.torch.logspace()
功能torch.logspace()返回一阶步张量的一维张量,与之间的底数成对数间隔
输出张量是尺寸步长的一维。
用法:torch.logspace(start, end, steps=100, base=10, out=None)
参数:
start: 点集的起始值
end: 点集的最终值
steps: 在开始和结束之间要采样的点数。默认值:100
base:对数函数的基数。默认值:10.0
out(Tensor, optional) : 输出张量
返回类型:张量
代码1:
# -*- coding: UTF-8 -*-import torch# Applying the logspace function and storing the resulting tensor in 't'
a = torch.logspace(3, 10, 5)
print("a = ", a)
"""
输出结果:
a = tensor([1.0000e+03, 5.6234e+04, 3.1623e+06, 1.7783e+08, 1.0000e+10])
"""b = torch.logspace(start=-10, end=10, steps=5)
print("b = ", b)
"""
输出结果:
b = tensor([1.0000e-10, 1.0000e-05, 1.0000e+00, 1.0000e+05, 1.0000e+10])
“”"
代码2:可视化
# -*- coding: UTF-8 -*-import torch
import numpy as np
import matplotlib.pyplot as plt# Applying the logspace function to get a tensor of size 15 with values from -5 to 5 using base 2
a = torch.logspace(-5, 5, 15, 2)
print(a)# Plotting
plt.plot(a.numpy(), np.zeros(a.numpy().shape), color = 'red', marker = "o")
plt.title("torch.linspace")
plt.xlabel("X")
plt.ylabel("Y")plt.show()
输出:
tensor([3.1250e-02, 5.1271e-02, 8.4119e-02, 1.3801e-01, 2.2643e-01, 3.7150e-01,6.0951e-01, 1.0000e+00, 1.6407e+00, 2.6918e+00, 4.4164e+00, 7.2458e+00,1.1888e+01, 1.9504e+01, 3.2000e+01])
1.7.8.ones/zeros/eye
1.7.8.1.ones
torch.eye()返回a返回大小为n * m的2-D张量,对角线为1,其他位置为零。
用法:torch.eye(n, m, out=None)
参数:
n : 行数
m : 列数。默认值-n
out(Tensor, optional) : 输出张量
返回类型:二维张量
代码1:
# -*- coding: UTF-8 -*-import torcha = torch.eye(3, 4)
print("a = ", a)b = torch.eye(3, 3)
print("b = ", b)c = torch.eye(5, 1)
print("c = ", c)
输出:
a = tensor([[1., 0., 0., 0.],[0., 1., 0., 0.],[0., 0., 1., 0.]])
b = tensor([[1., 0., 0.],[0., 1., 0.],[0., 0., 1.]])
c = tensor([[1.],[0.],[0.],[0.],[0.]])
1.7.8.2.zeros
torch.zeros()返回一个由标量值0填充的张量,其形状由变量参数size定义。
用法:torch.zeros(size, out=None)
参数:
size: 定义输出张量形状的整数序列
out(Tensor, optional): 输出张量
返回类型:一个张量,其标量值为0,形状与尺寸相同。
# -*- coding: UTF-8 -*-import torcha = torch.zeros([3, 4])
print("a = ", a)b = torch.zeros([1, 5])
print("b = ", b)c = torch.zeros([5, 1])
print("c = ", c)d = torch.zeros([3,3,2])
print("d = ", d)
输出结果:
a = tensor([[0., 0., 0., 0.],[0., 0., 0., 0.],[0., 0., 0., 0.]])
b = tensor([[0., 0., 0., 0., 0.]])
c = tensor([[0.],[0.],[0.],[0.],[0.]])
d = tensor([[[0., 0.],[0., 0.],[0., 0.]],[[0., 0.],[0., 0.],[0., 0.]],[[0., 0.],[0., 0.],[0., 0.]]])
1.7.8.3.eye
torch.eye()返回a返回大小为n * m的2-D张量,对角线为1,其他位置为零。
用法:torch.eye(n, m, out=None)
参数:
n : 行数
m : 行列。默认值-n
out (Tensor, optional):输出张量
返回类型:二维张量
代码1:
# -*- coding: UTF-8 -*-import torcha = torch.eye(3, 4)
print("a = ", a)b = torch.eye(3, 3)
print("b = ", b)c = torch.eye(5, 1)
print("c = ", c)
输出:
a = tensor([[1., 0., 0., 0.],[0., 1., 0., 0.],[0., 0., 1., 0.]])
b = tensor([[1., 0., 0.],[0., 1., 0.],[0., 0., 1.]])
c = tensor([[1.],[0.],[0.],[0.],[0.]])
1.7.9.torch.randperm()
randperm(n, out=None, dtype=torch.int64)–>LongTensor
# torch中没有random.shuffle
# y = torch.randperm(n) y是把1到n这些数随机打乱得到的一个数字序列
import torch# randperm(n, out=None, dtype=torch.int64) -->LongTensor
idx = torch.randperm(3)
a = torch.Tensor(4, 2)
print(a)
print(idx, idx.type())
print(a[idx])
输出结果:
tensor([[0., 0.],[0., 0.],[0., 0.],[0., 0.]])
tensor([2, 0, 1]) torch.LongTensor
tensor([[0., 0.],[0., 0.],[0., 0.]])
1.7.10.sin
torch.sin()提供对PyTorch中正弦函数的支持。它期望输入为弧度形式,并输出范围为[-1, 1]
输入类型为张量,如果输入包含多个元素,则将按元素计算正弦。
用法:torch.sin(x,out=None)
参数:
X: 输入张量
name(可选) : 输出张量
返回类型:与X具有相同类型的张量。
代码1:
# -*- coding: UTF-8 -*-import torch# A constant tensor of size 6
a = torch.FloatTensor([1.0, -0.5, 3.4, -2.1, 0.0, -6.5])
print(a)# Applying the sin function and storing the result in 'b'
b = torch.sin(a)
print(b)
输出:
tensor([ 1.0000, -0.5000, 3.4000, -2.1000, 0.0000, -6.5000])
tensor([ 0.8415, -0.4794, -0.2555, -0.8632, 0.0000, -0.2151])
代码2:可视化
# -*- coding: UTF-8 -*-import torch
import numpy as np
import matplotlib.pyplot as plt# A vector of size 15 with values from -5 to 5
a = np.linspace(-5, 5, 15)# Applying the sine function and storing the result in 'b'
b = torch.sin(torch.FloatTensor(a))
print(b)# Plotting
plt.plot(a, b.numpy(), color='red', marker="o")
plt.title("torch.sin")
plt.xlabel("X")
plt.ylabel("Y")plt.show()
输出结果:
tensor([ 0.9589, 0.9103, 0.4167, -0.2806, -0.8408, -0.9899, -0.6551, 0.0000,0.6551, 0.9899, 0.8408, 0.2806, -0.4167, -0.9103, -0.9589])
1.7.11.Sinh
torch.sinh()为PyTorch中的双曲正弦函数提供支持。它期望以弧度形式输入。输入类型为张量,如果输入包含多个元素,则将计算按元素的双曲正弦值。
用法:torch.sinh(x, out=None)
参数:
X:输入张量
name(可选):输出张量
返回类型:与x具有相同类型的张量
代码1:
# -*- coding: UTF-8 -*-import torch# A constant tensor of size 6
a = torch.FloatTensor([1.0, -0.5, 3.4, -2.1, 0.0, -6.5])
print(a)# Applying the sinh function and storing the result in 'b'
b = torch.sinh(a)
print(b)
输出结果:
tensor([ 1.0000, -0.5000, 3.4000, -2.1000, 0.0000, -6.5000])
tensor([ 1.1752, -0.5211, 14.9654, -4.0219, 0.0000, -332.5701])
代码2:可视化
# -*- coding: UTF-8 -*-import torch
import numpy as np
import matplotlib.pyplot as plt# A vector of size 15 with values from -5 to 5
a = np.linspace(-5, 5, 15)
print(a)# Applying the hyperbolic sine function and storing the result in 'b'
b = torch.sinh(torch.FloatTensor(a))
print(b)# Plotting
plt.plot(a, b.numpy(), color = 'red', marker = "o")
plt.title("torch.sinh")
plt.xlabel("X")
plt.ylabel("Y")plt.show()
输出结果:
[-5. -4.28571429 -3.57142857 -2.85714286 -2.14285714 -1.42857143-0.71428571 0. 0.71428571 1.42857143 2.14285714 2.857142863.57142857 4.28571429 5. ]
tensor([-74.2032, -36.3203, -17.7696, -8.6771, -4.2032, -1.9665, -0.7766,0.0000, 0.7766, 1.9665, 4.2032, 8.6771, 17.7696, 36.3203,74.2032])
1.7.12.Cosh
torch.cosh()为PyTorch中的双曲余弦功能提供支持。它期望以弧度形式输入。输入类型为张量,如果输入包含多个元素,则将计算按元素的双曲余弦值。
用法:torch.cosh(x, out=None)
参数:
X: 输入张量。
name(可选):输出张量。
返回类型:与X具有相同类型的张量。
代码1:
# -*- coding: UTF-8 -*-import torch# A constant tensor of size 6
a = torch.FloatTensor([1.0, -0.5, 3.4, -2.1, 0.0, -6.5])
print(a)# Applying the cosh function and storing the result in 'b'
b = torch.cosh(a)
print(b)
输出结果:
tensor([ 1.0000, -0.5000, 3.4000, -2.1000, 0.0000, -6.5000])
tensor([ 1.5431, 1.1276, 14.9987, 4.1443, 1.0000, 332.5716])
代码2:可视化
# Importing the PyTorch library
import torch# Importing the NumPy library
import numpy as np# Importing the matplotlib.pylot function
import matplotlib.pyplot as plt# A vector of size 15 with values from -1 to 1
a = np.linspace(-1, 1, 15)# Applying the hyperbolic cosine function and storing the result in 'b'
b = torch.cosh(torch.FloatTensor(a))print(b)# Plotting
plt.plot(a, b.numpy(), color='red', marker="o")
plt.title("torch.cosh")
plt.xlabel("X")
plt.ylabel("Y")plt.show()
1.7.13.Tanh
许多激活函数之一是双曲正切函数(也称为tanh),其定义为:
双曲正切函数的输出范围为(-1,1),因此将强负输入映射为负值。与sigmoid函数不同,仅将接近零的值映射到接近零的输出,这在某种程度上解决了"vanishing gradients"问题。双曲正切函数在每个点都是微分的,其导数为:
由于表达式包含tanh函数,因此可以重用其值以使向后传播更快。
尽管与S形函数相比,网络获得”stuck”的机会较低,但是双曲正切函数仍然受到”vanishing gradients”的影响。整流线性单元(ReLU)可用于克服此问题。
功能torch.tanh()为PyTorch中的双曲正切函数提供支持。它期望输入为弧度形式,并且输出在[-∞,∞]范围内。输入类型为张量,如果输入包含多个元素,则将计算按元素的双曲正切值。
用法:torch.tanh(x, out=None)
参数:
X: 输出张量
name(可选):输出张量
返回类型:与X具有相同类型的张量。
代码1:
# Importing the PyTorch library
import torch# A constant tensor of size 6
a = torch.FloatTensor([1.0, -0.5, 3.4, -2.1, 0.0, -6.5])
print(a)# Applying the tanh function and storing the result in 'b'
b = torch.tanh(a)
print(b)
输出:
tensor([ 1.0000, -0.5000, 3.4000, -2.1000, 0.0000, -6.5000])
tensor([ 0.7616, -0.4621, 0.9978, -0.9705, 0.0000, -1.0000])
代码2:可视化
# Importing the PyTorch library
import torch
import numpy as np
import matplotlib.pyplot as plt# A vector of size 15 with values from -5 to 5
a = np.linspace(-5, 5, 15)# Applying the hyperbolic tangent function and storing the result in 'b'
b = torch.tanh(torch.FloatTensor(a))
print(b)# Plotting
plt.plot(a, b.numpy(), color='red', marker='o')
plt.title("torch.tanh")
plt.xlabel("X")
plt.ylabel("Y")plt.show()
输出结果:
tensor([-0.9999, -0.9996, -0.9984, -0.9934, -0.9728, -0.8914, -0.6134, 0.0000,0.6134, 0.8914, 0.9728, 0.9934, 0.9984, 0.9996, 0.9999])
1.7.14.cons
torch.cos()为PyTorch中的余弦函数提供支持。它期望输入为弧度形式,并且输出范围为[-1, 1]。
输入类型为张量,如果输入包含多个元素,则将计算按元素的余弦值。
用法:torch.cos(x, out=None)
参数:
X: 输入张量
name(可选):输出张量
返回类型:与x具有相同类型的张量。
代码1:
# Importing the PyTorch library
import torch# A constant tensor of size 6
a = torch.FloatTensor([1.0, -0.5, 3.4, -2.1, 0.0, -6.5])
print(a)# Applying the cos function and storing the result in 'b'
b = torch.cos(a)
print(b)
输出:
tensor([ 1.0000, -0.5000, 3.4000, -2.1000, 0.0000, -6.5000])
tensor([ 0.5403, 0.8776, -0.9668, -0.5048, 1.0000, 0.9766])
代码2:可视化
# Importing the PyTorch library
import torch
import numpy as np
import matplotlib.pyplot as plt
a = np.linspace(-5, 5, 15)# Applying the consine function and storing the result in 'b'
b = torch.cos(torch.FloatTensor(a))
print(b)# Plotting
plt.plot(a, b.numpy(), color='red', marker="o")
plt.title("torch.cos")
plt.xlabel("X")
plt.ylabel("Y")plt.show()
输出:
tensor([ 0.2837, -0.4138, -0.9090, -0.9598, -0.5414, 0.1417, 0.7556, 1.0000,0.7556, 0.1417, -0.5414, -0.9598, -0.9090, -0.4138, 0.2837])
1.7.15.Tan
功能torch.tan()提供对PyTorch中切线功能的支持。它期望输入为弧度形式,并且输出在[-∞,∞]范围内。输入类型为张量,如果输入包含多个元素,则将计算按元素的切线。
用法:torch.tan(x, out=None)
参数:
X: 输入张量
name(可选):输出张量
返回类型:与X具有相同类型的张量。
代码1:
# Importing the PyTorch libraryimport torch# A constant tensor of size 6
a = torch.FloatTensor([1.0, -0.5, 3.4, -2.1, 0.0, -6.5])
print(a)# Applying the tan function and storing the result in 'b'
b = torch.tan(a)
print(b)
输出:
tensor([ 1.0000, -0.5000, 3.4000, -2.1000, 0.0000, -6.5000])
tensor([ 1.5574, -0.5463, 0.2643, 1.7098, 0.0000, -0.2203])
代码2:可视化
# Importing the PyTorch libraryimport torch
import numpy as np
import matplotlib.pyplot as plt# A vector of size 15 with values from -1 to 1
a = np.linspace(-1, 1, 15)# Applying the tangent function and storing the result in 'b'
b = torch.tan(torch.FloatTensor(a))print(b)# Plotting
plt.plot(a, b.numpy(), color='red', marker='o')
plt.title("torch.tanh")
plt.xlabel("X")
plt.ylabel("Y")plt.show()
输出:
tensor([-1.5574, -1.1549, -0.8670, -0.6430, -0.4569, -0.2938, -0.1438, 0.0000,0.1438, 0.2938, 0.4569, 0.6430, 0.8670, 1.1549, 1.5574])
1.7.16.Asin
torch.asin()为PyTorch中的反正弦函数提供支持。它期望输入在[-1,1]范围内,并以弧度形式给出输出。如果输入不在[-1,1]范围内,则返回nan。输入类型为张量,如果输入包含多个元素,则将计算按元素的反正弦值。
用法:torch.asin(x, out=None)
参数:
X: 输入张量
name(可选):输出张量
返回类型:与x具有相同类型的张量。
代码1:
# Importing the PyTorch library
import torch# A constant tensor of size 6
a = torch.FloatTensor([1.0, -0.5, 3.4, 0.2, 0.0, -2])
print(a)# Applying the inverse sin function and
# storing the result in 'b'
b = torch.asin(a)
print(b)
输出:
tensor([ 1.0000, -0.5000, 3.4000, 0.2000, 0.0000, -2.0000])
tensor([ 1.5708, -0.5236, nan, 0.2014, 0.0000, nan])
代码2:可视化
# Importing the PyTorch library
import torch# Importing the NumPy library
import numpy as np# Importing the matplotlib.pylot function
import matplotlib.pyplot as plt# A vector of size 15 with values from -1 to 1
a = np.linspace(-1, 1, 15)# Applying the inverse sine function and
# storing the result in 'b'
b = torch.asin(torch.FloatTensor(a))print(b)# Plotting
plt.plot(a, b.numpy(), color='red', marker="o")
plt.title("torch.asin")
plt.xlabel("X")
plt.ylabel("Y")plt.show()
输出结果:
tensor([-1.5708, -1.0297, -0.7956, -0.6082, -0.4429, -0.2898, -0.1433, 0.0000,0.1433, 0.2898, 0.4429, 0.6082, 0.7956, 1.0297, 1.5708])
1.7.17.acos
torch.acos()为PyTorch中的反余弦函数提供支持。它期望输入在[-1,1]范围内,并以弧度形式给出输出。如果输入不在[-1,1]范围内,则返回nan。输入类型为张量,如果输入包含多个元素,则将计算按元素的反余弦值。
用法:torch.acos(x, out=None)
参数:
返回类型:与X具有相同类型的张量。
代码1:
# Importing the PyTorch library
import torch# Importing the NumPy library
import numpy as np# Importing the matplotlib.pylot function
import matplotlib.pyplot as plt# A vector of size 15 with values from -1 to 1
a = np.linspace(-1, 1, 15)# Applying the inverse sine function and
# storing the result in 'b'
b = torch.acos(torch.FloatTensor(a))print(b)# Plotting
plt.plot(a, b.numpy(), color='red', marker="o")
plt.title("torch.acos")
plt.xlabel("X")
plt.ylabel("Y")plt.show()
输出结果:
tensor([3.1416, 2.6005, 2.3664, 2.1790, 2.0137, 1.8605, 1.7141, 1.5708, 1.4274,1.2810, 1.1279, 0.9626, 0.7752, 0.5411, 0.0000])
1.7.18.atan
torch.atan()为PyTorch中的反正切函数提供支持。它以弧度形式给出输出。输入类型为张量,如果输入包含多个元素,则将计算按元素的反正切
参数:
X: 输入张量
name(可选):输出张量
返回类型:与x具有相同类型的张量。
代码1:
import torch# A constant tensor of size 6
a = torch.FloatTensor([1.0, -0.5, 3.4, 0.2, 0.0, -2])
print(a)# Applying the inverse tan function and storing the result in 'b'
b = torch.atan(a)
print(b)
输出:
tensor([ 1.0000, -0.5000, 3.4000, 0.2000, 0.0000, -2.0000])
tensor([ 0.7854, -0.4636, 1.2847, 0.1974, 0.0000, -1.1071])
代码2:可视化
# Importing the PyTorch library
import torch# Importing the NumPy library
import numpy as np# Importing the matplotlib.pylot function
import matplotlib.pyplot as plt# A vector of size 15 with values from -5 to 5
a = np.linspace(-5, 5, 15)# Applying the inverse tangent function and
# storing the result in 'b'
b = torch.atan(torch.FloatTensor(a))print(b)# Plotting
plt.plot(a, b.numpy(), color='red', marker="o")
plt.title("torch.atan")
plt.xlabel("X")
plt.ylabel("Y")plt.show()
输出结果:
tensor([-1.3734, -1.3416, -1.2978, -1.2341, -1.1342, -0.9601, -0.6202, 0.0000,0.6202, 0.9601, 1.1342, 1.2341, 1.2978, 1.3416, 1.3734])