tensor

pytorch tensor与numpy转换

戏子无情 提交于 2019-11-27 15:39:44
从官网拷贝过来的,就是做个学习记录。版本 0.4 tensor to numpy a = torch.ones(5) print(a) 输出 tensor([1., 1., 1., 1., 1.]) 进行转换 b = a.numpy() print(b) 输出 [1. 1. 1. 1. 1.] 注意,转换后的tensor与numpy指向同一地址,所以,对一方的值改变另一方也随之改变 a.add_(1) print(a) print(b) numpy to tensor import numpy as np a = np.ones(5) b = torch.from_numpy(a) np.add(a, 1, out=a) print(a) print(b) 输出 [2. 2. 2. 2. 2.] tensor([2., 2., 2., 2., 2.], dtype=torch.float64) 除chartensor外所有tensor都可以转换为numpy 来源: https://www.cnblogs.com/wzyuan/p/9733433.html

Pytorch Tensor 常用操作

坚强是说给别人听的谎言 提交于 2019-11-27 13:48:30
https://pytorch.org/docs/stable/tensors.html dtype: tessor的数据类型,总共有8种数据类型,其中默认的类型是torch.FloatTensor,而且这种类型的别名也可以写作torch.Tensor。 device : 这个参数表示了 tensor 将会在哪个设备上分配内存。它包含了设备的类型( cpu 、 cuda )和可选设备序号。如果这个值是缺省的,那么默认为当前的活动设备类型。 require_grad : 这个标志表明这个tensor的操作是否会被pytorch的自动微分系统(Autograd)记录其操作过程,以便后续自动求导。 layout : 表示了tensor的内存分布方式。目前,pytorch支持 torch.strided 方式以及实验性质地支持 torch.sparse_coo 。前者是目前普遍的使用方式。每一个 strided tensor 都关联一个 torch.storage 以保存其数据。 创建 典型的tensor构建方法: torch.tensor(data, dtype=None, device=None, requires_grad=False) 从其他形式转换而来: torch.as_tensor(data, dtype=None, device=None) torch.from_numpy

How to understand the term `tensor` in TensorFlow?

无人久伴 提交于 2019-11-27 10:21:35
I am new to TensorFlow. While I am reading the existing documentation, I found the term tensor really confusing. Because of it, I need to clarify the following questions: What is the relationship between tensor and Variable , tensor vs. tf.constant , 'tensor' vs. tf.placeholder ? Are they all types of tensors? Yaroslav Bulatov TensorFlow doesn't have first-class Tensor objects, meaning that there are no notion of Tensor in the underlying graph that's executed by the runtime. Instead the graph consists of op nodes connected to each other, representing operations. An operation allocates memory

pytorch只用中要注意通道问题

删除回忆录丶 提交于 2019-11-27 08:31:01
cv读进来的是BGR图像,通道是hcw,在torch中使用要注意维度转换 def __getitem__(self, idx): '''Load image. Args: idx: (int) image index. img_org = Image.open(self.root_src + 'reference_cutBlock' + fname_org) Returns: img: (tensor) image tensor. loc_targets: (tensor) location targets. cls_targets: (tensor) class label targets. ''' # Load image fname_org = self.fnames[idx] img_org = cv2.imread(self.root_src + 'dn_dataset/' + fname_org) # img_org = np.asarray(img_org) coin = np.random.randint(0, 50) img_dis = skimage.util.random_noise(img_org, mode='gaussian', seed=None, var=(coin / 255.0) ** 2) # add gaussian noise # img

Pytorch中通道转化问题

泄露秘密 提交于 2019-11-27 08:30:26
cv读进来的是BGR图像,通道是hcw,在torch中使用要注意维度转换 def __getitem__(self, idx): '''Load image. Args: idx: (int) image index. img_org = Image.open(self.root_src + 'reference_cutBlock' + fname_org) Returns: img: (tensor) image tensor. loc_targets: (tensor) location targets. cls_targets: (tensor) class label targets. ''' # Load image fname_org = self.fnames[idx] img_org = cv2.imread(self.root_src + 'dn_dataset/' + fname_org) # img_org = np.asarray(img_org) coin = np.random.randint(0, 50) img_dis = skimage.util.random_noise(img_org, mode='gaussian', seed=None, var=(coin / 255.0) ** 2) # add gaussian noise # img

深度学习小记

人走茶凉 提交于 2019-11-27 05:35:31
深度学习小记 0 前言 近段时间,由于工作需要,一直在看深度学习的各种框架,主要是Caffe和Tensorflow。并且在可预见的未来,还会看更多不同的深度学习框架。最开始我是以软件工程师的角度去阅读这些框架的,说实话,Caffe的代码框架逻辑清晰相对好理解一点,而TensorFlow就比较麻烦了,里面内容太多,函数调用链非常长,且使用了大量的C++11语法,这对于C++功底不好的我来说无疑是重大打击...因此,我必须跳出软件工程师的思维,以算法工程师的视角来审视这些框架。考虑到它们都是为深度学习服务的,因此我转而去思考深度学习的本质,希冀能够触类旁通。 鉴于深度学习与神经网络的关系类似于漂移与汽车的关系。深度学习可以理解成用(深度)神经网络来进行机器学习,漂移可以理解成用汽车来做一些风骚的走位操作。因此,我的思考重点又聚焦到神经网络身上。网上对神经网络的解释非常多,但一般都是直接扔给你一大堆陌生的名称以及一堆看起来就很烦的公式,配合上他们说教的语气,给人的感觉就是:你看,神经网络就这么简单,你现在一脸懵逼觉得复杂是因为你没我厉害,跟着我学几年也许你就会了。 不可否认,这些专有名词和公式非常重要,但是我们也必须知道所有的名词和公式都是数学家为了方便运算/记录,而对某些概念或者规则进行的抽象处理,因此我们应该先理解这些名词或公式背后的概念或规则,然后再来反推这些公式

pytorch笔记

南楼画角 提交于 2019-11-27 05:02:38
Tensor tensor.size()返回torch.Size对象,它是tuple的子类,但其使用方式与tuple略有区别。 tensor.shape等价于tensor.size() 需要注意 的是,t.Tensor(*sizes)创建时,系统不会马上分配空间,只会计算剩余的内存是否足够使用,使用到tensor时才会分配,而其他操作都是在创建完tensor后马上进行空间分配。 tensor.view:可以调整tensor的形状,但必须保证总数一致。不会修改自身的数据,共享内存,即更改其中一个,另外一个也会跟着改变。 squeeze和unsqueeze:添加或减少某一维度。 resize:也可用来调整size,但可修改尺寸,如b.resize_(1, 3) CPU tensor与GPU tensor之间的互相转换通过tensor.cuda和tensor.cpu的方法实现。 pytorch的线性函数主要封装了Blas和Lapack,其用法和接口都与之类似。 当遇到Tensor不支持的操作时,可先转成Numpy数组,处理后再转回tensor,其转换开销很小。 tensor分为头信息区(Tensor)和存储区(Storage) 在科学计算中应当避免使用Python原生的for循环,尽量使用向量化的数值计算,有近10倍的速度差。 因此在平时写代码时,就应养成向量化的思维习惯。

TensorFlow: Max of a tensor along an axis

心已入冬 提交于 2019-11-27 02:37:57
问题 My question is in two connected parts: How do I calculate the max along a certain axis of a tensor? For example, if I have x = tf.constant([[1,220,55],[4,3,-1]]) I want something like x_max = tf.max(x, axis=1) print sess.run(x_max) output: [220,4] I know there is a tf.argmax and a tf.maximum , but neither give the maximum value along an axis of a single tensor. For now I have a workaround: x_max = tf.slice(x, begin=[0,0], size=[-1,1]) for a in range(1,2): x_max = tf.maximum(x_max , tf.slice(x

Best way to save a trained model in PyTorch?

半城伤御伤魂 提交于 2019-11-27 02:25:50
I was looking for alternative ways to save a trained model in PyTorch. So far, I have found two alternatives. torch.save() to save a model and torch.load() to load a model. model.state_dict() to save a trained model and model.load_state_dict() to load the saved model. I have come across to this discussion where approach 2 is recommended over approach 1. My question is, why the second approach is preferred? Is it only because torch.nn modules have those two function and we are encouraged to use them? I've found this page on their github repo, I'll just paste the content here. Recommended