Noted that this self-learning notes is based on the tutorial from ML Study Jam 2020.
1 Tensorflow基础
Tensorflow 安装与环境配置
1.1 自动求导机制
tf.reduce_sum( )
代表对输入张量的所有元素求和,输出一个形状为空的纯量张量(可以通过axis参数来指定求和的纬度,不指定则默认对所有元素求和)。
‘x’ is [[1, 1, 1], [1, 1, 1]]
运算 | code | result |
---|---|---|
求和 | tf.reduce_sum(x) | 6 |
按列求和 | tf.reduce_sum(x,0) | [2, 2, 2] |
按行求和 | tf.reduce_sum(x, 1) | [3, 3] |
按照行的维度求和 | tf.reduce_sum(x, 1, keep_dims=True) | [[3], [3]] |
行列求和 | tf.reduce_sum(x, [0, 1]) | 6 |
1.2 基础示例:线性回归
np.dot( )
numpy.dot(a,b)
如果a和b都是一维数组,那么它返回的就是向量的内积。
import numpy as np
c = np.arange(0,9)
d = c[::-1]
np.dot(c,d)
Out[35]: 84
如果a和b都是二维数组,那么它返回的是矩阵乘法。
import numpy as np
a = [[1, 0], [0, 1]]
b = [[4, 1], [2, 2]]
np.dot(a, b)
Out[1]:
array([[4, 1],
[2, 2]])
需要注意的是,np.dot(a,b) 和 np.dot(b,a) 得到的结果是不一定相同的。