How to concatenate a sequence of tensors
井民全, Jing, mqjing@gmail.com
Using cat method, you can concate a serial of tensors.
Key
t1 = torch.cat([tensor, tensor2])
Env
Code
File: test.py
Run
python test.py
How to concatenate a sequence of tensors
井民全, Jing, mqjing@gmail.com
Using cat method, you can concate a serial of tensors.
t1 = torch.cat([tensor, tensor2])
# git, zsh sudo apt-get install git zsh sh -c "$(wget -O- https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" # python, pytorch wget https://repo.anaconda.com/archive/Anaconda3-5.3.1-Linux-x86_64.sh bash ./Anaconda3-5.3.1-Linux-x86_64.sh conda create --name venv_test source activate venv_test # CPU only, pytorch conda install pytorch torchvision torchaudio cpuonly -c pytorch # GPU, pytorch conda install pytorch torchvision torchaudio -c pytorch |
File: test.py
import torch data = [[1, 2], [3, 4]] tensor = torch.tensor(data) # setup the tensor from array print(f"tensor = {tensor}") print(f"First row: {tensor[0]}") # show the first row tensor2 = torch.tensor([[5, 6], [7, 8]]) t1 = torch.cat([tensor, tensor2]) print(f"t1 = {t1}") print(f"First row of t1: {t1[0]}") print(f"Second row of t1: {t1[1]}") print(f"Third row of t1: {t1[2]}") |
python test.py