<jupyter_start><jupyter_text>1.[enumerate](1) 2.[lambda](2) 3.[pandas](3) 4.[numpy](4) 5.[markdown](5) 6.[pytorch](6) 7.[plot](7) L.[其他操作](last)<jupyter_code>import math
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import torch
from torchviz import make_dot
from colorama import Fore, Back
import seaborn as sns; sns.set(color_codes=True)<jupyter_output><empty_output><jupyter_text>1.enumerate<jupyter_code>#枚举
LL = ['a','b','c']
aa =  enumerate(LL)
print(aa, type(aa))
for i ,e in enumerate(LL):
    print(i,e)
for e in enumerate(LL,3):
    print(e)<jupyter_output>(3, 'a')
(4, 'b')
(5, 'c')<jupyter_text>2.lambda * 快速构建函数<jupyter_code>epsilon_by_frame = lambda frame_idx:math.exp(-frame_idx/100)
plt.plot([epsilon_by_frame(i) for i in range(500)])<jupyter_output><empty_output><jupyter_text>* 多返回值<jupyter_code>multi_return_value = lambda a, b :(a+1,b+2)
a, b =multi_return_value(1,1)
print(a,b)<jupyter_output>2 3<jupyter_text>* 和map连用快速定义并返回<jupyter_code>L = [1,2,3,4]
list(map(lambda x : x**3, L))<jupyter_output><empty_output><jupyter_text>* 和filter连用进行数据的挑选<jupyter_code>list(filter(lambda x: x>=2, L))<jupyter_output><empty_output><jupyter_text>3.pandas * pd.qcut根据值均匀分配将数据分为n个梯度<jupyter_code>pd.qcut(range(6),3)
pd.qcut(range(5), 3, labels=["good", "medium", "bad"])<jupyter_output><empty_output><jupyter_text>4.numpy * np.random.choice进行选择<jupyter_code># np.random.choice(a,size=None,replace=True,p=None)
# a:一维数组(表示在其中选一个)或者int数(表示在[0,i-1]中选一个)
# size:int数代表一维数组，也可返回指定大小的多维矩阵
# replace=True 表示可能会返回重复的项
# p:对a中的每个数设置权重，即被选中的概率
print(np.random.choice(range(6)))
print(np.random.choice(["a","b","c","d"]))
print(np.random.choice(["a","b","c","d"],p = [0,1,0,0]))<jupyter_output>4
a
b<jupyter_text>* 图片的numpy和torch.Tensor之间的转化要用到transpose()函数    对于numpy 的图片:H x W x C    对于$\;$torch$\;$ 的图片:C x H x W  5.markdown $${\max_{a'}}$$  6.pytorch * pytorch 使用 **(a)** torch.tensor.item()和 **(b)** torch.tensor.cpu().numpy()的异同    同 : 都是将tensor张量转化为python标量值    异 : (a)只能对单个张量操作 | (b)既可以对单个张量操作也可以对进行数组操作<jupyter_code>test = torch.tensor(5)
print("test :", test)
print(".item() :", test.item())
print(".cpu().numpy() :", test.cpu().numpy())
test = torch.tensor([1,2])
print("test :", test)
print(".cpu().numpy() :",test.cpu().numpy())
print("")
try:
    print(".item() :", test.item())
except ValueError :
    print(Fore.WHITE + Back.RED + "ValueError: only one element tensors can be converted to Python scalars")<jupyter_output>test : tensor([1, 2])
.cpu().numpy() : [1 2]

[37m[41mValueError: only one element tensors can be converted to Python scalars<jupyter_text>* torchviz画图该包有两个方法,make_dot_from_trace和make_dot。感觉我只用到了第一个其中，make_dot(model).render("picture")保存的是pdf文档make_dot(model).render("picture", format="png")保存的是png格式图片和图片信息文件<jupyter_code>lstm_cell = torch.nn.LSTMCell(128, 128)
x = torch.randn(3, 128)
make_dot(lstm_cell(x), params=dict(list(lstm_cell.named_parameters())))<jupyter_output><empty_output><jupyter_text>* 理解.detach()    1. 简单理解为该tensor的复制，且require_grad = False    2. 通常我们使用到model里输出的量是require_grad = True的，也就是说其包含原始梯度，而这时我们又需要用其计算Loss，此时便需要重置其梯度，使其不受原始梯度的影响。所以.detach()也就起到了重置梯度的作用    3. 下图的演示展示了.detach(),require_grad = False    4. detach()不能乱用，在计算loss function时，根据是不是需要求导(论文中的求导公式)来使用.detach()<jupyter_code>x = torch.ones(10, requires_grad=True)
y = x**2
z = x**3
r = (y + z).sum()
r.backward()
print("x.grad:", x.grad)
make_dot(r)
x = torch.ones(10, requires_grad=True)
y = x**2
z = x.detach()**3
r = (y + z).sum()
r.backward()
print("x.grad:", x.grad)
make_dot(r)<jupyter_output>x.grad: tensor([2., 2., 2., 2., 2., 2., 2., 2., 2., 2.])<jupyter_text>plot * 使用seaborn画出论文里带有置信区间的图<jupyter_code>num_fig = 5
start, end = -1, 1
x_list_sub = lambda x, y : np.arange(x, y, 0.1)
x_list_target = np.stack((x_list_sub(start, end) for _ in range(num_fig)), axis=1).flatten()
y_list_target = x_list_target ** 2 + np.random.randn(x_list_target.shape[0]) * 0.2
data = pd.DataFrame(data=dict(x=x_list_target, y=y_list_target))
sns.lineplot(x="x", y="y", data=data, color="g")<jupyter_output>/opt/conda/lib/python3.6/site-packages/IPython/core/interactiveshell.py:3214: FutureWarning: arrays to stack must be passed as a "sequence" type such as list or tuple. Support for non-sequence iterables such as generators is deprecated as of NumPy 1.16 and will raise an error in the future.
  if (yield from self.run_code(code, result)):<jupyter_text>其他操作 * 对axis = 0,或axis = 1 的理解    都表示跨越，0跨行，1跨列<jupyter_code>a = np.arange(9).reshape(3,3)
print(a)
print("0跨行:",a.sum(0))
print("1跨列:",a.sum(1))
print("总和:",a.sum())<jupyter_output>总和: 36<jupyter_text>* 对函数中\__len__(self)的理解    重写len方法供调用<jupyter_code>class Test():
    def __init__(self, *num):
        self.nums = num
    def __len__(self):
        return len(self.nums)

test = Test("A","B","C")
print(len(test),test.__len__())<jupyter_output>3 3<jupyter_text>* \*args 和 \*\*kwargs的异同(some code reference Geeks for Geeks)    都是可变参数的传值，两者都可当正常函数的传值处理    \*args是不定长的可变参数，而\*\*kwargs使用的是类似于元组的key to value的可变参数<jupyter_code>#对于*args
def myFun(*argv): 
    for arg in argv:
        print(arg)
        
myFun('Hello', 'Welcome', 'to', 'HHQ\'utils') 
#对于**kwargs我分为了两种情况:
#第一种(定义可变参数函数):
def myFun(**kwargs): 
    for  key, value in kwargs.items():
        print("%s == %s"%(key, value))

myFun(first ='Geeks', mid ='for', last='Geeks')
#第二种(传入可变参数的字典):
def myFun(first_list, last_list):
    print("the first_list is {}, last_list is {}".format(first_list, last_list))

sample = {"first_list":[1,2],"last_list":[3,4]}
myFun(**sample)<jupyter_output>the first_list is [1, 2], last_list is [3, 4]<jupyter_text>* 提取list中索引值为奇数或者偶数的数    some_list[start:stop:step]<jupyter_code>np.arange(8)[::2]<jupyter_output><empty_output><jupyter_text>* 关于类中方法的错误使用(DDPG 添加Noise的时候发现的)<jupyter_code>class Test:
    def __init__(self):
        self.state = np.zeros(1)
    
    def sample_right(self):
        self.state = self.state + np.random.randn(1)
        return self.state
    
    def sample_error(self):
        self.state += np.random.randn(1)
        return self.state

plt.figure(figsize=(16, 6))

plt.subplot(121)
test = Test()
right_sample = [test.sample_right() for _ in range(1000)]
error_sample = [test.sample_error() for _ in range(1000)]

plt.plot(right_sample, label="a")
plt.plot(error_sample, label="b")
plt.legend()

plt.subplot(122)
test1 = Test()
right_sample = [test1.sample_right()[0] for _ in range(1000)]
error_sample = [test1.sample_error()[0] for _ in range(1000)]

plt.plot(right_sample, label="a")
plt.plot(error_sample, label="b")
plt.legend()

plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.17987249544626593, 'fraction_numerical': 0.026411657559198543, 'mean_word_length': 6.248624862486249, 'pattern_counts': {'":': 2, '<': 60, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '17', 'dir': 'jupyter-structured-clean-dedup', 'id': '489012', 'license': 'Apache-2.0', 'n_tokens_mistral': 3142, 'n_tokens_neox': 3123, 'n_words': 464, 'path': '.ipynb_checkpoints/utils-checkpoint.ipynb', 'repo_name': 'sunnyswag/RL_notes_and_codes', 'size': '6588'}