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. 《挑战30天C++入门极限》C++面向对象编程入门:构造函数与析构函数

        C++面向对象编程入门:构造函数与析构函数 请注意,这一节内容是c++的重点,要特别注意! 我们先说一下什么是构造函数. 上一个教程我们简单说了关于类的一些基本内容,对于类对象成员的初始化我们 ...

  2. Pytest权威教程04-断言的编写和报告

    目录 断言的编写和报告 使用assert语句进行断言 异常断言 警示断言 使用上下文对比 自定义断言对比信息 高级断言内省 返回: Pytest权威教程 断言的编写和报告 使用assert语句进行断言 ...

  3. CF1187F Expected Square Beauty(期望)

    题目 CF1187F Expected Square Beauty 做法 \(B(x)=\sum\limits_{i=1}^n I_i(x),I_i(x)=\begin{cases}1&x_i ...

  4. 2019年领航杯 江苏省网络信息安全竞赛 初赛部分writeup

    赛题已上传,下载连接:https://github.com/raddyfiy/2019linghangcup 做出了全部的misc和前三道逆向题,排名第10,暂且贴一下writeup. 关卡一 编码解 ...

  5. 第2课第2节_Java面向对象编程_封装性_P【学习笔记】

    摘要:韦东山android视频学习笔记  面向对象程序的三大特性之封装性:把属性和方法封装在一个整体,同时添加权限访问. 1.封装性的简单程序如下,看一下第19行,如果我们不对age变量进行权限的管控 ...

  6. 透过字节码分析Java动态代理机制。

    一.创建动态代理代码 1.创建接口 public interface Subject { void request(); } 2.创建接口实现类 public class RealSubject im ...

  7. Nfs固定端口 nfs 端口公网映射

  8. 完美解决Cannot download "https://github.com/sass/node-sass/releases/download/binding.nod的问题

    ①:例如很多人第一步就会这样做: 出现:Cannot download "https://github.com/sass/node-sass/releases/download/版本号/XX ...

  9. composer 安装以及使用教程

    CentOS 7 安装 Composer: composer 官方下载文档:https://getcomposer.org/download/ 首先 centos 必须安装 php-cli,也就是在命 ...

  10. flutter Slider滑块组件

    滑块,允许用户通过滑动滑块来从一系列值中选择. import 'package:flutter/material.dart'; class SliderDemo extends StatefulWid ...