From 671ad49c413c04c7c7de57259d5f11a8403fd058 Mon Sep 17 00:00:00 2001 From: lipeng Date: Thu, 28 May 2020 14:16:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=9D=E5=AD=98=E4=B8=8E=E5=8A=A0=E8=BD=BD?= =?UTF-8?q?=E6=9D=83=E9=87=8D=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../feedforward_neural_network.py | 24 ++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/深度学习笔记/前馈神经网络/feedforward_neural_network.py b/深度学习笔记/前馈神经网络/feedforward_neural_network.py index f3af4bb..58a669f 100755 --- a/深度学习笔记/前馈神经网络/feedforward_neural_network.py +++ b/深度学习笔记/前馈神经网络/feedforward_neural_network.py @@ -17,7 +17,7 @@ def relu(z): L = 0 ##### neural network model -def neural_network(X, Y, learning_rate=0.01, num_iterations=8000, lambd=0): +def neural_network(X, Y, learning_rate=0.01, num_iterations=2000, lambd=0): n0, m = X.shape n1 = 20 n2 = 7 @@ -124,21 +124,33 @@ def vectorize_sequences(sequences, dimension=10000): results[i, sequence] = 1. # 索引results矩阵中的位置,赋值为1,全部都是从第0行0列开始的 return results +def load(path="./weigths.npz"): + weigths = np.load(path, allow_pickle=True) + return weigths + +def save(weigths, path="./weigths.npz"): + np.savez(path, param_w=weigths["param_w"], param_b=weigths["param_b"]) + ### np.savetxt("./weigths.out", weigths["param_w"], fmt='%s') + (train_data, train_labels), (test_data, test_labels) = imdb.load_data(path="imdb/imdb.npz",num_words=10000) # Our vectorized training data -x_train = vectorize_sequences(train_data[:1000]) +x_train = vectorize_sequences(train_data[:100]) # Our vectorized test data x_test = vectorize_sequences(test_data[:5]) -y_train = np.asarray(train_labels[:1000]).astype('float32') +y_train = np.asarray(train_labels[:100]).astype('float32') y_test = np.asarray(test_labels[:5]).astype('float32') -parameters = neural_network(x_train.T, y_train) -y_p = predict(x_test.T, parameters) +parameters = neural_network(x_train.T, y_train, num_iterations=1000) +save(parameters) +weigths = load() + +y_p = predict(x_test.T, weigths) print(y_p) print(y_test) -y_p2 = predict(x_train[:5].T, parameters) + +y_p2 = predict(x_train[:5].T, weigths) print(y_p2) print(y_train[:5])