tensor

How to resize a PyTorch tensor?

ぃ、小莉子 提交于 2021-02-18 23:01:34
问题 Now I have a torch.Tensor of size (5, 1, 44, 44) in Pytorch. 5 = batch size 1 = channel 44= image height 44= image width and I want to 'resize' it to shape (5, 1, 224, 224) How can I do that? What functions should I use? 回答1: It seems like you are looking for interpolate (a function in nn.functional ): import torch.nn.functional as nnf x = torch.rand(5, 1, 44, 44) out = nnf.interpolate(x, size=(224, 224), mode='bicubic', align_corners=False) If you really care about the accuracy of the

How to resize a PyTorch tensor?

一笑奈何 提交于 2021-02-18 23:00:20
问题 Now I have a torch.Tensor of size (5, 1, 44, 44) in Pytorch. 5 = batch size 1 = channel 44= image height 44= image width and I want to 'resize' it to shape (5, 1, 224, 224) How can I do that? What functions should I use? 回答1: It seems like you are looking for interpolate (a function in nn.functional ): import torch.nn.functional as nnf x = torch.rand(5, 1, 44, 44) out = nnf.interpolate(x, size=(224, 224), mode='bicubic', align_corners=False) If you really care about the accuracy of the

Proper usage of `tf.scatter_nd` in tensorflow-r1.2

▼魔方 西西 提交于 2021-02-16 19:08:09
问题 Given indices with shape [batch_size, sequence_len] , updates with shape [batch_size, sequence_len, sampled_size] , to_shape with shape [batch_size, sequence_len, vocab_size] , where vocab_size >> sampled_size , I'd like to use tf.scatter to map the updates to a huge tensor with to_shape , such that to_shape[bs, indices[bs, sz]] = updates[bs, sz] . That is, I'd like to map the updates to to_shape row by row. Please note that sequence_len and sampled_size are scalar tensors, while others are

Proper usage of `tf.scatter_nd` in tensorflow-r1.2

╄→гoц情女王★ 提交于 2021-02-16 19:07:29
问题 Given indices with shape [batch_size, sequence_len] , updates with shape [batch_size, sequence_len, sampled_size] , to_shape with shape [batch_size, sequence_len, vocab_size] , where vocab_size >> sampled_size , I'd like to use tf.scatter to map the updates to a huge tensor with to_shape , such that to_shape[bs, indices[bs, sz]] = updates[bs, sz] . That is, I'd like to map the updates to to_shape row by row. Please note that sequence_len and sampled_size are scalar tensors, while others are

Dot product along third axis

青春壹個敷衍的年華 提交于 2021-02-15 13:35:26
问题 I'm trying to take a tensor dot product in numpy using tensordot , but I'm not sure how I should reshape my arrays to achieve my computation. (I'm still new to the mathematics of tensors, in general.) I have arr = np.array([[[1, 1, 1], [0, 0, 0], [2, 2, 2]], [[0, 0, 0], [4, 4, 4], [0, 0, 0]]]) w = [1, 1, 1] And I want to take a dot product along axis=2 , such that I have the matrix array([[3, 0, 6], [0, 12, 0]]) What's the proper numpy syntax for this? np.tensordot(arr, [1, 1, 1], axes=2)

Dot product along third axis

爷,独闯天下 提交于 2021-02-15 13:32:54
问题 I'm trying to take a tensor dot product in numpy using tensordot , but I'm not sure how I should reshape my arrays to achieve my computation. (I'm still new to the mathematics of tensors, in general.) I have arr = np.array([[[1, 1, 1], [0, 0, 0], [2, 2, 2]], [[0, 0, 0], [4, 4, 4], [0, 0, 0]]]) w = [1, 1, 1] And I want to take a dot product along axis=2 , such that I have the matrix array([[3, 0, 6], [0, 12, 0]]) What's the proper numpy syntax for this? np.tensordot(arr, [1, 1, 1], axes=2)

How to get layer weight while training?

只愿长相守 提交于 2021-02-10 14:35:23
问题 I have a model and I would like to get the weight matrix of a specific layer to use it while defining custom loss function. Is there any way to get a weight of specific layer, inside the model? P.S. I am currently working with tensorflow 2, and keras functional API. I tested How do I get the weights of a layer in Keras? approach, but it did not work. P.P.S. By using the above described approach, I get the following error: AttributeError Traceback (most recent call last) <ipython-input-26

PyTorch get indices of value in two-dimensional tensor

可紊 提交于 2021-02-10 05:29:05
问题 Given the following tensor (or any random tensor with two dimension), I want to get the index of '101': tens = tensor([[ 101, 146, 1176, 21806, 1116, 1105, 18621, 119, 102, 0, 0, 0, 0], [ 101, 1192, 1132, 1136, 1184, 146, 1354, 1128, 1127, 117, 1463, 119, 102], [ 101, 6816, 1905, 1132, 14918, 119, 102, 0, 0, 0, 0, 0, 0]]) From the related answers I know that I can do something like this: idxs = torch.tensor([(i == 101).nonzero() for i in tens]) But this seems messy and potentially quite slow.

How to compare two arrays using tensorflow?

南楼画角 提交于 2021-02-08 05:08:34
问题 I need to compare two arrays and get either true or false,not elementwise result. My code is X = tf.constant([0.05, 0.10], dtype=tf.float32, shape=[1, 2]) y = tf.constant([0.01, 0.99], dtype=tf.float32, shape=[1, 2]) equality = tf.equal(X, y) prints [False, False] my requirement is to get true or false, not an array. 回答1: Assuming that you want to return False if any of your values are not equal then you can use the reduce_all operation: equality = tf.math.reduce_all(tf.equal(X, y)) 回答2: I

Deriving the structure of a pytorch network

我是研究僧i 提交于 2021-02-04 08:03:10
问题 For my use case, I require to be able to take a pytorch module and interpret the sequence of layers in the module so that I can create a “connection” between the layers in some file format. Now let’s say I have a simple module as below class mymodel(nn.Module): def __init__(self, input_channels): super(mymodel, self).__init__() self.fc = nn.Linear(input_channels, input_channels) def forward(self, x): out = self.fc(x) out += x return out if __name__ == "__main__": net = mymodel(5) for mod in