最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python如何獲取tensor()數(shù)據(jù)類型中的值

 更新時(shí)間:2022年07月16日 08:54:58   作者:weixin_45963617  
這篇文章主要介紹了python如何獲取tensor()數(shù)據(jù)類型中的值,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

獲取tensor()數(shù)據(jù)類型的值

一、問題

只想要216.8973那個(gè)數(shù)。

二、解決方法

1、單個(gè)tensor

tensor.item()

就可以得到216.8973。

2、多個(gè)tensor

tensor.tolist()

 

完美解決~

tensorflow筆記:tensor數(shù)據(jù)類型

常見的數(shù)據(jù)類型載體

  • list
  • np.array
  • tf.tensor
  • list: 可以存儲(chǔ)不同數(shù)據(jù)類型,缺點(diǎn)不適合存儲(chǔ)較大的數(shù)據(jù),如圖片
  • np.array: 解決同類型大數(shù)據(jù)數(shù)據(jù)的載體,方便數(shù)據(jù)運(yùn)算,缺點(diǎn)是在深度學(xué)習(xí)之前就設(shè)計(jì)好的,不支持GPU
  • tf.tensor:更適合深度學(xué)習(xí),支持GPU

Tensor是什么

  • scalar: 1.1
  • vector:[1.1] , [1.1,2.2,……]
  • matrix:[[1,2,3,],[4,5,6],[7,8,9]]
  • torsor:rank > 2 (一般指的是維度大于2的數(shù)據(jù))

但是,在tensorflow里面我們把數(shù)據(jù)的數(shù)據(jù)都叫tensor

Tensor支持的類型

  • int, float, double
  • bool
  • string

創(chuàng)建不同類型的Tensor

import tensorflow as tf
# 創(chuàng)建一個(gè)整型的數(shù)據(jù)
tf.constant(1)
# Out[3]: <tf.Tensor: shape=(), dtype=int32, numpy=1>
# 注意因?yàn)檫@里的constant就是一個(gè)普通的tensor,不要理解為常量了(TF1.0是代表一個(gè)常量)

# 創(chuàng)建一個(gè)浮點(diǎn)類型的數(shù)據(jù)
tf.constant(1.)
# Out[4]: <tf.Tensor: shape=(), dtype=float32, numpy=1.0>

# 若給定一個(gè)浮點(diǎn)型的數(shù)據(jù),但是指定為int類型會(huì)報(bào)錯(cuò)
tf.constant(2.2,dtype=tf.int32)
# TypeError: Cannot convert 2.2 to EagerTensor of dtype int32

# 給一數(shù)指定雙精度
tf.constant(2.,dtype=tf.double)
# Out[6]: <tf.Tensor: shape=(), dtype=float64, numpy=2.0>

# 創(chuàng)建bool類型的數(shù)據(jù)
tf.constant([True,False])
# Out[7]: <tf.Tensor: shape=(2,), dtype=bool, numpy=array([ True, False])>

# 創(chuàng)建字符串型數(shù)據(jù)(很少用)
tf.constant("hello,world")
# Out[8]: <tf.Tensor: shape=(), dtype=string, numpy=b'hello,world'>

Tensor Property

下面開始介紹Tensor常用的屬性

tf.device

import tensorflow as tf
with tf.device("cpu"):
    a = tf.constant([1])
with tf.device("gpu"):
    b = tf.range(6)

print(a.device)
print(b.device)
# 數(shù)據(jù)在CPU和GPU上的轉(zhuǎn)換
aa = a.gpu()
print(aa.device)
bb = b.cpu()
print(bb.device)

輸出結(jié)果:

/job:localhost/replica:0/task:0/device:CPU:0
/job:localhost/replica:0/task:0/device:GPU:0
/job:localhost/replica:0/task:0/device:GPU:0
/job:localhost/replica:0/task:0/device:CPU:0

轉(zhuǎn)換為numpy

c = tf.range(10)
#Out[14]: <tf.Tensor: shape=(10,), dtype=int32, numpy=array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])>
c.numpy()
#Out[15]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])?

Tensor的維度與形狀

d = tf.range(10)

d.shape
# Out[17]: TensorShape([10])

d.ndim
# Out[18]: 1

# 用rank查看tensor的維度(秩):返回的是一個(gè)tensor類型的數(shù)據(jù)
tf.rank(d)
# Out[19]: <tf.Tensor: shape=(), dtype=int32, numpy=1>
tf.rank(tf.ones([3,4,2]))
# Out[20]: <tf.Tensor: shape=(), dtype=int32, numpy=3>

# tf.name
# 是Tensorflow1.0中的概念,現(xiàn)在基本已經(jīng)淘汰了

python中判斷一個(gè)數(shù)據(jù)是不是Tensor

import numpy as np
import tensorflow as tf

a = tf.constant(1.)
b = tf.constant([True,False])
c = tf.constant("hello,world")
d = np.arange(4)

isinstance(a,tf.Tensor)
# Out[27]: True
tf.is_tensor(b)
# Out[28]: True
tf.is_tensor(d)
# Out[29]: False

a.dtype,b.dtype,c.dtype,d.dtype
# Out[32]: (tf.float32, tf.bool, tf.string, dtype('int32'))

a.dtype == tf.float32
Out[33]: True
c.dtype == tf.string
Out[34]: True

數(shù)據(jù)類型的轉(zhuǎn)換

a = np.arange(5)
a.dtype
Out[36]: dtype('int32')
aa = tf.convert_to_tensor(a) ?# numpy數(shù)據(jù)轉(zhuǎn)化方法為.astype(np.int64)
# Out[38]: <tf.Tensor: shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4])>
aa = tf.convert_to_tensor(a, dtype=tf.float32)
# Out[40]: <tf.Tensor: shape=(5,), dtype=float32, numpy=array([0., 1., 2., 3., 4.], dtype=float32)>

# 用頭tf.cast()數(shù)據(jù)轉(zhuǎn)化
tf.cast(aa,dtype = tf.float32)
# Out[41]: <tf.Tensor: shape=(5,), dtype=float32, numpy=array([0., 1., 2., 3., 4.], dtype=float32)>
aaa = tf.cast(aa,dtype=tf.double)
# Out[43]: <tf.Tensor: shape=(5,), dtype=float64, numpy=array([0., 1., 2., 3., 4.])>
tf.cast(aaa,dtype=tf.int32)
# Out[44]: <tf.Tensor: shape=(5,), dtype=int32, numpy=array([0, 1, 2, 3, 4])>


# bool 與 int 的轉(zhuǎn)化
b = tf.constant([0,1])
tf.cast(b,tf.bool)
# Out[46]: <tf.Tensor: shape=(2,), dtype=bool, numpy=array([False, ?True])>
bb = tf.cast(b,dtype=tf.bool)
tf.cast(bb,tf.int32)
# Out[48]: <tf.Tensor: shape=(2,), dtype=int32, numpy=array([0, 1])>

tf.Variable

tf.Variable在tensorflow中相比tf.constan一樣也是Tensor,tf.Variable特指Tensorflow中哪些可以優(yōu)化的參數(shù),比如自動(dòng)求導(dǎo)。

tf.Variable可以理解為是專門為神經(jīng)網(wǎng)絡(luò)所設(shè)立的一個(gè)類型。

a = tf.range(5)
b = tf.Variable(a)
# Out[51]: <tf.Variable 'Variable:0' shape=(5,) dtype=int32, numpy=array([0, 1, 2, 3, 4])>
b.dtype
# Out[52]: tf.int32
b.name
# Out[53]: 'Variable:0'
b = tf.Variable(a, name = "input_data")
b.name
# Out[55]: 'input_data:0'
b.trainable
# Out[56]: True

isinstance(b,tf.Tensor)
# Out[57]: False
isinstance(b,tf.Variable)
# Out[58]: True
tf.is_tensor(b)
# Out[59]: True

b.numpy()
# Out[60]: array([0, 1, 2, 3, 4])

將Tensor類型轉(zhuǎn)化為python中的數(shù)據(jù)類型

a = tf.ones([])
# Out[63]: <tf.Tensor: shape=(), dtype=float32, numpy=1.0>
a.numpy()
# Out[64]: 1.0
int(a)
# Out[65]: 1
float(a)
# Out[66]: 1.0

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • DRF跨域后端解決之django-cors-headers的使用

    DRF跨域后端解決之django-cors-headers的使用

    這篇文章主要介紹了DRF跨域后端解決之django-cors-headers的使用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2019-01-01
  • Python列表推導(dǎo)式詳情

    Python列表推導(dǎo)式詳情

    這篇文章主要介紹了Python列表推導(dǎo)式,列表生成式即List Comprehensions,是Python內(nèi)置的非常簡單卻強(qiáng)大的可以用來創(chuàng)建list的生成式,下面來看看詳細(xì)內(nèi)容吧
    2021-11-11
  • python開發(fā)之thread線程基礎(chǔ)實(shí)例入門

    python開發(fā)之thread線程基礎(chǔ)實(shí)例入門

    這篇文章主要介紹了python開發(fā)之thread線程基礎(chǔ),以三個(gè)實(shí)例形式分析了Python中thread線程的基本使用方法,涉及串行與并行程序的執(zhí)行原理及線程的操作技巧,需要的朋友可以參考下
    2015-11-11
  • Python中的裝飾器類詳解

    Python中的裝飾器類詳解

    Python?裝飾器在很多情況下是一個(gè)非常有用的工具,它們可以用于修改或增強(qiáng)函數(shù)或類的行為,本篇文章將深入探討如何在?Python?中使用類裝飾器
    2023-06-06
  • Python實(shí)現(xiàn)將數(shù)據(jù)寫入netCDF4中的方法示例

    Python實(shí)現(xiàn)將數(shù)據(jù)寫入netCDF4中的方法示例

    這篇文章主要介紹了Python實(shí)現(xiàn)將數(shù)據(jù)寫入netCDF4中的方法,涉及Python數(shù)據(jù)處理與文件讀寫相關(guān)操作技巧,需要的朋友可以參考下
    2018-08-08
  • 使用Pytorch搭建模型的步驟

    使用Pytorch搭建模型的步驟

    這篇文章主要介紹了使用Pytorch搭建模型的步驟,幫助大家更好的理解和使用Pytorch,進(jìn)行機(jī)器學(xué)習(xí),感興趣的朋友可以了解下
    2020-11-11
  • 解決Python3 抓取微信賬單信息問題

    解決Python3 抓取微信賬單信息問題

    這篇文章主要介紹了Python3 抓取微信賬單信息,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-07-07
  • python入門之語句(if語句、while語句、for語句)

    python入門之語句(if語句、while語句、for語句)

    這篇文章主要介紹了python入門之語句,主要包括if語句、while語句、for語句的使用,需要的朋友可以參考下
    2015-01-01
  • Django的restframework接口框架自定義返回?cái)?shù)據(jù)格式的示例詳解

    Django的restframework接口框架自定義返回?cái)?shù)據(jù)格式的示例詳解

    這篇文章主要介紹了Django的restframework接口框架自定義返回?cái)?shù)據(jù)格式,本文介紹了通過Django的restframework接口框架自定義Response返回對象來自定義返回?cái)?shù)據(jù)格式,本文通過示例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2022-07-07
  • Python利用PIL進(jìn)行圖片壓縮

    Python利用PIL進(jìn)行圖片壓縮

    有時(shí)在發(fā)送一些文件如PPT、Word時(shí),由于文件中的圖片太大,導(dǎo)致文件也太大,無法發(fā)送,所以本文為大家介紹了Python中圖片壓縮的方法,需要的可以參考下
    2025-02-02

最新評論

乌兰察布市| 晋中市| 天门市| 德阳市| 北宁市| 揭阳市| 阿勒泰市| 泸溪县| 桐梓县| 嘉义县| 天津市| 新乡市| 彭泽县| 抚顺市| 丰原市| 三穗县| 井研县| 新余市| 庄河市| 泾川县| 北海市| 前郭尔| 浏阳市| 贡山| 惠水县| 白城市| 临沧市| 万全县| 牡丹江市| 固安县| 林口县| 灵山县| 苍溪县| 喀喇沁旗| 凤山县| 巴中市| 遂宁市| 定结县| 壤塘县| 定陶县| 政和县|