from_numpy
函数原型:
torch.from_numpy(ndarray) → Tensor
从numpy.ndarray
对象中创建一个Tensor
。
注意返回的tensor和ndarray
共享同一块内存。对tensor的修改会影响ndarray
中的数据,反过来也是一样。这个返回的tensor是resizable。
目前这个函数接受数据类型为numpy.float64
、numpy.float32
、numpy.float16
、numpy.complex64
、numpy.complex128
、numpy.int64
、numpy.int32
、numpy.int16
、numpy.int8
、numpy.uint8
以及numpy.bool
的ndarray
使用示例:
>>> a = numpy.array([1, 2, 3])
>>> t = torch.from_numpy(a)
>>> t
tensor([ 1, 2, 3])
>>> t[0] = -1
>>> a
array([-1, 2, 3])
sqrt
函数原型:
torch.sqrt(input, *, out=None) → Tensor
这个函数返回输入input
的开方:
o u t i = i n p u t i out_i=\sqrt{input_i} outi=inputi
参数:
- input (Tensor):输入tensor
keyword参数:
- out (Tensor, optional):输出tensor
使用示例:
>>> a = torch.randn(4)
>>> a
tensor([-2.0755, 1.0226, 0.0831, 0.4806])
>>> torch.sqrt(a)
tensor([ nan, 1.0112, 0.2883, 0.6933])