tensorflow2.0 基础一 常用数据类型及转换

本文深入探讨了TensorFlow中张量的概念,包括标量、向量、矩阵和高阶张量的定义,以及数据类型如整数、浮点数、布尔型、字符串等。详细介绍了如何创建张量,张量属性,类型检查与转换,布尔与整数转换,tf.Variable的使用,以及张量转numpy数组的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.youkuaiyun.com/weixin_43619065/article/details/101080128
            </div>
                                                <!--一个博主专栏付费入口-->
         
         <!--一个博主专栏付费入口结束-->
        <link rel="stylesheet" href="https://csdnimg.cn/release/phoenix/template/css/ck_htmledit_views-4a3473df85.css">
                                    <div id="content_views" class="markdown_views prism-atelier-sulphurpool-light">
                <!-- flowchart 箭头图标 勿删 -->
                <svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
                    <path stroke-linecap="round" d="M5,0 0,2.5 5,5z" id="raphael-marker-block" style="-webkit-tap-highlight-color: rgba(0, 0, 0, 0);"></path>
                </svg>
                                        <p></p><div class="toc"><h3><a name="t0"></a>目录</h3><ul><li><a href="#Tensor__2" rel="nofollow" target="_self">Tensor 是什么</a></li><ul><li><a href="#TF_11" rel="nofollow" target="_self">TF数据类型</a></li><li><a href="#Create_Tensor_16" rel="nofollow" target="_self">Create Tensor</a></li><li><a href="#Tensor_Property_37" rel="nofollow" target="_self">Tensor Property</a></li><li><a href="#Check_Tensor_Type_40" rel="nofollow" target="_self">Check Tensor Type</a></li><li><a href="#Convert__43" rel="nofollow" target="_self">Convert 转换</a></li><li><a href="#bool_int_46" rel="nofollow" target="_self">bool int</a></li><li><a href="#tfVariable_49" rel="nofollow" target="_self">tf.Variable</a></li><li><a href="#To_numpy_53" rel="nofollow" target="_self">To numpy</a></li></ul></ul></div><p></p>

Tensor 是什么

scalar: 1.1
vector: [1.1],[1.1,2.2,...]
matrix: [[1.1,2.2],[3.3,4.4],[5.5,6.6]]
tensor: rank > 2

 
  • 1
  • 2
  • 3
  • 4

以上都可以叫做tensor

TF数据类型

  • int, float, double
  • bool
  • string

Create Tensor

In[3]: tf.constant(1)
Out[3]: <tf.Tensor: id=0, shape=(), dtype=int32, numpy=1>

In[4]: tf.constant(1.)
Out[4]: <tf.Tensor: id=6, shape=(), dtype=float32, numpy=1.0>

In[5]: tf.constant(2.2,dtype=tf.int32)
Out[5]: TypeError: Cannot convert provided value to EagerTensor. Provided value: 2.2 Requested dtype: int32

In[6]: tf.constant(2., dtype=tf.double)
Out[6]: <tf.Tensor: id=25, shape=(), dtype=float64, numpy=2.0>

In[7]: tf.constant([True,False])
Out[7]: <tf.Tensor: id=39, shape=(2,), dtype=bool, numpy=array([ True, False])>

In[8]: tf.constant(‘hello, world.’)
Out[8]: <tf.Tensor: id=56, shape=(), dtype=string, numpy=b’hello, world.’>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17

Tensor Property

在这里插入图片描述

Check Tensor Type

在这里插入图片描述

Convert 转换

在这里插入图片描述

bool int

在这里插入图片描述

tf.Variable

在这里插入图片描述
特别注意tf.Variable 通过isinstance判断是否为tensor会返回False,所以尽量使用tf.is_tensor。

To numpy

在这里插入图片描述

                                </div>
            <link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-b6c3c6d139.css" rel="stylesheet">
                </div>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值