【tensorflow2.0】中階api--模型、損失函數、優化器、數據管道、特征列等


下面的范例使用TensorFlow的中階API實現線性回歸模型。

TensorFlow的中階API主要包括各種模型層,損失函數,優化器,數據管道,特征列等等。

import tensorflow as tf
from tensorflow.keras import layers,losses,metrics,optimizers
 
 
# 打印時間分割線
@tf.function
def printbar():
    ts = tf.timestamp()
    today_ts = ts%(24*60*60)
 
    hour = tf.cast(today_ts//3600+8,tf.int32)%tf.constant(24)
    minite = tf.cast((today_ts%3600)//60,tf.int32)
    second = tf.cast(tf.floor(today_ts%60),tf.int32)
 
    def timeformat(m):
        if tf.strings.length(tf.strings.format("{}",m))==1:
            return(tf.strings.format("0{}",m))
        else:
            return(tf.strings.format("{}",m))
 
    timestring = tf.strings.join([timeformat(hour),timeformat(minite),
                timeformat(second)],separator = ":")
    tf.print("=========="*8,end = "")
    tf.print(timestring)
 
# 樣本數量
n = 800
 
# 生成測試用數據集
X = tf.random.uniform([n,2],minval=-10,maxval=10) 
w0 = tf.constant([[2.0],[-1.0]])
b0 = tf.constant(3.0)
Y = X@w0 + b0 + tf.random.normal([n,1],mean = 0.0,stddev= 2.0)  # @表示矩陣乘法,增加正態擾動
 
# 構建輸入數據管道
ds = tf.data.Dataset.from_tensor_slices((X,Y)) \
     .shuffle(buffer_size = 1000).batch(100) \
     .prefetch(tf.data.experimental.AUTOTUNE)  
 
# 定義優化器
optimizer = optimizers.SGD(learning_rate=0.001)
 
linear = layers.Dense(units = 1)
linear.build(input_shape = (2,)) 
 
@tf.function
def train(epoches):
    for epoch in tf.range(1,epoches+1):
        L = tf.constant(0.0) #使用L記錄loss值
        for X_batch,Y_batch in ds:
            with tf.GradientTape() as tape:
                Y_hat = linear(X_batch)
                loss = losses.mean_squared_error(tf.reshape(Y_hat,[-1]),tf.reshape(Y_batch,[-1]))
            grads = tape.gradient(loss,linear.variables)
            optimizer.apply_gradients(zip(grads,linear.variables))
            L = loss
 
        if(epoch%100==0):
            printbar()
            tf.print("epoch =",epoch,"loss =",L)
            tf.print("w =",linear.kernel)
            tf.print("b =",linear.bias)
            tf.print("")
 
train(500)

結果:

InternalError: 2 root error(s) found.
  (0) Internal:  No unary variant device copy function found for direction: 1 and Variant type_index: tensorflow::data::(anonymous namespace)::DatasetVariantWrapper
     [[{{node while_input_5/_12}}]]
     [[Func/while/body/_1/cond/then/_78/StatefulPartitionedCall/cond/then/_105/input/_133/_96]]
  (1) Internal:  No unary variant device copy function found for direction: 1 and Variant type_index: tensorflow::data::(anonymous namespace)::DatasetVariantWrapper
     [[{{node while_input_5/_12}}]]
0 successful operations.
0 derived errors ignored. [Op:__inference_train_302016]

Function call stack:
train -> train

這里出現了一個問題,我是在谷歌colab上使用gpu進行運行的,會報這個錯誤,但當我切換成cpu運行時就不報錯了:

================================================================================15:34:47
epoch = 100 loss = 4.7718153
w = [[2.00853848]
 [-1.00294471]]
b = [2.51343322]

================================================================================15:34:49
epoch = 200 loss = 3.71054626
w = [[2.01135874]
 [-1.00254476]]
b = [3.019526]

================================================================================15:34:51
epoch = 300 loss = 3.84821081
w = [[2.01109028]
 [-1.00210166]]
b = [3.12148571]

================================================================================15:34:53
epoch = 400 loss = 3.35442448
w = [[2.01156759]
 [-1.0024389]]
b = [3.14201045]

================================================================================15:34:55
epoch = 500 loss = 3.98874116
w = [[2.00852275]
 [-1.00062764]]
b = [3.14614844]

 

參考:

開源電子書地址:https://lyhue1991.github.io/eat_tensorflow2_in_30_days/

GitHub 項目地址:https://github.com/lyhue1991/eat_tensorflow2_in_30_days


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM