Skip to content

Commit 846a6aa

Browse files
committed
第二章的代码
1 parent 5060ee3 commit 846a6aa

5 files changed

+291
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,4 @@ ENV/
9999

100100
# mypy
101101
.mypy_cache/
102+
.idea*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
from keras.datasets import mnist
2+
from keras import models
3+
from keras import layers
4+
from keras.utils import to_categorical
5+
6+
# 读取Keras自带的mnist数据集
7+
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
8+
9+
# 查看训练数据
10+
print(train_images.shape)
11+
print(len(train_labels))
12+
print(train_labels)
13+
14+
# 查看测试数据
15+
print(test_images.shape)
16+
print(len(test_labels))
17+
print(test_labels)
18+
19+
# 网络架构
20+
network = models.Sequential()
21+
network.add(layers.Dense(512, activation='relu', input_shape=(28 * 28,)))
22+
network.add(layers.Dense(10, activation='softmax'))
23+
24+
# 编译
25+
network.compile(optimizer='rmsprop',
26+
loss='categorical_crossentropy',
27+
metrics=['accuracy'])
28+
29+
# 预处理图像数据
30+
train_images = train_images.reshape((60000, 28 * 28))
31+
train_images = train_images.astype('float32') / 255
32+
33+
test_images = test_images.reshape((10000, 28 * 28))
34+
test_images = test_images.astype('float32') / 255
35+
36+
# 预处理图像标签
37+
train_labels = to_categorical(train_labels)
38+
test_labels = to_categorical(test_labels)
39+
40+
# 训练网络
41+
network.fit(train_images, train_labels, epochs=5, batch_size=128)
42+
43+
# 评价网络
44+
test_loss, test_acc = network.evaluate(test_images, test_labels)
45+
print('test_acc:', test_acc)

chapter2/tensor-operation.py

+136
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
import numpy as np
2+
3+
4+
# 基于元素的relu原生实现
5+
def naive_relu(x):
6+
# x是一个二维张量
7+
assert len(x.shape) == 2
8+
9+
x = x.copy() # 避免覆盖输入的张量
10+
for i in range(x.shape[0]):
11+
for j in range(x.shape[1]):
12+
x[i, j] = max(x[i, j], 0)
13+
return x
14+
15+
16+
# 基于元素的add原生实现
17+
def naive_add(x, y):
18+
# x和y是二维张量
19+
assert len(x.shape) == 2
20+
assert x.shape == y.shape
21+
22+
x = x.copy() # 避免覆盖输入的张量
23+
for i in range(x.shape[0]):
24+
for j in range(x.shape[1]):
25+
x[i, j] += y[i, j]
26+
return x
27+
28+
29+
# 只能广播最后一个维度、最后两个维度。。。
30+
x = np.array([[[5, 78, 2, 34, 0],
31+
[6, 79, 3, 35, 1],
32+
[7, 80, 4, 36, 2]],
33+
[[5, 78, 2, 34, 0],
34+
[6, 79, 3, 35, 1],
35+
[7, 80, 4, 36, 2]],
36+
[[5, 78, 2, 34, 0],
37+
[6, 79, 3, 35, 1],
38+
[7, 80, 4, 36, 2]],
39+
[[5, 78, 2, 34, 0],
40+
[6, 79, 3, 35, 1],
41+
[7, 80, 4, 36, 2]]])
42+
# y = np.array([1, 2, 3]) # 维度不匹配
43+
# y = np.array([1, 2, 3, 4]) # 维度不匹配
44+
# y = np.array([1, 2, 3, 4, 5])
45+
# y = np.array([[1, 2, 3, 4, 5],
46+
# [2, 3, 4, 5, 6],
47+
# [1, 2, 3, 4, 5],
48+
# [1, 2, 3, 4, 5]]) # 维度不匹配
49+
y = np.array([[1, 2, 3, 4, 5],
50+
[2, 3, 4, 5, 6],
51+
[1, 2, 3, 4, 5]])
52+
z = x + y
53+
print(z)
54+
55+
56+
# 矩阵加向量的原生实现
57+
def naive_add_matrix_and_vector(x, y):
58+
# x是一个二维张量
59+
# y是一个向量
60+
assert len(x.shape) == 2
61+
assert len(y.shape) == 1
62+
assert x.shape[1] == y.shape[0]
63+
64+
x = x.copy() # 避免覆盖输入的张量
65+
for i in range(x.shape[0]):
66+
for j in range(x.shape[1]):
67+
x[i, j] += y[j]
68+
return x
69+
70+
71+
# x是一个随机张量,大小为(64, 3, 32, 10)
72+
x = np.random.random((64, 3, 32, 10))
73+
# y是一个随机张量,大小为(32, 10)
74+
y = np.random.random((32, 10))
75+
76+
# z的大小为(64, 3, 32, 10)
77+
z = np.maximum(x, y)
78+
print(z.shape)
79+
80+
81+
# 向量dot的原生实现
82+
def naive_vector_dot(x, y):
83+
# x和y是向量
84+
assert len(x.shape) == 1
85+
assert len(y.shape) == 1
86+
assert x.shape[0] == y.shape[0]
87+
88+
z = 0
89+
for i in range(x.shape[0]):
90+
z += x[i] * y[i]
91+
return z
92+
93+
94+
# 矩阵-向量dot的原生实现
95+
def naive_matrix_vector_dot(x, y):
96+
# x是矩阵
97+
# y是向量
98+
assert len(x.shape) == 2
99+
assert len(y.shape) == 1
100+
assert x.shape[1] == y.shape[0]
101+
102+
z = np.zeros(x.shape[0])
103+
for i in range(x.shape[0]):
104+
# for j in range(x.shape[1]):
105+
# z[i] += x[i][j] * y[j]
106+
z[i] = naive_vector_dot(x[i, :], y)
107+
return z
108+
109+
110+
x = np.array([[1, 2, 3],
111+
[1, 2, 3]])
112+
y = np.array([1, 2, 3])
113+
z = naive_matrix_vector_dot(x, y)
114+
print(z)
115+
116+
117+
# 矩阵dot的原生实现
118+
def naive_matrix_dot(x, y):
119+
# x和y是矩阵
120+
assert len(x.shape) == 2
121+
assert len(y.shape) == 2
122+
assert x.shape[1] == y.shape[0]
123+
124+
z = np.zeros((x.shape[0], y.shape[1]))
125+
for i in range(y.shape[1]):
126+
z[:, i] = naive_matrix_vector_dot(x, y[:, i])
127+
return z
128+
129+
130+
x = np.array([[1, 2, 3],
131+
[1, 2, 3]])
132+
y = np.array([[1, 2, 3, 4],
133+
[1, 2, 3, 4],
134+
[1, 2, 3, 4]])
135+
z = naive_matrix_dot(x, y)
136+
print(z)

chapter2/tensor-reshaping.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from keras.datasets import mnist
2+
import numpy as np
3+
4+
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
5+
6+
# 改形
7+
train_images = train_images.reshape((60000, 28 * 28))
8+
9+
x = np.array([[0., 1.],
10+
[2., 3.],
11+
[4., 5.]])
12+
print(x.shape)
13+
x = x.reshape((6, 1))
14+
print(x)
15+
x = x.reshape((2, 3))
16+
print(x)
17+
18+
# 矩阵转置
19+
x = np.zeros((300, 20))
20+
print(x.shape)
21+
x = np.transpose(x)
22+
print(x.shape)
23+
24+
'''
25+
past_velocity = 0
26+
momentum = 0.1 # 一个常量因子
27+
while loss > 0.01: # 优化循环
28+
w, loss, gradient = get_current_parameters()
29+
velocity = past_velocity * momentum - learning_rate * gradient # 书上写的是加号,我认为是减号,看了keras的源码发现确实是减号
30+
w = w + momentum * velocity - learning_rate * gradient
31+
past_velocity = velocity
32+
update_parameter(w)
33+
'''

chapter2/tensor.py

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
from keras.datasets import mnist
2+
import numpy as np
3+
import matplotlib.pyplot as plt
4+
5+
# 标量
6+
x = np.array(12)
7+
print(x)
8+
print(x.shape)
9+
print(x.ndim)
10+
11+
# 向量
12+
x = np.array([12, 3, 6, 14])
13+
print(x)
14+
print(x.shape)
15+
print(x.ndim)
16+
17+
# 矩阵
18+
x = np.array([[5, 78, 2, 34, 0],
19+
[6, 79, 3, 35, 1],
20+
[7, 80, 4, 36, 2]])
21+
print(x.shape)
22+
print(x.ndim)
23+
24+
# 3维张量及高维张量
25+
x = np.array([[[5, 78, 2, 34, 0],
26+
[6, 79, 3, 35, 1],
27+
[7, 80, 4, 36, 2]],
28+
[[5, 78, 2, 34, 0],
29+
[6, 79, 3, 35, 1],
30+
[7, 80, 4, 36, 2]],
31+
[[5, 78, 2, 34, 0],
32+
[6, 79, 3, 35, 1],
33+
[7, 80, 4, 36, 2]],
34+
[[5, 78, 2, 34, 0],
35+
[6, 79, 3, 35, 1],
36+
[7, 80, 4, 36, 2]]])
37+
print(x.shape)
38+
print(x.ndim)
39+
40+
# 查看MNIST数据集的维度和尺寸
41+
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
42+
print(train_images.ndim)
43+
print(train_images.shape)
44+
print(train_images.dtype)
45+
46+
# 画出训练集中的一张图片
47+
digit = train_images[4]
48+
49+
plt.imshow(digit, cmap=plt.cm.binary)
50+
plt.show()
51+
52+
# tensor切片
53+
my_slice = train_images[10:100]
54+
print(my_slice.shape)
55+
56+
# 等价切片1
57+
my_slice = train_images[10:100, :, :]
58+
print(my_slice.shape)
59+
60+
# 等价切片2
61+
my_slice = train_images[10:100, 0:28, 0:28]
62+
print(my_slice.shape)
63+
64+
# 切右下角的14*14像素
65+
my_slice = train_images[:, 14:, 14:]
66+
67+
# 切正中间的14*14像素
68+
my_slice = train_images[:, 7:-7, 7:-7]
69+
70+
# 批量数据
71+
batch = train_images[:128]
72+
# 下一批数据
73+
batch = train_images[128:256]
74+
# 第n个批量
75+
n = 2
76+
batch = train_images[128 * n:128 * (n + 1)]

0 commit comments

Comments
 (0)