[Peking University] tensorflow 2.0 Lecture 3
BetterBench 2021-06-04 21:14:21

1 Abstract of this lecture

(1) Objective of this lecture : Use eight strands to build a neural network
(2) Abstract
• Neural networks build eight strands
• Iris Code reappearance
• MNIST Data sets
• Training MNIST Data sets
• Fashion Data sets

3 Six steps to build a network

use Tensorflow Of API:tf.keras Build a network of eight shares
(1) Six steps

imort
train,test
# Build a network structure 
```python
model = tf.keras.models.Sequential
# Configure training methods , Optimizer 、 Parameters 、 Evaluation indicators 
model.compile
# Perform the training process , Inform the training set and test set of input characteristics and tags 
model.fit
# Print network structure and parameter statistics 
model.summary

(2) Network structure

model = tf.keras.models.Sequential([ Network structure ])# Describe each layer of network 

Network structure example :
Straightening layer

tf.keras.layers.Flatten()

Fully connected layer

tf.keras.layers.Dense( Number of neurons ,activation = " Activation function "
, kernel_regularizer = What kind of regularization )
activation( The string gives ) Optional relusoftmax sigmoid tanh
kernel_regularizer Optional tf.keras.regularizers.l1()、tf.keras.regularizers.l2()

Convolution layer :

tf.keras.layers.Conv2D(filters = Convolution and number ,kernel_size = Convolution kernel size ,
strides = Convolution step ,padding = "valid" or "same")

LSTM layer

tf.keras.layers.LSTM()
model.compile(optimizer = Optimizer ,
loss = Loss function
metrics = [" Accuracy rate "])

Optimizer Optional

"sgd" or tf.keras.optimizer.SGD(lr = Learning rate ,momentum = Momentum parameter )
"adagrad" of or tf.keras.optimizer.Adagrad(lr = Learning rate )
"adadelta" or or tf.keras.optimizer.Adadelta(lr = Learning rate )
"adam" or or tf.keras.optimizer.Adam(lr = Learning rate ,beta_1 = 0.9,beta_2 = 0.999)

loss Optional

"mse" or or tf.keras.losses.MeanSquaredError()
#from_logics, Ask if it's raw output , Output without probability distribution . If there is a probability distribution before the output of neural network prediction result, it is false, conversely 
"sparse_categorical_crossentropy " or or tf.keras.losses.SparseCategoricalCrossentropy(from_logics=False)

Metrics Optional

# metrics Tell the network evaluation index 
"accuracy": y_ and y It's all numbers , Such as y_=[1] y = [1]
"categorical_accuracy" :y_ and y It's all hot code ( A probability distribution ), Such as y_=[1] y = [0.256,0.695,0.048]
"sparse_categocial_accuracy":y_ Numerical value ,y It's the only hot code ( A probability distribution ), Such as y_=[1] The output is a probability distribution y = [0.256,0.695,0.048]

model.fit

model.fit( The input characteristics of the trainer , The label of the trainer 
batch_size = ,epochs = ,
validation_data = ( The input characteristics of the test set , The label of the test set ),
validation_split =  What percentage of the test set is divided from the training set ,
validation_freq =  How many times epoch Test once )

model.summary()
Print statistics of network structure parameters

(3)Demo

import tensorflow as tf
from sklearn import datasets
import numpy as np
x_train = datasets.load_iris().data
y_train = datasets.load_iris().target
# Realize the disorder of data set 
np.random.seed(116)
np.random.shuffle(x_train)
np.random.seed(116)
np.random.shuffle(y_train)
tf.random.set_seed(116)
# Build a network model 
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(3, activation='softmax', kernel_regularizer=tf.keras.regularizers.l2())
])
# Choose training parameters 
model.compile(optimizer=tf.keras.optimizers.SGD(lr=0.1),
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),# Because the last layer of the neural network uses softmax
metrics=['sparse_categorical_accuracy'])# Because the output is a probability distribution 
model.fit(x_train, y_train, batch_size=32, epochs=500, validation_split=0.2, validation_freq=20)
model.summary()

4 Build a network of eight shares class

Six steps

import
train,test
class MyModel(Model) model = MyModel
model.compile
model.fit
model.summary
class Encapsulating the structure of neural networks
class MyModel(Model)
def __init__(self):# Define the required network structure block 
super(MyModel,self).__init__()
Define the network structure block
def call(self,x)# Write forward propagation 
Call the network structure block , Achieve forward propagation
return y
model = MyModel()
import tensorflow as tf
from tensorflow.keras.layers import Dense
from tensorflow.keras import Model
from sklearn import datasets
import numpy as np
x_train = datasets.load_iris().data
y_train = datasets.load_iris().target
np.random.seed(116)
np.random.shuffle(x_train)
np.random.seed(116)
np.random.shuffle(y_train)
tf.random.set_seed(116)
class IrisModel(Model):
def __init__(self):
super(IrisModel, self).__init__()
self.d1 = Dense(3, activation='sigmoid', kernel_regularizer=tf.keras.regularizers.l2())
def call(self, x):
y = self.d1(x)
return y
model = IrisModel()
model.compile(optimizer=tf.keras.optimizers.SGD(lr=0.1),
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['sparse_categorical_accuracy'])
modelfit(x_train, y_train, batch_size=32, epochs=500, validation_split=0.2, validation_freq=20)
model.summary()

5 MINIST Data sets

(1)MINIST Data sets :
Provide 6 Ten thousand pictures for training , Provide 1 Ten thousand for testing
(2) Import dataset

mnist = tf.keras.datasets.mnist
(x_train,y_train), (x_test,y_test) = mnist.load_data()

(3) As an input feature , When you input the neural network , Stretch data into one dimension :

tf.keras.layers.Flatten()

(4) Draw grayscale , visualization

plt.imshow(x_train[0],cmap ='gary')
plt.show()
print("x_train[0]:\n",x_train[0])# Print the first input feature 
print("y_train[0]:",y_train[0])# Print the first input label
print("x_test.shape:",x_test,shape)

(5)Demo

import tensorflow as tf
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# normalization 
x_train, x_test = x_train / 255.0, x_test / 255.0
# Define network structure 
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])
# Configure training methods 
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['sparse_categorical_accuracy'])# Because the output is a probability distribution 
# Each iteration of the training set performs a test machine evaluation 
model.fit(x_train, y_train, batch_size=32, epochs=5, validation_data=(x_test, y_test), validation_freq=1)
model.summary()

6 FASHION Data sets

(1) Provide 6 ten thousand 28*28 Pictures, clothes, pants, etc. and labels . Used for training , Provide 1 Ten thousand for testing . Ten categories
• 0 T T-shirt T-shirt/top
• 1 The trousers Trouser
• 2 Pullover Pullover
• 3 dress Dress
• 4 coat coat
• 5 Sandals Scandal
• 6 shirt Shirt
• 7 Sports shoes Sneaker
• 8 package Bag
• 9 Boots Ankle boot
(2) Import dataset

fashion = tf.keras.datasets.fashion_mnist
(x_train,y_train),(x_test,y_test) = fashion.load_data()

(3)Demo

import tensorflow as tf
fashion = tf.keras.datasets.fashion_mnist
(x_train, y_train),(x_test, y_test) = fashion.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
metrics=['sparse_categorical_accuracy'])
model.fit(x_train, y_train, batch_size=32, epochs=5, validation_data=(x_test, y_test), validation_freq=1)
model.summary()
Please bring the original link to reprint ,thank
Similar articles

2021-08-09

2021-08-09