python如何获取tensor()数据类型中的值

 更新时间:2022年07月16日 08:54:58   作者:weixin_45963617  
这篇文章主要介绍了python如何获取tensor()数据类型中的值,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

获取tensor()数据类型的值

一、问题

只想要216.8973那个数。

二、解决方法

1、单个tensor

tensor.item()

就可以得到216.8973。

2、多个tensor

tensor.tolist()

 

完美解决~

tensorflow笔记:tensor数据类型

常见的数据类型载体

  • list
  • np.array
  • tf.tensor
  • list: 可以存储不同数据类型,缺点不适合存储较大的数据,如图片
  • np.array: 解决同类型大数据数据的载体,方便数据运算,缺点是在深度学习之前就设计好的,不支持GPU
  • tf.tensor:更适合深度学习,支持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的数据)

但是,在tensorflow里面我们把数据的数据都叫tensor

Tensor支持的类型

  • int, float, double
  • bool
  • string

创建不同类型的Tensor

import tensorflow as tf
# 创建一个整型的数据
tf.constant(1)
# Out[3]: <tf.Tensor: shape=(), dtype=int32, numpy=1>
# 注意因为这里的constant就是一个普通的tensor,不要理解为常量了(TF1.0是代表一个常量)

# 创建一个浮点类型的数据
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>

# 创建bool类型的数据
tf.constant([True,False])
# Out[7]: <tf.Tensor: shape=(2,), dtype=bool, numpy=array([ True, False])>

# 创建字符串型数据(很少用)
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中哪些可以优化的参数,比如自动求导。

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字节码与程序执行过程详解

    Python字节码与程序执行过程详解

    这篇文章主要为大家介绍了Python字节码与程序执行过程详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-05-05
  • python中的print()函数end=' '的使用及说明

    python中的print()函数end=' '的使用及说明

    这篇文章主要介绍了python中的print()函数end=' '的使用及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-02-02
  • django主动抛出403异常的方法详解

    django主动抛出403异常的方法详解

    这篇文章主要给大家介绍了关于django主动抛出403异常的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-01-01
  • python爬虫 猫眼电影和电影天堂数据csv和mysql存储过程解析

    python爬虫 猫眼电影和电影天堂数据csv和mysql存储过程解析

    这篇文章主要介绍了python爬虫 猫眼电影和电影天堂数据csv和mysql存储过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-09-09
  • 分享8个非常流行的 Python 可视化工具包

    分享8个非常流行的 Python 可视化工具包

    喜欢用 Python 做项目的小伙伴不免会遇到这种情况:做图表时,用哪种好看又实用的可视化工具包呢?今天小编给大家分享8个非常流行的 Python 可视化工具包,需要的朋友可以参考下
    2019-06-06
  • Django URL传递参数的方法总结

    Django URL传递参数的方法总结

    这篇文章主要介绍了Django URL传递参数的方法总结,需要的朋友可以参考下
    2016-08-08
  • Python利用PyPDF2库处理PDF文件的基本操作

    Python利用PyPDF2库处理PDF文件的基本操作

    PyPDF2是一个Python库,用于处理PDF文件,包括合并、分割、旋转和提取文本等操作,它是一个功能强大且灵活的工具,可用于自动化处理PDF文件,适用于各种应用,从文档管理到数据分析,本文将深入介绍PyPDF2库,掌握如何利用它来处理PDF文件,需要的朋友可以参考下
    2023-11-11
  • Python中getattr函数和hasattr函数作用详解

    Python中getattr函数和hasattr函数作用详解

    这篇文章主要介绍了Python中getattr函数和hasattr函数作用的相关知识,非常不错具有参考借鉴价值,需要的朋友可以参考下
    2016-06-06
  • Python排序算法实例代码

    Python排序算法实例代码

    这篇文章主要为大家详细介绍了Python实现排序算法的相关代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-08-08
  • Python实现CNN的多通道输入实例

    Python实现CNN的多通道输入实例

    今天小编就为大家分享一篇Python实现CNN的多通道输入实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-01-01

最新评论