torch.Tensor和numpy.ndarray
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的更多相关文章
- 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. ...
- 关于类型为numpy,TensorFlow.tensor,torch.tensor的shape变化以及相互转化
https://blog.csdn.net/zz2230633069/article/details/82669546 2018年09月12日 22:56:50 一只tobey 阅读数:727 1 ...
- 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 ...
- 解决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 ...
- PyTorch官方中文文档:torch.Tensor
torch.Tensor torch.Tensor是一种包含单一数据类型元素的多维矩阵. Torch定义了七种CPU tensor类型和八种GPU tensor类型: Data tyoe CPU te ...
- pytorch_13_pytorch 中tensor,numpy,PIL的转换
PIL:使用Python自带图像处理库读取出来的图片格式numpy:使用Python-opencv库读取出来的图片格式tensor:pytorch中训练时所采取的向量格式 import torch i ...
- torch.tensor(),torch.Tensor()
Pytorch tensor操作 https://www.cnblogs.com/jeshy/p/11366269.html 我们需要明确一下,torch.Tensor()是python类,更明 ...
- pytorch tensor与numpy转换
从官网拷贝过来的,就是做个学习记录.版本 0.4 tensor to numpy a = torch.ones(5) print(a) 输出 tensor([1., 1., 1., 1., 1.]) ...
- [深度学习] Pytorch学习(一)—— torch tensor
[深度学习] Pytorch学习(一)-- torch tensor 学习笔记 . 记录 分享 . 学习的代码环境:python3.6 torch1.3 vscode+jupyter扩展 #%% im ...
随机推荐
- Consul常用接口使用
prometheus.yml 配置 - job_name: 'node_exporter' consul_sd_configs: - server: 'consul_ip:8500' services ...
- ubuntu之路——day10.7 提高模型的表现
总结一下就是在提升偏差的方面(即贝叶斯最优误差和训练误差的差距) 1.尝试更大更深的网络 2.加入优化算法比如前面提过的momentum.RMSprop.Adam等 3.使用别的神经网络架构比如RNN ...
- java本地与树莓派中采用UDP传输文本、图片
今天解决了一个困扰好几天的问题,由于比赛需要,需要用java语言,并采用UDP传输协议,让树莓派与服务器(就是本机)建立连接传输视频,图片. 由于UDP是建立在无连接的协议上,因此就碰到了一个很尴尬的 ...
- 详解Unity Profiler内存分析问题
在使用Unity开发游戏的过程中,借助Profiler来分析内存使用状况是至关重要的.但许多开发者可能还对Profiler中各项数据表示的含义不甚明确,今天我们Unity官方的技术工程师柳振东,将针对 ...
- C语言实现简单的停车场管理系统
问题描述:停车场是一个能放n辆车的狭长通道,只有一个大门,汽车按到达的先后次序停放.若车场满了,车要停在门外的便道上等候,一旦有车走,则便道上第一辆车进入.当停车场中的车离开时,由于通道窄,在它后面呢 ...
- windows powershell学习
PowerShell,从名字可以知道,他首先是一个shell,shell的意思就是和Linux的bash等一样.和原来的cmd一样就是在里边敲命令(可执行文件)使用: 而Power就意味他是一个功能强 ...
- 批量管理工具:pssh/ansible
ssh 免密码 批量管理1.创建用户useradd user1echo “123456”| passwd --stdin user12.创建秘钥ssh-keygen -t dsa然后一直回车 非交互式 ...
- Statement.setQueryTimeout(seconds)在家中环境的再次试验 证明此语句还是有效的
对比实验:https://www.cnblogs.com/xiandedanteng/p/11955887.html 这次实验的环境是T440p上安装的Windows版Oracle11g,版本为: O ...
- 【转】Python常见web系统返回码
responses = { 100: ('Continue', 'Request received, please continue'), 101: ('Switching Protocols', ' ...
- RabbitMQ 入门教程(PHP版) 第四部分:路由(Routing)
路由(Routing) 在前面的第三部分教程中,我们实现了一个简单的日志系统.可以把日志消息广播给多个接收者. 本篇教程中我们打算新增一个功能——使得它能够只订阅消息的一个字集.例如,我们只需要把严重 ...