cs231n

cs231n

it's never too late

Python Keywords

assert

in dictionary

verb

  1. to say that something is certainly true断言
  2. to do something to show that you have power主张
    • assert your authority/right

in python

definition

The assert keyword is used when debugging code.
用于调试代码。
The assert keyword lets you test if a condition in your code returns True, if not, the program will raise an AssertionError.
如果assert后的条件判断为真,则无事发生继续运行;为假则报错AssertionError。

usage

>>> x = 9527
>>> assert x == 9527
>>> assert x == 1551
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError

yield

in dictionary

verb

  1. produce产生
    • The process yields oil for industrial use.
    • Early radio equipment yielded poor sound quality.
    • The experiments yielded some surprising results.
  2. give up放弃
    • Despite renewed pressure to give up the occupied territory, they will not yield.
  3. bend/break弯折
    • His legs began to yield under the sheer weight of his body.
  4. stop让路
    • If you’re going downhill, you need to yield to bikers going uphill.
      • Phrasal verb: yield to something

in python

definition

The yield keyword is used to return a list of values from a function.
用来返回函数值。
Unlike the return keyword which stops further execution of the function, the yield keyword continues to the end of the function.
跟return相比,yield返回值后,函数会继续运行。
When you call a function with yield keyword(s), the return value will be a list of values, one for each yield.
无论yield几次(哪怕只有一次),都会以generator的形式返回。

usage

>>> def test1():
... yield 9527
...
>>> print(type(test1()))
<class 'generator'>

可以用for遍历,或以迭代器的方式遍历

>>> def test2():
... yield 1
... yield 2
... yield 3
...
>>> x = test2() #a generator object
>>> print(next(x))
1
>>> print(next(x))
2
>>> print(next(x))
3
>>> for i in test2():
... print(i)
...
1
2
3

generator? what?

iterator first

https://docs.python.org/3/glossary.html#term-iterator
iterator is an object representing a stream of data.
迭代器是个数据流对象。
Repeated calls to the iterator’s __next__() method (or passing it to the built-in function next()) return successive items in the stream. When no more data are available a StopIteration exception is raised instead.
调用迭代器的next方法或用python内置函数next(迭代器)可以得到这个流的后继数据,没数据了就抛出StopIteration异常。
根据这个定义可以自造迭代器类。

>>> class MyNumbers:
... def __iter__(self):
... self.a = 1
... return self
... def __next__(self):
... x = self.a
... self.a += 1
... return x
...
>>> myclass = MyNumbers()
>>> myiter = iter(myclass)
>>> print(next(myiter))
1
>>> print(next(myiter))
2
>>> print(next(myiter))
3

这里的iter()也是个内置函数,iter(object, sentinel)。
Without a second argument, object must be a collection object which supports the iterable protocol (the __iter__() method), or it must support the sequence protocol (the __getitem__() method with integer arguments starting at 0).
如果没有sentinel哨兵参数,object就必须实现iter或getitem方法。

now generator

https://docs.python.org/3/glossary.html#term-generator
A function which returns a generator iterator. It looks like a normal function except that it contains yield expressions for producing a series of values usable in a for-loop or that can be retrieved one at a time with the next() function.
“generator function”是个包含yield的函数,返回结果是个可以被for循环或python内置函数next()遍历的iterator。

Usually refers to a generator function, but may refer to a generator iterator in some contexts. In cases where the intended meaning isn’t clear, using the full terms avoids ambiguity.
“generator”可能指代函数或迭代器,所以最好说全“生成器函数”或“生成器迭代器”而不只是说“生成器”。

Generator iterator is an object created by a generator function.
Each yield temporarily suspends processing, remembering the location execution state (including local variables and pending try-statements). When the generator iterator resumes, it picks up where it left off (in contrast to functions which start fresh on every invocation).
每个yield都会暂时中止处理,记住位置执行状态(包括局部变量和待处理的try语句)。当生成器迭代器恢复时,它会从上次中断的地方继续执行(与每次调用时都从头开始的函数不同)。

即,调用包含yield的函数会返回一个iterator,每次调用next(iterator)的时候函数就会运行到下一个yield处,python会记录这个yield的位置状态,下次next(iterator)从这里开始运行函数

>>> def test():
... yield
...
>>> print(type(test()))
<class 'generator'>
>>> print(type(test))
<class 'function'>
>>> def test():
... return
...
>>> print(type(test()))
<class 'NoneType'>
>>>

根据上述官方文档,这里的<class 'generator'>实际上是个”generator iterator”。这里的<class 'function'>实际上是个”generator function”。

generator expression

以前常用的下面这种写法也是个generator,()括号里面写一个循环(必须有括号)。

>>> x = (i*i for i in range(10))
>>> print(type(x))
<class 'generator'>
>>> for i in x:
... print(i)
...
0
1
4
9
16
25
36
49
64
81

实际上相当于下面这种写法。
>>> def genEx(num):
... i = 0
... while i < num:
... yield i * i
... i = i + 1
...
>>> x = genEx(10)
>>> for i in x:
... print(i)

在上面这种只需要print结果的情况里,其实yield可以直接替换成print。
但很多时候不是需要打印而是需要取用这个值,如果把(i * i)存入其他容器也可实现相同功能。yield为这种情况提供了方便。

yield写斐波那契数列

The Fibonacci sequence is the series of numbers where each number is the sum of the two preceding numbers. For example, 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, …

>>> def fib(limit): # a generator function
... a, b = 0, 1
... while (b < limit):
... yield b
... a, b = b, a + b
...
>>> x = fib(200) # a generator iterator
>>> for i in x:
... print(i)
...
1
1
2
3
5
8
13
21
34
55
89
144
>>>

Notes

Assignments

Colab

# This mounts your Google Drive to the Colab VM.
from google.colab import drive
drive.mount('/content/drive')

2022在商汤时跟着卢博用熟了jupyter发现非常方便,只是有的操作会很卡。虽然一年半过去,如今试图回忆往昔只剩脑袋空空,不过再打开colab操作起来还是很舒适自然的。本科时觉得colab跟我全权掌控的小云服务器相比有各种不便,读研实习尝试过vivo网易各个公司的infra后发现各家都是大同小异,就像去哪都要适应新的开发环境一样,colab只是把适应对象从公司的gpu clusters换成google cloud而已。
再看当时跑的cogview和OFA,其实都只是无脑地跑inference。我一直说深度学习是经验科学无迹可寻,只是因为2020、2021年这些assignments里的代码基础我没玩熟练。普通的computer science对我来说其实就像machine learning一样,计算机的底层系统是怎样运行的我也不甚清楚,一句print(“Hello, World”)背后怎么变成字节码?字节码怎么由cpython的c语言逐条执行/解释/转换成汇编指令?汇编指令又怎么变成机器码?机器码又怎么被cpu处理?
这其实跟理解反向传播的计算图一样,没必要每写一个def function():都想清楚cpython在干嘛,没必要每写一个net都想透梯度怎么七十二变,但做哪个任务比如image classification那他的pipeline一定得玩熟,网络的每一个模块是什么作用一定得玩熟。当然可以借助工具,今天我记不起“assert”、“yield”的作用了,记不起Softmax、Batch Normalization的作用了,当然可以查资料,但这都建立在能很快搭一个积木,供尝试Softmax、Batch Normalization的基础上,如果一个最简单任务的pipeline我都没法熟练随手搭建,何谈尝试陌生模块?就好像今天记不起yield的作用,我就必须会写def function():、知道怎么写type()、for-loop才能测试。

Regular Expression

写博客写代码时经常会用regex正则批量修改文本,所以在这里记录一些常用的以免重复造轮子。

加粗标题

以此为例,第一行为被替换原文本,第二行为替换后文本。
^(#+) ((?!\*)(.*))
$1 **$2**

  1. 先用(pattern)匹配markdown标题井号;
  2. 然后是个negative lookhead(?!)匹配井号后没有星号的文本,这里用转义符escape character反斜杠。

图片缩放

markdown图片语法不方便改大小位置,改成html语法的img比较方便操作,写markdown的时候每个都手敲img又太费劲不如![]()
!\[(.*)\]\((.*)\)
<img src="$2" alt="$1" width="50%" height="50%">
or
<div align=center><img src="$2" alt="$1" width="50%" height="50%"></div>

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×