fastai notebook - midlevel data
soruce
Transform
# When we studied tokenization and numericalization in the last chapter, we started by grabbing a bunch of texts:
files = get_text_files(path, folders = ['train', 'test'])
txts = L(o.open().read() for o in files[:2000])
# We then showed how to tokenize them with a Tokenizer:
tok = Tokenizer.from_folder(path)
tok.setup(txts)
to...
fastai notebook - write a custom model
soruce
write a custom model
from fastai.vision.all import *
path = untar_data(URLs.PETS)
files = get_image_files(path/"images")
class SiameseImage(fastuple):
def show(self, ctx=None, **kwargs):
img1,img2,same_breed = self
if not isinstance(img1, Tensor):
if img2.size != img1.size: img2 = img2.resize(img1.size)
...
fastai notebook - vision
soruce
Single-label classification
Cats vs Dogs
from fastai.vision.all import *
path = untar_data(URLs.PETS)
path.ls()
files = get_image_files(path/"images")
len(files)
def label_func(f): return f[0].isupper()
dls = ImageDataLoaders.from_name_func(path, files, label_func, item_tfms=Resize(224))
dls.show_batch()
learn = vision_learner(dls,...
fastai notebook - transformer
soruce
Text transfer learning
!pip install -Uq transformers
from transformers import GPT2LMHeadModel, GPT2TokenizerFast
from fastai.text.all import *
pretrained_weights = 'gpt2'
tokenizer = GPT2TokenizerFast.from_pretrained(pretrained_weights)
model = GPT2LMHeadModel.from_pretrained(pretrained_weights)
ids = tokenizer.encode('This is an exam...
fastai notebook - text transfer learning
soruce
Text transfer learning
from fastai.text.all import *
Train a text classifier from a pretrained model
Using the high-level API
path = untar_data(URLs.IMDB)
dls = TextDataLoaders.from_folder(untar_data(URLs.IMDB), valid='test')
dls.show_batch()
text category
0 xxbos xxmaj match 1 : xxmaj tag xxmaj team xxmaj table xxmaj match xx...
fastai notebook - tabular
soruce
Tabular training
from fastai.tabular.all import *
path = untar_data(URLs.ADULT_SAMPLE)
path.ls()
df = pd.read_csv(path/'adult.csv')
df.head()
dls = TabularDataLoaders.from_csv(path/'adult.csv', path=path, y_names="salary",
cat_names = ['workclass', 'education', 'marital-status', 'occupation', 'relationship', 'race'],
cont_names...
fastai notebook - other
soruce
fine-tune
Fine-tuning: A transfer learning technique where the parameters of a pretrained model are updated by training for additional epochs using a different task to that used for pretraining.
When you use the fine_tune method, fastai will use these tricks for you. There are a few parameters you can set (which we’ll discuss later), bu...
fastai notebook - mid tier api
soruce
Transform
from fastai.text.all import *
from fastai.vision.all import *
source = untar_data(URLs.MNIST_TINY)/'train'
items = get_image_files(source)
fn = items[0]; fn
img = PILImage.create(fn); img
tconv = ToTensor()
img = tconv(img)
img.shape,type(img)
lbl = parent_label(fn); lbl
tcat = Categorize(vocab=['3','7'])
lbl = tcat(lbl); l...
共计 106 篇文章,14 页。