深度学习基础1

2022-02-07  本文已影响0人  Rain师兄

A Single Neuron

学习:linear units, the building blocks of deep learning.

学了这些你就可以建立自己的deep neural networks了。

能学到什么?

创建一个全连接的神经网络架构

将神经网络应用到经典ml问题。

用随机梯度下降来训练神经网络

什么是深度学习:深度学习是一种机器学习方法,其特点是深度计算堆栈。

Deep learning is an approach to machine learning characterized by deep stacks of computations.

1.The Linear Unit

这就是带着一个输入的单个神经元。无论什么时候,一个值流过一个连接的时候,您将该值乘以连接的权重。For the input x, what reaches the neuron is w * x. A neural network "learns" by modifying its weights.

2.Example - The Linear Unit as a Model

3.Linear Units in Keras

from tensorflow import keras

from tensorflow.keras import layers

Create a network with 1 linear unit

model = keras.Sequential([

layers.Dense(units=1, input_shape=[3])

])

第一个参数:units定义了有多少输出,这里是1个,input_shape是输入的维度,这里是3.('sugars', 'fiber', and 'protein').

为什么 input_shape 是 Python 列表?

我们将在本课程中使用的数据将是表格数据,就像在 Pandas 数据框中一样。我们将为数据集中的每个特征提供一个输入。特征按列排列,所以我们总是有 input_shape=[num_columns]。 Keras 在这里使用列表的原因是允许使用更复杂的数据集。例如,图像数据可能需要三个维度:[高度、宽度、通道]。

4. exercise

在本教程中,我们了解了神经网络的构建块

在本练习中,您将构建一个线性模型并练习在 Keras 中使用模型。

这里重要的是啥?

就是例子

一个输入,通过连接乘以一个权重,再加上一个bias就是输出了。这是单个神经元,也可以有多个输入。

然后介绍了这些代码:

from tensorflow import keras

from tensorflow.keras import layers

Create a network with 1 linear unit

model = keras.Sequential([

layers.Dense(units=1, input_shape=[3])

])

keras.Sequential是将神经网络创建为层堆栈。

练习:

构建线性模型

项目:处理多个输入的,一个输出的单个神经元。

首先:

import pandas as pd

red_wine = pd.read_csv('../input/dl-course-data/red-wine.csv')

red_wine.head()

red_wine.shape #返回(行,列)

from tensorflow import keras

from tensorflow.keras import layers

model = keras.Sequential([

layers.Dense(units=1, input_shape=[11])

])

w, b = model.weights

(model.weights可以看到权重。)

import tensorflow as tf

import matplotlib.pyplot as plt

model = keras.Sequential([

layers.Dense(1, input_shape=[1]),

])

x = tf.linspace(-1.0, 1.0, 100)

y = model.predict(x)

plt.figure(dpi=100)

plt.plot(x, y, 'k')

plt.xlim(-1, 1)

plt.ylim(-1, 1)

plt.xlabel("Input: x")

plt.ylabel("Target y")

w, b = model.weights # you could also use model.get_weights() here

plt.title("Weight: {:0.2f}\nBias: {:0.2f}".format(w[0][0], b[0]))

plt.show()

(这一块是测试,把未经训练的模型来预测,得到的图是不一样的,因为没训练的模型的权重选择是随机的。

重点:
model = keras.Sequential([

layers.Dense(units=1, input_shape=[3])

])

上一篇 下一篇

猜你喜欢

热点阅读