微信扫一扫打赏支持

tensorflow2知识总结---2、多层感知器实例

tensorflow2知识总结---2、多层感知器实例

一、总结

一句话总结:

1、多层感知器也就是创建的模型是多层的模型而已,其实基本架构(创建模型、训练模型、模型预测)也都是一样的
2、隐藏层:tf.keras.layers.Dense(10,input_shape=(3,),activation='relu')
3、输出层:tf.keras.layers.Dense(1)
model = tf.keras.Sequential([tf.keras.layers.Dense(10,input_shape=(3,),activation='relu'),
                            tf.keras.layers.Dense(1)])

 

 

1、这句代码的意思:tf.keras.layers.Dense(10,input_shape=(3,),activation='relu')?

神经网络隐藏层,节点10个,输入数据是3维(TV,radio,newspaper),激活函数是 relu

 

 

2、为什么实例中的 dense_1 的参数是40个(输入层3个维度、中间层神经元10个,输出1个)?

因为输入是3个(TV,radio,newspaper),每一个和中间的每个神经元都是有参数,然后有一个bias,所以是(3+1)

 

 

3、为什么实例中的 dense_2 的参数是11个(输入层3个维度、中间层神经元10个,输出1个)?

中间层10个神经元的 wi,再加一个bias

 

4、tensorflow2的model.summary()的作用?

神经网络模型情况的总结,比如有多少层,每层有多少参数
Model: "sequential_3"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_1 (Dense)              (None, 10)                40        
_________________________________________________________________
dense_2 (Dense)              (None, 1)                 11        
=================================================================
Total params: 51
Trainable params: 51
Non-trainable params: 0

 

 

5、tensorflow2的模型配置优化函数和损失器?

model的compile方法:model.compile(optimizer='adam',loss='mse')

 

 

二、多层感知器实例

博客对应课程的视频位置:

 

In [12]:
 
import tensorflow as tf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
In [13]:
 
data = pd.read_csv('dataset/Advertising.csv')
data
Out[13]:
 Unnamed: 0TVradionewspapersales
0 1 230.1 37.8 69.2 22.1
1 2 44.5 39.3 45.1 10.4
2 3 17.2 45.9 69.3 9.3
3 4 151.5 41.3 58.5 18.5
4 5 180.8 10.8 58.4 12.9
... ... ... ... ... ...
195 196 38.2 3.7 13.8 7.6
196 197 94.2 4.9 8.1 9.7
197 198 177.0 9.3 6.4 12.8
198 199 283.6 42.0 66.2 25.5
199 200 232.1 8.6 8.7 13.4

200 rows × 5 columns

In [14]:
 
plt.scatter(data.TV,data.sales)
Out[14]:
<matplotlib.collections.PathCollection at 0x1c3e0759088>
In [15]:
 
plt.scatter(data.radio,data.sales)
Out[15]:
<matplotlib.collections.PathCollection at 0x1c3e18c4e08>
In [16]:
 
 
plt.scatter(data.newspaper,data.sales)
Out[16]:
<matplotlib.collections.PathCollection at 0x1c3e19819c8>
In [18]:
 
x = data.iloc[:,1:-1]
y = data.iloc[:,-1]
x
Out[18]:
 TVradionewspaper
0 230.1 37.8 69.2
1 44.5 39.3 45.1
2 17.2 45.9 69.3
3 151.5 41.3 58.5
4 180.8 10.8 58.4
... ... ... ...
195 38.2 3.7 13.8
196 94.2 4.9 8.1
197 177.0 9.3 6.4
198 283.6 42.0 66.2
199 232.1 8.6 8.7

200 rows × 3 columns

In [19]:
 
 
y
Out[19]:
0      22.1
1      10.4
2       9.3
3      18.5
4      12.9
       ... 
195     7.6
196     9.7
197    12.8
198    25.5
199    13.4
Name: sales, Length: 200, dtype: float64
In [20]:
 
# 多层感知器
# 隐藏层,10层
# 3表示输出的数据有三维,(TV,radio,newspaper)
# 第一层(输入层) tf.keras.layers.Dense(10,input_shape=(3,))
# 输出层 tf.keras.layers.Dense(1)
model = tf.keras.Sequential([tf.keras.layers.Dense(10,input_shape=(3,),activation='relu'),
                            tf.keras.layers.Dense(1)])
# 运行模型
model.summary()
Model: "sequential_3"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_1 (Dense)              (None, 10)                40        
_________________________________________________________________
dense_2 (Dense)              (None, 1)                 11        
=================================================================
Total params: 51
Trainable params: 51
Non-trainable params: 0
_________________________________________________________________

dense_1 (Dense) (None, 10) 中间层,10个神经元,

为什么 dense_1 的参数是40个

因为输入是3个(TV,radio,newspaper),每一个和中间的每个神经元都是有参数,然后有一个bias,所以是(3+1)*10

为什么 dense_2 的参数是11个

中间层10个神经元的 wi,再加一个bias
In [21]:
 
 
# 配置优化函数和损失器
model.compile(optimizer='adam',loss='mse')
In [22]:
 
 
# 开始训练
history = model.fit(x,y,epochs=500) #epochs表示训练的次数
Epoch 1/500
7/7 [==============================] - 0s 575us/step - loss: 8468.7900
Epoch 2/500
7/7 [==============================] - 0s 712us/step - loss: 7525.4370
......
Epoch 499/500
7/7 [==============================] - 0s 716us/step - loss: 2.4758
Epoch 500/500
7/7 [==============================] - 0s 712us/step - loss: 2.4634
In [24]:
 
 
# 看一下模型的预测能力
# 前10行,中间的3列,也就是那三列输入
test = data.iloc[:10,1:-1]
model.predict(test)
Out[24]:
array([[21.8337   ],
       [10.60583  ],
       [ 8.626378 ],
       [18.846931 ],
       [13.088361 ],
       [ 8.012542 ],
       [11.895675 ],
       [11.442116 ],
       [ 1.4440132],
       [10.994094 ]], dtype=float32)
In [25]:
 
test = data.iloc[:10,-1]
test
Out[25]:
0    22.1
1    10.4
2     9.3
3    18.5
4    12.9
5     7.2
6    11.8
7    13.2
8     4.8
9    10.6
Name: sales, dtype: float64

 

 

 
posted @ 2020-07-20 23:52  范仁义  阅读(290)  评论(0编辑  收藏  举报