tensorflow2.0学习笔记(一)

一、线性回归

1、库:tensorflow,pandas,matplotlib.pyplot

2、其他函数:data = pd.read_csv('路径') 读取csv格式文件

     data.head() 读取前五行

     plt.scatter(data.Education,data.Income)#绘制散点图

3、搭建模型:

序列式模型:keras.Sequential()

定义模型

model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(1,input_shape=(1,))) #全连接层,表示ax+b

model.summary()#显示模型结构

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
dense (Dense)                (None, 1)                 2         
=================================================================
Total params: 2
Trainable params: 2
Non-trainable params: 0
_________________________________________________________________

模型编译,定义优化器和损失函数

model.compile(optimizer='adam',
             loss='mse'
             )#编译

模型训练,数据输入,设置训练轮数

history = model.fit(x,y,epochs=5000)

模型预测

model.predict(x)
model.predict(pd.Series([20]))

二、多层感知器

1、目的:解决xor异或问题,加强模型的拟合能力

2、库:

import tensofow as tf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

3、处理数据函数

data = pd.read_csv('F:/python_file/tf2exc/Advertising.csv')
x=data.iloc[:,1:-1]#第二列-倒数第二列
y = data.iloc[:,-1]#倒数第一列

4、模型搭建

序列式模型 keras.Sequential()

定义模型

model=tf.keras.Sequential(
[tf.keras.layers.Dense(10,input_shape=(3,),activation='relu'),    #第一层需要声明shape
 tf.keras.layers.Dense(1)]
)

编译模型

model.compile(optimizer='adam',
             loss='mse')

训练模型

model.fit(x,y,epochs=500)

预测模型

test = data.iloc[:10,1:-1]
model.predict(test)

三、逻辑回归(分类)

1、库

import tensofow as tf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

2、模型搭建

序列式模型 keras.Sequential()

定义模型

model=tf.keras.Sequential()
model.add(tf.keras.layers.Dense(10,input_shape=(15,),activation='relu'))
model.add(tf.keras.layers.Dense(5,activation='relu'))
model.add(tf.keras.layers.Dense(1,activation='sigmoid')

编译模型

model.compile(optimizer='adam',
             loss='binary_crossentropy',
             metrics=['acc'])

训练模型

history = model.fit(x,y,epochs=500)

查看history键名

history.history.keys()

可视化loss,acc

plt.plot(history.epoch,history.history.get('loss'))
plt.plot(history.epoch,history.history.get('acc'))

模型预测

model.predict(x)

四、softmax多分类

1、库:

import tensofow as tf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

2、处理数据函数

(train_image,train_label),(test_image,test_label) = tf.keras.datasets.fashion_mnist.load_data()
train_image.shape#训练图像形状
train_label #展示训练标签
test_image.shape,test_label.shape #测试集形状
plt.imshow(train_image[0]#展示第一张图
#数据归一化
train_image = train_image/255
test_image = test_image/255
#one-hot编码label
train_label_onehot = tf.keras.utils.to_categorical(train_label)
test_label_onehot = tf.keras.utils.to_categorical(test_label)

3、模型搭建

序列式模型 keras.Sequential()

定义模型

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(96,activation='relu'))
model.add(tf.keras.layers.Dense(48,activation='relu'))
model.add(tf.keras.layers.Dense(10,activation='softmax'))

编译模型

model.compile(optimizer='adam',
             loss='sparse_categorical_crossentropy',#label用数字编码
             metrics=['acc'])

model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),
             loss='categorical_crossentropy',#label用onehot编码,即有几类,就用一维几列的向量表示,5类中第一类,label表示为[1,0,0,0,0]
             metrics=['acc'])

训练模型

history=model.fit(train_image,train_label_onehot,epochs=10,validation_data=(test_image,test_label_onehot))

 

预测模型

predict = model.predict(test_image)
np.argmax(predict[0])#取出第一张图片预测值最大值的索引,即预测为第几

可视化训练过程

history.history.keys()
#dict_keys(['loss', 'acc', 'val_loss', 'val_acc'])
#绘制训练损失和验证损失的折线图
plt.plot(history.epoch, history.history.get('loss'),label='loss')
plt.plot(history.epoch, history.history.get('val_loss'),label='val_loss')
plt.legend()
#绘制训练精度和验证精度的折线图
plt.plot(history.epoch, history.history.get('acc'),label='acc')
plt.plot(history.epoch, history.history.get('val_acc'),label='val_acc')
plt.legend()

简单数据用简单网络,往往学习到更好的关键特征

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

model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.001),
             loss='categorical_crossentropy',
             metrics=['acc'])#label用onehot编码


history=model.fit(train_image,train_label_onehot,epochs=10,validation_data=(test_image,test_label_onehot))


plt.plot(history.epoch, history.history.get('acc'),label='acc')
plt.plot(history.epoch, history.history.get('val_acc'),label='val_acc')
plt.legend()

 

五、函数式api

库:

import tensorflow as tf
import pandas as pd
import numpy as np
from tensorflow import keras
import matplotlib.pyplot as plt
%matplotlib inline

数据处理

fashion_mnist = keras.datasets.fashion_mnist
(train_image,train_label),(test_image,test_label) = fashion_mnist.load_data()

train_image = train_image/255.0
test_image = test_image/255.0

搭建模型

定义层,输入、输出加入Model

input1 = keras.Input(shape=(28,28))
input2 = keras.Input(shape=(28,28))
x1 = keras.layers.Flatten()(input1)
x2 = keras.layers.Flatten()(input2)
x = keras.layers.concatenate([x1,x2])
x = keras.layers.Dense(32,activation='relu')(x)
x = keras.layers.Dropout(0.5)(x)
x = keras.layers.Dense(64,activation='relu')(x)
output = keras.layers.Dense(1,activation='sigmoid')(x)
model = keras.Model(inputs=[input1,input2],outputs=output)

model.summary()


Model: "model_3"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_4 (InputLayer)            [(None, 28, 28)]     0                                            
__________________________________________________________________________________________________
input_5 (InputLayer)            [(None, 28, 28)]     0                                            
__________________________________________________________________________________________________
flatten_4 (Flatten)             (None, 784)          0           input_4[0][0]                    
__________________________________________________________________________________________________
flatten_5 (Flatten)             (None, 784)          0           input_5[0][0]                    
__________________________________________________________________________________________________
concatenate (Concatenate)       (None, 1568)         0           flatten_4[0][0]                  
                                                                 flatten_5[0][0]                  
__________________________________________________________________________________________________
dense_6 (Dense)                 (None, 32)           50208       concatenate[0][0]                
__________________________________________________________________________________________________
dropout_2 (Dropout)             (None, 32)           0           dense_6[0][0]                    
__________________________________________________________________________________________________
dense_7 (Dense)                 (None, 64)           2112        dropout_2[0][0]                  
__________________________________________________________________________________________________
dense_8 (Dense)                 (None, 1)            65          dense_7[0][0]                    
==================================================================================================
Total params: 52,385
Trainable params: 52,385
Non-trainable params: 0

 

 

 

 

总结:

网络容量:与网络可训练参数量成正比

网络中神经单元数越多,层数越多,神经网络的拟合能力越强。

但是训练难度越大,训练速度越慢,越容易产生过拟合。

超参数:需要人为选择,不是通过优化算法优化的参数,如,中间层的神经元个数,学习率

如何提高网络的拟合能力?

增大网络容量:

1、增加层,大大提高网络的拟合能力

2、增加隐藏神经元个数,提高不是很明显

过拟合:训练精度很高,测试精度很低。

解决方法:

dropout,随机丢弃神经网络的层中的节点

1、取平均的作用:相当于相同数据训练几个不同的神经网络,得到的结果取平均或多数取胜的投票策略

2、减少神经元之间复杂的共适应关系

正则化:l1,l2

增加数据集:图像增强

减小网络规模:减少层

欠拟合:训练精度和测试精度都低。单层神经元个数不能太小,会造成信息瓶颈

 

构建网络的总原则

1、增大网络容量,直到过拟合

2、采取措施抑制过拟合

3、继续增大网络容量,直到过拟合

 

TensorFlow2.0构建模型方式:

1、keras.Sequential()

2、函数式:先定义层在加入到网络中,keras.Model(inputs=[],outputs=[])

3、子类式




posted @ 2020-07-01 11:20  X18301096  阅读(1401)  评论(0编辑  收藏  举报