[tools] Fastai & Fastcore

Setup update cuda version check current version nvcc version if it is old, go to CUDA Toolkit 12.1 Downloads https://developer.nvidia.com/cuda downloads 添加图片注释,不超过 140 字(可选) 这里我的系统

Setup

update cuda version

check current version

nvcc --version

if it is old, go to

CUDA Toolkit 12.1 Downloads

添加图片注释,不超过 140 字(可选)

这里我的系统是windows 11, local是offline下载,network是在线下载

按照步骤安装完后重启,然后nvcc --version查看

c++ install

由于我用的最新的python version,很多包还没有,需要手动安装

下载visual studio installer

Microsoft C++ Build Tools - Visual Studio

安装完之后,选中desktop development with C++,安装即可

添加图片注释,不超过 140 字(可选)

或者考虑干脆downgrade python 到3.9

pip install

pip install -U fastai
pip install fastcore

delegates

decorator, 解释**kwargs

from fastcore.meta import delegates

def func1(a,b): return a+b

@delegates(func1)
def func2(c,**kwargs): return c+ func1(**kwargs)

测的时候用func2??即可

fastcore L

创建L

L(1,4,5,7,8)
L([1,2,3,4,5])
L(*"abcdefg") #output a,b,c,d,e,f,g

L.enumerate()

a =L(1,5,8,2,4)
a.enumerate() # output (#5) [(0, 1),(1, 5),(2, 8),(3, 2),(4, 4)]

L.attrgot('key') 得到class attribute value

def ns(a,b,c): return SimpleNamespace(apple=a,banana=b,cat=c)
k =ns(1,2,3)
c = L(k,k)
c.attrgot('apple') # output [1,1]

map(func)和starmap(func)

map take list里的每一个item,function里的argument只能是一个list里的item

L([(1, 2), (3, 4), (5, 6)]).map(lambda a: a[0]+a[1]) # output [3,7,11]

starmap 可以把list里item 的每一项作为position argument输入,所以function里的argument是list item里的position argument

L([(1, 2), (3, 4), (5, 6)]).starmap(lambda a,b: a+b)

filter()会把None 和False 过滤掉

L(1,None,2).filter() #output (#2) [1,2]

filter里面也可以加function,filter(func)

L(1,3,5).filter(lambda x: None if x==3 else x) # output (#2) [1,5]

sorted() 排序, unique()

L(2,5,1,9,2).unique().sorted() # output (#4) [1,2,5,9]

itemgot(idx) 得到每个item的第几项

L([(1, 'apple'), (2, 'banana'), (3, 'cherry')]).itemgot(0) #output [1,2,3]

concat(),把list里的每个item合并成一个,如果每个item 不包含多项,就不会变

L([(1, 'apple'), (2, 'banana'), (3, 'cherry')]).concat() # output [1,'apple',2,'banana',3,'cherry']
L(1,2,3,4).concat() # output [1,2,3,4]

sum() 取和, product()乘,不能应用于iterable item和字符

L(1,2,3,4).sum()
L(1,2,3,4).product()

zip(), combine iterables element-wise

L([1,2,3],[4,5,6]).zip()
# output [(1, 4),(2, 5),(3, 6)]

zipwith(list), 和zip相似,不过zip是把几个list放到前面的L里,zipwith是把另一些list放到后面的括弧里,逗号相隔

L([1, 2, 3]).zipwith([4, 5, 6],[7,8,9])
# output [(1, 4, 7),(2, 5, 8),(3, 6, 9)]

map_dict(func) 返回dictionary

如果什么function 都不放,L().map_dict()会返回一个k和v都是一样的dictionary

L(['a','b','c']).map_dict() # output{'a': 'a', 'b': 'b', 'c': 'c'}

map_dict放function,可以得到一个key是list item,value是implement function的dictionary

L([1, 2, 3]).map_dict(lambda x: x**2)

cycle()返回iterator

L([1, 2, 3]).cycle()
# output iterator of 1,2,3,1,2,3 ...

setattrs(attribute name, value) 可以给list里的class item添加attribute和value

k=SimpleNamespace(apple=1,cat=2)
a = L(k,k).setattrs('age',30)
a #output [namespace(apple=1, cat=2, age=30),namespace(apple=1, cat=2, age=30)]

reduce(func)可以让list里的item一个一个的按照function坍塌成一个value

L([1, 2, 3, 4]).reduce(lambda x, y: x + y)
# 会把所有数加在一起, output 10

# 如果set initial,那会从这个数开始算
L([1, 2, 3, 4]).reduce(lambda x, y: x + y, initial=10) # output 20

argwhere(function condition),可以找到满足这个condition的index

L([10, 20, 30, 40, 50]).argwhere(lambda x: x > 25) # Output: L([2, 3, 4])

negate=True可以找到和condition相反的index

L().shuffle()会返回一个shuffle list

一些L的classmethod

L.split('a,b,c',',') # output ['a','b','c']

L.range(10)  # output [0,1,2,3,4,5,6,7,8,9]

L.splitlines('a\nb\nc\n') # output [a,b,c]