python如何獲取tensor()數據類型中的值
獲取tensor()數據類型的值
一、問題

只想要216.8973那個數。
二、解決方法
1、單個tensor
tensor.item()
就可以得到216.8973。
2、多個tensor
tensor.tolist()

完美解決~
tensorflow筆記:tensor數據類型
常見的數據類型載體
listnp.arraytf.tensorlist: 可以存儲不同數據類型,缺點不適合存儲較大的數據,如圖片np.array: 解決同類型大數據數據的載體,方便數據運算,缺點是在深度學習之前就設計好的,不支持GPUtf.tensor:更適合深度學習,支持GPU
Tensor是什么
scalar: 1.1vector:[1.1] , [1.1,2.2,……]matrix:[[1,2,3,],[4,5,6],[7,8,9]]torsor:rank > 2 (一般指的是維度大于2的數據)
但是,在tensorflow里面我們把數據的數據都叫tensor
Tensor支持的類型
int,float,doubleboolstring
創(chuàng)建不同類型的Tensor
import tensorflow as tf
# 創(chuàng)建一個整型的數據
tf.constant(1)
# Out[3]: <tf.Tensor: shape=(), dtype=int32, numpy=1>
# 注意因為這里的constant就是一個普通的tensor,不要理解為常量了(TF1.0是代表一個常量)
# 創(chuàng)建一個浮點類型的數據
tf.constant(1.)
# Out[4]: <tf.Tensor: shape=(), dtype=float32, numpy=1.0>
# 若給定一個浮點型的數據,但是指定為int類型會報錯
tf.constant(2.2,dtype=tf.int32)
# TypeError: Cannot convert 2.2 to EagerTensor of dtype int32
# 給一數指定雙精度
tf.constant(2.,dtype=tf.double)
# Out[6]: <tf.Tensor: shape=(), dtype=float64, numpy=2.0>
# 創(chuàng)建bool類型的數據
tf.constant([True,False])
# Out[7]: <tf.Tensor: shape=(2,), dtype=bool, numpy=array([ True, False])>
# 創(chuàng)建字符串型數據(很少用)
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)
# 數據在CPU和GPU上的轉換
aa = a.gpu()
print(aa.device)
bb = b.cpu()
print(bb.device)輸出結果:
/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
轉換為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的維度(秩):返回的是一個tensor類型的數據 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中的概念,現在基本已經淘汰了
python中判斷一個數據是不是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數據類型的轉換
a = np.arange(5)
a.dtype
Out[36]: dtype('int32')
aa = tf.convert_to_tensor(a) ?# numpy數據轉化方法為.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()數據轉化
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 的轉化
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)化的參數,比如自動求導。
tf.Variable可以理解為是專門為神經網絡所設立的一個類型。
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類型轉化為python中的數據類型
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
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
python中的print()函數end=' '的使用及說明
這篇文章主要介紹了python中的print()函數end=' '的使用及說明,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02
python爬蟲 貓眼電影和電影天堂數據csv和mysql存儲過程解析
這篇文章主要介紹了python爬蟲 貓眼電影和電影天堂數據csv和mysql存儲過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-09-09
Python中getattr函數和hasattr函數作用詳解
這篇文章主要介紹了Python中getattr函數和hasattr函數作用的相關知識,非常不錯具有參考借鑒價值,需要的朋友可以參考下2016-06-06

