计算激活函数

Sigmoid和ReLU

使用ndarray数组可以很方便的构建数学函数,并利用其底层的矢量计算能力快速实现计算。下面以神经网络中比较常用激活函数Sigmoid和ReLU为例,介绍代码实现过程。

  • 计算Sigmoid激活函数

📃 NumPy的应用 - 图1

  • 计算ReLU激活函数

📃 NumPy的应用 - 图2

使用Numpy计算激活函数Sigmoid和ReLU的值,使用matplotlib画出图形,代码如下所示。

  1. # ReLU和Sigmoid激活函数示意图
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. import matplotlib.patches as patches
  5. %matplotlib inline
  6. #设置图片大小
  7. plt.figure(figsize=(8, 3))
  8. # x是1维数组,数组大小是从-10. 到10.的实数,每隔0.1取一个点
  9. x = np.arange(-10, 10, 0.1)
  10. # 计算 Sigmoid函数
  11. s = 1.0 / (1 + np.exp(- x))
  12. # 计算ReLU函数
  13. y = np.clip(x, a_min = 0., a_max = None)
  14. #########################################################
  15. # 以下部分为画图程序
  16. # 设置两个子图窗口,将Sigmoid的函数图像画在左边
  17. f = plt.subplot(121)
  18. # 画出函数曲线
  19. plt.plot(x, s, color='r')
  20. # 添加文字说明
  21. plt.text(-5., 0.9, r'$y=\sigma(x)$', fontsize=13)
  22. # 设置坐标轴格式
  23. currentAxis=plt.gca()
  24. currentAxis.xaxis.set_label_text('x', fontsize=15)
  25. currentAxis.yaxis.set_label_text('y', fontsize=15)
  26. # 将ReLU的函数图像画在右边
  27. f = plt.subplot(122)
  28. # 画出函数曲线
  29. plt.plot(x, y, color='g')
  30. # 添加文字说明
  31. plt.text(-3.0, 9, r'$y=ReLU(x)$', fontsize=13)
  32. # 设置坐标轴格式
  33. currentAxis=plt.gca()
  34. currentAxis.xaxis.set_label_text('x', fontsize=15)
  35. currentAxis.yaxis.set_label_text('y', fontsize=15)
  36. plt.show()

📃 NumPy的应用 - 图3

tanh

tanh 的函数定义如下:
📃 NumPy的应用 - 图4

  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. import matplotlib.patches as patches
  4. %matplotlib inline
  5. #设置图片大小
  6. plt.figure(figsize=(6, 4))
  7. # x是1维数组,数组大小是从-10. 到10.的实数,每隔0.1取一个点
  8. x = np.arange(-10, 10, 0.1)
  9. # 计算 tanh 函数
  10. s = (np.exp(x) - np.exp(-x)) / (np.exp(x) + np.exp(-x))
  11. # 画出函数曲线
  12. plt.plot(x, s, color='r')
  13. # 添加文字说明
  14. plt.text(-5., 0.9, r'$y=\tanh(x)$', fontsize=13)
  15. # 设置坐标轴格式
  16. currentAxis=plt.gca()
  17. currentAxis.xaxis.set_label_text('x', fontsize=15)
  18. currentAxis.yaxis.set_label_text('y', fontsize=15)
  19. plt.show()

📃 NumPy的应用 - 图5

使用numpy作数字图像处理

图像是由像素点构成的矩阵,其数值可以用ndarray来表示。将上述介绍的操作用在图像数据对应的ndarray上,可以很轻松的实现图片的翻转、裁剪和亮度调整,具体代码和效果如下所示。

  1. # 导入需要的包
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4. from PIL import Image
  5. # 读入图片
  6. image = Image.open('./work/logo.png')
  7. image = np.array(image)

查看数据形状,其形状是[H, W, 3],其中H代表高度, W是宽度,3代表RGB三个通道

  1. >>> image.shape
  2. (640, 640, 3)

查看原始图像:

  1. plt.imshow(image)

输出:

  1. <matplotlib.image.AxesImage at 0x1fb6072b788>

📃 NumPy的应用 - 图6

翻转

垂直方向翻转
垂直翻转,其实就是将图像的第一行跟最后一行置换,第(n-1)行跟第(2)行置换…:

  1. image2 = image[::-1, :, :]
  2. plt.imshow(image2)

📃 NumPy的应用 - 图7
水平方向翻转
跟垂直方向翻转原理一致,在第二个维度做处理即可:

  1. image3 = image[:, ::-1, :]
  2. plt.imshow(image3)

📃 NumPy的应用 - 图8

剪裁

有了刚才翻转的基础,其实剪裁很好理解,就是在某一个维度上设置起始位置和终止位置即可:

  1. # 高度方向裁剪
  2. H, W = image.shape[0], image.shape[1]
  3. # 注意此处用整除,H_start必须为整数
  4. image4 = image[H//2:H, :, :]
  5. plt.imshow(image4)

📃 NumPy的应用 - 图9

  1. # 宽度方向裁剪
  2. H, W = image.shape[0], image.shape[1]
  3. image5 = image[:, W//2:W, :]
  4. plt.imshow(image5)

📃 NumPy的应用 - 图10

  1. # 两个方向同时裁剪
  2. image5 = image[H//2:H, W//2:W, :]
  3. plt.imshow(image5)

📃 NumPy的应用 - 图11

调整亮度

将每个像素点(包含RGB三色信息)同时乘以同一个标量,即可以改变图像的亮度,注意:RGB像素值最高到255。

减少亮度**

  1. image6 = image * 0.5
  2. plt.imshow(image6.astype('uint8'))

📃 NumPy的应用 - 图12
增加亮度

  1. image7 = image * 2.0
  2. # 由于图片的RGB像素值必须在0-255之间,此处使用np.clip进行数值裁剪
  3. image7 = np.clip(image7, a_min=None, a_max=255.)
  4. plt.imshow(image7.astype('uint8'))

📃 NumPy的应用 - 图13

拉伸及压缩图片

拉伸及压缩图片有很多策略,比如以下策略:

高度方向每隔一行取像素点**

  1. image8 = image[::2, :, :]
  2. plt.imshow(image8)

📃 NumPy的应用 - 图14

宽度方向每隔一列取像素点

  1. image9 = image[:, ::2, :]
  2. plt.imshow(image9)

📃 NumPy的应用 - 图15
间隔行列采样
间隔行列采样,图像尺寸会减半,清晰度变差

  1. image10 = image[::2, ::2, :]
  2. plt.imshow(image10)

📃 NumPy的应用 - 图16

保存图像至文件

同样通过save保存即可:

  1. outputImage = Image.fromarray(image3)
  2. outputImage.save('output.jpg')