addition of 2 pytorch tensors with diffrent size - pytorch

I have 2 tensors with dimension, A = [64,155,300] and B =[64,155,100]
when I add this 2 tensors ie. C= A+B,
I get this error ==> " RuntimeError: The size of tensor a (300) must match the size of tensor b (100) at non-singleton dimension 2 "
could anyone please help how should I add above tensors? any help will be appreciated!

As error says you can not add two tensor with mis-match shapes
but if you want you can repeat your third dim of B tensor so it can match with A
using torch.Tensor.repeat try A + B.repeat(1,1,3)
>>> A.shape
torch.Size([64, 155, 300])
>>> B.shape
torch.Size([64, 155, 100])
>>> B = B.repeat(1,1,3)
>>> B.shape
torch.Size([64, 155, 300])
>>> C = A + B
>>> C.shape
torch.Size([64, 155, 300])

Related

Tensor Reshape Query

I have a image tensor of shape :-
N,C,H,W = 5,512,13,13
I need to take a mean across H and W dimensions so that the output is of shape :-
N,C,1,1
I am trying doing for loop but is there some better way to do so using reshape. .
import torch
tz = torch.rand(5, 512, 13, 13)
tzm = tz.mean(dim=(2,3), keepdim=True)
tzm.shape
Output
torch.Size([5, 512, 1, 1])

Concat two tensors

I want to concat two tensors of size a: torch.Size([16, 1]) and b: torch.Size([16, 120])
to be of size torch.Size([16, 121])
could you please help with that?
Here you can use the torch.cat() function.
Example:
>>> a = torch.rand([16,1])
>>> b = torch.rand([16,120])
>>> a.size()
torch.Size([16, 1])
>>> b.size()
torch.Size([16, 120])
>>> c = torch.cat((a,b),dim=1)
>>> c.size()
torch.Size([16, 121])
What you want to do is to concatenate the tensors on the first dimension (dim=1).

the meaning of (-1,)+x.shape[2:] where x is a torch.tensor

I once saw code segment using torch.reshape as following:
for name, value in samples.items():
value_flat = torch.reshape(value, (-1,) + value.shape[2:]))
what does (-1,) + value.shape[2:]) means here? Here value is of type torch.tensor.
(-1,) is a one element tuple containing -1.
value.shape[2:] selects from the third to last elements from value.shape (the shape of tensor value).
All in all, what happens is the tuple gets concatenated with the torch.Size object to make a new tuple. Let's take an example tensor:
>>> x = torch.rand(2, 3, 63, 64)
>>> x.shape[2:]
torch.Size([64, 64])
>>> (-1,) + x.shape[2:]
(-1, 64, 64)
When using -1 in torch.reshape, it indicates to 'put the rest of the dimensions on that axis'. Here it will essentially flatten the first and second axes (batch and channel) together.
In our example, the shape will go from (2, 3, 64, 64) to (6, 64, 64), i.e. if the tensor has four dimensions, the operation is equivalent to
value.reshape(value.size(0)*value.size(1), value.size(2), value.size(3))
but is certainly very clumsy to write it this way.

Split and extract values from a PyTorch tensor according to given dimensions

I have a tensor Aof sizetorch.Size([32, 32, 3, 3]) and I want to split it and extract a tensor B of size torch.Size([16, 16, 3, 3]) from it. The tensor can be 1d or 4d and split has to be according to the given new tensor dimensions. I have been able to generate the target dimensions but I'm unable to split and extract the values from the source tensor. I ave tried torch.narrow but it takes only 3 arguments and I need 4 in many cases. torch.split takes dim as an int due to which tensor is split along one dimension only. But I want to split it along multiple dimensions.
You have multiple options:
use .split multiple times
use .narrow multiple times
use slicing
e.g.:
t = torch.rand(32, 32, 3, 3)
t0, t1 = t.split((16, 16), 0)
print(t0.shape, t1.shape)
>>> torch.Size([16, 32, 3, 3]) torch.Size([16, 32, 3, 3])
t00, t01 = t0.split((16, 16), 1)
print(t00.shape, t01.shape)
>>> torch.Size([16, 16, 3, 3]) torch.Size([16, 16, 3, 3])
t00_alt, t01_alt = t[:16, :16, :, :], t[16:, 16:, :, :]
print(t00_alt.shape, t01_alt.shape)
>>> torch.Size([16, 16, 3, 3]) torch.Size([16, 16, 3, 3])

Shape must be rank 1 but is rank 2 tflearn error

I am using a DNN provided by tflearn to learn from some data. My data variable has a shape of (6605, 32) and my labels data has a shape of (6605,) which I reshape in the code below to (6605, 1)...
# Target label used for training
labels = np.array(data[label], dtype=np.float32)
# Reshape target label from (6605,) to (6605, 1)
labels = tf.reshape(labels, shape=[-1, 1])
# Data for training minus the target label.
data = np.array(data.drop(label, axis=1), dtype=np.float32)
# DNN
net = tflearn.input_data(shape=[None, 32])
net = tflearn.fully_connected(net, 32)
net = tflearn.fully_connected(net, 32)
net = tflearn.fully_connected(net, 1, activation='softmax')
net = tflearn.regression(net)
# Define model.
model = tflearn.DNN(net)
model.fit(data, labels, n_epoch=10, batch_size=16, show_metric=True)
This gives me a couple of errors, the first is...
tensorflow.python.framework.errors_impl.InvalidArgumentError: Shape must be rank 1 but is rank 2 for 'strided_slice' (op: 'StridedSlice') with input shapes: [6605,1], [1,16], [1,16], [1].
...and the second is...
During handling of the above exception, another exception occurred:
ValueError: Shape must be rank 1 but is rank 2 for 'strided_slice' (op: 'StridedSlice') with input shapes: [6605,1], [1,16], [1,16], [1].
I have no idea what rank 1 and rank 2 are, so I do not have an idea as to how to fix this issue.
In Tensorflow, rank is the number of dimensions of a tensor (not similar to the matrix rank). As an example, following tensor has a rank of 2.
t1 = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
print(t1.shape) # prints (3, 3)
Moreover, following tensor has a rank of 3.
t2 = np.array([[[1, 1, 1], [2, 2, 2]], [[3, 3, 3], [4, 4, 4]]])
print(t2.shape) # prints (2, 2, 3)
Since tflearn is build on top of Tensorflow, inputs should not be tensors. I have modified your code as follows and commented where necessary.
# Target label used for training
labels = np.array(data[label], dtype=np.float32)
# Reshape target label from (6605,) to (6605, 1)
labels =np.reshape(labels,(-1,1)) #makesure the labels has the shape of (?,1)
# Data for training minus the target label.
data = np.array(data.drop(label, axis=1), dtype=np.float32)
data = np.reshape(data,(-1,32)) #makesure the data has the shape of (?,32)
# DNN
net = tflearn.input_data(shape=[None, 32])
net = tflearn.fully_connected(net, 32)
net = tflearn.fully_connected(net, 32)
net = tflearn.fully_connected(net, 1, activation='softmax')
net = tflearn.regression(net)
# Define model.
model = tflearn.DNN(net)
model.fit(data, labels, n_epoch=10, batch_size=16, show_metric=True)
Hope this helps.

Resources