自定义感知机

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# 00_percetron.py
# 实现感知机

# 实现逻辑和
def AND(x1, x2):
w1, w2, theta = 0.5, 0.5, 0.7
tmp = x1 * w1 + x2 * w2
if tmp <= theta:
return 0
else:
return 1

print(AND(1, 1))
print(AND(1, 0))


# 实现逻辑或
def OR(x1, x2):
w1, w2, theta = 0.5, 0.5, 0.2
tmp = x1 * w1 + x2 * w2
if tmp <= theta:
return 0
else:
return 1

print(OR(0, 1))
print(OR(0, 0))

# 实现异或
def XOR(x1, x2):
s1 = not AND(x1, x2) # 与非门
s2 = OR(x1, x2)
y = AND(s1, s2)
return y

print(XOR(1, 0))
print(XOR(0, 1))
print(XOR(1, 1))
print(XOR(0, 0))

计算N个概率的交叉熵

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# 计算交叉熵
import math

p_true = [0, 1, 0, 0, 0] # 真实概率
p_pred1 = [0.1, 0.6, 0.1, 0.1, 0.1] # 预测概率
p_pred2 = [0.1, 0.7, 0.1, 0.05, 0.05] # 预测概率
p_pred3 = [0.1, 0.8, 0.04, 0.03, 0.03] # 预测概率

print(sum(p_true))
print(sum(p_pred1))
print(sum(p_pred2))
print(sum(p_pred3))

cross_entropy1 = 0.0
cross_entropy2 = 0.0
cross_entropy3 = 0.0

# 计算每组预测概率和真实概率的交叉熵(实际上只计算对应正确解标签的输出的自然对数)
for i in range(len(p_true)):
cross_entropy1 += (p_true[i] * math.log(p_pred1[i]))
cross_entropy2 += (p_true[i] * math.log(p_pred2[i]))
cross_entropy3 += (p_true[i] * math.log(p_pred3[i]))

# 打印结果
print("交叉熵1:", -cross_entropy1)
print("交叉熵2:", -cross_entropy2)
print("交叉熵3:", -cross_entropy3)

验证图像卷积运算效果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
from scipy import signal
from scipy import misc
import matplotlib.pyplot as plt
import numpy as np
import scipy.ndimage as sn

im = misc.imread("data/zebra.png", flatten=True)
# face = sn.imread("data/zebra.png", flatten=True)
flt = np.array([[-1, 0, 1],
[-2, 0, 2],
[-1, 0, 1]])

flt2 = np.array([[1, 2, 1],
[0, 0, 0],
[-1, -2, -1]])

# 把图像的face数组和设计好的卷积和作二维卷积运算,设计边界处理方式为symm
conv_img1 = signal.convolve2d(im, flt,
boundary='symm',
mode='same').astype("int32")

conv_img2 = signal.convolve2d(im, flt2,
boundary='symm',
mode='same').astype("int32")

plt.figure("Conv2D")
plt.subplot(131)
plt.imshow(im, cmap='gray') # 显示原始的图
plt.xticks([])
plt.yticks([])

plt.subplot(132)
plt.xticks([])
plt.yticks([])
plt.imshow(conv_img1, cmap='gray') # 卷积后的图

plt.subplot(133)
plt.xticks([])
plt.yticks([])
plt.imshow(conv_img2, cmap='gray') # 卷积后的图

plt.show()

执行结果:

img_conv

OpenCV版:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from scipy import misc
from scipy import signal
import matplotlib.pyplot as plt
import matplotlib
import numpy as np
import scipy.ndimage as sn
import cv2
import pylab

im = cv2.imread("../data/zebra.png",0)

flt = np.array([[-1,0,1],
[-2,0,2],
[-1,0,1]])/2
flt2 = np.array([[1,2,1],
[0,0,0],
[-1,-2,-1]])/2

conv_img1 = cv2.filter2D(im, -1, flt,borderType=1)
conv_img2 = cv2.filter2D(im, -1, flt2,borderType=1)

cv2.imshow("im",im)
cv2.imshow("conv_img1",conv_img1)
cv2.imshow("conv_img2",conv_img2)
cv2.waitKey()
cv2.destroyAllWindows()