Daniel Voigt Godoy - Deep Learning with PyTorch Step-by-Step A Beginner’s Guide-leanpub

peiying410632
from peiying410632 More from this publisher
22.02.2024 Views

Additional SetupThis is a special chapter when it comes to its setup: We won’t be using only PyTorchbut rather a handful of other packages as well, including the de facto standard forNLP tasks—HuggingFace.Before proceeding, make sure you have all of them installed by running thecommands below:!pip install gensim==3.8.3!pip install allennlp==0.9.0!pip install flair==0.8.0.post1 # uses PyTorch 1.7.1!pip install torchvision==0.8.2# HuggingFace!pip install transformers==4.5.1!pip install datasets==1.6.0Some packages, like flair, may have strict dependencies andeventually require the downgrading of some other packages inyour environment, even PyTorch itself.The versions above were used to generate the outputs presentedin this chapter, but you can use newer versions if you want(except for the allennlp package since this specific version isrequired by flair for retrieving ELMo embeddings).ImportsFor the sake of organization, all libraries needed throughout the code used in anygiven chapter are imported at its very beginning. For this chapter, we’ll need thefollowing imports:import osimport jsonimport errnoimport requestsimport numpy as npfrom copy import deepcopyfrom operator import itemgetterJupyter Notebook | 881

import torchimport torch.optim as optimimport torch.nn as nnimport torch.nn.functional as Ffrom torch.utils.data import DataLoader, Dataset, random_split, \TensorDatasetfrom data_generation.nlp import ALICE_URL, WIZARD_URL, download_textfrom stepbystep.v4 import StepByStep# These are the classes we built in Chapters 9 and 10from seq2seq import *import nltkfrom nltk.tokenize import sent_tokenizeimport gensimfrom gensim import corpora, downloaderfrom gensim.parsing.preprocessing import *from gensim.utils import simple_preprocessfrom gensim.models import Word2Vecfrom flair.data import Sentencefrom flair.embeddings import ELMoEmbeddings, WordEmbeddings, \TransformerWordEmbeddings, TransformerDocumentEmbeddingsfrom datasets import load_dataset, Splitfrom transformers import (DataCollatorForLanguageModeling,BertModel, BertTokenizer, BertForSequenceClassification,DistilBertModel, DistilBertTokenizer,DistilBertForSequenceClassification,AutoModelForSequenceClassification,AutoModel, AutoTokenizer, AutoModelForCausalLM,Trainer, TrainingArguments, pipeline, TextClassificationPipeline)from transformers.pipelines import SUPPORTED_TASKS882 | Chapter 11: Down the Yellow Brick Rabbit Hole

Additional Setup

This is a special chapter when it comes to its setup: We won’t be using only PyTorch

but rather a handful of other packages as well, including the de facto standard for

NLP tasks—HuggingFace.

Before proceeding, make sure you have all of them installed by running the

commands below:

!pip install gensim==3.8.3

!pip install allennlp==0.9.0

!pip install flair==0.8.0.post1 # uses PyTorch 1.7.1

!pip install torchvision==0.8.2

# HuggingFace

!pip install transformers==4.5.1

!pip install datasets==1.6.0

Some packages, like flair, may have strict dependencies and

eventually require the downgrading of some other packages in

your environment, even PyTorch itself.

The versions above were used to generate the outputs presented

in this chapter, but you can use newer versions if you want

(except for the allennlp package since this specific version is

required by flair for retrieving ELMo embeddings).

Imports

For the sake of organization, all libraries needed throughout the code used in any

given chapter are imported at its very beginning. For this chapter, we’ll need the

following imports:

import os

import json

import errno

import requests

import numpy as np

from copy import deepcopy

from operator import itemgetter

Jupyter Notebook | 881

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!