Deep Learning note1


## pyhton的方法和类

声明类
```python
class Myclass(object):
def _init_(self,name):
self.name = name
def greet(self, loud=False):
print("%s",self.name)
```
注意

1. class的括号里声明该类的父类
2. class中的方法参数`self` 是一个隐藏参数,表明传入自身作为参数,这样才能在方法体中调用本类的成员变量和方法

## python的向量和矩阵
python使用numpy中的`dot`函数进行矩阵运算
```python
a = np.random.rand(100)
b = np.random.rand(100)
c = np.dot(a,b)
d = a.reshape(50,2)
e = b.reshape(50,2)
f = np.dot(d,e)
```
当两个参数都是一维时,求的是点积
$$np.dot(a,b)=a_1b_1+a_2b_2+a_3b_3+...$$
但当两个参数不都是一维时,进行正常的矩阵运算
```python
a.shape #The shape of a is (7,3)
b.shape #The shape of b is(4,5)
np.dot(a,b) #Error
```

注意,定义向量时一定要指定维度
```python
a = np.random.randn(6)
a.shape #shape is (6 ,) ,a既不是列向量也不是行向量
b = np.random.randn(1,6)
b.shape #shape is (1,6)
```

## 广播机制(Broadcast)
矩阵在运算过程中的一种补齐机制,例:
$$\left[ \begin{matrix} 100 &101 & 102 \\ 103 & 104 & 105  \end{matrix} \right]+\left[\begin{matrix} 100 \\200 \end{matrix} \right] =$$

$$\left[ \begin{matrix} 100 &101 & 102 \\ 103 & 104 & 105  \end{matrix} \right]+\left[\begin{matrix} 100 & 100 & 100\\200 & 200 & 200 \end{matrix} \right] =$$

$$\left[ \begin{matrix} 200 & 201 & 202 \\ 303 & 304 & 305  \end{matrix} \right]$$

#学习路径##Python#
全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务