這邊介紹先前課堂遇過的例子, 會常用到的tensorflow API
import tf.keras
#建立Sequential model並訓練
#Sequential model是一層接一層往後串接
#第一層都需要指定input_shape
#
#建立單層model
model = keras.Sequential([keras.layers.Dense(units=1, input_shape=[1])])
#建立多層model,
model = keras.Sequential([keras.layers.Dense(256, input_shape=[1]),
keras.layers.Dense(128, activation=tf.nn.relu),
keras.layers.Dense(64, activation=tf.nn.relu),
keras.layers.Dense(10, activation=tf.nn.softmax)])
#指定optimizer, loss function
model.compile(optimizer='sgd', loss='mean_squared_error')
#開始訓練
model.fit(xs, ys, epochs=500)
callback可以參考 https://www.tensorflow.org/api_docs/python/tf/keras/callbacks/Callback
這裡主要用到的只有on_epoch_end
根據logs.get("相關參數")來獲取想要參考的資訊
#建立callback, 在訓練的時候, 觀察內部統計狀態, 做對應的動作
#可以使用keys = list(logs.keys())來獲得logs裡面有甚麼
#主要會使用accuracy跟loss
class myCallback(tf.keras.callbacks.Callback):
def on_epoch_end(self, epoch, logs=None):
if (logs.get("loss")<0.4):
print("\nLoss is low so cancelling training!")
self.model.stop_training = True
callbacks = myCallback()
#history記錄著logs裡面的東西
history = model.fit(training_images, training_labels, epochs=500, callbacks=[callbacks])
load mnist data
mnist = tf.keras.datasets.fashion_mnist
(training_images, training_labels), (test_images, test_labels) = mnist.load_data()
DNN on image input
#第一層先把image展開
model = keras.Sequential([keras.layers.Flatten(input_shape=(28, 28)),
keras.layers.Dense(128, activation=tf.nn.relu),
keras.layers.Dense(64, activation=tf.nn.relu),
keras.layers.Dense(10, activation=tf.nn.softmax)])
compile
#tf.optimizers裡面可以選多種的optimizer
#loss如果是分類問題, 且答案是給1,2,3,4,5,6...... 沒有經過one-hot encoded,
#就要選擇sparse_categorical_crossentropy
model.compile(optimizer=tf.optimizers.Adam(),
loss='sparse_categorical_crossentropy')
#如果有做one-hot encoded, 改成categorical_crossentropy
model.compile(optimizer=tf.optimizers.Adam(),
loss='categorical_crossentropy')
#如果是回歸問題, 就使用mse就可以
model.compile(optimizer='sgd', loss='mean_squared_error')
CNN Model on image input
#Conv2D需要指定filter number, kernel size
#最後要接到Dense之前要記得Flatten
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(64, (3, 3), activation="relu", input_shape=(28, 28, 1)),
tf.keras.layers.MaxPool2D(2, 2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(64, activation=tf.nn.relu),
tf.keras.layers.Dense(10, activation=tf.nn.softmax)
# YOUR CODE ENDS HERE
])
另外一種建立model的方法, 以及把中間層output的方法
#透過Model的方式輸入inputs, outputs來定義model
#下面是把先前定義model每一層output, 放在outputs
#這樣predict的時候每一層都會output出來
layer_outputs = [layer.output for layer in model.layers]
activation_model = tf.keras.models.Model(inputs=model.input, outputs=layer_outputs)
二元分類問題, 可以使用sigmoid來定義最後一層
#最後一層使用sigmoid
#compile的loss可以使用binary_crossentropy
#在optimizer裡面可以定義learning rate
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(16, (3,3), activation='relu', input_shape=(150, 150, 3)),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Conv2D(32, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(512, activation='relu'),
tf.keras.layers.Dense(1, activation='sigmoid')
])
model.compile(loss='binary_crossentropy',
optimizer=RMSprop(lr=0.001),
metrics=['accuracy'])
接下來算是重頭戲, ImageDataGenerator
https://keras.io/zh/preprocessing/image/
裡面可以定義argumentation
from tensorflow.keras.preprocessing.image import ImageDataGenerator
#先透過ImageDataGenerator建立datagen物件
#在ImageDataGenerator裡面可以定義image argumentation
#最簡單可以定義rescale
train_datagen = ImageDataGenerator(rescale=1/255)
#直接fit已經在RAM上的buffer
datagen.fit(x_train)
model.fit_generator(datagen.flow(x_train, y_train, batch_size=32),
steps_per_epoch=len(x_train) / 32, epochs=epochs)
# Flow training images in batches of 128 using train_datagen generator
#flow_from_directory從資料夾裡面拉資料出來
#資料夾必須要定義好
# [main_folder] / training
# / validation
# / cat
# / dog
#並且可以定義resize跟batch_size
#但要注意batch_size * steps_per_epoch要=total data size
train_generator = train_datagen.flow_from_directory (
train_dir, # This is the source directory for training images
target_size=(150, 150), # All images will be resized to 150x150
batch_size=128,
# Since we use binary_crossentropy loss, we need binary labels
class_mode='binary')
history = model.fit(
train_generator,
steps_per_epoch=80,
epochs=15,
verbose=1)
load pretrained model
from tensorflow.keras.applications.inception_v3 import InceptionV3
path_inception = "inception_v3_weights_tf_dim_ordering_tf_kernels_notop.h5"
local_weights_file = path_inception
pre_trained_model = InceptionV3(input_shape=(150,150,3), include_top=False)# Your Code Here
pre_trained_model.load_weights(local_weights_file)
#這樣就可以關掉不要訓練該layer
for layer in pre_trained_model.layers:
layer.trainable = False
# Print the model summary
#可以看出model的架構
pre_trained_model.summary()
#從model裡面抓出想要的那一層
last_layer = pre_trained_model.get_layer("mixed7")# Your Code Here)
print('last layer output shape: ', last_layer.output_shape)
last_output = last_layer.output# Your Code Here
從last_output繼續往下接
# Flatten the output layer to 1 dimension
x = layers.Flatten()(last_output)
# Add a fully connected layer with 1,024 hidden units and ReLU activation
x = layers.Dense(1024, activation='relu')(x)# Your Code Here)(x)
# Add a dropout rate of 0.2
x = layers.Dropout(0.2)(x)# Your Code Here)(x)
# Add a final sigmoid layer for classification
x = layers.Dense (1, activation='sigmoid')(x)# Your Code Here)(x)
#最後組起model需要把input放回來
model = Model(pre_trained_model.input, x)
imdb, tfds load data
#info 就是紀錄imdb data的一些資訊, 例如training data size, test data size
imdb, info = tfds.load("imdb_reviews", with_info=True, as_supervised=True)
train_data, test_data = imdb['train'], imdb['test']
training_sentences = []
training_labels = []
testing_sentences = []
testing_labels = []
#s,l都是儲存成tf.Tensor
#要把data取出來就用.numpy()
for s,l in train_data:
training_sentences.append(str(s.numpy()))
training_labels.append(l.numpy())
for s, l in test_data:
testing_sentences.append(str(s.numpy()))
testing_labels.append(l.numpy())
#list再轉成numpy
training_labels_final = np.array(training_labels)
testing_labels_final = np.array(testing_labels)
發現後面還有一大段sequences的API要介紹, 這邊先在這裡做個結尾
下一篇會介紹關於series以及文本相關的API