#### 生成数据

```def create_dataset():
np.random.seed(1)
m = 400 # 数据量
N = int(m/2) # 每个标签的实例数
D = 2 # 数据维度
X = np.zeros((m,D)) # 数据矩阵
Y = np.zeros((m,1), dtype='uint8') # 标签维度
a = 4

for j in range(2):
ix = range(N*j,N*(j+1))
t = np.linspace(j*3.12,(j+1)*3.12,N) + np.random.randn(N)*0.2 # theta
r = a*np.sin(4*t) + np.random.randn(N)*0.2 # radius
X[ix] = np.c_[r*np.sin(t), r*np.cos(t)]
Y[ix] = j

X = X.T
Y = Y.T
return X, Y```

#### 定义网络结构

```def layer_sizes(X, Y):
n_x = X.shape[0] # 输入层大小
n_h = 4 # 隐藏层大小
n_y = Y.shape[0] # 输出层大小
return (n_x, n_h, n_y)```

#### 初始化模型参数

```def initialize_parameters(n_x, n_h, n_y):
W1 = np.random.randn(n_h, n_x)*0.01
b1 = np.zeros((n_h, 1))
W2 = np.random.randn(n_y, n_h)*0.01
b2 = np.zeros((n_y, 1))

assert (W1.shape == (n_h, n_x))
assert (b1.shape == (n_h, 1))
assert (W2.shape == (n_y, n_h))
assert (b2.shape == (n_y, 1))
parameters = {"W1": W1,
"b1": b1,
"W2": W2,
"b2": b2}

return parameters```

#### 前向传播

```def forward_propagation(X, parameters):
# 获取各参数初始值
W1 = parameters['W1']
b1 = parameters['b1']
W2 = parameters['W2']
b2 = parameters['b2']
# 执行前向计算
Z1 = np.dot(W1, X) + b1
A1 = np.tanh(Z1)
Z2 = np.dot(W2, A1) + b2
A2 = sigmoid(Z2)
assert(A2.shape == (1, X.shape[1]))
cache = {"Z1": Z1,
"A1": A1,
"Z2": Z2,
"A2": A2}
return A2, cache```

#### 计算当前训练损失

```def compute_cost(A2, Y, parameters):
# 训练样本量
m = Y.shape[1]
# 计算交叉熵损失
logprobs = np.multiply(np.log(A2),Y) + np.multiply(np.log(1-A2), 1-Y)
cost = -1/m * np.sum(logprobs)
# 维度压缩
cost = np.squeeze(cost)
assert(isinstance(cost, float))
return cost```

#### 执行反向传播

```def backward_propagation(parameters, cache, X, Y):
m = X.shape[1]
# 获取W1和W2
W1 = parameters['W1']
W2 = parameters['W2']
# 获取A1和A2
A1 = cache['A1']
A2 = cache['A2']
# 执行反向传播
dZ2 = A2-Y
dW2 = 1/m * np.dot(dZ2, A1.T)
db2 = 1/m * np.sum(dZ2, axis=1, keepdims=True)
dZ1 = np.dot(W2.T, dZ2)*(1-np.power(A1, 2))
dW1 = 1/m * np.dot(dZ1, X.T)
db1 = 1/m * np.sum(dZ1, axis=1, keepdims=True)
"db1": db1,
"dW2": dW2,
"db2": db2}

#### 权值更新

```def update_parameters(parameters, grads, learning_rate=1.2):
# 获取参数
W1 = parameters['W1']
b1 = parameters['b1']
W2 = parameters['W2']
b2 = parameters['b2']
# 获取梯度
# 参数更新
W1 -= dW1 * learning_rate
b1 -= db1 * learning_rate
W2 -= dW2 * learning_rate
b2 -= db2 * learning_rate
parameters = {"W1": W1,
"b1": b1,
"W2": W2,
"b2": b2}
return parameters```

```def nn_model(X, Y, n_h, num_iterations=10000, print_cost=False):
np.random.seed(3)
n_x = layer_sizes(X, Y)[0]
n_y = layer_sizes(X, Y)[2]
# 初始化模型参数
parameters = initialize_parameters(n_x, n_h, n_y)
W1 = parameters['W1']
b1 = parameters['b1']
W2 = parameters['W2']
b2 = parameters['b2']
# 梯度下降和参数更新循环
for i in range(0, num_iterations):
# 前向传播计算
A2, cache = forward_propagation(X, parameters)
# 计算当前损失
cost = compute_cost(A2, Y, parameters)
# 反向传播
grads = backward_propagation(parameters, cache, X, Y)
# 参数更新
# 打印损失
if print_cost and i % 1000 == 0:
print ("Cost after iteration %i: %f" %(i, cost))

return parameters```

```def predict(parameters, X):
A2, cache = forward_propagation(X, parameters)
predictions = (A2>0.5)
return predictions```

```parameters = nn_model(X, Y, n_h = 4,
num_iterations=10000,
print_cost=True)```

```# 预测准确率
predictions = predict(parameters, X)
print ('Accuracy: %d' % float((np.dot(Y,predictions.T) +
np.dot(1-Y,1-predictions.T))/float(Y.size)*100) + '%')```