# [译] 面向初学者： 神经网络概述（一）

## 1. 基本构建模块：神经元（Neurons）

$$\begin{array}{l} x_{1} \rightarrow x_{1} * w_{1} \\ x_{2} \rightarrow x_{2} * w_{2} \end{array}$$

$$\left(x_{1} * w_{1}\right)+\left(x_{2} * w_{2}\right)+b$$

$$y=f\left(x_{1} * w_{1}+x_{2} * w_{2}+b\right)$$

\begin{aligned} \frac{1}{1+e^{-x}} \end{aligned}

• 把无限小的负数转化为无限接近于0
• 把无限大的正数转化为无限接近于1

### 一个简单的例子

$$w = [0 ，1]$$ 是 $$w_{1} = 0，w_{2} = 1$$

\begin{aligned} (w \cdot x)+b &=\left(\left(w_{1} * x_{1}\right)+\left(w_{2} * x_{2}\right)\right)+b \\ &=0 * 2+1 * 3+4 \\ &=7 \end{aligned}

$$y=f(w \cdot x+b)=f(7)=\bm{0.999}$$

#### 编写一个神经元

import numpy as np

def sigmoid(x):
# Our activation function: f(x) = 1 / (1 + e^(-x))
return 1 / (1 + np.exp(-x))

class Neuron:
def __init__(self, weights, bias):
self.weights = weights
self.bias = bias

def feedforward(self, inputs):
# Weight inputs, add bias, then use the activation function
total = np.dot(self.weights, inputs) + self.bias
return sigmoid(total)

weights = np.array([0, 1]) # w1 = 0, w2 = 1
bias = 4                   # b = 4
n = Neuron(weights, bias)

x = np.array([2, 3])       # x1 = 2, x2 = 3
print(n.feedforward(x))    # 0.9990889488055994

## 2. 将神经元组合成神经网络

### 举个栗子：前馈

\begin{aligned} h_{1}=h_{2} &=f(w \cdot x+b) \\ &=f((0 * 2)+(1 * 3)+0) \\ &=f(3) \\ &=0.9526 \end{aligned}

\begin{aligned} o_{1} &=f\left(w \cdot\left[h_{1}, h_{2}\right]+b\right) \\ &=f\left(\left(0 * h_{1}\right)+\left(1 * h_{2}\right)+0\right) \\ &=f(0.9526) \\ &=\bm{0.7216} \end{aligned}

### 编写一个神经网络：前馈

import numpy as np

# ... code from previous section here

class OurNeuralNetwork:
'''
A neural network with:
- 2 inputs
- a hidden layer with 2 neurons (h1, h2)
- an output layer with 1 neuron (o1)
Each neuron has the same weights and bias:
- w = [0, 1]
- b = 0
'''
def __init__(self):
weights = np.array([0, 1])
bias = 0

# The Neuron class here is from the previous section
self.h1 = Neuron(weights, bias)
self.h2 = Neuron(weights, bias)
self.o1 = Neuron(weights, bias)

def feedforward(self, x):
out_h1 = self.h1.feedforward(x)
out_h2 = self.h2.feedforward(x)

# The inputs for o1 are the outputs from h1 and h2
out_o1 = self.o1.feedforward(np.array([out_h1, out_h2]))

return out_o1

network = OurNeuralNetwork()
x = np.array([2, 3])
print(network.feedforward(x)) # 0.7216325609518421

1.2k 声望
128 粉丝
0 条评论