# Generate a dataset and plot it np.random.seed(0) # 这是两个卫星的数据集, 但是现在有一些噪音, 需要将它们分开 X, y = sklearn.datasets.make_moons(200, noise=0.15) y = y.reshape(200,1)
1
from keras.layers import Dense, Activation
1
from keras.models import Sequential
1
model = Sequential()
WARNING:tensorflow:From /home/didong/anaconda/anaconda3/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:74: The name tf.get_default_graph is deprecated. Please use tf.compat.v1.get_default_graph instead.
1
model.add(Dense(3,input_dim=2))
WARNING:tensorflow:From /home/didong/anaconda/anaconda3/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:517: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.
WARNING:tensorflow:From /home/didong/anaconda/anaconda3/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:4138: The name tf.random_uniform is deprecated. Please use tf.random.uniform instead.
WARNING:tensorflow:From /home/didong/anaconda/anaconda3/lib/python3.7/site-packages/keras/optimizers.py:790: The name tf.train.Optimizer is deprecated. Please use tf.compat.v1.train.Optimizer instead.
WARNING:tensorflow:From /home/didong/anaconda/anaconda3/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py:3376: The name tf.log is deprecated. Please use tf.math.log instead.
WARNING:tensorflow:From /home/didong/anaconda/anaconda3/lib/python3.7/site-packages/tensorflow/python/ops/nn_impl.py:180: add_dispatch_support.<locals>.wrapper (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.where in 2.0, which has the same broadcast rule as np.where
# 反向传播函数 defbackward_prop(model,cache,y): # Load parameters from model W1, b1, W2, b2 = model['W1'], model['b1'], model['W2'], model['b2'] # Load forward propagation results a0,a1, a2 = cache['a0'],cache['a1'],cache['a2'] # Backpropagation # Calculate loss derivative with respect to output # 这里本来就是对dz2进行求导 dz2 = bce_loss_derivative(y=y,y_hat=a2) # Calculate loss derivative with respect to second layer weights dW2 = (a1.T).dot(dz2) # Calculate loss derivative with respect to second layer bias # 考虑到db2并不是一列值, 而是一个单独的值, 所以求和叠加 db2 = np.sum(dz2, axis=0, keepdims=True) # Calculate loss derivative with respect to first layer dz1 = dz2.dot(W2.T) * tanh_derivative(a1) # Calculate loss derivative with respect to first layer weights dW1 = np.dot(a0.T, dz1) # Calculate loss derivative with respect to first layer bias db1 = np.sum(dz1, axis=0) # Store gradients grads = {'dW2':dW2,'db2':db2,'dW1':dW1,'db1':db1} return grads
# Helper function to plot a decision boundary. # If you don't fully understand this function don't worry, it just generates the contour plot below. defplot_decision_boundary(pred_func): # Set min and max values and give it some padding x_min, x_max = X[:, 0].min() - .5, X[:, 0].max() + .5 y_min, y_max = X[:, 1].min() - .5, X[:, 1].max() + .5 h = 0.01 # Generate a grid of points with distance h between them xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h)) # Predict the function value for the whole gid Z = pred_func(np.c_[xx.ravel(), yy.ravel()]) Z = Z.reshape(xx.shape) # Plot the contour and training examples plt.contourf(xx, yy, Z, cmap=plt.cm.Spectral) plt.scatter(X[:, 0], X[:, 1], c=y.flatten(), cmap=plt.cm.Spectral)
1 2 3 4 5
# Generate a dataset and plot it np.random.seed(0) X, y = sklearn.datasets.make_moons(200, noise=0.15) y = y.reshape(200,1) plt.scatter(X[:,0], X[:,1], s=40, c=y.flatten(), cmap=plt.cm.Spectral)
<matplotlib.collections.PathCollection at 0x7fb08a5e0240>
1 2 3 4 5 6 7 8 9 10
defpredict(model, x): # Do forward pass c = forward_prop(model,x) #get y_hat y_hat = c['a2'] # Turn values to either 1 or 0 y_hat[y_hat > 0.5] = 1 y_hat[y_hat < 0.5] = 0 return y_hat
1 2 3 4 5 6 7 8 9 10 11
defcalc_accuracy(model,x,y): # Get total number of examples m = y.shape[0] # Do a prediction with the model pred = predict(model,x) # Ensure prediction and truth vector y have the same shape pred = pred.reshape(y.shape) # Calculate the number of wrong examples error = np.sum(np.abs(pred-y)) # Calculate accuracy return (m - error)/m * 100
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
definitialize_parameters(nn_input_dim,nn_hdim,nn_output_dim): # First layer weights W1 = 2 *np.random.randn(nn_input_dim, nn_hdim) - 1 # First layer bias b1 = np.zeros((1, nn_hdim)) # Second layer weights W2 = 2 * np.random.randn(nn_hdim, nn_output_dim) - 1 # Second layer bias b2 = np.zeros((1, nn_output_dim)) # Package and return model model = { 'W1': W1, 'b1': b1, 'W2': W2, 'b2': b2} return model
# Backpropagation grads = backward_prop(model,cache,y) # Gradient descent parameter update # Assign new parameters to the model model = update_parameters(model=model,grads=grads,learning_rate=learning_rate) # Pring loss & accuracy every 100 iterations if print_loss and i % 100 == 0: y_hat = cache['a2'] print('Loss after iteration',i,':',bce_loss(y,y_hat)) print('Accuracy after iteration',i,':',calc_accuracy(model,X_,y_),'%') return model
1 2 3 4
# Hyper parameters hiden_layer_size = 3 # I picked this value because it showed good results in my experiments learning_rate = 0.01
1 2 3 4 5
# Initialize the parameters to random values. We need to learn these. np.random.seed(0) # This is what we return at the end model = initialize_parameters(nn_input_dim=2, nn_hdim= hiden_layer_size, nn_output_dim= 1) model = train(model,X,y,learning_rate=learning_rate,num_passes=1000,print_loss=True)
Loss after iteration 0 : 0.7590872634269914
Accuracy after iteration 0 : 86.5 %
Loss after iteration 100 : 0.2574839032266012
Accuracy after iteration 100 : 87.5 %
Loss after iteration 200 : 0.23296065120486092
Accuracy after iteration 200 : 91.0 %
Loss after iteration 300 : 0.06607469435615165
Accuracy after iteration 300 : 98.5 %
Loss after iteration 400 : 0.039048891767398106
Accuracy after iteration 400 : 99.0 %
Loss after iteration 500 : 0.03162355657934422
Accuracy after iteration 500 : 99.5 %
Loss after iteration 600 : 0.02808346934457852
Accuracy after iteration 600 : 99.5 %
Loss after iteration 700 : 0.02596724219386473
Accuracy after iteration 700 : 99.5 %
Loss after iteration 800 : 0.02453302540660454
Accuracy after iteration 800 : 99.5 %
Loss after iteration 900 : 0.023480001190425943
Accuracy after iteration 900 : 99.5 %
1 2 3
# Plot the decision boundary plot_decision_boundary(lambda x: predict(model,x)) plt.title("Decision Boundary for hidden layer size 3")
Text(0.5, 1.0, 'Decision Boundary for hidden layer size 3')