欢迎来到TensorFlow入门实操课程的学习
MOOC TensorFlow入门实操课程代码回顾总结(二)
MOOC TensorFlow入门实操课程代码回顾总结(三)
注:
用于表示python代码
粘贴运行结果
%config IPCompleter.greedy=True # TAB键代码自动提示
import tensorflow as tf
print(tf.__version__)
2.3.0
import numpy as np
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation
import tensorflow as tf
# 构建模型
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]))
[[18.976088]]
from tensorflow import keras
fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
train_images.shape
(60000, 28, 28)
test_images.shape
(10000, 28, 28)
train_images[0]
train_labels.shape
(60000,)
train_labels[:5]
array([9, 0, 0, 3, 0], dtype=uint8)
# 查看图片样式
import matplotlib.pyplot as plt
plt.imshow(train_images[0])
# 查看图片样式
import matplotlib.pyplot as plt
plt.imshow(train_images[7])
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))
model.summary()
# (28*28+1)*128=100480
# (128+1)*10=1290
# 加1是有1个bias,但最终输出层没有
train_images = train_images/255 #归一化
model.compile(optimizer=tf.optimizers.Adam(), loss=tf.losses.sparse_categorical_crossentropy, metrics=['accuracy'])
model.fit(train_images, train_labels, epochs=5)
# 训练没有问题。正在对1875批次(每批次32张图像)而不是1875张图像进行模型训练。 1875 * 32 = 60000张图像
test_images_scaled = test_images/255
model.evaluate(test_images_scaled,test_labels)
0 T-shirt/top(体恤) 1 Trouser(裤子) 2 Pullover(套头衫) 3 Dress(连衣裙) 4 Coat(外套) 5 Sandal(凉鞋) 6 Shirt(衬衫) 7 Sneaker(运动鞋) 8 Bag(袋子) 9 Ankle boot(短靴)
print(np.argmax(model.predict((test_images[0]/255).reshape(1,28,28,1))))
print(test_labels[0])
plt.imshow(test_images[0])
class myCallback(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs={
}):
if(logs.get('loss')<0.4):
print("\nLoss is low so cancelling training!")
self.model.stop_training = True
callbacks = myCallback()
mnist = tf.keras.datasets.fashion_mnist
(training_images, training_labels), (test_images, test_labels) = mnist.load_data()
training_images_scaled = training_images/255.0
test_images_scaled = test_images/255.0
model = tf.keras.models.Sequential([tf.keras.layers.Flatten(),tf.keras.layers.Dense(512,activation=tf.nn.relu),tf.keras.layers.Dense(10,activation=tf.nn.softmax)])
model.compile(optimizer='adam',loss='sparse_categorical_crossentropy')
model.fit(training_images_scaled, training_labels, epochs=5, callbacks=[callbacks])
import tensorflow as tf
from tensorflow import keras
# 前提有一个全连接的神经元网络
fashion_mnist = tf.keras.datasets.fashion_mnist
(training_images, training_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))
training_images_scaled = training_images/255
model.compile(optimizer=tf.optimizers.Adam(), loss=tf.losses.sparse_categorical_crossentropy, metrics=['accuracy'])
model.fit(training_images_scaled.reshape(-1,28,28,1), training_labels,epochs=5)
model.summary()
import matplotlib.pyplot as plt
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
plt.imshow(pred[0][0,:,:,1])
plt.imshow(pred[0][0,:,:,2])
!wget --no-check-certificate \ https://storage.googleapis.com/laurencemoroney-blog.appspot.com/validation-horse-or-human.zip -o /tmp/validation-harse-or-human.zip
!wget --no-check-certificate \ https://storage.googleapis.com/laurencemoroney-blog.appspot.com/horse-or-human.zip -o /tmp/harse-or-human.zip
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(
'/home/zzr/data/horse-or-human', # 训练数据所在文件夹
target_size=(300,300), # 指定输出尺寸
batch_size=32,
class_mode='binary') # 指定二分类
# 指向测试数据文件夹
validation_generator = validation_datagen.flow_from_directory(
'/home/zzr/data/validation-horse-or-human', # 测试数据所在文件夹
target_size=(300,300), # 指定输出尺寸
batch_size=32,
class_mode='binary') # 指定二分类
Found 1027 images belonging to 2 classes.
Found 256 images belonging to 2 classes.
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(16,(3,3),activation='relu',input_shape=(300,300,3)),
tf.keras.layers.MaxPool2D(2,2),
tf.keras.layers.Conv2D(32,(3,3),activation='relu'),
tf.keras.layers.MaxPool2D(2,2),
tf.keras.layers.Conv2D(64,(3,3),activation='relu'),
tf.keras.layers.MaxPool2D(2,2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(512,activation='relu'),
tf.keras.layers.Dense(1,activation='sigmoid')
])
model.summary()
# Optimizers优化函数这里使用的是RMS
from tensorflow.keras.optimizers import RMSprop
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)
pip install -U keras-tuner
import os
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(
'/home/zzr/data/horse-or-human', # 训练数据所在文件夹
target_size=(150,150), # 指定输出尺寸
batch_size=32,
class_mode='binary') # 指定二分类
# 指向测试数据文件夹
validation_generator = validation_datagen.flow_from_directory(
'/home/zzr/data/validation-horse-or-human', # 测试数据所在文件夹
target_size=(150,150), # 指定输出尺寸
batch_size=32,
class_mode='binary') # 指定二分类
hp = HyperParameters()
def build_model(hp):
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Conv2D(hp.Choice('num_filters_layer0',values=[16,64],default=16),(3,3),activation='relu',input_shape=(150,150,3)))
model.add(tf.keras.layers.MaxPooling2D(2,2))
for i in range(hp.Int("num_conv_layers",1,3)):
model.add(tf.keras.layers.Conv2D(hp.Choice(f'num_filters_layer{i}',values=[16,64],default=16),(3,3),activation='relu'))
model.add(tf.keras.layers.MaxPooling2D(2,2))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(hp.Int('hidden_units',128,512,step=32),activation='relu'))
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,epochs=10,validation_data=validation_generator)
Trial 30 Complete [00h 02m 53s]
val_acc: 0.88671875Best val_acc So Far: 0.9375 Total elapsed time: 00h 33m 33s
INFO:tensorflow:Oracle triggered exit
best_hps=tuner.get_best_hyperparameters(1)[0]
print(best_hps.values)
model=tuner.hypermodel.build(best_hps)
model.summary()