zoukankan      html  css  js  c++  java
  • torch.from_numpy()

    torch.from_numpy(ndarray) → Tensor

    Creates a Tensor from a numpy.ndarray.

    The returned tensor and ndarray share the same memory. Modifications to the tensor will be reflected in the ndarray and vice versa. The returned tensor is not resizable.

    It currently accepts ndarray with dtypes of numpy.float64, numpy.float32, numpy.float16, numpy.int64, numpy.int32, numpy.int16, numpy.int8, numpy.uint8, and numpy.bool.

    简单说一下,就是torch.from_numpy()方法把数组转换成张量,且二者共享内存,对张量进行修改比如重新赋值,那么原始数组也会相应发生改变。

    Example:

    >>> a = numpy.array([1, 2, 3])
    >>> t = torch.from_numpy(a)
    >>> t
    tensor([ 1, 2, 3])
    >>> t[0] = -1
    >>> a
    array([-1, 2, 3])
     

    原文链接:https://blog.csdn.net/weixin_36670529/article/details/101776514

  • 相关阅读:
    面向对象编程
    面向对象编程进阶
    pycharm常用快捷键
    面向对象
    深拷贝和浅拷贝
    hashlib模块
    日志配置
    常用模块大全
    正则详解
    软件目录规范
  • 原文地址:https://www.cnblogs.com/xyzluck/p/12807153.html
Copyright © 2011-2022 走看看