2022年3月14日 星期一

[pytorch, tensor] Show the first row of tensor

 Show the first row of tensor

井民全, Jing, mqjing@gmail.com


Env

# 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


Code

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

Run

python test.py

Result

(venv_test) ➜  test python test.py

tensor = tensor([[1, 2],

        [3, 4]])

First row: tensor([1, 2])