数据加载
from tensorflow. keras import datasets
( x, y) , ( x_val, y_val) = datasets. mnist. load_data( )
x = tf. convert_to_tensor( x, dtype= tf. float32) / 255 .
y = tf. convert_to_tensor( y, dtype= tf. int32)
y = tf. one_hot( y, depth= 10 )
train_dataset = tf. data. Dataset. from_tensor_slices( ( x, y) )
train_dataset. shuffle( 60000 ) , batch( 100 )
def preprocess ( x, y) :
x = tf. convert_to_tensor( x, dtype= tf. float32) / 255 .
y = tf. convert_to_tensor( y, dtype= tf. int32)
y = tf. one_hot( y, depth= 10 )
return x, y
train_dataset = tf. data. Dataset. from_tensor_slices( ( x, y) )
train_dataset = train_dataset. map ( preprocess) . batch( 32 )
res = next ( iter ( train_dataset) )
res[ 0 ] . shape, res[ 1 ] . shape
db = tf. data. Dataset. from_tensor_slices( ( x, y) )
db = db. map ( preprocess) . shuffle( 10000 ) . batch( batchsz)
for epoch in range ( epochs) :
for step, ( x, y) in enumerate ( db) :
全连接层
net = tf. keras. layers. Dense( 512 )
x = tf. random. normal( [ 4 , 784 ] )
out = net( x)
net. kernel. shape
net. bias. shape
net. build( input_shape= ( 2 , 4 ) )
x = tf. random. normal( [ 2 , 3 ] )
model = keras. Sequential( [
keras. layers. Dense( 2 , activation= 'relu' ) ,
keras. layers. Dense( 2 , activation= 'relu' ) ,
keras. layers. Dense( 2 )
] )
model. build( input_shape= [ None , 3 ] )
误差计算
tf. reduce_mean( tf. losses. MSE( out, y) )
tf. losses. categorical_crossentropy( [ 0 , 1 , 0 , 0 ] , [ 0.25 , 0.25 , 0.25 , 0.25 ] )
tf. losses. binary_crossentropy( [ 1 ] , [ 0.1 ] )
tf. losses. categorical_crossentropy( tf. one_hot( y, depth= 3 ) , logits, from_logits= True )
梯度下降
w = tf. constant( 1 . )
x = tf. constant( 2 . )
with tf. GradientTape( ) as tape:
tape. watch( [ w] )
y= x* w
grad = tape. gradient( y, [ w] )
with tf. GradientTape( persistent= True ) as tape:
损失函数梯度
with tf. GradientTape( ) as tape:
tape. watch( [ w, b] )
prob = tf. nn. softmax( x@w+ b, axis= 1 )
loss = tf. reduce_mean( tf. losses. MSE( tf. one_hot( y, depth= 3 ) , prob) )
grads = tape. gradient( y, [ w, b] )
可视化
Tensorboard——tensorflow Visdom——pytorch
current_time = datatime. datatime. now( ) . shrftime( "%Y%m%d-%H%M%S" )
log_dir = 'logs/' + current_time
summary_writer = tf. summary. create_file_writer( log_dir)
with summary_writer. as_default( ) :
tf. summary. scalar( 'loss' , float ( loss) , step= epoch)
tf. summary. scalar( 'accuracy' , float ( train_accuracy) , step= epoch)
with summary_writer. as_default( ) :
tf. summary. image( "sample:" , sample_img, step= 0 )
sample_imgs = tf. reshape( sample_imgs, [ - 1 , 28 , 28 , 1 ] )
with summary_writer. as_default( ) :
tf. summary. image( "samples:" , sample_imgs, max_output= 25 , step= 0 )
figure = image_grid( sample_imgs)
tf. summary. image( "samples:" , plot_to_image( figure) , step= 0 )
keras 高阶API
dataset layers losses metrics optimizers
acc_meter = metrics. Accuracy( )
loss_meter = metrics. Mean( )
acc_meter. update_state( y, pred)
loss_meter. update_state( loss)
print ( 'loss:' , loss_meter. result( ) . numpy( ) )
acc_meter. reset_states( )
network. compile ( optimizer = optimizers. Adam( lr= 0.01 ) , loss= tf. losses. CategoricalCrossentropy( from_logits= True ) )
network. fit( db, epochs= 10 )
network. compile ( optimizer = optimizers. Adam( lr= 0.01 ) , loss= tf. losses. CategoricalCrossentropy( from_logits= True ) , metrics= [ 'accuracy' ] )
network. fit( db, epochs= 10 , validation_data= db_val, validation_freq= 2 )
network. evaluate( db_test)
自定义网络层
keras.Sequential 自定义层继承自keras.layers.Layer,自定义模型继承自keras.Model
network = Sequential( [ layers. Dense( 256 , activation= 'relu' ) ,
layers. Dense( 64 , activation= 'relu' ) ,
layers. Dense( 10 ) ] )
network. build( input_shape= ( None , 28 * 28 ) )
network. summary( )
class myDense ( layers. Layer) :
def __init__ ( self, in_dim, out_dim) :
super ( MyDense, self) . __init__( )
self. kernel = self. add_variable( 'w' , [ in_dim, out_dim] )
self. bias = self. add_variable( 'b' , [ out_dim] )
def call ( self, inputs, training= None ) :
out = inputs @ self. kernel + self. bias
return out
class MyModel ( keras. Model) :
def __init__ ( self) :
super ( MyModel, self) . __init__( )
self. fc1 = MyDense( 28 * 28 , 256 )
self. fc2 = MyDense( 256 , 64 )
self. fc3 = MyDense( 64 , 10 )
def call ( self, inputs, training= None ) :
x = self. fc1( inputs)
x = tf. nn. relu( x)
x = self. fc2( inputs)
x = tf. nn. relu( x)
x = self. fc3( inputs)
return x
模型的加载与保存
network = Sequential( ) network. compile ( )
network. fit( )
network. evaluate( )
model. save_weights( './weights.ckpt' )
del network
network = Sequential( ) network. compile ( )
network. load_weights( './weights.ckpt' )
network. evaluate( )
network. save( 'model.h5' )
network = tf. keras. models. load_model( 'model.h5' )
交叉验证
for epoch in range ( epochs) :
idx = tf. range ( 60000 )
idx = tf. random. shuffle( idx)
x_train, y_train = tf. gather( x, idx[ : 50000 ] ) , tf. gather( y, idx[ : 50000 ] )
x_val, y_val = tf. gather( x, idx[ - 10000 : ] ) , tf. gather( y, idx[ - 10000 : ] )
network. fit( db, epochs= 6 , validation_split= 0.1 , validation_freq= 2 )
其他知识
keras. layers. Dense( 16 , kernel_regularizer= keras. regularizers. l2( 0.001 ) , activation= tf, nn, relu)
loss = ~
loss_regularization = [ ]
for p in network. trainable_variables:
loss_regularization. append( tf. nn. l2_loss( p) )
loss_regularization = tf. reduce_sum( tf. stack( loss_regularization) )
loss = loss + 0.0001 * loss_regularization
optimizer = SGD( learning_rate= 0.02 , momentum= 0.9 )
optimizer = SGD( learning_rate= 0.1 )
for epoch in range ( 100 ) :
optimizer. learning_rate = 0.1 * ( 100 - epoch) / 100
layers. Dense( 256 , activation= 'relu' )
layers. Dropout( 0.3 )