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()
- 1
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()
2. 多层感知机
2.1 概述
单层神经元的缺陷是神经元要求数据必须是线性可分的,而异或问题无法找到一条直线分割两个类。
为了继续使用神经网络解决这种不具备线性可分性的问题,采取在神经网络的输入端和输出端之间插入更多的神经元。
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() • 1 • 2
plt.scatter(data.radio, data.sales) plt.show() • 1 • 2
plt.scatter(data.newspaper, data.sales) plt.show() • 1 • 2
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,只需导入和加载数据即可。