Collecting torch==2.0.1 Downloading torch-2.0.1-cp311-cp311-manylinux1_x86_64.whl.metadata (24 kB) Collecting torchtext==0.15.2 Downloading torchtext-0.15.2-cp311-cp311-manylinux1_x86_64.whl.metadata (7.4 kB) Collecting portalocker==2.8.2 Downloading portalocker-2.8.2-py3-none-any.whl.metadata (8.5 kB) Requirement already satisfied: filelock in /usr/local/lib/python3.11/dist-packages (from torch==2.0.1) (3.18.0) Requirement already satisfied: typing-extensions in /usr/local/lib/python3.11/dist-packages (from torch==2.0.1) (4.13.2) Requirement already satisfied: sympy in /usr/local/lib/python3.11/dist-packages (from torch==2.0.1) (1.13.1) Requirement already satisfied: networkx in /usr/local/lib/python3.11/dist-packages (from torch==2.0.1) (3.4.2) Requirement already satisfied: jinja2 in /usr/local/lib/python3.11/dist-packages (from torch==2.0.1) (3.1.6) Collecting nvidia-cuda-nvrtc-cu11==11.7.99 (from torch==2.0.1) Downloading nvidia_cuda_nvrtc_cu11-11.7.99-2-py3-none-manylinux1_x86_64.whl.metadata (1.5 kB) Collecting nvidia-cuda-runtime-cu11==11.7.99 (from torch==2.0.1) Downloading nvidia_cuda_runtime_cu11-11.7.99-py3-none-manylinux1_x86_64.whl.metadata (1.6 kB) Collecting nvidia-cuda-cupti-cu11==11.7.101 (from torch==2.0.1) Downloading nvidia_cuda_cupti_cu11-11.7.101-py3-none-manylinux1_x86_64.whl.metadata (1.6 kB) Collecting nvidia-cudnn-cu11==8.5.0.96 (from torch==2.0.1) Downloading nvidia_cudnn_cu11-8.5.0.96-2-py3-none-manylinux1_x86_64.whl.metadata (1.6 kB) Collecting nvidia-cublas-cu11==11.10.3.66 (from torch==2.0.1) Downloading nvidia_cublas_cu11-11.10.3.66-py3-none-manylinux1_x86_64.whl.metadata (1.6 kB) Collecting nvidia-cufft-cu11==10.9.0.58 (from torch==2.0.1) Downloading nvidia_cufft_cu11-10.9.0.58-py3-none-manylinux2014_x86_64.whl.metadata (1.5 kB) Collecting nvidia-curand-cu11==10.2.10.91 (from torch==2.0.1) Downloading nvidia_curand_cu11-10.2.10.91-py3-none-manylinux1_x86_64.whl.metadata (1.6 kB) Collecting nvidia-cusolver-cu11==11.4.0.1 (from torch==2.0.1) Downloading nvidia_cusolver_cu11-11.4.0.1-2-py3-none-manylinux1_x86_64.whl.metadata (1.6 kB) Collecting nvidia-cusparse-cu11==11.7.4.91 (from torch==2.0.1) Downloading nvidia_cusparse_cu11-11.7.4.91-py3-none-manylinux1_x86_64.whl.metadata (1.6 kB) Collecting nvidia-nccl-cu11==2.14.3 (from torch==2.0.1) Downloading nvidia_nccl_cu11-2.14.3-py3-none-manylinux1_x86_64.whl.metadata (1.8 kB) Collecting nvidia-nvtx-cu11==11.7.91 (from torch==2.0.1) Downloading nvidia_nvtx_cu11-11.7.91-py3-none-manylinux1_x86_64.whl.metadata (1.7 kB) Collecting triton==2.0.0 (from torch==2.0.1) Downloading triton-2.0.0-1-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl.metadata (1.0 kB) Requirement already satisfied: tqdm in /usr/local/lib/python3.11/dist-packages (from torchtext==0.15.2) (4.67.1) Requirement already satisfied: requests in /usr/local/lib/python3.11/dist-packages (from torchtext==0.15.2) (2.32.3) Requirement already satisfied: numpy in /usr/local/lib/python3.11/dist-packages (from torchtext==0.15.2) (2.0.2) Collecting torchdata==0.6.1 (from torchtext==0.15.2) Downloading torchdata-0.6.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (13 kB) Requirement already satisfied: setuptools in /usr/local/lib/python3.11/dist-packages (from nvidia-cublas-cu11==11.10.3.66->torch==2.0.1) (75.2.0) Requirement already satisfied: wheel in /usr/local/lib/python3.11/dist-packages (from nvidia-cublas-cu11==11.10.3.66->torch==2.0.1) (0.45.1) Requirement already satisfied: urllib3>=1.25 in /usr/local/lib/python3.11/dist-packages (from torchdata==0.6.1->torchtext==0.15.2) (2.3.0) Requirement already satisfied: cmake in /usr/local/lib/python3.11/dist-packages (from triton==2.0.0->torch==2.0.1) (3.31.6) Collecting lit (from triton==2.0.0->torch==2.0.1) Downloading lit-18.1.8-py3-none-any.whl.metadata (2.5 kB) Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.11/dist-packages (from jinja2->torch==2.0.1) (3.0.2) Requirement already satisfied: charset-normalizer<4,>=2 in /usr/local/lib/python3.11/dist-packages (from requests->torchtext==0.15.2) (3.4.1) Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.11/dist-packages (from requests->torchtext==0.15.2) (3.10) Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.11/dist-packages (from requests->torchtext==0.15.2) (2025.1.31) Requirement already satisfied: mpmath<1.4,>=1.1.0 in /usr/local/lib/python3.11/dist-packages (from sympy->torch==2.0.1) (1.3.0) Downloading torch-2.0.1-cp311-cp311-manylinux1_x86_64.whl (619.9 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 619.9/619.9 MB 2.8 MB/s eta 0:00:00 Downloading torchtext-0.15.2-cp311-cp311-manylinux1_x86_64.whl (2.0 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 2.0/2.0 MB 31.1 MB/s eta 0:00:00 Downloading portalocker-2.8.2-py3-none-any.whl (17 kB) Downloading nvidia_cublas_cu11-11.10.3.66-py3-none-manylinux1_x86_64.whl (317.1 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 317.1/317.1 MB 5.0 MB/s eta 0:00:00 Downloading nvidia_cuda_cupti_cu11-11.7.101-py3-none-manylinux1_x86_64.whl (11.8 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 11.8/11.8 MB 106.0 MB/s eta 0:00:00 Downloading nvidia_cuda_nvrtc_cu11-11.7.99-2-py3-none-manylinux1_x86_64.whl (21.0 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 21.0/21.0 MB 92.1 MB/s eta 0:00:00 Downloading nvidia_cuda_runtime_cu11-11.7.99-py3-none-manylinux1_x86_64.whl (849 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 849.3/849.3 kB 51.8 MB/s eta 0:00:00 Downloading nvidia_cudnn_cu11-8.5.0.96-2-py3-none-manylinux1_x86_64.whl (557.1 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 557.1/557.1 MB 2.2 MB/s eta 0:00:00 Downloading nvidia_cufft_cu11-10.9.0.58-py3-none-manylinux2014_x86_64.whl (168.4 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 168.4/168.4 MB 6.0 MB/s eta 0:00:00 Downloading nvidia_curand_cu11-10.2.10.91-py3-none-manylinux1_x86_64.whl (54.6 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 54.6/54.6 MB 12.1 MB/s eta 0:00:00 Downloading nvidia_cusolver_cu11-11.4.0.1-2-py3-none-manylinux1_x86_64.whl (102.6 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 102.6/102.6 MB 8.2 MB/s eta 0:00:00 Downloading nvidia_cusparse_cu11-11.7.4.91-py3-none-manylinux1_x86_64.whl (173.2 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 173.2/173.2 MB 6.3 MB/s eta 0:00:00 Downloading nvidia_nccl_cu11-2.14.3-py3-none-manylinux1_x86_64.whl (177.1 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 177.1/177.1 MB 6.3 MB/s eta 0:00:00 Downloading nvidia_nvtx_cu11-11.7.91-py3-none-manylinux1_x86_64.whl (98 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 98.6/98.6 kB 9.1 MB/s eta 0:00:00 Downloading torchdata-0.6.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (4.6 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 4.6/4.6 MB 105.5 MB/s eta 0:00:00 Downloading triton-2.0.0-1-cp311-cp311-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (63.3 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 63.3/63.3 MB 12.4 MB/s eta 0:00:00 Downloading lit-18.1.8-py3-none-any.whl (96 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 96.4/96.4 kB 9.1 MB/s eta 0:00:00 Installing collected packages: lit, portalocker, nvidia-nvtx-cu11, nvidia-nccl-cu11, nvidia-cusparse-cu11, nvidia-curand-cu11, nvidia-cufft-cu11, nvidia-cuda-runtime-cu11, nvidia-cuda-nvrtc-cu11, nvidia-cuda-cupti-cu11, nvidia-cublas-cu11, nvidia-cusolver-cu11, nvidia-cudnn-cu11, triton, torch, torchdata, torchtext Attempting uninstall: triton Found existing installation: triton 3.2.0 Uninstalling triton-3.2.0: Successfully uninstalled triton-3.2.0 Attempting uninstall: torch Found existing installation: torch 2.6.0+cu124 Uninstalling torch-2.6.0+cu124: Successfully uninstalled torch-2.6.0+cu124 ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts. torchvision 0.21.0+cu124 requires torch==2.6.0, but you have torch 2.0.1 which is incompatible. torchaudio 2.6.0+cu124 requires torch==2.6.0, but you have torch 2.0.1 which is incompatible. Successfully installed lit-18.1.8 nvidia-cublas-cu11-11.10.3.66 nvidia-cuda-cupti-cu11-11.7.101 nvidia-cuda-nvrtc-cu11-11.7.99 nvidia-cuda-runtime-cu11-11.7.99 nvidia-cudnn-cu11-8.5.0.96 nvidia-cufft-cu11-10.9.0.58 nvidia-curand-cu11-10.2.10.91 nvidia-cusolver-cu11-11.4.0.1 nvidia-cusparse-cu11-11.7.4.91 nvidia-nccl-cu11-2.14.3 nvidia-nvtx-cu11-11.7.91 portalocker-2.8.2 torch-2.0.1 torchdata-0.6.1 torchtext-0.15.2 triton-2.0.0
import numpy as np
targets = []
for x in train_iter:
targets.append(x[0])
print(len(targets))
print(np.unique(targets, return_counts=True))25000
(array([1, 2]), array([12500, 12500]))
targets = []
for x in test_iter:
targets.append(x[0])
print(len(targets))
print(np.unique(targets, return_counts=True))25000
(array([1, 2]), array([12500, 12500]))
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-22-e2ab3ea573f3> in <cell line: 0>() ----> 1 train_loader[0] TypeError: 'DataLoader' object is not subscriptable
1
I rented I AM CURIOUS-YELLOW from my video store because of all the controversy that surrounded it when it was first released in 1967. I also heard that at first it was seized by U.S. customs if it ever tried to enter this country, therefore being a fan of films considered "controversial" I really had to see this for myself.<br /><br />The plot is centered around a young Swedish drama student named Lena who wants to learn everything she can about life. In particular she wants to focus her attentions to making some sort of documentary on what the average Swede thought about certain political issues such as the Vietnam War and race issues in the United States. In between asking politicians and ordinary denizens of Stockholm about their opinions on politics, she has sex with her drama teacher, classmates, and married men.<br /><br />What kills me about I AM CURIOUS-YELLOW is that 40 years ago, this was considered pornographic. Really, the sex and nudity scenes are few and far between, even then it's not shot like some cheaply made porno. While my countrymen mind find it shocking, in reality sex and nudity are a major staple in Swedish cinema. Even Ingmar Bergman, arguably their answer to good old boy John Ford, had sex scenes in his films.<br /><br />I do commend the filmmakers for the fact that any sex shown in the film is shown for artistic purposes rather than just to shock people and make money to be shown in pornographic theaters in America. I AM CURIOUS-YELLOW is a good film for anyone wanting to study the meat and potatoes (no pun intended) of Swedish cinema. But really, this film doesn't have much of a plot.
'I rented I AM CURIOUS-YELLOW from my video store because of all the controversy that surrounded it when it was first released in 1967. I also heard that at first it was seized by U.S. customs if it ever tried to enter this country, therefore being a fan of films considered "controversial" I really had to see this for myself.<br /><br />The plot is centered around a young Swedish drama student named Lena who wants to learn everything she can about life. In particular she wants to focus her attentions to making some sort of documentary on what the average Swede thought about certain political issues such as the Vietnam War and race issues in the United States. In between asking politicians and ordinary denizens of Stockholm about their opinions on politics, she has sex with her drama teacher, classmates, and married men.<br /><br />What kills me about I AM CURIOUS-YELLOW is that 40 years ago, this was considered pornographic. Really, the sex and nudity scenes are few and far between, even then it\'s not shot like some cheaply made porno. While my countrymen mind find it shocking, in reality sex and nudity are a major staple in Swedish cinema. Even Ingmar Bergman, arguably their answer to good old boy John Ford, had sex scenes in his films.<br /><br />I do commend the filmmakers for the fact that any sex shown in the film is shown for artistic purposes rather than just to shock people and make money to be shown in pornographic theaters in America. I AM CURIOUS-YELLOW is a good film for anyone wanting to study the meat and potatoes (no pun intended) of Swedish cinema. But really, this film doesn\'t have much of a plot.'
#3 Build a Vocabulary
from torchtext.vocab import build_vocab_from_iterator
def yield_tokens(data_iter):
for _, text in data_iter:
yield tokenizer(text)
vocab = build_vocab_from_iterator(yield_tokens(train_iter), specials=["<pad>", "<unk>"])
vocab.set_default_index(vocab["<unk>"])/usr/local/lib/python3.11/dist-packages/torch/utils/data/datapipes/iter/combining.py:297: UserWarning: Some child DataPipes are not exhausted when __iter__ is called. We are resetting the buffer and each child DataPipe will read from the start again.
warnings.warn("Some child DataPipes are not exhausted when __iter__ is called. We are resetting "
#4 Function to convert a batch of text data into numbers using the tokenizer and vocabulary
# [this movie was great] ---> [0, 345, 56, 1993]
def collate_batch(batch):
label_list, text_list = [], []
for label, text in batch:
label_list.append(label_changer(label))
preprocessed_text = torch.tensor(vocab(tokenizer(text)))
text_list.append(preprocessed_text)
label_list = torch.tensor(label_list)
text_list= pad_sequence(text_list, batch_first=True)
return label_list, text_listtensor([[[ 1.7452e+00, 1.1915e+00, 1.0513e-01, 1.5350e+00, 6.8558e-01],
[ 5.4954e-01, 2.3717e-02, -5.2975e-03, -1.5760e+00, -1.9034e-01],
[-8.6543e-02, -6.3622e-02, 5.0911e-01, -7.9676e-01, 1.0452e+00],
[ 7.1784e-01, -7.7452e-01, 5.6181e-01, -6.6701e-01, 3.7154e-01],
[ 1.1136e+00, -1.3947e+00, 1.1086e+00, -3.6365e-01, 1.0847e+00],
[-2.0389e-01, 4.7250e-01, 2.5203e+00, 8.6000e-01, -1.0914e+00],
[ 5.2231e-01, -2.9732e-01, 1.3578e+00, 1.6107e+00, 5.7232e-01],
[ 4.3576e-01, 2.0664e-01, 2.7528e-01, 2.3408e+00, 1.1571e-01],
[-7.3007e-01, -2.1055e-01, 3.1924e-01, -1.6216e+00, 1.1819e+00],
[-8.5004e-01, -7.4053e-01, 1.3104e-01, -4.5072e-01, -7.9320e-01],
[ 5.2948e-02, -1.1229e-01, -8.9678e-02, -7.2127e-01, 3.6742e-01],
[ 1.2762e+00, -1.4445e+00, -9.0989e-02, 1.6072e-01, 1.8419e-01],
[ 3.7475e-02, -2.3541e-01, 1.0243e+00, -3.2463e-01, 1.1583e+00],
[-1.0676e+00, -6.7346e-01, -2.0435e-01, -1.4789e-01, 1.4269e+00],
[ 1.8562e-01, -1.1863e+00, 1.1764e+00, -3.8751e-01, -2.8803e-01],
[-2.1164e-01, -3.4845e-01, 3.6868e-01, 2.9270e-01, 2.8227e-01]],
[[ 5.5964e-01, -3.0591e-01, -1.6335e-01, -1.3985e+00, 1.5148e+00],
[-3.2034e-01, -8.7098e-01, -4.6590e-01, -1.0458e-01, -1.2790e+00],
[ 2.2856e-01, 5.6094e-01, -9.9853e-01, 1.4668e+00, 7.3393e-01],
[ 8.0701e-01, 1.0856e+00, -1.3828e-02, 1.7142e-01, 6.3287e-01],
[-3.5771e-01, -1.4355e-01, -6.8578e-01, -5.4341e-01, 7.1584e-01],
[ 4.7111e-01, 2.1690e+00, 6.8830e-02, -4.3219e-01, -9.9666e-01],
[ 1.2650e+00, -7.7109e-01, -6.0006e-02, -4.6398e-01, 3.5992e-01],
[ 8.8449e-01, 5.2694e-01, 9.7821e-01, 6.9974e-01, -1.4336e+00],
[ 4.2377e-02, -1.1574e+00, -1.6450e-01, -1.2856e+00, 1.1031e+00],
[ 1.0317e+00, -1.0245e+00, -4.1200e-01, -5.1649e-01, 1.1332e+00],
[-5.7120e-02, 4.3887e-01, -4.3320e-01, -1.6264e+00, -7.4626e-01],
[-2.7598e-01, -5.7185e-01, -5.0551e-01, 1.7314e+00, -2.8043e-01],
[-2.1935e-01, 2.2078e+00, 5.5559e-01, 2.2359e-01, -9.4284e-02],
[ 1.3101e-01, -4.3089e-01, 3.2200e-01, -3.9848e+00, -4.8590e-01],
[-3.6687e-01, 6.0195e-01, 6.9070e-01, 1.0514e+00, 1.6113e+00],
[-2.3213e-01, 4.0619e-01, -1.5481e+00, -1.7274e+00, -6.9081e-01]],
[[ 6.0899e-01, -1.7024e+00, 9.5701e-01, -8.1992e-01, -1.4366e+00],
[-3.9478e-01, 4.0548e-01, -1.7659e-01, -1.8660e+00, -1.3461e+00],
[-1.5040e-01, -1.0348e+00, 1.3180e+00, 1.1065e+00, -7.3077e-01],
[ 5.2666e-01, 1.1992e+00, -3.5988e-01, -2.9501e-01, 2.0795e+00],
[-6.7042e-02, -1.7586e-01, 2.0439e+00, -7.6140e-01, 7.2035e-01],
[-7.1960e-01, -2.4923e-01, -1.7938e+00, 8.9301e-02, 6.5535e-01],
[ 3.6758e-01, -1.5691e+00, 1.0057e+00, 1.4146e-03, 3.9434e-01],
[-2.0900e+00, 1.0799e+00, -7.9042e-03, -9.1641e-01, -1.7792e-01],
[ 1.1016e+00, -2.0094e-01, 1.8381e+00, -4.0227e-01, 8.5112e-01],
[ 2.4849e-01, 1.0068e+00, -1.6531e+00, 1.2874e+00, 7.6110e-01],
[ 2.9994e+00, -1.6781e-01, -4.1145e-01, 5.5409e-01, -1.3952e-01],
[ 1.1292e-01, 3.0677e-01, 1.6387e-01, -2.4152e+00, -4.0973e-01],
[-9.4221e-01, 1.3023e+00, 1.5698e+00, -1.9668e-01, -1.7904e-01],
[-1.9767e+00, -1.3519e-01, -3.2869e-01, -1.5618e+00, -7.9885e-02],
[ 1.5242e+00, 3.4323e-01, 2.7711e-01, 2.5087e+00, -7.3374e-01],
[ 5.5845e-01, -3.1605e-01, -1.6597e+00, 6.8897e-01, -6.2941e-01]],
[[-8.5335e-01, -1.3343e-01, -1.4178e-01, -6.4474e-01, 6.8338e-01],
[ 2.2749e+00, -9.3600e-01, 7.4977e-02, 1.1171e+00, -9.2734e-01],
[ 4.4352e-01, -1.9186e-01, 1.9825e-02, 1.1415e+00, -1.1546e+00],
[ 1.0790e+00, -4.4184e-01, -2.5587e-02, 6.4155e-01, -1.4958e-01],
[-1.1414e+00, 6.9588e-02, 4.9729e-01, -1.5275e+00, -4.1367e-01],
[ 1.5090e-01, -2.1054e-01, -3.3734e-01, 6.7756e-01, -2.4780e-01],
[ 1.8982e+00, 1.0016e+00, -9.3952e-02, -3.3206e+00, -6.1818e-01],
[-3.0007e+00, 9.0433e-01, -1.2904e-01, 5.6225e-01, -6.8011e-01],
[-2.9576e-01, -7.5863e-01, -1.2726e+00, -5.2914e-02, 2.2165e+00],
[-4.8208e-01, 4.9071e-01, 1.6466e+00, 1.2542e+00, 1.3833e+00],
[ 1.4405e+00, 5.0935e-02, -1.0110e+00, 3.9003e-01, -6.4796e-01],
[ 8.6895e-01, -4.7228e-01, -1.8440e-01, -8.5809e-01, -3.0392e-01],
[ 1.4240e+00, 1.3703e+00, 1.5980e+00, 1.2814e+00, -8.2913e-01],
[-5.9055e-01, 5.5920e-01, -1.3819e+00, 1.6287e+00, 4.5842e-01],
[-8.3238e-01, 1.1723e-01, -2.3654e+00, 3.1911e-01, -1.0054e+00],
[-6.9444e-01, -1.7538e+00, 1.4298e-01, 1.0922e+00, 6.8478e-01]]])
tensor([[[-0.2309, -0.4207, -0.5205],
[-0.3100, -0.6584, -0.1300],
[-0.3125, -0.4885, -0.1085],
[-0.1973, -0.3946, -0.3020],
[-0.1725, -0.6281, -0.2283],
[-0.4683, -0.3084, -0.3193],
[-0.3994, -0.3638, -0.1657],
[-0.4580, -0.4381, -0.0948],
[-0.4366, -0.6288, 0.3149],
[-0.4528, -0.0827, -0.1171],
[-0.3233, -0.3484, -0.1343],
[-0.2243, -0.3750, -0.3576],
[-0.3701, -0.7493, 0.1338],
[-0.4788, -0.6296, 0.3344],
[-0.2813, -0.3121, 0.0136],
[-0.3861, -0.3789, -0.1816]],
[[-0.1640, -0.6445, -0.2545],
[-0.4716, -0.2812, -0.4293],
[-0.4749, -0.5219, -0.0224],
[-0.3491, -0.6711, 0.3021],
[-0.3708, -0.5570, 0.1871],
[-0.3204, 0.0849, -0.4464],
[-0.1745, -0.3761, -0.4716],
[-0.3777, -0.3244, -0.5115],
[-0.3564, -0.7523, 0.1945],
[-0.1712, -0.5166, -0.0573],
[-0.3102, -0.3556, -0.3504],
[-0.5112, -0.3812, -0.2560],
[-0.5442, -0.5521, 0.2877],
[-0.2517, -0.0284, -0.1783],
[-0.4011, -0.3136, -0.3160],
[-0.4020, -0.1781, -0.3602]],
[[-0.2415, -0.4682, -0.4220],
[-0.4244, -0.2148, -0.4361],
[-0.4941, -0.2259, -0.2952],
[-0.3123, -0.6281, 0.1462],
[-0.2935, -0.3134, -0.1674],
[-0.4678, -0.2659, -0.3255],
[-0.3925, -0.7712, 0.3880],
[-0.5709, -0.3438, 0.2649],
[-0.1521, -0.1434, -0.1620],
[-0.3950, -0.3536, -0.4365],
[-0.2564, -0.4698, -0.3113],
[-0.2978, -0.5336, -0.1771],
[-0.4808, -0.1326, -0.3025],
[-0.5375, -0.1228, -0.1642],
[-0.3397, -0.0335, -0.4252],
[-0.4204, -0.5418, -0.1828]],
[[-0.4609, -0.6625, 0.0008],
[-0.2034, 0.0109, -0.5308],
[-0.4534, -0.5777, -0.2547],
[-0.3992, -0.6863, 0.2182],
[-0.4778, -0.4376, 0.2111],
[-0.3730, 0.0487, -0.3500],
[-0.0893, -0.4577, -0.3279],
[-0.7038, -0.2792, -0.2528],
[-0.4612, -0.6821, 0.4874],
[-0.4544, -0.5510, 0.4320],
[-0.1776, 0.1324, -0.4974],
[-0.2849, -0.7024, -0.1599],
[-0.2976, -0.2642, -0.4123],
[-0.5245, -0.4349, -0.2249],
[-0.5486, -0.2867, -0.0640],
[-0.4921, -0.7082, 0.4825]]], grad_fn=<TransposeBackward1>)
tensor([[-0.3861, -0.3789, -0.1816],
[-0.4020, -0.1781, -0.3602],
[-0.4204, -0.5418, -0.1828],
[-0.4921, -0.7082, 0.4825]], grad_fn=<SelectBackward0>)
#6 Define RNN-based model
class senitmentModel(nn.Module):
def __init__(self, ):
super().__init__()
self.embedding = nn.Embedding(num_embeddings=len(vocab), embedding_dim=64)
self.rnn = nn.RNN(input_size=64, hidden_size=32, num_layers=1, batch_first=True)
self.linear = nn.Linear(in_features=32, out_features=2) # binary classification
def forward(self,text_data):
embedings = self.embedding(text_data)
states, outputs = self.rnn(embedings)
x = self.linear(outputs[-1])
return x#8 Train loop
model.train()
for epoch in range(EPOCHS):
loss_per_epoch = 0
for labels, features in train_loader:
features = features.to(device)
labels = labels.to(device)
# forward pass
y_pred = model.forward(features)
# loss computation
loss_func = nn.CrossEntropyLoss()
loss = loss_func(y_pred, labels)
#make gradients zero
optimizer.zero_grad()
# backward pass
loss.backward()
#weight updates
optimizer.step()
loss_per_epoch += loss
print("loss per epoch =", loss_per_epoch)loss per epoch = tensor(855.6309, device='cuda:0', grad_fn=<AddBackward0>)
loss per epoch = tensor(862.5518, device='cuda:0', grad_fn=<AddBackward0>)
loss per epoch = tensor(861.1894, device='cuda:0', grad_fn=<AddBackward0>)
loss per epoch = tensor(869.3461, device='cuda:0', grad_fn=<AddBackward0>)
loss per epoch = tensor(872.0623, device='cuda:0', grad_fn=<AddBackward0>)
loss per epoch = tensor(869.7094, device='cuda:0', grad_fn=<AddBackward0>)
loss per epoch = tensor(863.3782, device='cuda:0', grad_fn=<AddBackward0>)
loss per epoch = tensor(864.2837, device='cuda:0', grad_fn=<AddBackward0>)
loss per epoch = tensor(866.9114, device='cuda:0', grad_fn=<AddBackward0>)
loss per epoch = tensor(868.6473, device='cuda:0', grad_fn=<AddBackward0>)
loss per epoch = tensor(867.5347, device='cuda:0', grad_fn=<AddBackward0>)
--------------------------------------------------------------------------- KeyboardInterrupt Traceback (most recent call last) <ipython-input-14-964224935a44> in <cell line: 0>() 24 25 # backward pass ---> 26 loss.backward() 27 28 #weight updates /usr/local/lib/python3.11/dist-packages/torch/_tensor.py in backward(self, gradient, retain_graph, create_graph, inputs) 485 inputs=inputs, 486 ) --> 487 torch.autograd.backward( 488 self, gradient, retain_graph, create_graph, inputs=inputs 489 ) /usr/local/lib/python3.11/dist-packages/torch/autograd/__init__.py in backward(tensors, grad_tensors, retain_graph, create_graph, grad_variables, inputs) 198 # some Python versions print out the first line of a multi-line function 199 # calls in the traceback and some print out the last line --> 200 Variable._execution_engine.run_backward( # Calls into the C++ engine to run the backward pass 201 tensors, grad_tensors_, retain_graph, create_graph, inputs, 202 allow_unreachable=True, accumulate_grad=True) # Calls into the C++ engine to run the backward pass KeyboardInterrupt:
# evaluation on train data
total = 0
correct = 0
with torch.no_grad():
for batch_labels, batch_features in train_loader:
# move data to gpu
batch_features, batch_labels = batch_features.to(device), batch_labels.to(device)
outputs = model(batch_features)
_, predicted = torch.max(outputs, 1)
total = total + batch_labels.shape[0]
correct = correct + (predicted == batch_labels).sum().item()
print(correct/total)--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) <ipython-input-23-2e2cc8ce98d1> in <cell line: 0>() 8 9 # move data to gpu ---> 10 batch_features, batch_labels = batch_features.to(device), batch_labels.to(device) 11 12 outputs = model(batch_features) AttributeError: 'tuple' object has no attribute 'to'
#9 evaluate on test data
model.eval()
with torch.no_grad():
loss_per_epoch = 0
for labels, features in test_loader:
features = features.to(device)
labels = labels.to(device)
# forward pass
y_pred = model.forward(features)
# loss computation
loss_func = nn.CrossEntropyLoss()
loss = loss_func(y_pred, labels)
loss_per_epoch += loss
print("loss per epoch =", loss_per_epoch)loss per epoch = tensor(4680.2925)
# Accuracy on test data
total = 0
correct = 0
with torch.no_grad():
for batch_labels, batch_features in test_loader:
# move data to gpu
batch_features, batch_labels = batch_features.to(device), batch_labels.to(device)
outputs = model(batch_features)
_, predicted = torch.max(outputs, 1)
total = total + batch_labels.shape[0]
correct = correct + (predicted == batch_labels).sum().item()
print(correct/total)0.49956