1. torch.Tensor和numpy.ndarray相互转换

import torch
import numpy as np
# <class 'numpy.ndarray'>
np_data = np.arange(6).reshape((2,3))
# <class 'torch.Tensor'>
torch_data = torch.from_numpy(np_data)
# <class 'numpy.ndarray'>
tensor2array = torch_data.numpy()
print('numpy array:\n',np_data,type(np_data),
'\ntorch tensor:\n',torch_data,type(torch_data),
'\ntensor to array:\n',tensor2array,type(tensor2array))
# numpy array:
# [[0 1 2]
# [3 4 5]] <class 'numpy.ndarray'>
# torch tensor:
# tensor([[0, 1, 2],
# [3, 4, 5]]) <class 'torch.Tensor'>
# tensor to array:
# [[0 1 2]
# [3 4 5]] <class 'numpy.ndarray'> torch.Tensor:是一个包含了一种数据类型元素的多维矩阵,缺省为torch.FloatTensor
2. torch.Tensor和numpy.ndarray一些简单操作,如均值,绝对值,sin,log等
data = [-1,-2,1,2]
tensor_default = torch.Tensor(data)
tensor = torch.FloatTensor(data)
print('tensor default type:\n',tensor_default,
'\ntensor FloatTensor type:\n',tensor,
'\nabs:',
'\nnumpy:',np.abs(data),
'\ntorch:',torch.abs(tensor),
'\nsin:',
'\nnumpy:',np.sin(data),
'\ntorch:',torch.sin(tensor),
'\nmean:',
'\nnumpy:',np.mean(data),
'\ntorch:',torch.mean(tensor),)
# tensor default type:
# tensor([-1., -2., 1., 2.])
# tensor FloatTensor type:
# tensor([-1., -2., 1., 2.])
# abs:
# numpy: [1 2 1 2]
# torch: tensor([1., 2., 1., 2.])
# sin:
# numpy: [-0.84147098 -0.90929743 0.84147098 0.90929743]
# torch: tensor([-0.8415, -0.9093, 0.8415, 0.9093])
# mean:
# numpy: 0.0
# torch: tensor(0.) 3. 矩阵乘法(正确的做法)
data = [[1,2], [3,4]]
tensor = torch.FloatTensor(data)
print(
'\nmatrix multiplication (matmul):',
'\nnumpy:\n', np.matmul(data, data), # [[7, 10], [15, 22]]
'\ntorch:\n', torch.mm(tensor, tensor)) # [[7, 10], [15, 22]]
# matrix multiplication (matmul):
# numpy:
# [[ 7 10]
# [15 22]]
# torch:
# tensor([[ 7., 10.],
# [15., 22.]])

torch.Tensor和numpy.ndarray的更多相关文章

  1. Python中 list, numpy.array, torch.Tensor 格式相互转化

    1.1 list 转 numpy ndarray = np.array(list) 1.2 numpy 转 list list = ndarray.tolist() 2.1 list 转 torch. ...

  2. 关于类型为numpy,TensorFlow.tensor,torch.tensor的shape变化以及相互转化

    https://blog.csdn.net/zz2230633069/article/details/82669546 2018年09月12日 22:56:50 一只tobey 阅读数:727   1 ...

  3. has invalid type <class 'numpy.ndarray'>, must be a string or Tensor

    转自: https://blog.csdn.net/jacke121/article/details/78833922 has invalid type <class 'numpy.ndarra ...

  4. 解决Tensorflow ValueError: Failed to convert a NumPy array to a Tensor (Unsupported object type numpy.ndarray)

    问题描述 在将一个数组送入tensorflow训练时,报错如下: ValueError: Failed to convert a NumPy array to a Tensor (Unsupporte ...

  5. PyTorch官方中文文档:torch.Tensor

    torch.Tensor torch.Tensor是一种包含单一数据类型元素的多维矩阵. Torch定义了七种CPU tensor类型和八种GPU tensor类型: Data tyoe CPU te ...

  6. pytorch_13_pytorch 中tensor,numpy,PIL的转换

    PIL:使用Python自带图像处理库读取出来的图片格式numpy:使用Python-opencv库读取出来的图片格式tensor:pytorch中训练时所采取的向量格式 import torch i ...

  7. torch.tensor(),torch.Tensor()

    Pytorch tensor操作 https://www.cnblogs.com/jeshy/p/11366269.html    我们需要明确一下,torch.Tensor()是python类,更明 ...

  8. pytorch tensor与numpy转换

    从官网拷贝过来的,就是做个学习记录.版本 0.4 tensor to numpy a = torch.ones(5) print(a) 输出 tensor([1., 1., 1., 1., 1.]) ...

  9. [深度学习] Pytorch学习(一)—— torch tensor

    [深度学习] Pytorch学习(一)-- torch tensor 学习笔记 . 记录 分享 . 学习的代码环境:python3.6 torch1.3 vscode+jupyter扩展 #%% im ...

随机推荐

  1. 坑:jmeter代理服务器录制脚本出现target controller is configured to "use recording Controller" but no such controller exists...

    配置好代理服务器后,运行代理服务器 run 报错: target controller is configured to "use recording Controller" bu ...

  2. Chrome浏览器控制台[DOM] Password field is not contained in a form:

    [DOM] Password field is not contained in a form: ( [DOM]密码字段不包含在form表单中) 解决方案:添加一层form标签 <div cla ...

  3. python 日志内容提取

    问题:如下,一个很大的日志文件,提取 start: 到 end: 标志中间的内容 日志文件a.log xxxxx yyyyy start: start: hahahaha end: start: ha ...

  4. mac eclipse 创建Java 工程

    首先创建Java工程testjavapro,创建包testjavapro,接着创建类testjava 参考: https://www.jianshu.com/p/20280b850c95

  5. ZR#1015

    ZR#1015 解法: 我们需要求得, $ g_i $ 表示长度为的最长不下降子序列个数. 设 $ f_{i,j} $ 表示统计第前$ i $ 个数字,得到最长不下降子序列末端为 $ j $ . 显然 ...

  6. java代码拼写sql

    java后台    String zffwdm=map.get("zffwmc")==null?"":map.get("zffwmc"); ...

  7. Assignment6:白盒测试方法

    常用的软件测试方法有两大类:静态测试方法和动态测试方法. 其中软件的静态测试不要求在计算机上实际执行所测程序,主要以一些人工的模拟技术对软件进行分析和测试:而软件的动态测试是通过输入一组预先按照一定的 ...

  8. 10个超漂亮的CSS 3D特效

    10个超漂亮的CSS 3D特效 一.总结 一句话总结: 后面有空得好好练一练,也可以作为录课素材 二.10个超漂亮的CSS 3D特效 转自或参考:10个超漂亮的CSS 3D特效https://blog ...

  9. 文献阅读 | Epigenetics in ENS development and Hirschsprung disease

    系列篇: Epigenetics in ENS development and Hirschsprung disease - Review Epigenetic Mechanisms in Hirsc ...

  10. Maven Multi-Module Example

    Maven Multi-Module - 国内版 Binghttps://cn.bing.com/search?q=Maven+Multi-Module&qs=n&form=QBRE& ...