1、包和装饰器
abc.abstractmethod
@property
装饰器来创建只读属性,@property装饰器会将方法转换为相同名称的只读
转载
typing
设置变量类型,例如:str,int,float,dict,list,tuple,也可以自定义类型NewTyoe
转载
gin
pip install gin-config 它允许函数或类被注释为@gin.configurable,这使得能够使用清晰而强大的语法通过简单的配置文件来设置它们的参数。这种方法减少了配置维护,同时使实验配置透明且易于重复。转载
unicodedata
Unicode 的统一编码,又称「万国码」,收录了世界上各个国家大部分的文字,并且仍然在不断增修,今年六月份发布了第十一个正式版本。目前使用最广泛的是 Unicode 实现是 UTF-8 编码。转载
adb
01.cd c:\adb 02.adb shell 03.cd data/system 04.ls 可以看到有一个password.key的文件,这个就是密码的文件,我们就是把这个给删除就可以了! 最后输入命令: rm password.key 4.输入reboot或手动重启手机生效
FlashText
可用于匹配或替换句子中的关键词。
collection.namedtuple()
collections模块的namedtuple子类不仅可以使用item的index访问item,还可以通过item的name进行访问。可以将namedtuple理解为c中的struct结构,其首先将各个item命名,然后对每个item赋予数据 转载
coordinate = namedtuple('Coordinate', ['x', 'y'])
co = coordinate(10,20)
print co.x,co.y
print co[0],co[1]
co = coordinate._make([100,200])
print co.x,co.y
co = co._replace(x = 30)
print co.x,co.y
2、numpy
numpy.clip
np.clip(a, a_min, a_max, out=None)
clip这个函数将将数组中的元素限制在a_min, a_max之间
numpy.ndenumerate(arr)
一个多维索引迭代器,可以产生一对数组坐标和值。
a = np.array([[1, 2], [3, 4]])
for index, x in np.ndenumerate(a):
... print(index, x)
(0, 0) 1
(0, 1) 2
(1, 0) 3
(1, 1) 4
numpy.testing.assert_allclose()
numpy.testing.assert_allclose(actual, desired, rtol=1e-07, atol=0, equal_nan=True, err_msg=‘’, verbose=True) 如果两个对象不等于期望的公差,则引发AssertionError
转载
3、pandas
- [ ]
4、tf2.0
tf.tile()
平铺之意,用于在同一维度上的复制
with tf.Graph().as_default():
a = tf.constant([[1,2],[3,4]],name='a')
b = tf.tile(a,[2,3])
sess = tf.Session()
print(sess.run(b))
输出:
[[1 2 1 2 1 2]
[3 4 3 4 3 4]
[1 2 1 2 1 2]
[3 4 3 4 3 4]]
tf.pad()
t = tf.constant([[1, 2, 3], [4, 5, 6]])
paddings = tf.constant([[1, 1,], [2, 2]])
# 'constant_values' is 0.
# rank of 't' is 2.
tf.pad(t, paddings, "CONSTANT") # [[0, 0, 0, 0, 0, 0, 0],
# [0, 0, 1, 2, 3, 0, 0],
# [0, 0, 4, 5, 6, 0, 0],
# [0, 0, 0, 0, 0, 0, 0]]
tf.pad(t, paddings, "REFLECT") # [[6, 5, 4, 5, 6, 5, 4],
# [3, 2, 1, 2, 3, 2, 1],
# [6, 5, 4, 5, 6, 5, 4],
# [3, 2, 1, 2, 3, 2, 1]]
tf.pad(t, paddings, "SYMMETRIC") # [[2, 1, 1, 2, 3, 3, 2],
# [2, 1, 1, 2, 3, 3, 2],
# [5, 4, 4, 5, 6, 6, 5],
# [5, 4, 4, 5, 6, 6, 5]]
CONSTANT:第一个[1,1]表示在 t 的第一行上填充一组0,在最后一行,填充一组0;第二个[2,2] 表示在一列前填充2组0,但一列后填充两组0.所以 list 内列数必须是2,但是 list 组数则是根据t的维度来。
REFLECT 和 SYMMETRIC 这两种模式用的不多,但是以 t 为中心,shape 还是一致的,只是填充的数字有区别
tf.clip_by_norm(), tf.clip_by_global_norm
这里的clip_by_norm是指对梯度进行裁剪,通过控制梯度的最大范式,防止梯度爆炸的问题,是一种比较常用的梯度规约的方式
tf.clip_by_global_norm(t_list, clip_norm, use_norm=None, name=None) ,通过权重梯度的总和的比率来截取多个张量的值。t_list 是梯度张量, clip_norm 是截取的比率, 这个函数返回截取过的梯度张量和一个所有张量的全局范数
t_list[i] * clip_norm / max(global_norm, clip_norm),其中global_norm = sqrt(sum([l2norm(t)**2 for t in t_list])) global_norm 是所有梯度的平方和,如果 clip_norm > global_norm ,就不进行截取。但是这个函数的速度比clip_by_norm() 要慢,因为在截取之前所有的参数都要准备好
Tensorflow & Keras的loss函数总结
tf.feature_column.indicator_column()
indicator_column可用于包装任何categorical_column_*产生的向量,然后作为NN模型的input_layer的输入,使用方式:tf.feature_column.indicator_column(categorical_column)
转载
tfds.features.text.SubwordTextEncoder.build_from_corpus()
Tensorflow官网解释该函数的功能就是把我们的文本做成类似字典的结构,既每个字都有对应的唯一数字
#Build
encoder = tfds.features.text.SubwordTextEncoder.build_from_corpus(
corpus_generator, target_vocab_size=2**15)
encoder.save_to_file(vocab_filename)
#Load
encoder = tfds.features.text.SubwordTextEncoder.load_from_file(vocab_filename)
ids = encoder.encode("hello world")
text = encoder.decode([1, 2, 3, 4])
逻辑运算
- tf.logical_and(a, b): 与
- tf.logical_or(a, b): 或
- tf.logical_not(a): 非
- tf.logical_xor(a, b): 异或
tf.py_func()
包装一个普通的 Python 函数,这个函数接受 numpy 的数组作为输入和输出,让这个函数可以作为 TensorFlow 计算图上的计算节点 OP 来使用
转载
'''
py_func(
func,
inp,
Tout,
stateful=True,
name=None
)
'''
def my_func(x):
return np.sinh(x)
inp = tf.placeholder(tf.float32)
y = tf.py_func(my_func, [inp], tf.float32)
tensorflow 中dataset.padded_batch()
padded_batch(batch_size, padded_shapes=None, padding_values=None, drop_remainder=False )
tf.linalg.band_part()
主要功能是以对角线为中心,取它的副对角线部分,其他部分用0填充., 转载
5、bazel
6、conda虚拟环境
启动终端自动进入conda环境
vim ~/.zshrc
source ~/.bash_profile
创建虚拟环境
- 默认位置创建 conda create -n your_env_name python=x.x 指定位置创建
- conda create --prefix 目录/tf_gpu_env python=版本
- –channel conda-forge添加这个强制python版本
激活虚拟环境
- conda activate tf_gpu_env
退出虚拟环境
- conda deactivate
复制虚拟环境
直接复制文件也可以,或者:
转载
导出项目下的包
- pip install pipreqs
- pipreqs ./ --encoding=utf8
导出虚拟环境的所有包
- pip freeze > requirements.txt
安装requirements.txt
- pip install -r requirements.txt
删除虚拟环境
- your_env_name(虚拟环境名称) --all
- conda remove --name $your_env_name $package_name(包名)
7、git
基本入门
git init
git add .
git commit -m "first"
git remote add origin git@git.jd.com:wangjie648/****.git
git pull origin dev(远程分枝名字)
git push origin master:dev(远程分枝名字)。 -f是强制提交,覆盖远程的文件
进阶
1、git clone -b 分支名字 https://:@gitserver
如果密码有特殊字符,需要进行编码,参考如下:转载
2、回退commit,也会删除当前工作区,soft不删除
- git reset --hard HEAD^
- git rest --soft HEAD^
3、查看历史操作,包括删除操作
- git reflog
- git log
4、git pull拉去代码有merge冲突
git stash,存储本地工作去内容到git栈
git pull
git stash pop,取出来自动合并,解决冲突文件
5、git checkout -b dev,创建本地分枝
git push origin 本地分支名:远程分支名
修改代码后可以直接提交远程,会自动创建远程分支
6、git push origin --delete wjdev,删除远程分枝
7、git branch -d wjdev,删除本地分支
8、git branch -m old new,重新命名本地分枝
8、hive
hive -f file文件名
9、jupyter
!jupyter nbconvert --to (python/md/html/pdf) utils.ipynb
ctrl+o:滚动输出单元格
conda环境出错:转载