tensorflow 常量(constant)
創建常量
矩陣創建常量、常量定義類型、定義一個隨機數(標量)
t = tf. constant( [ [ 1 . , 2 . , 3 . ] , [ 2 . , 3 . , 4 . ] ] )
zero_vector = tf. zeros( shape= ( 2 ) , dtype= tf. int32)
random_float = tf. random. uniform( shape= ( ) )
定義一個有2個元素的零向量、定義兩個2×2的常量矩陣
zero_vector = tf. zeros( shape= ( 2 ) )
A = tf. constant( [ [ 1 . , 2 . ] , [ 3 . , 4 . ] ] )
B = tf. constant( [ [ 5 . , 6 . ] , [ 7 . , 8 . ] ] )
0維張量、字符串張量
t = tf. constant( 2.718 )
矩陣操作
舉例加法、平方、矩陣相乘、轉置
print ( t+ 10 )
print ( tf. square( t) )
print ( t @ tf. transpose( t) )
輸出矩陣信息
打印矩陣、打印範圍行、打印第幾行、用numpy輸出
print ( t)
print ( t[ : , 1 : ] )
print ( t[ . . . , 1 ] )
print ( t. numpy( ) )
輸出矩陣形狀、類型
print ( t. shape)
print ( A. dtype)
和numpy相互轉換
print ( t. numpy( ) )
np_t = np. array( [ [ 1 . , 2 . , 3 . ] , [ 4 . , 5 . , 6 . ] ] )
print ( tf. constant( np_t) )
字符串張量
創建字符串常量、打印字符串常量
t = tf. constant( "cafe" )
t = tf. constant( [ "cafe" , "coffee" , "咖啡" ] )
print ( t)
獲得字符串長度、獲得在utf8編碼下的長度、將字符串從unicode轉換成utf-8
print ( tf. strings. length( t) )
print ( tf. strings. length( t, unit = "UTF8_CHAR" ) )
print ( tf. strings. unicode_decode( t, "UTF8" ) )
不等長矩陣、不等長矩陣轉換成普通矩陣
r = tf. ragged. constant( [ [ 1 . , 2 . ] , [ 2 . , 4 . , 5 . ] , [ ] , [ 41 . ] ] )
print ( r. to_tensor( ) )
矩陣拼接
print ( tf. concat( [ r, r2] , axis = 0 ) )
print ( tf. concat( [ r, r2] , axis = 1 ) )
創建稀疏矩陣
s = tf. SparseTensor(
indices = [ [ 0 , 1 ] , [ 1 , 0 ] , [ 2 , 3 ] ] ,
values= [ 1 . , 2 . , 3 . ] ,
dense_shape = [ 3 , 4 ]
)
將稀疏矩陣轉成密集矩陣、和密集矩陣相乘
s6 = tf. sparse. reorder( s)
print ( tf. sparse. to_dense( s6) )
print ( tf. sparse. to_dense( s) )
print ( tf. sparse. sparse_dense_matmul( s, s4) )
tensorflow 變量(Variable)
創建變量矩陣
v = tf. Variable( [
[ 1 . , 2 . , 3 . ] ,
[ 4 . , 5 . , 6 . ]
] )
打印變量矩陣、常量引用、numpy輸出
print ( v)
print ( v. value( ) )
print ( v. numpy( ) )
變量重賦值
v. assign( 2 * v)
v[ 0 , 1 ] . assign( 42 )
v[ 1 ] . assign( [ 7 . , 8 . , 9 . ] )
自定義損失函數
def customized_mse ( y_true, y_pred) :
return tf. reduce_mean( tf. square( y_pred - y_true) )
自定義層次(繼承)
class customizedDenseLayer ( keras. layers. Layer) :
def __init__ ( selff, unit, actication = None , ** kwargs) :
self. units = units
self. activation = keras. layers. Activation( activation)
super ( CustomizedDenseLayer, self) . __init__( ** kwargs)
def build ( self, input_shape) :
"""構建所需要的參數"""
self. kernel = self. add_weight(
name = 'kernel' ,
shape = ( input_shape[ 1 ] , self. unis) ,
initializer = 'uniform' ,
trainable = True
)
self. bias = self. add_weight(
name = 'bias' ,
shape = ( self. units, ) ,
initializer = 'zeros' ,
trainable = True
)
super ( customizedDenseLayer, self) . build( input_shape)
def call ( self, x) :
"""完成正向計算"""
return self. activation( x @ self. kernel + self. bias)
自定義層次(lambda)
customized_softplus = keras. layers. Lambda( lambda x : tf. nn. softplus( x) )
打印層信息
打印layers所有參數、打印可訓練參數、查看layer的方法和參考
layer. variables
layer. trainable_variables
help ( layer)
tf.function()
def scaled_elu ( z, scale= 1.0 , alpha= 1.0 ) :
is_positive = tf. greater_equal( z, 0.0 )
return scale * tf. where( is_positive, z, alpha * tf. nn. elu( z) )
scaled_elu_tf = tf. function( scaled_elu)
print ( scaled_elu_tf. python_function is scaled_elu )
@tf.function
@tf. function
def converge_to_2 ( n_iters) :
total = tf. constant( 0 . )
increment = tf. constant( 1 . )
for _ in range ( n_iters) :
total += increment
increment /= 2.0
return total
print ( converge_to_2( 20 ) )
var = tf. Variable( 0 . )
@tf. function
def add_21 ( ) :
return var. assign_add( 21 )
print ( add_21( ) )
@tf. function
def cube ( z) :
return tf. pow ( z, 3 )
print ( cube( tf. constant( [ 1 . , 2 . , 3 . ] ) ) )
print ( cube( tf. constant( [ 1 , 2 , 3 ] ) ) )
簽名函數
@tf. function( input_signature = [ tf. TensorSpec( [ None ] ,
tf. int32,
name = 'x' ) ] )
def cube ( z) :
return tf. pow ( z, 3 )
print ( cube( tf. constant( [ 1 . , 2 . , 3 . ] ) ) )
展示@tf.function生成的tensorflow代碼
def display_tf_code ( func) :
code = tf. autograph. to_code( func)
from ipython. display import display, Markdown
display( Markdown( '```python\n{}\n```' . format ( code) ) )
display_tf_code( scaled_elu)
轉換成圖結構
cube_func_int32 = cube. get_concrete_function(
tf. TensorSpec( [ None ] , tf. int32)
)
print ( cube_func_int32)
轉換圖
cube_func_int32. graph
得到圖結構裏的所有操作
cube_func_int32. graph. get_operations( )
打印圖結構裏的操作
pow_op = cube_func_int32. graph. get_operations( ) [ 2 ]
print ( pow_op)
打印操作輸入輸出
print ( list ( pow_op. inputs) )
print ( list ( pow_op. outputs) )
通過名字獲取tenser
cube_func_int32. graph. get_tensor_by_name( "z:0" )
打印所有節點信息
cube_func_int32. graph. as_graph_def( )
自動求導
import tensorflow as tf
x = tf. Variable( initial_value= 3 . )
with tf. GradientTape( ) as tape:
y = tf. square( x)
y_grad = tape. gradient( y, x)
print ( [ y, y_grad] )
自動求偏導
x1 = tf. Variable( 2.0 )
x2 = tf. Variable( 3.0 )
def g ( x1, x2) :
return ( x1+ 5 ) * ( x2** 2 )
with tf. GradientTape( persistent= True ) as tape:
z = g( x1, x2)
dz_x1 = tape. gradient( z, x1)
dz_x2 = tape. gradient( z, x2)
del tape
print ( dz_x1)
print ( dz_x2)
x1 = tf. Variable( 2.0 )
x2 = tf. Variable( 3.0 )
def g ( x1, x2) :
return ( x1+ 5 ) * ( x2** 2 )
with tf. GradientTape( persistent= True ) as tape:
z = g( x1, x2)
dz = tape. gradient( z, [ x1, x2] )
del tape
print ( dz)
自定義求偏導(常量模式)
x1 = tf. constant( 2.0 )
x2 = tf. constant( 3.0 )
def g ( x1, x2) :
return ( x1+ 5 ) * ( x2** 2 )
with tf. GradientTape( persistent= True ) as tape:
tape. watch( x1)
tape. watch( x2)
z = g( x1, x2)
dz = tape. gradient( z, [ x1, x2] )
del tape
print ( dz)
多個目標函數對一個變量求導
x = tf. Variable( 5.0 )
with tf. GradientTape( ) as tape:
z1 = 3 * x
z2 = x ** 2
dz = tape. gradient( [ z1, z2] , x)
print ( dz)
二階求導
x1 = tf. Variable( 2.0 )
x2 = tf. Variable( 3.0 )
def g ( x1, x2) :
return ( x1+ 5 ) * ( x2** 2 )
with tf. GradientTape( persistent= True ) as outer_tape:
with tf. GradientTape( persistent= True ) as inner_tape:
z = g( x1, x2)
inner_grads = inner_tape. gradient( z, [ x1, x2] )
outer_grads = [ outer_tape. gradient( inner_grad, [ x1, x2] )
for inner_grad in inner_grads]
del outer_tape, inner_tape
print ( outer_grads)
模擬梯度下降
learning_rate = 0.1
x = tf. Variable( 1.0 )
def f ( x) :
return x* x
for i in range ( 100 ) :
with tf. GradientTape( ) as tape:
z = f( x)
dz_dx = tape. gradient( z, x)
x. assign_sub( learning_rate * dz_dx)
print ( x)
keras優化器與求導結合實現梯度下降
learning_rate = 0.1
x = tf. Variable( 1.0 )
def f ( x) :
return x* x
optimizer = keras. optimizers. SGD( lr = learning_rate)
for i in range ( 100 ) :
with tf. GradientTape( ) as tape:
z = f( x)
dz_dx = tape. gradient( z, x)
optimizer. apply_gradients( [ ( dz_dx, x) , ] )
print ( x)
線性迴歸
X = tf. constant( X)
y = tf. constant( y)
a = tf. Variable( initial_value= 0 . )
b = tf. Variable( initial_value= 0 . )
variables = [ a, b]
num_epoch = 10000
optimizer = tf. keras. optimizers. SGD( learning_rate= 1e - 3 )
for e in range ( num_epoch) :
with tf. GradientTape( ) as tape:
y_pred = a * X + b
loss = 0.5 * tf. reduce_sum( tf. square( y_pred - y) )
grads = tape. gradient( loss, variables)
optimizer. apply_gradients( grads_and_vars= zip ( grads, variables) )
print ( a, b)
自定義fit函數
epochs = 100
batch_size = 32
steps_per_epoch = len ( x_train_scaled) // batch_size
optimizer = keras. optimizers. SGD( )
metric = keras. metrics. MeanSquaredError( )
def random_batch ( x, y, batch_size = 32 ) :
idx = np. random. randint( 0 , len ( x) , size = batch_size)
return x[ idx] , y[ idx]
model = keras. models. Sequential( [
keras. layers. Dense( 30 , activation = 'relu' ,
input_shape= x_train. shape[ 1 : ] ) ,
keras. layers. Dense( 1 ) ,
] )
for epoch in range ( epochs) :
metric. reset_states( )
for step in range ( steps_per_epoch) :
x_batch , y_batch = random_batch( x_train_scaled, y_train, batch_size)
with tf. GradientTape( ) as tape:
y_pred = model( x_batch)
loss = tf. reduce_mean( keras. losses. mean_squared_error( y_batch, y_pred) )
metric( y_batch, y_pred)
grads = tape. gradient( loss , model. variables)
grads_and_vars = zip ( grads, model. variables)
optimizer. apply_gradients( grads_and_vars)
print ( "\rEpoch" , epoch, "train mse:" , metric. result( ) . numpy( ) , end= "" )
y_valid_pred = model( x_valid_scaled)
valid_loss = tf. reduce_mean( keras. losses. mean_squared_error( y_valid_pred, y_valid) )
print ( "\t" , "valid mse:" , valid_loss. numpy( ) )