主页

decorator

soruce 基本的装饰器 概括的讲,装饰器的作用就是为已经存在的函数或对象添加额外的功能。 def debug(func): def wrapper(*args, **kwargs): # 指定宇宙无敌参数 print "[DEBUG]: enter {}()".format(func.__name__) print 'Prepare and say...', return func(*args, **kwargs) return wrapper # 返回 @debug def say(something): print "hello {}!".format(something) 带参数的装饰...

阅读更多

import

soruce import 整个模块(需要使用前缀) # 导入sys整个模块 import sys # 使用sys模块名作为前缀来访问模块中的成员 print(sys.argv[0]) # 导入sys、os两个模块 import sys,os 设置别名(需要使用前缀) # 导入sys整个模块,并指定别名为s import sys as s # 使用s模块别名作为前缀来访问模块中的成员 print(s.argv[0]) # 导入sys、os两个模块,并为sys指定别名s,为os指定别名o import sys as s,os as o from xx import x1(无须使用任何前缀) # 导入sys模块的argv成员 from sys import argv # 使用...

阅读更多

pandas

soruce import import numpy as np # linear algebra import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv) import matplotlib.pyplot as plt import seaborn as sns from sklearn.preprocessing import LabelEncoder read file train = pd.read_csv("/kaggle/input/store-sales-time-series-forecasting/train.csv") test = pd.read_csv("/kaggle/...

阅读更多

fastai notebook - NLP

soruce Tokenization Word-based:: Split a sentence on spaces, as well as applying language-specific rules to try to separate parts of meaning even when there are no spaces (such as turning “don’t” into “do n’t”). Generally, punctuation marks are also split into separate tokens. Subword based:: Split words into smaller parts, based on the most co...

阅读更多

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...

阅读更多