第三周编程作业-Planar data classification with one hidden layer(二)

简介: 第三周编程作业-Planar data classification with one hidden layer(二)

4.4 - Integrate parts 4.1, 4.2 and 4.3 in nn_model()


Question: Build your neural network model in nn_model().

Instructions: The neural network model has to use the previous functions in the right order.


# GRADED FUNCTION: nn_model
def nn_model(X, Y, n_h, num_iterations = 10000, print_cost=False):
    """
    Arguments:
    X -- dataset of shape (2, number of examples)
    Y -- labels of shape (1, number of examples)
    n_h -- size of the hidden layer
    num_iterations -- Number of iterations in gradient descent loop
    print_cost -- if True, print the cost every 1000 iterations
    Returns:
    parameters -- parameters learnt by the model. They can then be used to predict.
    """
    np.random.seed(3)
    n_x = layer_sizes(X, Y)[0]
    n_y = layer_sizes(X, Y)[2]
    # Initialize parameters, then retrieve W1, b1, W2, b2. Inputs: "n_x, n_h, n_y". Outputs = "W1, b1, W2, b2, parameters".
    ### START CODE HERE ### (≈ 5 lines of code)
    parameters = initialize_parameters(n_x,n_h,n_y)
    W1 = parameters["W1"]
    b1 = parameters["b1"]
    W2 = parameters["W2"]
    b2 = parameters["b2"]
    ### END CODE HERE ###
    # Loop (gradient descent)
    for i in range(0, num_iterations):
        ### START CODE HERE ### (≈ 4 lines of code)
        # Forward propagation. Inputs: "X, parameters". Outputs: "A2, cache".
        A2, cache =forward_propagation(X, parameters)
        # Cost function. Inputs: "A2, Y, parameters". Outputs: "cost".
        cost = compute_cost(A2, Y, parameters)
        # Backpropagation. Inputs: "parameters, cache, X, Y". Outputs: "grads".
        grads = backward_propagation(parameters, cache, X, Y)
        # Gradient descent parameter update. Inputs: "parameters, grads". Outputs: "parameters".
        parameters = update_parameters(parameters, grads)
        ### END CODE HERE ###
        # Print the cost every 1000 iterations
        if print_cost and i % 1000 == 0:
            print ("Cost after iteration %i: %f" %(i, cost))
    return parameters


X_assess, Y_assess = nn_model_test_case()
parameters = nn_model(X_assess, Y_assess, 4, num_iterations=10000, print_cost=False)
print("W1 = " + str(parameters["W1"]))
print("b1 = " + str(parameters["b1"]))
print("W2 = " + str(parameters["W2"]))
print("b2 = " + str(parameters["b2"]))


/opt/conda/lib/python3.5/site-packages/ipykernel/__main__.py:20: RuntimeWarning: divide by zero encountered in log
/home/jovyan/work/Week 3/Planar data classification with one hidden layer/planar_utils.py:34: RuntimeWarning: overflow encountered in exp
  s = 1/(1+np.exp(-x))
W1 = [[-4.18494056  5.33220609]
 [-7.52989382  1.24306181]
 [-4.1929459   5.32632331]
 [ 7.52983719 -1.24309422]]
b1 = [[ 2.32926819]
 [ 3.79458998]
 [ 2.33002577]
 [-3.79468846]]
W2 = [[-6033.83672146 -6008.12980822 -6033.10095287  6008.06637269]]
b2 = [[-52.66607724]]


Expected Output:

W1 [[-4.18494056 5.33220609]

[-7.52989382 1.24306181]

[-4.1929459 5.32632331]

[ 7.52983719 -1.24309422]]

b1 [[ 2.32926819]

[ 3.79458998]

[ 2.33002577]

[-3.79468846]]

W2 [[-6033.83672146 -6008.12980822 -6033.10095287 6008.06637269]]
b2

[[-52.66607724]]


4.5 Predictions


Question: Use your model to predict by building predict().

Use forward propagation to predict results.


Reminder: predictions = $y_{prediction} = \mathbb 1 \text{{activation > 0.5}} = \begin{cases}

1 & \text{if}\ activation > 0.5 \

0 & \text{otherwise}

\end{cases}$

As an example, if you would like to set the entries of a matrix X to 0 and 1 based on a threshold you would do: X_new = (X > threshold)


# GRADED FUNCTION: predict
def predict(parameters, X):
    """
    Using the learned parameters, predicts a class for each example in X
    Arguments:
    parameters -- python dictionary containing your parameters 
    X -- input data of size (n_x, m)
    Returns
    predictions -- vector of predictions of our model (red: 0 / blue: 1)
    """
    # Computes probabilities using forward propagation, and classifies to 0/1 using 0.5 as the threshold.
    ### START CODE HERE ### (≈ 2 lines of code)
    A2, cache = forward_propagation(X, parameters)
    predictions = (A2>0.5)
    ### END CODE HERE ###
    return predictions


parameters, X_assess = predict_test_case()
predictions = predict(parameters, X_assess)
print("predictions mean = " + str(np.mean(predictions)))


predictions mean = 0.666666666667


Expected Output:

predictions mean 0.666666666667

It is time to run the model and see how it performs on a planar dataset. Run the following code to test your model with a single hidden layer of $n_h$ hidden units.


# Build a model with a n_h-dimensional hidden layer
parameters = nn_model(X, Y, n_h = 4, num_iterations = 10000, print_cost=True)
# Plot the decision boundary
plot_decision_boundary(lambda x: predict(parameters, x.T), X, Y)
plt.title("Decision Boundary for hidden layer size " + str(4))


Cost after iteration 0: 0.693048
Cost after iteration 1000: 0.288083
Cost after iteration 2000: 0.254385
Cost after iteration 3000: 0.233864
Cost after iteration 4000: 0.226792
Cost after iteration 5000: 0.222644
Cost after iteration 6000: 0.219731
Cost after iteration 7000: 0.217504
Cost after iteration 8000: 0.219454
Cost after iteration 9000: 0.218607
<matplotlib.text.Text at 0x7f1b55d40b38>


3.png

output_50_2.png

Expected Output:

Cost after iteration 9000 0.218607


# Print accuracy
predictions = predict(parameters, X)
print ('Accuracy: %d' % float((np.dot(Y,predictions.T) + np.dot(1-Y,1-predictions.T))/float(Y.size)*100) + '%')


Accuracy: 90%


Expected Output:

Accuracy 90%

Accuracy is really high compared to Logistic Regression. The model has learnt the leaf patterns of the flower! Neural networks are able to learn even highly non-linear decision boundaries, unlike logistic regression.

Now, let's try out several hidden layer sizes.


4.6 - Tuning hidden layer size (optional/ungraded exercise)


Run the following code. It may take 1-2 minutes. You will observe different behaviors of the model for various hidden layer sizes.


# This may take about 2 minutes to run
plt.figure(figsize=(16, 32))
hidden_layer_sizes = [1, 2, 3, 4, 5, 20, 50]
for i, n_h in enumerate(hidden_layer_sizes):
    plt.subplot(5, 2, i+1)
    plt.title('Hidden Layer of size %d' % n_h)
    parameters = nn_model(X, Y, n_h, num_iterations = 5000)
    plot_decision_boundary(lambda x: predict(parameters, x.T), X, Y)
    predictions = predict(parameters, X)
    accuracy = float((np.dot(Y,predictions.T) + np.dot(1-Y,1-predictions.T))/float(Y.size)*100)
    print ("Accuracy for {} hidden units: {} %".format(n_h, accuracy))


Accuracy for 1 hidden units: 67.5 %
Accuracy for 2 hidden units: 67.25 %
Accuracy for 3 hidden units: 90.75 %
Accuracy for 4 hidden units: 90.5 %
Accuracy for 5 hidden units: 91.25 %
Accuracy for 20 hidden units: 90.0 %
Accuracy for 50 hidden units: 90.25 %


4.png


output_56_1.png

Interpretation:

  • The larger models (with more hidden units) are able to fit the training set better, until eventually the largest models overfit the data.
  • The best hidden layer size seems to be around n_h = 5. Indeed, a value around here seems to  fits the data well without also incurring noticable overfitting.
  • You will also learn later about regularization, which lets you use very large models (such as n_h = 50) without much overfitting.


Optional questions:

Note: Remember to submit the assignment but clicking the blue "Submit Assignment" button at the upper-right.

Some optional/ungraded questions that you can explore if you wish:

  • What happens when you change the tanh activation for a sigmoid activation or a ReLU activation?
  • Play with the learning_rate. What happens?
  • What if we change the dataset? (See part 5 below!)

You've learnt to:

  • Build a complete neural network with a hidden layer
  • Make a good use of a non-linear unit
  • Implemented forward propagation and backpropagation, and trained a neural network
  • See the impact of varying the hidden layer size, including overfitting.

Nice work!


5) Performance on other datasets


If you want, you can rerun the whole notebook (minus the dataset part) for each of the following datasets.


# Datasets
noisy_circles, noisy_moons, blobs, gaussian_quantiles, no_structure = load_extra_datasets()
datasets = {"noisy_circles": noisy_circles,
            "noisy_moons": noisy_moons,
            "blobs": blobs,
            "gaussian_quantiles": gaussian_quantiles}
### START CODE HERE ### (choose your dataset)
dataset = "noisy_moons"
### END CODE HERE ###
X, Y = datasets[dataset]
X, Y = X.T, Y.reshape(1, Y.shape[0])
# make blobs binary
if dataset == "blobs":
    Y = Y%2
# Visualize the data
plt.scatter(X[0, :], X[1, :], c=Y, s=40, cmap=plt.cm.Spectral);


5.png

output_63_0.png

Congrats on finishing this Programming Assignment!

Reference:

相关文章
|
计算机视觉 Python
解决pycharm调用plt.show()后无图片显示问题
解决pycharm调用plt.show()后无图片显示问题
2629 0
|
7月前
|
人工智能 弹性计算 安全
Hermes Agent 极速部署指南+免费Token领取教程
Hermes Agent是全球增长最快(GitHub星标超14万)的开源自进化智能体框架,具备持久记忆、自主学习与技能优化能力。阿里云提供一键部署方案,2步即可完成配置,轻松启用越用越聪明的AI助手。
1295 1
|
传感器 机器学习/深度学习 编解码
【IMU Kalman滤波器】9轴IMU传感器(加速度计、陀螺仪、磁力计)的卡尔曼滤波器算法研究(Matlab代码实现)
【IMU Kalman滤波器】9轴IMU传感器(加速度计、陀螺仪、磁力计)的卡尔曼滤波器算法研究(Matlab代码实现)
1385 124
|
12月前
|
人工智能 算法 安全
集之互动AI创意视频解决方案:商业级可控,让品牌创意从“灵感”直达“落地”
集之互动依托国家备案“无垠大模型”与广告专属控制算法,打造AI创意视频解决方案,实现从脚本到成片的全流程“商业级可控”。面向品牌调性、视觉风格与内容安全,助力企业将创意想象转化为可量化、可复用的营销资产,推动营销进入高效、稳定、安全的AI工业化时代。
643 4
|
机器学习/深度学习 人工智能 自然语言处理
《AI大模型技术全景解读》从机器学习到现代大模型
人工智能历经从机器学习到深度学习的演进,以Transformer架构为里程碑,推动大模型时代到来。技术发展涵盖CNN、RNN、BERT、GPT等核心模型,逐步实现语言理解、生成与多模态能力突破,正朝高效推理、安全对齐与普惠应用迈进。(238字)
|
Linux 虚拟化 iOS开发
Windows Server 2008 R2 OVF (2025 年 4 月更新) - VMware 虚拟机模板
Windows Server 2008 R2 OVF (2025 年 4 月更新) - VMware 虚拟机模板
741 29
Windows Server 2008 R2 OVF (2025 年 4 月更新) - VMware 虚拟机模板
|
传感器 人工智能 安全
详述Agent智能体含义
智能体(Agent)技术正推动人工智能从被动响应迈向主动决策。它具备自主性、反应性、主动性和社交能力,能感知环境、规划任务、调用工具、学习经验,实现复杂目标。从个人助理到科研加速,从软件开发到自动驾驶,智能体已在多个领域落地。然而,其可靠性、长程规划、安全伦理及认知局限仍是待解难题。未来,智能体将作为人类智能延伸,助力我们聚焦创造与战略,开启人机协同新篇章。
2827 7
|
缓存 NoSQL Java
京东电商下单黄金链路:防止订单重复提交与支付的深度解析
【10月更文挑战第21天】在电商领域,尤其是在像京东这样的大型电商平台中,防止订单重复提交与支付是一项至关重要的任务。
1053 44
|
Python
python中的reduce(function, iterable[, initializer])
【4月更文挑战第4天】`reduce()` 是 Python 的内置函数(Python 3 需从 `functools` 导入),用于对列表或元组等可迭代对象进行累积操作。它接收一个二元操作函数和一个可迭代对象,连续应用函数至所有元素,最终得到单一结果。可选参数 `initializer` 用于设置初始值。示例展示了使用 `lambda` 计算元素之和、乘积以及带有初始值的例子。
904 0
python中的reduce(function, iterable[, initializer])
|
数据安全/隐私保护
加速度、速度、位移互相转换,加速度转位移matlab程序,任意数据类型
地震波格式转换、时程转换、峰值调整、规范反应谱、计算反应谱、计算持时、生成人工波、时频域转换、数据滤波、基线校正、Arias截波、傅里叶变换、耐震时程曲线、脉冲波合成与提取、三联反应谱、地震动参数、延性反应谱、地震波缩尺、功率谱密度