tensor

PyTorch torch.max over multiple dimensions

不羁的心 提交于 2020-08-27 05:26:21
问题 Have tensor like : x.shape = [3, 2, 2] . import torch x = torch.tensor([ [[-0.3000, -0.2926],[-0.2705, -0.2632]], [[-0.1821, -0.1747],[-0.1526, -0.1453]], [[-0.0642, -0.0568],[-0.0347, -0.0274]] ]) I need to take .max() over the 2nd and 3rd dimensions. I expect some like this [-0.2632, -0.1453, -0.0274] as output. I tried to use: x.max(dim=(1,2)) , but this causes an error. 回答1: As of today, there is no way to do .min() or .max() over multiple dimensions in PyTorch. There is an open issue

PyTorch torch.max over multiple dimensions

眉间皱痕 提交于 2020-08-27 05:25:02
问题 Have tensor like : x.shape = [3, 2, 2] . import torch x = torch.tensor([ [[-0.3000, -0.2926],[-0.2705, -0.2632]], [[-0.1821, -0.1747],[-0.1526, -0.1453]], [[-0.0642, -0.0568],[-0.0347, -0.0274]] ]) I need to take .max() over the 2nd and 3rd dimensions. I expect some like this [-0.2632, -0.1453, -0.0274] as output. I tried to use: x.max(dim=(1,2)) , but this causes an error. 回答1: As of today, there is no way to do .min() or .max() over multiple dimensions in PyTorch. There is an open issue

Efficient tensor contraction with Python

坚强是说给别人听的谎言 提交于 2020-07-22 06:08:41
问题 I have a piece of code with a bottleneck calculation involving tensor contractions. Lets say I want to calculate a tensor A_{i,j,k,l}( X ) whose non-zero entries for a single x\in X are N ~ 10^5, and X represents a grid with M total points, with M~1000 approximately. For a single element of the tensor A, the rhs of the equation looks something like: A_{ijkl}(M) = Sum_{m,n,p,q} S_{i,j, m,n }(M) B_{m,n,p,q}(M) T_{ p,q, k,l }(M) In addition, the middle tensor B_{m,n,p,q}(M) is obtained by

How to conditionally scale values in Keras Lambda layer?

ぐ巨炮叔叔 提交于 2020-07-09 08:51:30
问题 The input tensor rnn_pv is of shape (?, 48, 1) . I want to scale every element in this tensor, so I try to use Lambda layer as below: rnn_pv_scale = Lambda(lambda x: 1 if x >=1000 else x/1000.0 )(rnn_pv) But it comes the error: TypeError: Using a `tf.Tensor` as a Python `bool` is not allowed. Use `if t is not None:` instead of `if t:` to test if a tensor is defined, and use TensorFlow ops such as tf.cond to execute subgraphs conditioned on the value of a tensor. So what is the proper way to

Access elements of a Tensor

自作多情 提交于 2020-06-27 18:28:53
问题 I have the following TensorFlow tensors. tensor1 = tf.constant(np.random.randint(0,255, (2,512,512,1)), dtype='int32') #All elements in range [0,255] tensor2 = tf.constant(np.random.randint(0,255, (2,512,512,1)), dtype='int32') #All elements in range [0,255] tensor3 = tf.keras.backend.flatten(tensor1) tensor4 = tf.keras.backend.flatten(tensor2) tensor5 = tf.constant(np.random.randint(0,255, (255,255)), dtype='int32') #All elements in range [0,255] I wish to use the values stored in tensor 3

Access elements of a Tensor

删除回忆录丶 提交于 2020-06-27 18:28:45
问题 I have the following TensorFlow tensors. tensor1 = tf.constant(np.random.randint(0,255, (2,512,512,1)), dtype='int32') #All elements in range [0,255] tensor2 = tf.constant(np.random.randint(0,255, (2,512,512,1)), dtype='int32') #All elements in range [0,255] tensor3 = tf.keras.backend.flatten(tensor1) tensor4 = tf.keras.backend.flatten(tensor2) tensor5 = tf.constant(np.random.randint(0,255, (255,255)), dtype='int32') #All elements in range [0,255] I wish to use the values stored in tensor 3

PyTorch: How to get the shape of a Tensor as a list of int

北城以北 提交于 2020-06-10 02:14:18
问题 In numpy, V.shape gives a tuple of ints of dimensions of V. In tensorflow V.get_shape().as_list() gives a list of integers of the dimensions of V. In pytorch, V.size() gives a size object, but how do I convert it to ints? 回答1: For PyTorch v1.0 and possibly above: >>> import torch >>> var = torch.tensor([[1,0], [0,1]]) # Using .size function, returns a torch.Size object. >>> var.size() torch.Size([2, 2]) >>> type(var.size()) <class 'torch.Size'> # Similarly, using .shape >>> var.shape torch

pytorch: “multi-target not supported” error message

佐手、 提交于 2020-05-29 08:39:50
问题 so I want to classify some (3,50,50) pictures. First I loaded the dataset from the file without a dataloader or batches, it worked. Now, after adding both things I get that error: RuntimeError: multi-target not supported at /pytorch/aten/src/THCUNN/generic/ClassNLLCriterion.cu:15 I found a lot of answers in the internet, mostly to use "target.squeeze(1)" but it doesn´t work for me. My target-batch looks like following: tensor([[1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0]],

pytorch: “multi-target not supported” error message

百般思念 提交于 2020-05-29 08:37:20
问题 so I want to classify some (3,50,50) pictures. First I loaded the dataset from the file without a dataloader or batches, it worked. Now, after adding both things I get that error: RuntimeError: multi-target not supported at /pytorch/aten/src/THCUNN/generic/ClassNLLCriterion.cu:15 I found a lot of answers in the internet, mostly to use "target.squeeze(1)" but it doesn´t work for me. My target-batch looks like following: tensor([[1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0], [1, 0]],