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])