C2_W1_Lab01_Neurons_and_Layers吴恩达中英 pytorch版

Optional Lab - Neurons and Layers

In this lab we will explore the inner workings of neurons/units and layers. In particular, the lab will draw parallels to the models you have mastered in Course 1, the regression/linear model and the logistic model. The lab will introduce Tensorflow and demonstrate how these models are implemented in that framework.
在这次实验中,我们将探讨神经元和层的内部工作原理。特别是,该实验将把模型与你在课程1中掌握的模型进行类比,回归/线性模型和逻辑回归模型。该实验将介绍Tensorflow,并演示如何在该框架中实现这些模型。
在这里插入图片描述

Packages

Tensorflow and Keras
Tensorflow is a machine learning package developed by Google. In 2019, Google integrated Keras into Tensorflow and released Tensorflow 2.0. Keras is a framework developed independently by François Chollet that creates a simple, layer-centric interface to Tensorflow. This course will be using the Keras interface.
Tensorflow是一个由谷歌开发的机器学习包。2019年,谷歌将Keras集成到Tensorflow中,并发布了Tensorflow 2.0。Keras是由François Chollet独立开发的框架,创建了一个简单、基于层的界面。本课程将使用Keras

import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.keras.layers import Dense, Input
from tensorflow.keras import Sequential
from tensorflow.keras.losses import MeanSquaredError, BinaryCrossentropy
from tensorflow.keras.activations import sigmoid
from lab_utils_common import dlc
from lab_neurons_utils import plt_prob_1d, sigmoidnp, plt_linear, plt_logistic
plt.style.use('./deeplearning.mplstyle')
import logging
logging.getLogger("tensorflow").setLevel(logging.ERROR)
tf.autograph.set_verbosity(0)

Neuron without activation - Regression/Linear Model

DataSet

We’ll use an example from Course 1, linear regression on house prices.

X_train = np.array([[1.0], [2.0]], dtype=np.float32)           #(size in 1000 square feet)
Y_train = np.array([[300.0], [500.0]], dtype=np.float32)       #(price in 1000s of dollars)

fig, ax = plt.subplots(1,1)
ax.scatter(X_train, Y_train, marker='x', c='r', label="Data Points")
ax.legend( fontsize='xx-large')
ax.set_ylabel('Price (in 1000s of dollars)', fontsize='xx-large')
ax.set_xlabel('Size (1000 sqft)', fontsize='xx-large')
plt.show()

在这里插入图片描述

Regression/Linear Model

The function implemented by a neuron with no activation is the same as in Course 1, linear regression:
f w , b ( x ( i ) ) = w ⋅ x ( i ) + b (1) f_{\mathbf{w},b}(x^{(i)}) = \mathbf{w}\cdot x^{(i)} + b \tag{1} fw,b(x(i))=wx(i)+b(1)

We can define a layer with one neuron or unit and compare it to the familiar linear regression function.
我们可以定义一个具有一个神经元的层或单位,并将其与熟悉的线性回归函数进行比较。

linear_layer = tf.keras.layers.Dense(units=1, activation = 'linear', )

# 用pytorch定义一个具有一个神经元的层或单位
# linear_layer = nn.Sequential(
#     nn.Linear(1,1)
# )

Let’s examine the weights.(让我们来看看权重)

linear_layer.get_weights()

[]

There are no weights as the weights are not yet instantiated. Let’s try the model on one example in X_train. This will trigger the instantiation of the weights. Note, the input to the layer must be 2-D, so we’ll reshape it.
这里没有权重,因为权重还没有被实例化。让我们试着在一个例子上检查模型,X_train。这将触发权重的实例化。注意,层输入必须是2维的,因此我们必须重塑它。

a1 = linear_layer(X_train[0].reshape(1,1))
print(a1
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值