课程地址:https://www.icourse163.org/learn/youdao-1460578162?tid=1461280442#/learn/content?type=detail&id=1239072224&cid=1259990170&replay=true
tensorflow简介
一个神经元的网络
# #!/usr/bin/env python
# # -*- coding:utf-8 -*-
import tensorflow as tf
#绘图
import seaborn as sns
#数值计算
import numpy as np
from tensorflow import keras
#建立模型
model = keras.Sequential([keras.layers.Dense(units=1,input_shape=[1])])
#创建损失函数
model.compile(optimizer='sgd',loss='mean_squared_error')
#添加数据
xs = np.array([-1.0,0.0,1.0,2.0,3.0,4.0],dtype=float)
ys = np.array([-3.0,-1.0,1.0,3.0,5.0,7.0],dtype=float)
#训练数据
model.fit(xs,ys,epochs=500)
#输出预测值
print(model.predict([10.0]))
计算机视觉
用Fashion MNIST进行简单的分类
# #!/usr/bin/env python
# # -*- coding:utf-8 -*-
import tensorflow as tf
import numpy as np
from tensorflow import keras
import matplotlib.pyplot as plt
#创建和加载数据
#当满足一定条件时自动终止训练
class myCallback(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs={}):
if(logs.get('loss')>0.4): #当loss小于0.4时停止训练,在一定时间后会停止训练
print("\nLoss is low so cancelling training!")
self.model.stop_training=True
#实例化方法
callbacks = myCallback()
fashion_mnist = keras.datasets.fashion_mnist
(train_images,train_labels),(test_images,test_labels) = fashion_mnist.load_data()
# print(train_images.shape)
# print(train_images[0])
# print(train_labels.shape)
# print(train_labels[:5])
# plt.imshow(test_images[1])
# plt.show()
#构建全连接的网络结构
model = keras.Sequential()
model.add(keras.layers.Flatten(input_shape=(28,28)))
model.add(keras.layers.Dense(128,activation=tf.nn.relu))
model.add(keras.layers.Dense(10,activation=tf.nn.softmax))
#一共有((784+1)*128个参数) +1是因为每一层都有一个bias
#查看网络结构 print(model.summary())
#print(model.summary())
#训练和评估网络
#制定优化和损失函数
train_images_scaled=train_images/255 #normalizition 归一化可以提高准确率
model.compile(optimizer=tf.optimizers.Adam(),loss=tf.losses.sparse_categorical_crossentropy,metrics=['accuracy'])
model.fit(train_images_scaled,train_labels,epochs=5,callbacks=[callbacks])
#用测试集评估网络
test_images_scaled = test_images/255
print(model.evaluate(test_images_scaled,test_labels))
#预测单张图片并输出结果
print(np.argmax(model.predict([[test_images[1]/255]])))
print(test_labels[1])
plt.imshow(test_images[1])
plt.show()
卷积简介
卷积神经网络
Max Pooling后可以增强特征,减少数据
卷积网络结构
# #!/usr/bin/env python
# # -*- coding:utf-8 -*-
import tensorflow as tf
import numpy as np
from tensorflow import keras
import matplotlib.pyplot as plt
#创建和加载数据
#当满足一定条件时自动终止训练
class myCallback(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs={}):
if(logs.get('accuracy')>0.8): #当loss小于0.4时停止训练,在一定时间后会停止训练
print("\nLoss is low so cancelling training!")
self.model.stop_training=True
#实例化方法
callbacks = myCallback()
fashion_mnist = keras.datasets.fashion_mnist
(train_images,train_labels),(test_images,test_labels) = fashion_mnist.load_data()
#构建卷积神经网络结构
model = keras.Sequential()
model.add(keras.layers.Conv2D(64,(3,3),activation='relu',input_shape=(28,28,1)))
model.add(keras.layers.MaxPool2D(2,2))
model.add(keras.layers.Conv2D(64,(3,3),activation='relu'))
model.add(keras.layers.MaxPool2D(2,2))
model.add(keras.layers.Flatten())
model.add(keras.layers.Dense(128,activation=tf.nn.relu))
model.add(keras.layers.Dense(10,activation=tf.nn.softmax))
#训练和评估网络
#制定优化和损失函数
train_images_scaled=train_images/255 #normalizition 归一化可以提高准确率
model.compile(optimizer=tf.optimizers.Adam(),loss=tf.losses.sparse_categorical_crossentropy,metrics=['accuracy'])
model.fit(train_images_scaled.reshape(-1,28,28,1),train_labels,epochs=5,callbacks=[callbacks])
#预测和显示图片
layer_outputs = [layer.output for layer in model.layers]
activation_model = tf.keras.models.Model(inputs = model.input,outputs=layer_outputs)
pred = activation_model.predict(test_images[0].reshape(1,28,28,1))
#pred[1] 表示某一层的图像 [0,:,:,1]表示某一个过滤器的图像
plt.imshow(pred[1][0,:,:,1])
plt.show()
更复杂的图像应用:人马分类识别
ImageDataGenerator
构建并训练模型
# #!/usr/bin/env python
# # -*- coding:utf-8 -*-
import tensorflow as tf
import numpy as np
from tensorflow import keras
import matplotlib.pyplot as plt
from tensorflow.keras.optimizers import RMSprop
from tensorflow.keras.preprocessing.image import ImageDataGenerator
#创建两个数据生成器,指定scaling范围0-1
train_datagen = ImageDataGenerator(rescale=1/255)
validation_datagen = ImageDataGenerator(rescale=1/255)
#指向训练数据文件夹
train_generator = train_datagen.flow_from_directory(
'/tmp/horse-or-human/', #训练数据所在文件夹
target_size=(300,300), #指定输出尺寸
batch_size=32,
class_mode='binary' #指定二分类
)
#指向测试数据文件夹
validation_generator = validation_datagen.flow_from_directory(
'/tmp/validation-horse-or-human/',
target_size=(300,300),
batch_size=32,
class_mode='binary'
)
model = tf.keras.models.Sequential([
#this is the first convolution
tf.keras.layers.Conv2D(16,(3,3),activation='relu',input_shape=(300,300,3)),
tf.keras.layers.MaxPooling2D(2,2),
#the second convolution
tf.keras.layers.Conv2D(32,(3,3),activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
#the third convolution
tf.keras.layers.Conv2D(64,(3,3),activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
#Flatten the results to feed into a DNN
tf.keras.layers.Flatten(),
#512 neuron hidden layer
tf.keras.layers.Dense(512,activation='relu'),
#Only 1 output neuron.it will contain a value from 0-1 where 0 for 1 class ('horse') and 1 for the other ('humans')
tf.keras.layers.Dense(1,activation='sigmoid')
])
model.compile(loss='binary_crossentropy',optimizer=RMSprop(lr=0.001),metrics=['acc'])
history = model.fit(
train_generator,
epochs=15,
verbose=1,
validation_data=validation_generator,
validation_steps=8
)
优化模型参数,提高识别率
不能运行
# #!/usr/bin/env python
# # -*- coding:utf-8 -*-
import tensorflow as tf
import numpy as np
from tensorflow import keras
import matplotlib.pyplot as plt
from tensorflow.keras.optimizers import RMSprop
from tensorflow.keras.preprocessing.image import ImageDataGenerator
#用kerastuner优化模型
from kerastuner.tuners import Hyperband
from kerastuner.engine.hyperparameters import Hyperparameters
#创建两个数据生成器,指定scaling范围0-1
train_datagen = ImageDataGenerator(rescale=1/255)
validation_datagen = ImageDataGenerator(rescale=1/255)
#指向训练数据文件夹
train_generator = train_datagen.flow_from_directory(
'/tmp/horse-or-human/', #训练数据所在文件夹
target_size=(300,300), #指定输出尺寸
batch_size=32,
class_mode='binary' #指定二分类
)
#指向测试数据文件夹
validation_generator = validation_datagen.flow_from_directory(
'/tmp/validation-horse-or-human/',
target_size=(300,300),
batch_size=32,
class_mode='binary'
)
hp = Hyperparameters()
def build_model(hp):
model = tf.keras.models.Sequential()
#this is the first convolution
# num_filters_layer0 参数的变量名
# values 范围
model.add(tf.keras.layers.Conv2D(hp.Choice('num_filters_layer0',values=[16,64],defaule=16),(3,3),activation='relu',input_shape=(300,300,3)))
model.add(tf.keras.layers.MaxPooling2D(2,2))
#the second convolution
for i in range(hp.Int("num_conv_layers",1,3)):
model.add(tf.keras.layers.Conv2D(f'num_filters_layer1',values=[16,64],defaule=16),(3,3),activation='relu'))
model.add(tf.keras.layers.MaxPooling2D(2,2))
#Flatten the results to feed into a DNN
model.add(tf.keras.layers.Flatten())
#512 neuron hidden layer
model.add(tf.keras.layers.Dense(hp.Int("hidden_units",128,512,step=32),activation='relu'))
#Only 1 output neuron.it will contain a value from 0-1 where 0 for 1 class ('horse') and 1 for the other ('humans')
model.add(tf.keras.layers.Dense(1,activation='sigmoid'))
model.compile(loss='binary_crossentropy',optimizer=RMSprop(lr=0.001),metrics=['acc'])
return model
tuner = Hyperband(
build_model,
objective = 'val_acc',
max_epochs = 15,
directory = 'horse_human_params',#参数保存的位置
hyperparameters = hp,
project_name = 'my_horse_human_project'
)
tuner.search(train_generator,epoch=10,validation_data=validation_generator)