python 学习
1、cast 调整数值类型
代码
tf.cast( ) 或者K.cast( ) 是执行 tensorflow 中的张量数据类型转换,比如读入的图片是int8类型的,一定要在训练的时候把图片的数据格式转换为float32.这样就能够将其转换成 0 或 1 的序列,进行one-hot encoding。
例子:
源码:
def cast(typ, val):
· · """Cast a value to a type.
· · · This returns the value unchanged. To the type checker this
· · signals that the return value has the designated type, but at
· · runtime we intentionally don't check anything (we want this
· · to be as fast as possible).
· · """
· · return val
2、einsum 张量的内积外积计算
https://zhuanlan.zhihu.com/p/27739282
tf.matmul和tf.einsum实现矩阵相乘
3、Super
python中的super()用法以及多继承协同任务
https://www.cnblogs.com/miyauchi-renge/p/10923127.html
class A:
def x(self):
print(‘run A.x’)
super().x()
print(self)
class B:
def x(self):
print(‘run B.x’)
print(self)
class C(A,B):
def x(self):
print(‘run C.x’)
super().x()
print(self)
4、Call
call()方法的作用其实是把一个类的实例化对象变成了可调用对象,也就是说把一个类的实例化对象变成了可调用对象,只要类里实现了__call__()方法就行。
如当类里没有实现__call__()时,此时的对象p 只是个类的实例,不是一个可调用的对象,当调用它时会报错:‘Person’ object is not callable
https://blog.csdn.net/weixin_46260870/article/details/106304159
5、np.expand_dims这个东西的主要作用,就是增加一个维度
6、Raise
raise 语句的基本语法格式为:
raise [exceptionName [(reason)]]
其中,用 [] 括起来的为可选参数,其作用是指定抛出的异常名称,以及异常信息的相关描述。如果可选参数全部省略,则 raise 会把当前错误原样抛出;如果仅省略 (reason),则在抛出异常时,将不附带任何的异常描述信息。
也就是说,raise 语句有如下三种常用的用法:
raise:单独一个 raise。该语句引发当前上下文中捕获的异常(比如在 except 块中),或默认引发 RuntimeError 异常。
raise 异常类名称:raise 后带一个异常类名称,表示引发执行类型的异常。
raise 异常类名称(描述信息):在引发指定类型的异常的同时,附带异常的描述信息。