一、如何使用Numpy数组实现一个权重既定的三层神经网络的前向传播?
import numpy as np
def identity_function(x):
return x
def init_network():
network ={}
network['W1'] = np.array([[0.1, 0.3, 0.5], [0.2, 0.4, 0.6]]) # 2*3
network['b1'] = np.array([0.1, 0.2, 0.3]) # 1*3
network['W2'] = np.array([[0.1, 0.4], [0.2, 0.5], [0.3, 0.6]]) # 3*2
network['b2'] = np.array([0.1, 0.2]) # 1*2
network['W3'] = np.array([[0.1, 0.3], [0.2, 0.4]]) # 2*2
network['b3'] = np.array([0.1, 0.2]) # 1*2
return network
def forward(network, x): # x(1*2)
W1, W2, W3 = network['W1'], network['W2'], network['W3']
b1, b2, b3 = network['b1'], network['b2'], network['b3']
a1 = np.dot(x, W1) + b1 # 1*3
z1 = sigmoid(a1)
a2 = np.dot(z1, W2) + b2 # 1*2
z2 = sigmoid(a2)
a3 = np.dot(z2, W3) + b3 # 1*2
y = identity_function(a3)
return y
def sigmoid(x):
return 1/(1+np.exp(-x))
def main():
network = init_network()
x = np.array([1.0, 0.5])
y = forward(network, x)
print(y)
main()
代码运行结果与书中一致:
二、为什么分类任务的输出层常用Softmax函数?
softmax函数可以给出某一个输入属于某一类的概率,方便分类。
三、MNIST数据集包含什么样的数据?
训练集的图像、标签,验证集的图像、标签
四、为什么手写数字图像分类时,要对数据采用批处理?
Python和Matlab对数组的处理速度比对循环的处理速度要快,所以,如果采用循环方式将图像一个个传输进循环进行处理速度很慢,而采用批处理就是把多个图像一次性进行处理,具体操作其实就是矩阵的一些转置变换而已。