# 入门神经网络：前馈

## 准备工作

• 如何求导数
• 基本的矩阵乘法

• 懂一点机器学习的知识，比如线性回归
• 知道什么是 感知机（perceptron）

Sigmoid 函数
``````import numpy as np

def sigmoid(x):
# sigmoid function
return 1/(1 + np.exp(-x))

inputs = np.array([0.7, -0.3])
weights = np.array([0.1, 0.8])
bias = -0.1

# calculate the output
output = sigmoid(np.dot(weights, inputs) + bias)

print('Output:')
print(output)
``````

• 计算隐层的输入值
• 计算隐层的输出值
• 计算输出层的输出值
• 计算输出层的输出值
``````import numpy as np

def sigmoid(x):
# sigmoid function
return 1/(1+np.exp(-x))

# 神经网络各层神经元数量
N_input = 3
N_hidden = 2
N_output = 1

np.random.seed(42)
# Make some fake data
X = np.random.randn(4)

# 生成输入层到隐层／隐层到输出层权重
weights_in_hidden = np.random.normal(0, scale=0.1, size=(N_input, N_hidden))
weights_hidden_out = np.random.normal(0, scale=0.1, size=(N_hidden, N_output))

# 计算隐层的输入值／输出值
hidden_layer_in = np.dot(X, weights_in_hidden)
hidden_layer_out = sigmoid(hidden_layer_in)

print('Hidden-layer Output:')
print(hidden_layer_out)

# 计算输出层的输入值／输出值
output_layer_in = np.dot(hidden_layer_out, weights_hidden_out)
output_layer_out = sigmoid(output_layer_in)

print('Output-layer Output:')
print(output_layer_out)
``````

## 参考资料

Thanks for reading. If you find any mistake/typo in this blog, please don't hesitate to let me know, you can reach me by email: jyang7[at]ualberta.ca

CV / DL