日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

tensorflow中张量、常量、变量、占位符

發(fā)布時間:2025/3/19 编程问答 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 tensorflow中张量、常量、变量、占位符 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

引言

從實例出發(fā)

#先導(dǎo)入TensorFlow import tensorflow as tf# Create TensorFlow object called hello_constant hello_constant = tf.constant('Hello World!')with tf.Session() as sess:# Run the tf.constant operation in the sessionoutput = sess.run(hello_constant)print(output)

有人會奇怪為什么不直接輸出“hello world”,其實在tensorflow里面有它自己的 一套。

正文

1.tensor

在tensorflow中,數(shù)據(jù)是被封裝在tensor對象中的。tensor是張量的意思,即包含從0到任意維度的張量。常數(shù)是0維度的張量,向量是1維度的張量,矩陣是二維度的張量,以及還有多維度的張量。

# tensor1 是一個0維的 int32 tensor tensor1 = tf.constant(1234) # tensor2 是一個1維的 int32 tensor tensor2 = tf.constant([123,456,789]) # tensor3 是一個二維的 int32 tensor tensor3 = tf.constant([ [123,456,789], [222,333,444] ])

2.tf.constant

constant函數(shù)提供在tensorflow中定義常量(不可更改的張量)的方法

如:

tensor_constant = tf.constant([1,2,3,4)

3.tf.Variable

tensorflow中的變量是通過Variable類來實現(xiàn)的。tensorflow中需要定義為變量的包括訓(xùn)練過程中的輸入數(shù)據(jù),輸出數(shù)據(jù),以及控制從輸入到輸出的學(xué)習(xí)機(jī)制,即網(wǎng)絡(luò)參數(shù)。輸入輸出數(shù)據(jù)在tf中是用placeholder占位符來定義的,網(wǎng)絡(luò)參數(shù)是用tf.Variable來定義的。

4.tf.placeholder

用于聲明一個張量的數(shù)據(jù)格式,告訴系統(tǒng)這里會有一個這種格式的張量,但是還沒有傳入具體的值。

如:

X = tf.placeholder("float", shape=[None, 100])

上面聲明了一個張量X,數(shù)據(jù)類型是float,100列,行數(shù)不確定。

5.tf.Session

以上部分都是搭建一個計算圖的代碼,在tf中,先搭建好計算圖,然后再啟動session,運行定義好的圖。

import tensorflow as tfx = tf.placeholder("string") with tf.Session() as sess:output = sess.run(x, feed_dict={x : "run the map"})print(output)

通過上面的例子我們明白了如何使用占位符,首先定義x為占位符,然后運行的時候?qū)⑾胍獋魅氲闹祩鹘ox。

結(jié)尾

參考

https://blog.csdn.net/dcrmg/article/details/79016107

https://blog.csdn.net/fei13971414170/article/details/73309106/

總結(jié)

以上是生活随笔為你收集整理的tensorflow中张量、常量、变量、占位符的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。