C2_W1_Lab03_CoffeeRoasting_Numpy吴恩达_中英

本文介绍了如何在Python中使用Numpy构建一个小型神经网络,模拟之前在Tensorflow中的咖啡烘焙实验。包括数据预处理、标准化步骤以及构建多层网络结构,通过实例展示了如何使用循环和激活函数实现层操作。
摘要由CSDN通过智能技术生成

Optional Lab - Simple Neural Network

In this lab, we will build a small neural network using Numpy. It will be the same “coffee roasting” network you implemented in Tensorflow.
在本次实验中,我们将使用Numpy构建一个小型的神经网络。它将和你之前在Tensorflow中实现的“咖啡烘焙”网络相同。
显示错误

import numpy as np
import matplotlib.pyplot as plt
plt.style.use('./deeplearning.mplstyle')
import tensorflow as tf
from lab_utils_common import dlc, sigmoid
from lab_coffee_utils import load_coffee_data, plt_roast, plt_prob, plt_layer, plt_network, plt_output_unit
import logging
logging.getLogger("tensorflow").setLevel(logging.ERROR)
tf.autograph.set_verbosity(0)

DataSet

This is the same data set as the previous lab.

X,Y = load_coffee_data();
print(X.shape, Y.shape)
(200, 2) (200, 1)

Let’s plot the coffee roasting data below. The two features are Temperature in Celsius and Duration in minutes. Coffee Roasting at Home suggests that the duration is best kept between 12 and 15 minutes while the temp should be between 175 and 260 degrees Celsius. Of course, as the temperature rises, the duration should shrink.
让我们绘制下面的咖啡烘焙数据集。两个特征是摄氏温度和分钟持续时间。 在家烘焙绿色咖啡建议持续时间最好保持在12到15分钟之间,而温度应在175至260摄氏度之间。当然,随着温度的上升,持续时间应该缩短。

plt_roast(X,Y)

在这里插入图片描述

Normalize Data

To match the previous lab, we’ll normalize the data. Refer to that lab for more details
相对之前的实验,我们将标准化数据。有关更多详细信息,请参阅之前的实验

print(f"Temperature Max, Min pre normalization: {np.max(X[:,0]):0.2f}, {np.min(X[:,0]):0.2f}")
print(f"Duration    Max, Min pre normalization: {np.max(X[:,1]):0.2f}, {np.min(X[:,1]):0.2f}")
norm_l = tf.keras.layers.Normalization(axis=-1)
norm_l.adapt(X)  # learns mean, variance
Xn = norm_l(X)
print(f"Temperature Max, Min post normalization: {np.max(Xn[:,0]):0.2f}, {np.min(Xn[:,0]):0.2f}")
print(f"Duration    Max, Min post normalization: {np.max(Xn[:,1]):0.2f}, {np.min(Xn[:,1]):0.2f}")
Temperature Max, Min pre normalization: 284.99, 151.32
Duration    Max, Min pre normalization: 15.45, 11.51
Temperature Max, Min post normalization: 1.66, -1.69
Duration    Max, Min post normalization: 1.79, -1.70

Numpy Model (Forward Prop in NumPy)

显示错误

Let’s build the “Coffee Roasting Network” described in lecture. There are two layers with sigmoid activations.
让我们构建讲座中描述的“咖啡烘焙网络”。有两个具有sigmoid激活的层。

As described in lecture, it is possible to build your own dense layer using NumPy. This can then be utilized to build a multi-layer neural network.
按照讲座中的描述,可以使用NumPy构建自己的密集层。然后可以使用它来构建多层神经网络。

显示错误

In the first optional lab, you constructed a neuron in NumPy and in Tensorflow and noted their similarity. A layer simply contains multiple neurons/units. As described in lecture, one can utilize a for loop to visit each unit (j) in the layer and perform the dot product of the weights for that unit (W[:,j]) and sum the bias for the unit (b[j]) to form z. An activation function g(z) can then be applied to that result. Let’s try that below to build a “dense layer” subroutine.
在第一个可选实验中,您在NumPy和Tensorflow中构建了一个神经元,并注意了它们之间的相似性。一个层包含多个神经元/单元。按照讲座中的描述,可以使用for循环访问层中的每个单元(j),并对该单元的权重(W[:,j])和偏差(b[j])执行点积,以形成z。然后可以应用激活函数g(z)对该结果进行应用。让我们试着这样做,以构建一个“密集层”子程序。

def my_dense(a_in, W, b, g):
    """
    Computes dense layer
    Args:
      a_in (ndarray (n, )) : Data, 1 example 
      W    (ndarray (n,j)) : Weight matrix, n features per unit, j units
      b    (ndarray (j, )) : bias vector, j units  
      g    activation function (e.g. sigmoid, relu..)
    Returns
      a_out (ndarray (j,))  : j units|
    """
    units = W.shape[1]
    a_out = np.zeros(units)
    for j in range(units):               
        w = W[:,j]                                    
        z = np.dot(w, a_in) + b[j]         
        a_out[j] = g(z)               
    return(a_out)

The following cell builds a two-layer neural network utilizing the my_dense subroutine above.
下面的单元格构建了一个使用上述my_dense子程序的两层神经网络。

def my_sequential(x, W1, b1, W2, b2):
    a1 = my_dense(x,  W1, b1, sigmoid)
    a2 = my_dense(a1, W2, b2, sigmoid)
    return(a2)

We can copy trained weights and biases from the previous lab in Tensorflow.
我们可以从Tensorflow的前一个实验中复制训练好的权重和偏差。

W1_tmp = np.array( [[-8.93,  0.29, 12.9 ], [-0.1,  -7.32, 10.81]] )
b1_tmp = np.array( [-9.82, -9.28,  0.96] )
W2_tmp = np.array( [[-31.18], [-27.59], [-32.56]] )
b2_tmp = np.array( [15.41] )

Predictions

显示错误

Once you have a trained model, you can then use it to make predictions. Recall that the output of our model is a probability. In this case, the probability of a good roast. To make a decision, one must apply the probability to a threshold. In this case, we will use 0.5
一旦你有了一个训练好的模型,你就可以用它来做出预测。回想一下,模型的输出是一个概率。在这种情况下,是良好的烘焙概率。要做出决定,必须将概率应用于阈值。在这种情况下,我们将使用0.5

Let’s start by writing a routine similar to Tensorflow’s model.predict(). This will take a matrix X X X with all m m m examples in the rows and make a prediction by running the model.
让我们先编写一个类似于Tensorflow的model.predict()的例程。这将接受一个矩阵 X X X,其中所有 m m m个例子都在行中,并通过运行模型进行预测。

def my_predict(X, W1, b1, W2, b2):
    m = X.shape[0]
    p = np.zeros((m,1))
    for i in range(m):
        p[i,0] = my_sequential(X[i], W1, b1, W2, b2)
    return(p)

We can try this routine on two examples:
我们可以尝试这个例程在两个例子上:

X_tst = np.array([
    [200,13.9],  # postive example
    [200,17]])   # negative example
X_tstn = norm_l(X_tst)  # remember to normalize
predictions = my_predict(X_tstn, W1_tmp, b1_tmp, W2_tmp, b2_tmp)

To convert the probabilities to a decision, we apply a threshold:
将概率转换为决策需要应用一个阈值:

yhat = np.zeros_like(predictions)
for i in range(len(predictions)):
    if predictions[i] >= 0.5:
        yhat[i] = 1
    else:
        yhat[i] = 0
print(f"decisions = \n{yhat}")
decisions = 
[[1.]
 [0.]]

This can be accomplished more succinctly:

yhat = (predictions >= 0.5).astype(int)
print(f"decisions = \n{yhat}")
decisions = 
[[1]
 [0]]

Network function

This graph shows the operation of the whole network and is identical to the Tensorflow result from the previous lab.
The left graph is the raw output of the final layer represented by the blue shading. This is overlaid on the training data represented by the X’s and O’s.
这个图显示了整个网络的操作,它与上一个实验室的Tensorflow结果完全相同。左边的图是最终层的原始输出,用蓝色阴影表示。它被叠加在代表X和O的训练数据上。
The right graph is the output of the network after a decision threshold. The X’s and O’s here correspond to decisions made by the network.
右边的图像是经过决策阈值的网络输出。这里的X和O对应于网络做出的决策。

netf= lambda x : my_predict(norm_l(x),W1_tmp, b1_tmp, W2_tmp, b2_tmp)
plt_network(X,Y,netf)

显示错误

Congratulations!

You have built a small neural network in NumPy.
你使用numpy构建了一个小的神经网络。
Hopefully this lab revealed the fairly simple and familiar functions which make up a layer in a neural network.
希望这个实验室揭示了构成神经网络的一层相当简单和熟悉的函数。

  • 27
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值