当前位置:网站首页>Tensorflow package tf.keras module construction and training deep learning model
Tensorflow package tf.keras module construction and training deep learning model
2022-07-27 08:50:00 【qq_ twenty-seven million three hundred and ninety thousand and 】
1. Read in the data
With mnist Take the data , Handwritten numerals (0-9) The problem of classification , Supervised learning .
import tensorflow as tf
import numpy as np
### 1. Read in the data
# mnist Data set as demo data
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
assert x_train.shape == (60000, 28, 28)
assert x_test.shape == (10000, 28, 28)
assert y_train.shape == (60000,)
assert y_test.shape == (10000,)
# print(x_train[1]) # First picture data
#print(y_train)
# Add a dimension : Number of pictures , That's ok , Column , Layers
x_train=np.expand_dims(x_train,axis = 3)
x_test=np.expand_dims(x_test,axis = 3)
input_shape = x_train.shape[1:] # Remove the highest dimension !!!
print(input_shape)
#input_shape = tf.keras.layers.Flatten(input_shape=(28,28))
# data re-scale To 0~1.0 Between
x_train = x_train/255.
x_test = x_test/255.
# print(x_train[1])
# y Value is converted to one-hot code
def one_hot(labels):
onehot_labels=np.zeros(shape=[len(labels),10])
for i in range(len(labels)):
index=labels[i]
onehot_labels[i][index]=1
return onehot_labels
y_train = one_hot(y_train)
y_test = one_hot(y_test)
print(y_train[0])2. Build a model through the calculation process
### 2. Build a model through the calculation process
inputs = tf.keras.Input(shape=input_shape) # Returns a placeholder tensor
x = tf.keras.layers.Flatten()(inputs)
# A layer instance is callable on a tensor, and returns a tensor.
x = tf.keras.layers.Dense(128, activation='relu')(x)
predictions = tf.keras.layers.Dense(10, activation='softmax')(x)
# Instantiate the model given inputs and outputs.
model = tf.keras.Model(inputs=inputs, outputs=predictions)
model.compile(optimizer=tf.optimizers.Adam(learning_rate=0.001),
loss="categorical_crossentropy",
metrics=['accuracy'])
print("model fit")
model.fit(x=x_train,y=y_train,batch_size=128, epochs=10)
print("model prediction")
test_loss,test_acc=model.evaluate(x=x_test,y=y_test)
print("Test Accuracy %.2f"%test_acc)3. adopt Sequential Model
### 3. adopt Sequential Model
## Super parameter settings
#FILTERS_1 = 32
#KERNEL_SIZE_1 = 5
#STRIDES_1 = (1,1)
#FILTERS_2 = 64
#KERNEL_SIZE_2 = 4
#STRIDES_2 = (2,2)
#POOL_SIZE = (2,2)
#DROPOUT_RATE = 0.25
#DENSE_UNIT_NUM_1 = 128
#DENSE_UNIT_NUM_2 = 10
#BATCH_SIZE = 128
#EPOCHS_NUM=100
## Full connection
def mnist_net(input_shape):
model = tf.keras.Sequential()
model.add(tf.keras.layers.Flatten(input_shape=input_shape)) # Output layer
model.add(tf.keras.layers.Dense(units=128, activation=tf.nn.relu)) # Hidden layer
model.add(tf.keras.layers.Dense(units=10, activation=tf.nn.softmax)) # Output layer
return model
## Convolutional neural networks
def mnist_cnn(input_shape):
model = tf.keras.Sequential()
model.add(tf.keras.layers.Conv2D(filters=32,kernel_size = 5,strides = (1,1),
padding = 'same',activation = tf.nn.relu,input_shape = input_shape))
# kernel_size = (5,5)
model.add(tf.keras.layers.MaxPool2D(pool_size=(2,2), strides = (2,2), padding = 'valid'))
model.add(tf.keras.layers.Conv2D(filters=64,kernel_size = 3,strides = (1,1),padding = 'same',activation = tf.nn.relu))
model.add(tf.keras.layers.MaxPool2D(pool_size=(2,2), strides = (2,2), padding = 'valid'))
model.add(tf.keras.layers.Dropout(0.25))
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(units=128,activation = tf.nn.relu))
model.add(tf.keras.layers.Dropout(0.25))
model.add(tf.keras.layers.Dense(units=10,activation = tf.nn.softmax))
return model
# Model training and prediction
my_model = mnist_net(input_shape)
#my_model = mnist_cnn(input_shape)
my_model.compile(optimizer=tf.optimizers.Adam(learning_rate=0.001),
loss="categorical_crossentropy",
metrics=['accuracy'])
print("model fit")
my_model.fit(x=x_train,y=y_train,batch_size=64, epochs=10)
print("model prediction")
test_loss,test_acc=my_model.evaluate(x=x_test,y=y_test)
print("Test Accuracy %.2f"%test_acc)4. Custom model classes
### 4. Through the model class
## Define model classes , rewrite __init__ and call function
class My_model(tf.keras.Model):
def __init__(self,
input_shape,
l1=True,
l2=False,
kernel_size_1=5,
coefficient=0.01,
drop_rate=0.1,
hidden_dim=128,
feature_dim=10):
super(My_model, self).__init__()
tf.keras.backend.set_floatx('float64')
self.l1 = tf.keras.regularizers.l1(l=coefficient)
self.l2 = tf.keras.regularizers.l2(l=coefficient)
self.l1l2 = tf.keras.regularizers.l1_l2(l1=coefficient, l2=coefficient)
if l1 and l2:
self.reg = self.l1l2
else:
if l1:
self.reg = self.l1
elif l2:
self.reg = self.l2
else:
self.reg = None
self.conv2D_1 = tf.keras.layers.Conv2D(filters=32,
kernel_size = 5,
strides = (1,1),
padding = 'same',
activation = tf.nn.relu,
input_shape = input_shape)
self.maxPool2D_1 = tf.keras.layers.MaxPool2D(pool_size=(2,2),
strides = (2,2),
padding = 'valid')
self.conv2D_2 = tf.keras.layers.Conv2D(filters=64,
kernel_size = 3,
strides = (1,1),
padding = 'same',
activation = tf.nn.relu,
input_shape = input_shape)
self.maxPool2D_2 = tf.keras.layers.MaxPool2D(pool_size=(2,2),
strides = (2,2),
padding = 'valid')
self.dropout = tf.keras.layers.Dropout(drop_rate)
self.flatten = tf.keras.layers.Flatten()
self.dense = tf.keras.layers.Dense(hidden_dim,
activation=tf.nn.relu,
kernel_regularizer=self.reg)
self.final_output = tf.keras.layers.Dense(units=10,activation = tf.nn.softmax)
def call(self,x_input):
# First feature extraction
x = self.conv2D_1(x_input)
x = self.maxPool2D_1(x)
# First feature extraction
x = self.conv2D_2(x)
x = self.maxPool2D_2(x)
# dropout Improve the generalization of the model
x = self.dropout(x)
# Two layers are all connected
features1 = self.flatten(x)
features2 = self.dense(features1)
model_output = self.final_output(features2)
# return model_output,features1,features2 # Return multiple values , No use model.fit
return model_output
my_model = My_model(input_shape,
l1=True,
l2=False,
kernel_size_1=5,
coefficient=0.01,
drop_rate=0.1,
hidden_dim=128,
feature_dim=10)
my_model.compile(optimizer=tf.optimizers.Adam(learning_rate=0.001),
loss="categorical_crossentropy",
metrics=['accuracy'])
print(y_train.shape)
print("model fit")
my_model.fit(x=x_train,y=y_train,batch_size=512, epochs=3)
print("model prediction")
test_loss,test_acc=my_model.evaluate(x=x_test,y=y_test)
print("Test Accuracy %.2f"%test_acc)
## Define the model training function ( For complex models , You need to manually define the loss function and model training method )
import math
## Define the loss function
# When the loss function is complex , This function is necessary
def get_loss(y_pre,y_input):
cce = tf.keras.losses.CategoricalCrossentropy()
all_loss = cce(y_pre,y_input)
return all_loss
## Define the gradient calculation function
def get_grad(model, x_input, y_input):
with tf.GradientTape() as tape:
model_output = model(x_input)
all_loss = get_loss(model_output,y_input) # call get_loss function
grads = tape.gradient([all_loss],model.trainable_variables)
return all_loss, grads
## Define training function
def train_model(model,
x_train,
y_train,
batch_size=512,
optimizer=tf.optimizers.Adam(learning_rate=0.001),
num_epochs=5):
# determine stop Conditions
for epoch in range(1,num_epochs+1):
print("Epoch {}/{}".format(epoch,num_epochs))
step = 0
step_num = math.ceil(len(x_train)/batch_size)
for x in range(0, len(x_train), batch_size):
step += 1
x_inp = x_train[x: min(len(x_train),x + batch_size)]
y_inp = y_train[x: min(len(x_train),x + batch_size)]
all_loss, grads = get_grad(model, x_inp, y_inp)
optimizer.apply_gradients(zip(grads, model.trainable_variables))
print("\r", end="")
print ("{}/{}".format(step,step_num),end=" ")
print("loss:", all_loss.numpy())
# Print progress bar
#print("\r", end="")
#print("{}/{} [".format(step,step_num),"=" * (step//2), end="")
return model
my_model = My_model(input_shape,
l1=True,
l2=False,
kernel_size_1=5,
coefficient=0.01,
drop_rate=0.1,
hidden_dim=128,
feature_dim=10)
trained_model = train_model(my_model,x_train,y_train,num_epochs=3)
print("training is over.")
# The performance of the model on the test set
y_test_pre = trained_model(x_test)
test_loss = get_loss(y_test_pre,y_test) # call get_loss Letter
print("Test Loss %.2f"%test_loss)
test_acc=tf.keras.metrics.CategoricalAccuracy()(y_test_pre,y_test)
print("Test Accuracy %.2f"%test_acc)Reference resources :
边栏推荐
- 4278. 峰会
- What are the differences or similarities between "demand fulfillment to settlement" and "purchase to payment"?
- List delete collection elements
- 1.3.1 Full Permutation Problem
- Matlab uses m file to produce fuzzy controller
- Encountered 7 file(s) that should have been pointers, but weren‘t
- Mmrotate trains its dataset from scratch
- Forced login, seven cattle cloud upload pictures
- 4275. Dijkstra sequence
- Kibana uses JSON document data
猜你喜欢

How to permanently set source

Realization of background channel group management function

4279. 笛卡尔树

The wechat installation package has soared from 0.5m to 260m. Why are our programs getting bigger and bigger?

Mmrotate trains its dataset from scratch

User management - restrictions

UVM Introduction Experiment 1

Sliding conflict of view

4279. Cartesian tree

What are the differences or similarities between "demand fulfillment to settlement" and "purchase to payment"?
随机推荐
4279. 笛卡尔树
NIO this.selector.select()
The wechat installation package has soared from 0.5m to 260m. Why are our programs getting bigger and bigger?
Encountered 7 file(s) that should have been pointers, but weren‘t
Forced login, seven cattle cloud upload pictures
Use of flask
redis的string类型及bitmap
[flutter -- geTx] preparation
Sharing of four open source face recognition projects
4278. 峰会
How to upload qiniu cloud
tensorflow包tf.keras模块构建和训练深度学习模型
Sliding conflict of view
02 linear structure 3 reversing linked list
新年小目标!代码更规范!
Matlab画图技巧与实例:堆叠图stackedplot
Matlab求解微分代数方程 (DAE)
JWT authentication and login function implementation, exit login
NiO example
Sequential storage and chain storage of stack implementation