从零开始的机器学习之路(十五)---- Practice Lab 3 Neural Networks for Handwritten Digit Recognition, Binary
【代码】从零开始的机器学习之路(十五)---- Practice Lab 3 Neural Networks for Handwritten Digit Recognition, Binary。
·
文章目录
前引
今天还有三篇Lab 没有写 但这个Practice Lab确实很有必要 推荐写一下~
加上这篇Lab
从零开始的机器学习之路(十五)---- Practice Lab 3 Neural Networks for Handwritten Digit Recognition, Binary
1、Neural Network Recognize Digits 0/1 Start
这里先介绍一下 这里要做的对于20 * 20的像素图 去预测 当前图像为 0 或者 1
下面为实际的图像 上面的数字为实际的数字
Thats Target
2、Model representation
实际的模型 3层layer 第一层接受400个参数 25个输出第二层 接受25个参数 15个输出 第三层 接受15个输入 1个输出

3、Practice 1(Sequential model)
# UNQ_C1
# GRADED CELL: Sequential model
model = Sequential(
[
### START CODE HERE ###
tf.keras.Input(shape=(400,)),
Dense(25, activation='sigmoid', name = 'layer1'),
Dense(15, activation='sigmoid', name = 'layer2'),
Dense(1, activation='sigmoid', name = 'layer3'),
### END CODE HERE ###
], name = "my_model"
)
model.build(input_shape=(None, 400)) # None 表示任意 batch_size
model.summary()
# assert model.input.shape.as_list() == [None, 400] # 应通过
这里不知道test为什么不行 可以忽略

1、实际展示数据



4、Practice 2(my_dense)
下面也很简单 实际实现一下my_dense
# UNQ_C2
# GRADED FUNCTION: my_dense
def my_dense(a_in, W, b, g):
"""
Computes dense layer
Args:
a_in (ndarray (n, )) : Data, 1 example
W (ndarray (n,j)) : Weight matrix, n features per unit, j units
b (ndarray (j, )) : bias vector, j units
g activation function (e.g. sigmoid, relu..)
Returns
a_out (ndarray (j,)) : j units
"""
units = W.shape[1]
a_out = np.zeros(units)
### START CODE HERE ###
for i in range(units):
fw_b = np.dot(a_in , W[:,i]) + b[i]
z = g(fw_b)
a_out[i] = z
### END CODE HERE ###
return a_out


1、实际展示(numpy)
下面一共三位数字 第一个是实际数字的值 第二个则是上面用的tf 第三个是用的numpy 实际等效的实现


5、Practice 3(my_dense_v)
下面就是简单的讲 我们这样实现会更快一些 实际用了矩阵乘法
# UNQ_C3
# GRADED FUNCTION: my_dense_v
def my_dense_v(A_in, W, b, g):
"""
Computes dense layer
Args:
A_in (ndarray (m,n)) : Data, m examples, n features each
W (ndarray (n,j)) : Weight matrix, n features per unit, j units
b (ndarray (1,j)) : bias vector, j units
g activation function (e.g. sigmoid, relu..)
Returns
A_out (ndarray (m,j)) : m examples, j units
"""
### START CODE HERE ###
A_out = g(np.matmul(A_in, W) + b)
### END CODE HERE ###
return(A_out)
下面的数据则是 用上面的代码跑的 效果是一样的

更多推荐
所有评论(0)