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. ora-28000:the account is locked,Oracle修改密码有效期,Oracle设置密码不过期

    查询Oracle用户是否被锁定 --例如我这里是VMCXEDDB 是否被锁定 select username,account_status,lock_date from dba_users where ...

  2. Android Studio导入google training example gradle失败

    Error:Unable to tunnel through proxy. Proxy returns "HTTP/1.1 400 Bad Request 每次从github的Google ...

  3. 分享7个shell脚本实例--shell脚本练习必备

    概述 看多shell脚本实例自然就会有shell脚本的编写思路了,所以我一般比较推荐看脚本实例来练习shell脚本.下面分享几个shell脚本实例. 1.监测Nginx访问日志502情况,并做相应动作 ...

  4. 【Python 代码】CS231n中Softmax线性分类器、非线性分类器对比举例(含python绘图显示结果)

    1 #CS231n中线性.非线性分类器举例(Softmax) #注意其中反向传播的计算 # -*- coding: utf-8 -*- import numpy as np import matplo ...

  5. NOIP提高组初战告捷

    前天第一次参加NOIP初赛,竟然提高组考了57分进入复赛啊啊!原本自己估分是52竟然估少了[滑稽]这个成绩 是我们学校初一提高组成绩最高 还是不错(出乎我意料之外)的!

  6. ubuntu之路——day10.1 ML的整体策略——正交化

    orthogonalization 正交化的概念就是指,将你可以调整的参数设置在不同的正交的维度上,调整其中一个参数,不会或几乎不会影响其他维度上的参数变化,这样在机器学习项目中,可以让你更容易更快速 ...

  7. YARN 状态机可视化,生成状态机图

    由于在windows下面,配置好所有 编译hadoop2.4.1源码 的环境会很麻烦,好在我之前已经把hadoop2.4.1的源码成功导入eclipse,并解决了所有错误提示,所以我就可以在eclip ...

  8. Makefile教程(绝对经典,所有问题看这一篇足够了)【转】

    该篇文章为转载,是对原作者系列文章的总汇加上标注. 支持原创,请移步陈浩大神博客: http://blog.csdn.net/haoel/article/details/2886 makefile很重 ...

  9. 树莓派VNC

    sudo raspi-config Interfacing Options -> VNC 1.停止VNC窗口: vncserver -kill:1 2.修改密码 vncpasswd 3.重启服务 ...

  10. MySQL 正则(Regular Expression) 邮箱(Email)

    MySQL 正则表达式 | 菜鸟教程https://www.runoob.com/mysql/mysql-regexp.html (1条消息)常用正则表达式—邮箱(Email) - Samuel - ...