TensorFlow——常見張量操做的API函數

1.張量數組

張量能夠說是TensorFlow的標誌,由於整個框架的名稱TensorFlow就是張量流的意思,全面的認識一下張量。在TensorFlow程序使用tensor數據結構來表明全部的數據,在計算圖中,操做之間的數據都是Tensor,Tensor能夠看作n維的數組或列表,每一個tensor包含了類型(type),階(rank),和形狀(shape)。數據結構

2.tensor類型框架

tensor的類型主要有以下:dom

tf.float32 :32位浮點型
tf.float64 :64 位浮點型
tf.int64 :64位有符號整型
tf.int32 :32位有符號整型
tf.int16 :16位有符號整型
tf.int8 :8位有符號整型
tf.uint8 :8位無符號整型
tf.string :可變長度的字節數組
tf.bool :布爾型
tf.complex64 :兩個32位浮點型組成的複數:實部和虛部

rank(階)函數

rank(階)指的就是維度,能夠經過觀察它的括號層數,好比張量[ [1,2,3], [2,3,4], [3,4,5] ]的階爲2,標量,向量,矩陣的階數分別爲0,1,2。ui

shape(形狀)spa

shape用於描述張量內部的組織關係,形狀一般能夠用整數列表或者元組來表示,也能夠用TensorFlow中的相關形狀函數來表示。code

張量的相關操做orm

張量的相關操做包括類型操做,數字操做,形狀變換,數據操做blog

類型操做

if __name__ == '__main__':
    with tf.Session() as sess:
        print(sess.run(tf.string_to_number('123.456')))  # 將字符轉爲數字
        print(sess.run(tf.to_double(3)))        # 轉爲浮點型
        print(sess.run(tf.to_int32(3.1415)))    # 轉爲整型
        print(sess.run(tf.cast(3.1415, tf.int32)))  # 將類型轉爲指定類型

 

import tensorflow as tf

if __name__ == '__main__':
    with tf.Session() as sess:
        print(sess.run(tf.ones([2, 3], dtype=tf.float32)))  # 生成全1的數據
        print(sess.run(tf.zeros([2, 3], dtype=tf.float32)))   # 生成全0的數據
        print(sess.run(tf.ones_like([1, 2, 3, 4])))    # 生成指定形狀的全1數據
        print(sess.run(tf.zeros_like([2, 3, 4, 5])))  # 生成指定形狀的全0數據
        print(sess.run(tf.fill([2, 2], 6)))  # 使用指定值填充形狀
        print(sess.run(tf.constant((2, 3), 3)))  # 生成常量
        print(sess.run(tf.random_normal([3, 3], mean=2.5, stddev=1.0, dtype=tf.float32, )))

 

 太多了。。。。

相關文章
相關標籤/搜索