PyTorch torch.sin - torch.cos
torch
https://pytorch.org/docs/stable/torch.html
torch.sin()
- torch.cos()
的 input (Tensor)
都是弧度制数据,不是角度制数据。
-
torch.sin
(Python function, intorch.sin
) -
torch.Tensor.sin
(Python method, intorch.Tensor.sin
) -
torch.Tensor.sin_
(Python method, intorch.Tensor.sin_
) -
torch.cos
(Python function, intorch.cos
) -
torch.Tensor.cos
(Python method, intorch.Tensor.cos
) -
torch.Tensor.cos_
(Python method, intorch.Tensor.cos_
)
1. torch.sin
https://pytorch.org/docs/stable/generated/torch.sin.html
torch.sin(input, *, out=None) -> Tensor
Returns a new tensor with the sine of the elements of input
.
返回一个新的张量 tensor , 其元素是张量 input 元素的正弦。
out i = sin ( input i ) \text{out}_{i} = \sin(\text{input}_{i}) outi=sin(inputi)
- Parameters
input (Tensor)
- the input tensor.
- Keyword Arguments
out (Tensor, optional)
- the output tensor.
1.1. Example
(pt-1.4_py-3.6) yongqiang@yongqiang:~$ python
Python 3.6.10 |Anaconda, Inc.| (default, May 8 2020, 02:54:21)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> import numpy as np
>>>
>>> data = np.array([0, 1/2.0, 1, 3/2.0, 2])
>>> data
array([0. , 0.5, 1. , 1.5, 2. ])
>>>
>>> data = data * np.pi
>>> data
array([0. , 1.57079633, 3.14159265, 4.71238898, 6.28318531])
>>>
>>> torch.sin(data)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: sin(): argument 'input' (position 1) must be Tensor, not numpy.ndarray
>>>
>>> torch.sin(torch.Tensor(data))
tensor([ 0.0000e+00, 1.0000e+00, -8.7423e-08, -1.0000e+00, 1.7485e-07])
>>> exit()
(pt-1.4_py-3.6) yongqiang@yongqiang:~$
2. torch.cos
https://pytorch.org/docs/stable/generated/torch.cos.html
Returns a new tensor with the cosine of the elements of input
.
out i = cos ( input i ) \text{out}_{i} = \cos(\text{input}_{i}) outi=cos(inputi)
- Parameters
input (Tensor)
- the input tensor.
- Keyword Arguments
out (Tensor, optional)
- the output tensor.
2.1. Example
(pt-1.4_py-3.6) yongqiang@yongqiang:~$ python
Python 3.6.10 |Anaconda, Inc.| (default, May 8 2020, 02:54:21)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> import numpy as np
>>>
>>> data = np.array([0.0, 1/2.0, 1.0, 3/2.0, 2.0])
>>> data
array([0. , 0.5, 1. , 1.5, 2. ])
>>>
>>> data = data * np.pi
>>> data
array([0. , 1.57079633, 3.14159265, 4.71238898, 6.28318531])
>>>
>>> torch.cos(torch.Tensor(data))
tensor([ 1.0000e+00, -4.3711e-08, -1.0000e+00, 1.1925e-08, 1.0000e+00])
>>>
>>> torch.sin(torch.Tensor(data))
tensor([ 0.0000e+00, 1.0000e+00, -8.7423e-08, -1.0000e+00, 1.7485e-07])
>>>
>>> exit()
(pt-1.4_py-3.6) yongqiang@yongqiang:~$
References
[1] Yongqiang Cheng, https://yongqiang.blog.csdn.net/