溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

TensorFlow神經(jīng)網(wǎng)絡(luò)中張量與變量的概念分析

發(fā)布時間:2021-10-18 09:21:54 來源:億速云 閱讀:108 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要介紹TensorFlow神經(jīng)網(wǎng)絡(luò)中張量與變量的概念分析,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

    一、張量定義

    張量:TensorFlow的張量是n維數(shù)組,類型為tf.Tensor。

    標量:一個數(shù)字 (0階張量)

    向量:一維數(shù)組 (1階張量)

    矩陣:二維數(shù)組 (2階張量)

    二、張量屬性

    1、張量的類型

    TensorFlow神經(jīng)網(wǎng)絡(luò)中張量與變量的概念分析

    #創(chuàng)建常數(shù)張量
        a = tf.constant(3.0)    
        print(a)

    TensorFlow神經(jīng)網(wǎng)絡(luò)中張量與變量的概念分析

    2、張量的階

    TensorFlow神經(jīng)網(wǎng)絡(luò)中張量與變量的概念分析

    三、張量的指令

    1、常數(shù)張量(普通)

    #創(chuàng)建常數(shù)張量
        a = tf.constant(3.0)    
        print(a)

    TensorFlow神經(jīng)網(wǎng)絡(luò)中張量與變量的概念分析

    TensorFlow神經(jīng)網(wǎng)絡(luò)中張量與變量的概念分析

    2、張量數(shù)組

    1、固定張量數(shù)組(0)
    #創(chuàng)建張量數(shù)組
        #0:
        array_0 = tf.zeros(shape=[3,3])    #3*3數(shù)組(0)

    TensorFlow神經(jīng)網(wǎng)絡(luò)中張量與變量的概念分析

    2、固定張量數(shù)組(1)
    #1:
        array_1 = tf.ones(shape=[3,3])     #3*3數(shù)組(1)
    3、隨機張量數(shù)組
    #隨機:
        array_random = tf.random_normal(shape=[2,3], mean=1.75, stddev=0.12)
    #                                   2*3數(shù)組      均值(1.75) 標準差

    TensorFlow神經(jīng)網(wǎng)絡(luò)中張量與變量的概念分析

    3、查看張量值

    查看張量值:張量.eval()

    #會話(查看張量)
        with tf.Session() as sess:
            print(a.eval())
            print(array_0.eval())
            print(array_1.eval())
            print(array_random.eval())

    TensorFlow神經(jīng)網(wǎng)絡(luò)中張量與變量的概念分析

    4、張量類型改變

    #修改張量類型
        array_0 = tf.cast(array_0, tf.int32)

    TensorFlow神經(jīng)網(wǎng)絡(luò)中張量與變量的概念分析TensorFlow神經(jīng)網(wǎng)絡(luò)中張量與變量的概念分析

    5、張量形狀改變

    注:屬于動態(tài)改變張量,需要張量元素個數(shù)固定。

    #修改張量形狀
        array_random = tf.reshape(array_random, shape=[3,2])

    修改前:

    TensorFlow神經(jīng)網(wǎng)絡(luò)中張量與變量的概念分析

    修改后:

    TensorFlow神經(jīng)網(wǎng)絡(luò)中張量與變量的概念分析

    代碼

    # 張量(創(chuàng)建與修改)
    import tensorflow as tf
    # 創(chuàng)建張量
    def Create_Tensor():
        # 創(chuàng)建常數(shù)張量
        a = tf.constant(3.0)
        print(a)
     
        # 創(chuàng)建張量數(shù)組
        # 0:
        array_0 = tf.zeros(shape=[3, 3])  # 3*3數(shù)組(0)
     
        # 1:
        array_1 = tf.ones(shape=[3, 3])  # 3*3數(shù)組(1)
     
        # 隨機:
        array_random = tf.random_normal(shape=[2, 3], mean=1.75, stddev=0.12)
        #                                   2*3數(shù)組      均值(1.75) 標準差
     
        # 會話(查看張量)
        with tf.Session() as sess:
            print(a.eval())
            print(array_0.eval())
            print(array_1.eval())
            print(array_random.eval()) 
    # 修改張量
    def Modify_Tensor():
        global array_0, array_random
        print('修改后的:')
     
        # 修改張量類型
        array_0 = tf.cast(array_0, tf.int32)
     
        # 修改張量形狀
        array_random = tf.reshape(array_random, shape=[3, 2])
     
        # 會話(查看張量)
        with tf.Session() as sess:
            print(array_0.eval())
            print(array_random.eval())
     
    # 創(chuàng)建張量
    Create_Tensor()
    # 修改張量
    Modify_Tensor()

    四、變量

    1、定義變量

    # 定義變量
    a = tf.Variable(initial_value=2)
    b = tf.Variable(initial_value=4)
    c = tf.add(a,b)

    2、初始化變量

    TensorFlow的變量必須初始化,否則會報錯。

    # 初始化變量
    init = tf.global_variables_initializer()

    3、開啟會話(執(zhí)行)

    # 開啟會話
    with tf.Session() as sess:
        sess.run(init)
        print(sess.run(c))

    TensorFlow神經(jīng)網(wǎng)絡(luò)中張量與變量的概念分析

    代碼

    # 變量
    import tensorflow as tf
     
    # 定義變量
    a = tf.Variable(initial_value=2)
    b = tf.Variable(initial_value=4)
    c = tf.add(a,b)
     
    # 初始化變量
    init = tf.global_variables_initializer()
     
    # 開啟會話
    with tf.Session() as sess:
        sess.run(init)
        print(sess.run(c))

    以上是“TensorFlow神經(jīng)網(wǎng)絡(luò)中張量與變量的概念分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

    向AI問一下細節(jié)

    免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

    AI