# 【ML系列】手把手教你用Numpy构建神经网络！（附代码）

```import numpy as np

NN_ARCHITECTURE = [
{"input_dim": 2, "output_dim": 25, "activation": "relu"},
{"input_dim": 25, "output_dim": 50, "activation": "relu"},
{"input_dim": 50, "output_dim": 50, "activation": "relu"},
{"input_dim": 50, "output_dim": 25, "activation": "relu"},
{"input_dim": 25, "output_dim": 1, "activation": "sigmoid"},
]```

```def init_layers(nn_architecture, seed = 99):
# random seed initiation
np.random.seed(seed)
# number of layers in our neural network
number_of_layers = len(nn_architecture)
# parameters storage initiation
params_values = {}

# iteration over network layers
for idx, layer in enumerate(nn_architecture):
# we number network layers from 1
layer_idx = idx + 1

# extracting the number of units in layers
layer_input_size = layer["input_dim"]
layer_output_size = layer["output_dim"]

# initiating the values of the W matrix
# and vector b for subsequent layers
params_values['W' + str(layer_idx)] = np.random.randn(
layer_output_size, layer_input_size) * 0.1
params_values['b' + str(layer_idx)] = np.random.randn(
layer_output_size, 1) * 0.1

return params_values```

```def sigmoid(Z):
return 1/(1+np.exp(-Z))

def relu(Z):
return np.maximum(0,Z)

def sigmoid_backward(dA, Z):
sig = sigmoid(Z)
return dA * sig * (1 - sig)

def relu_backward(dA, Z):
dZ = np.array(dA, copy = True)
dZ[Z <= 0] = 0;
return dZ;```

```def single_layer_forward_propagation(A_prev, W_curr, b_curr, activation="relu"):
Z_curr = np.dot(W_curr, A_prev) + b_curr

if activation is "relu":
activation_func = relu
elif activation is "sigmoid":
activation_func = sigmoid
else:
raise Exception('Non-supported activation function')

return activation_func(Z_curr), Z_curr```

```def full_forward_propagation(X, params_values, nn_architecture):
memory = {}
A_curr = X

for idx, layer in enumerate(nn_architecture):
layer_idx = idx + 1
A_prev = A_curr

activ_function_curr = layer["activation"]
W_curr = params_values["W" + str(layer_idx)]
b_curr = params_values["b" + str(layer_idx)]
A_curr, Z_curr = single_layer_forward_propagation(A_prev, W_curr, b_curr, activ_function_curr)

memory["A" + str(idx)] = A_prev
memory["Z" + str(layer_idx)] = Z_curr

return A_curr, memory```

```def get_cost_value(Y_hat, Y):
m = Y_hat.shape[1]
cost = -1 / m * (np.dot(Y, np.log(Y_hat).T) + np.dot(1 - Y, np.log(1 - Y_hat).T))
return np.squeeze(cost)

def convert_prob_into_class(probs):
probs_ = np.copy(probs)
probs_[probs_ > 0.5] = 1
probs_[probs_ <= 0.5] = 0
return probs_

def get_accuracy_value(Y_hat, Y):
Y_hat_ = convert_prob_into_class(Y_hat)
return (Y_hat_ == Y).all(axis=0).mean()```

```def single_layer_backward_propagation(dA_curr, W_curr, b_curr, Z_curr, A_prev, activation="relu"):
m = A_prev.shape[1]

if activation is "relu":
backward_activation_func = relu_backward
elif activation is "sigmoid":
backward_activation_func = sigmoid_backward
else:
raise Exception('Non-supported activation function')

dZ_curr = backward_activation_func(dA_curr, Z_curr)
dW_curr = np.dot(dZ_curr, A_prev.T) / m
db_curr = np.sum(dZ_curr, axis=1, keepdims=True) / m
dA_prev = np.dot(W_curr.T, dZ_curr)

return dA_prev, dW_curr, db_curr```

```def full_backward_propagation(Y_hat, Y, memory, params_values, nn_architecture):
grads_values = {}
m = Y.shape[1]
Y = Y.reshape(Y_hat.shape)

dA_prev = - (np.divide(Y, Y_hat) - np.divide(1 - Y, 1 - Y_hat));

for layer_idx_prev, layer in reversed(list(enumerate(nn_architecture))):
layer_idx_curr = layer_idx_prev + 1
activ_function_curr = layer["activation"]

dA_curr = dA_prev

A_prev = memory["A" + str(layer_idx_prev)]
Z_curr = memory["Z" + str(layer_idx_curr)]
W_curr = params_values["W" + str(layer_idx_curr)]
b_curr = params_values["b" + str(layer_idx_curr)]

dA_prev, dW_curr, db_curr = single_layer_backward_propagation(
dA_curr, W_curr, b_curr, Z_curr, A_prev, activ_function_curr)

grads_values["dW" + str(layer_idx_curr)] = dW_curr
grads_values["db" + str(layer_idx_curr)] = db_curr

return grads_values```

```def update(params_values, grads_values, nn_architecture, learning_rate):
for idx, layer in enumerate(nn_architecture):
layer_idx = idx + 1
params_values["W" + str(layer_idx)] -= learning_rate * grads_values["dW" + str(layer_idx)]
params_values["b" + str(layer_idx)] -= learning_rate * grads_values["db" + str(layer_idx)]

return params_values;```

```def train(X, Y, nn_architecture, epochs, learning_rate):
params_values = init_layers(nn_architecture, 2)
cost_history = []
accuracy_history = []

for i in range(epochs):
Y_hat, cashe = full_forward_propagation(X, params_values, nn_architecture)
cost = get_cost_value(Y_hat, Y)
cost_history.append(cost)
accuracy = get_accuracy_value(Y_hat, Y)
accuracy_history.append(accuracy)

grads_values = full_backward_propagation(Y_hat, Y, cashe, params_values, nn_architecture)
params_values = update(params_values, grads_values, nn_architecture, learning_rate)

return params_values, cost_history, accuracy_history```

```N_SAMPLES = 1000
TEST_SIZE = 0.1

X, y = make_moons(n_samples = N_SAMPLES, noise=0.2, random_state=100)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=TEST_SIZE, random_state=42)

def make_plot(X, y, plot_name, file_name=None, XX=None, YY=None, preds=None, dark=False):
if (dark):
plt.style.use('dark_background')
else:
sns.set_style("whitegrid")
plt.figure(figsize=(16,12))
axes = plt.gca()
axes.set(xlabel="\$X_1\$", ylabel="\$X_2\$")
plt.title(plot_name, fontsize=30)
plt.subplots_adjust(left=0.20)
plt.subplots_adjust(right=0.80)
if(XX is not None and YY is not None and preds is not None):
plt.contourf(XX, YY, preds.reshape(XX.shape), 25, alpha = 1, cmap=cm.Spectral)
plt.contour(XX, YY, preds.reshape(XX.shape), levels=[.5], cmap="Greys", vmin=0, vmax=.6)
plt.scatter(X[:, 0], X[:, 1], c=y.ravel(), s=40, cmap=plt.cm.Spectral, edgecolors='black')
if(file_name):
plt.savefig(file_name)
plt.close()
make_plot(X, y, "Dataset")```

NN模型测试

```# 训练
params_values = train(np.transpose(X_train), np.transpose(y_train.reshape((y_train.shape[0], 1))), NN_ARCHITECTURE, 10000, 0.01)
# 预测
Y_test_hat, _ = full_forward_propagation(np.transpose(X_test), params_values, NN_ARCHITECTURE)

acc_test = get_accuracy_value(Y_test_hat, np.transpose(y_test.reshape((y_test.shape[0], 1))))
print("Test set accuracy: {:.2f}".format(acc_test))```
`Test set accuracy: 0.98 `

Keras模型测试

```model = Sequential()
model.add(Dense(25, input_dim=2,activation='relu'))
model.add(Dense(50, activation='relu'))
model.add(Dense(50, activation='relu'))
model.add(Dense(25, activation='relu'))
model.add(Dense(1, activation='sigmoid'))

model.compile(loss='binary_crossentropy', optimizer="sgd", metrics=['accuracy'])

# 训练
history = model.fit(X_train, y_train, epochs=200, verbose=0)

Y_test_hat = model.predict_classes(X_test)
acc_test = accuracy_score(y_test, Y_test_hat)
print("Test set accuracy: {:.2f} - Goliath".format(acc_test))```
`Test set accuracy: 0.98 `

0 条评论

• ### 无敌了！新闻情绪因子进阶来啦！

我们详细分析对比了采用不同情绪得分计算方法的因子表现。从而得出一个很重要且结论：即情绪因子构建时应该考虑新闻与股票的相关度即情绪得分的时间衰减。基于以上的结论，...

• ### 真假美猴王！基于XGBoost的『金融时序』 VS 『合成时序』

今天，公众号要给大家介绍，区分真实的金融时间序列和合成的时间序列。数据是匿名的，我们不知道哪个时间序列来自什么资产。

• ### 【Python机器学习】系列五决策树非线性回归与分类（深度详细附源码）

查看之前文章请点击右上角，关注并且查看历史消息 所有文章全部分类和整理，让您更方便查找阅读。请在页面菜单里查找。 相关内容：（点击标题可查看原文） 第1章 机...

• ### 基于NumPy手写神经网络

Keras、TensorFlow、PyTorch等高层框架让我们可以快速搭建复杂模型。然而，花一点时间了解下底层概念是值得的。前不久我发过一篇文章，以简单的方式...

• ### 用wxPython打造Python图形界面（上）

有许多图形用户界面(GUI)工具包可以与Python编程语言一起使用。其中三巨头是Tkinter、wxPython和PyQt。这些工具包中的每一个都将与Wind...

• ### PCL常见错误集锦

我刚刚开始接触PCL，懂的东西也很少，所以总是出现各种各样的问题，每次遇见问题的时候要查找各种各样的资料，很费时间。所以，今天我把我遇见的常见问题分享给大家，讲...

• ### 超越Google，腾讯推出自研图片编码格式TPG

近日，记者从国家知识产权局了解到，腾讯公司正式向国家知识产权局提交了一份关于图片编码技术的专利申请。此项专利被命名为TPG（Tiny Portable Grap...

• ### 超越 Google，腾讯推出自研图片编码格式 TPG

近日，记者从国家知识产权局了解到，腾讯公司正式向国家知识产权局提交了一份关于图片编码技术的专利申请。此项专利被命名为TPG（Tiny Portable Grap...

• ### 混合云存储：大数据应用的上云之道

? 企业数字化转型过程中，数据价值被显著放大，大数据应用成为不少企业探索的重点。 从技术上看，大数据业务由于数据体量大，且数据量很多时候呈急速膨胀状态；在进...

• ### 超越Google，腾讯推出自研图片编码格式TPG

腾讯公司正式向国家知识产权局提交了一份关于图片编码技术的专利申请，此项专利被命名为TPG（Tiny Portable Graphics），在数据上TPG图片格式...