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))=w⋅x(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

最低0.47元/天 解锁文章
1704

被折叠的 条评论
为什么被折叠?



