tebsorflow2.0 深度学习基础和tf.keras

时间:2021-10-30 01:24:47


1. tf.keras实现线性回归(1)

1.1 Income数据导入可视化

import tensorflow as tf
import pandas as pd
import matplotlib.pylab as plt
print('Tensorflow Version: {}'.format(tf.__version__))
data = pd.read_csv(r'C:\Users\lihuanyu\Desktop\数据集\Income1.csv')
plt.scatter(data.Education, data.Income)
plt.show()
data.head()

tebsorflow2.0 深度学习基础和tf.keras

Tensorflow Version: 2.0.0



Unnamed: 0

Education

Income

0

1

10.000000

26.658839

1

2

10.401338

27.306435

2

3

10.842809

22.132410

3

4

11.244147

21.169841

4

5

11.645485

15.192634

1.2 构建模型

data1 =  data.to_numpy()
x = data1[:,1]
y = data1[:,2]

model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(1,input_shape=(1,)))
#查看模型
model.summary()
model.compile(optimizer='adam',loss="mse")
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense (Dense)                (None, 1)                 2         
=================================================================
Total params: 2
Trainable params: 2
Non-trainable params: 0
_________________________________________________________________

1.3 训练

history = model.fit(x,y,epochs=10000)
plt.scatter(x,model.predict(x))
plt.scatter(data.Education, data.Income)
plt.show()

tebsorflow2.0 深度学习基础和tf.keras

2. 多层感知机

2.1 概述

单层神经元的缺陷是神经元要求数据必须是线性可分的,而异或问题无法找到一条直线分割两个类。

为了继续使用神经网络解决这种不具备线性可分性的问题,采取在神经网络的输入端和输出端之间插入更多的神经元。

tebsorflow2.0 深度学习基础和tf.keras

2.2 数据准备

import tensorflow as tf
print('Tensorflow Version: {}'.format(tf.__version__))

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
data = pd.read_csv(r'C:\Users\lihuanyu\Desktop\数据集\Advertising.csv')   
data1 = data.to_numpy()
data.head()
Tensorflow Version: 2.0.0



Unnamed: 0

TV

radio

newspaper

sales

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

2.3 观察数据分布

plt.scatter(data.TV, data.sales)
plt.show()

tebsorflow2.0 深度学习基础和tf.keras

plt.scatter(data.radio, data.sales)
plt.show()

tebsorflow2.0 深度学习基础和tf.keras

plt.scatter(data.newspaper, data.sales)
plt.show()

tebsorflow2.0 深度学习基础和tf.keras

2.4 建立模型及结果分析

x = data.iloc[:,1:-1].to_numpy()
y = data.iloc[:,-1].to_numpy()
model = tf.keras.Sequential([tf.keras.layers.Dense(10,input_shape=(3,),activation="relu"),
                             tf.keras.layers.Dense(1)]
                           )
model.compile(optimizer="adam",loss="mse")
model.summary()
model.fit(x,y,epochs=100)
Model: "sequential_5"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense_10 (Dense)             (None, 10)                40        
_________________________________________________________________
dense_11 (Dense)             (None, 1)                 11        
=================================================================
Total params: 51
Trainable params: 51
Non-trainable params: 0
_________________________________________________________________
Train on 200 samples
Epoch 1/100
200/200 [==============================] - 0s 2ms/sample - loss: 759.2462
.......省略
200/200 [==============================] - 0s 122us/sample - loss: 2.1529
Epoch 97/100
200/200 [==============================] - 0s 130us/sample - loss: 2.1491
Epoch 98/100
200/200 [==============================] - 0s 100us/sample - loss: 2.1935
Epoch 99/100
200/200 [==============================] - 0s 170us/sample - loss: 2.1596
Epoch 100/100
200/200 [==============================] - 0s 145us/sample - loss: 2.1212
print(model.predict(x[0:5,:]),"\n",y[0:5].reshape(-1,1))
[[21.708485 ]
 [11.457169 ]
 [ 9.0395775]
 [18.80669  ]
 [12.999142 ]] 
 [[22.1]
 [10.4]
 [ 9.3]
 [18.5]
 [12.9]]
# 3 . softmax多分类任务

3.1 任务概述

采用全连接神经网络,实现对Fashion MNIST的分类。Fashion MNIST 数据集包含 70000 张灰度图像,涵盖 10个类别。我们将使用 60000 张图像训练网络,并使用 10000 张图像评估经过学习的网络分类图像的准确率。可以从 TensorFlow 直接访问 Fashion MNIST,只需导入和加载数据即可。

3.2 数据准备

import tensorflow as tf
print('Tensorflow Version: {}'.format(tf.__version__))

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
#图像,标签,测试图像与标签
(train_image, train_lable), (test_image, test_label) = tf.keras.datasets.fashion_mnist.load_data()
print("train_image shappe:{}\ntest_image shape:{}".format(train_image.shape,test_image.shape))
plt.imshow(train_image[0])
plt.axis("off")
plt.show()
#归一化
train_image = train_image/255
test_image = test_image/255
Tensorflow Version: 2.0.0
train_image shappe:(60000, 28, 28)
test_image shape:(10000, 28, 28)

tebsorflow2.0 深度学习基础和tf.keras

3.3 建立模型并训练

model = tf.keras.Sequential()
model.add(tf.keras.layers.Flatten(input_shape=(28,28)))
model.add(tf.keras.layers.Dense(128,activation="relu"))
model.add(tf.keras.layers.Dense(10,activation="softmax"))
model.summary()
model.compile(optimizer = "adam",loss='sparse_categorical_crossentropy',metrics=['acc'])
model.fit(train_image,train_lable,epochs=5)
Model: "sequential_2"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
flatten_2 (Flatten)          (None, 784)               0         
_________________________________________________________________
dense_4 (Dense)              (None, 128)               100480    
_________________________________________________________________
dense_5 (Dense)              (None, 10)                1290      
=================================================================
Total params: 101,770
Trainable params: 101,770
Non-trainable params: 0
_________________________________________________________________
Train on 60000 samples
Epoch 1/5
60000/60000 [==============================] - 8s 138us/sample - loss: 3.1367 - acc: 0.6865
Epoch 2/5
60000/60000 [==============================] - 8s 130us/sample - loss: 0.6911 - acc: 0.7243
Epoch 3/5
60000/60000 [==============================] - 7s 114us/sample - loss: 0.6168 - acc: 0.7527
Epoch 4/5
60000/60000 [==============================] - 8s 128us/sample - loss: 0.5736 - acc: 0.7814
Epoch 5/5
60000/60000 [==============================] - 7s 117us/sample - loss: 0.5319 - acc: 0.8114
#评价
model.evaluate(test_image, test_label)
10000/1  - 1s 69us/sample - loss: 0.3796 - acc: 0.8244
[0.5249296184539795, 0.8244]

3.5 采用one-hot编码方式训练

train_label_onehot = tf.keras.utils.to_categorical(train_lable)
test_label_onehot = tf.keras.utils.to_categorical(test_label)
model = tf.keras.Sequential()
model.add(tf.keras.layers.Flatten(input_shape=(28,28)))  # 28*28
model.add(tf.keras.layers.Dense(128, activation='relu'))
model.add(tf.keras.layers.Dense(10, activation='softmax'))
model.summary()
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),
              loss='categorical_crossentropy',
              metrics=['acc']
)
model.fit(train_image, train_label_onehot, epochs=5)
Model: "sequential_3"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
flatten_3 (Flatten)          (None, 784)               0         
_________________________________________________________________
dense_6 (Dense)              (None, 128)               100480    
_________________________________________________________________
dense_7 (Dense)              (None, 10)                1290      
=================================================================
Total params: 101,770
Trainable params: 101,770
Non-trainable params: 0
_________________________________________________________________
Train on 60000 samples
Epoch 1/5
60000/60000 [==============================] - 9s 148us/sample - loss: 3.9736 - acc: 0.7085
Epoch 2/5
60000/60000 [==============================] - 7s 122us/sample - loss: 0.6800 - acc: 0.7587
Epoch 3/5
60000/60000 [==============================] - 8s 135us/sample - loss: 0.6008 - acc: 0.7850
Epoch 4/5
60000/60000 [==============================] - 7s 119us/sample - loss: 0.5508 - acc: 0.8023
Epoch 5/5
60000/60000 [==============================] - 8s 125us/sample - loss: 0.5175 - acc: 0.8154





<tensorflow.python.keras.callbacks.History at 0x2fe4bf91388>
#评价
model.evaluate(test_image, test_label_onehot)

1s 72us/sample - loss: 0.3707 - acc: 0.8031
[0.5642493257284165, 0.8031]

3.6 多添加几层隐藏层

model = tf.keras.Sequential()
model.add(tf.keras.layers.Flatten(input_shape=(28,28)))  # 28*28
model.add(tf.keras.layers.Dense(128, activation='relu'))
model.add(tf.keras.layers.Dense(128, activation='relu'))
model.add(tf.keras.layers.Dense(128, activation='relu'))
model.add(tf.keras.layers.Dense(10, activation='softmax'))
model.summary()
Model: "sequential_2"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
flatten_2 (Flatten)          (None, 784)               0         
_________________________________________________________________
dense_4 (Dense)              (None, 128)               100480    
_________________________________________________________________
dense_5 (Dense)              (None, 128)               16512     
_________________________________________________________________
dense_6 (Dense)              (None, 128)               16512     
_________________________________________________________________
dense_7 (Dense)              (None, 10)                1290      
=================================================================
Total params: 134,794
Trainable params: 134,794
Non-trainable params: 0
_________________________________________________________________
model.compile(optimizer="adam",
        loss='categorical_crossentropy',
        metrics=["acc"])
        #加入验证集
history = model.fit(train_image,train_label_onehot,epochs=20,validation_data=(test_image,test_label_onehot))
plt.plot(history.epoch,history.history.get("acc"),label="train_acc")
plt.plot(history.epoch,history.history.get("val_acc"),label="val_acc")
plt.legend()
plt.show()

tebsorflow2.0 深度学习基础和tf.keras


从图中,我们可训练集的准确率,在不断上升,而验证集的数据没有上升,甚至还有些下降,这是典型的过拟合状态,常见的,我们可以采用丢弃法或者减少层数来抑制过拟合状态。

3.7 添加dropout

model = tf.keras.Sequential()
model.add(tf.keras.layers.Flatten(input_shape=(28,28)))  # 28*28
model.add(tf.keras.layers.Dense(128, activation='relu'))
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.Dense(128, activation='relu'))
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.Dense(128, activation='relu'))
model.add(tf.keras.layers.Dropout(0.5))
model.add(tf.keras.layers.Dense(10, activation='softmax'))

model.compile(optimizer="adam",
        loss='categorical_crossentropy',
        metrics=["acc"])
#加入验证集
history = model.fit(train_image,train_label_onehot,epochs=20,validation_data=(test_image,test_label_onehot))

plt.plot(history.epoch,history.history.get("acc"),label="train_acc")
plt.plot(history.epoch,history.history.get("val_acc"),label="val_acc")
plt.legend()
plt.show()

tebsorflow2.0 深度学习基础和tf.keras


有上图我们可以发现,验证集的准确度比训练集的准确度高,所以dropout具有抑制过拟合的效果。