当前位置:网站首页>Tensorflow tensor introduction
Tensorflow tensor introduction
2022-04-23 17:53:00 【Stephen_ Tao】
List of articles
1. tensor (Tensor) The definition of
TensorFlow The tensor in is a n An array of dimensions , The type is tf.tensor. Be similar to numpy Medium ndarray.Tensor Has two important properties , Data types including tensors (dtype) And tensor shape (shape).
2. Instructions for creating tensors
Tensors are divided into fixed value tensors and random value tensors , Different types of tensors have different creation instructions .
2.1 Fixed value tensor
Common fixed value tensor creation instructions are as follows :
tf.zeros(shape,dtype=tf.float32,name=None)tf.zeros_like(shape,dtype=tf.float32,name=None)tf.ones(shape,dtype=tf.float32,name=None)tf.ones_like(shape,dtype=tf.float32,name=None)tf.constant(value,dtype=tf.float32,shape=None,name='Const')
2.2 Random valued tensor
It is mainly used to generate specific distribution , Random valued tensors such as normal distribution .
be based on Pycharm Create a random valued tensor :
use InteractiveSession() stay Python Console Operation in
2.2.1 Get into InteractiveSession Interactive conversation
import os
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
tf.compat.v1.InteractiveSession()
In this paper TensorFlow The version is 2.5.0 edition , In this version, there is no tf.InteractiveSession(), So compatible v1 The interactive session is called by version .
2.2.2 Generate normal distribution random value tensor
random_data = tf.random.normal([2,3],mean=0.0,stddev=1.0)
random_data.eval()
We will get the following results :
array([[-0.5411521 , -0.04788242, -0.14508048],
[-1.2735071 , -0.5523144 , -0.46699935]], dtype=float32)
3. Transformation of tensor
The transformation of tensor includes type change and shape change .
3.1 The type of tensor changes
Here are some functions of tensor type change :
tf.string_to_number(string_tensor,out_type=None,name=None)tf.to_double(x,name='ToDouble')tf.to_float(x,name='ToFloat')tf.cast(x,dtype,name=None)
3.2 The shape of the tensor changes
There are two kinds of shape changes of tensors , They are dynamic shape change and static shape change .
3.2.1 Change of static shape
API:object.set_shape
The rules that need to be met :
- After the static shape is fixed, it cannot be modified again
- When converting static shapes , Cannot convert across orders
Example 1:
with tf.compat.v1.Session() as sess:
a = tf.compat.v1.placeholder(dtype=tf.float32,shape=[3,4])
print("Origin a:",a.get_shape())
a.set_shape(shape=[2,6])
The above code modifies the shape when the static shape is fixed , The following error message will be generated :
ValueError: Dimension 0 in both shapes must be equal, but are 3 and 2. Shapes are [3,4] and [2,6].
Example 2:
with tf.compat.v1.Session() as sess:
a = tf.compat.v1.placeholder(dtype=tf.float32,shape=[None,3])
print("Origin a:",a.get_shape())
a.set_shape(shape=[3,2,3])
The above code changes shape across steps , The following error message will be generated :
ValueError: Shapes must be equal rank, but are 2 and 3
Example 3:
with tf.compat.v1.Session() as sess:
a = tf.compat.v1.placeholder(dtype=tf.float32,shape=[None,None])
print("Origin a:",a.get_shape())
a.set_shape(shape=[3,2])
print("changed a:",a.get_shape())
The above is the correct code , give the result as follows (set_shape It's in the original Tensor Based on , No new objects are generated ):
Origin a: (None, None)
changed a: (3, 2)
3.2.2 Dynamic shape changes
API:tf.reshape()
The rules that need to be met :
- Create new tensors dynamically , The number of elements of the tensor must match
Example :
with tf.compat.v1.Session() as sess:
a = tf.compat.v1.placeholder(dtype=tf.float32,shape=[3,4])
print("a:",a.get_shape())
b = tf.reshape(a,[3,2,2])
c = tf.reshape(a,[2,6])
print("a:",a.get_shape())
print("b:",b.get_shape())
print("c:",c.get_shape())
The above is the correct code , give the result as follows (reshape Yes, a new object will be generated , Do not change the original Tensor The shape of the ):
a: (3, 4)
a: (3, 4)
b: (3, 2, 2)
c: (2, 6)
版权声明
本文为[Stephen_ Tao]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230548468864.html
边栏推荐
- Error in created hook: "referenceerror:" promise "undefined“
- Go's gin framework learning
- Go对文件操作
- Kubernetes service discovery monitoring endpoints
- 239. Maximum value of sliding window (difficult) - one-way queue, large top heap - byte skipping high frequency problem
- 92. 反转链表 II-字节跳动高频题
- EasymodbusTCP之clientexample解析
- Amount input box, used for recharge and withdrawal
- 102. Sequence traversal of binary tree
- Halo 开源项目学习(二):实体类与数据表
猜你喜欢

Examination question bank and online simulation examination of the third batch (main person in charge) of special operation certificate of safety officer a certificate in Guangdong Province in 2022

Double pointer advanced -- leetcode title -- container with the most water

On the problem of V-IF display and hiding

Comparison between xtask and kotlin coroutine

2022年上海市安全员C证操作证考试题库及模拟考试

SystemVerilog(六)-变量

Go的Gin框架学习
![SQL optimization for advanced learning of MySQL [insert, primary key, sort, group, page, count]](/img/60/e4d47d458dd98a0c6ba51874e07c30.png)
SQL optimization for advanced learning of MySQL [insert, primary key, sort, group, page, count]

Compilation principle first set follow set select set prediction analysis table to judge whether the symbol string conforms to the grammar definition (with source code!!!)

394. String decoding - auxiliary stack
随机推荐
vite配置proxy代理解决跨域
Cloud native Virtualization: building edge computing instances based on kubevirt
Go对文件操作
JS interview question: FN call. call. call. Call (FN2) parsing
394. 字符串解码-辅助栈
41. The first missing positive number
Client example analysis of easymodbustcp
2022 Jiangxi Photovoltaic Exhibition, China distributed Photovoltaic Exhibition, Nanchang solar energy utilization Exhibition
470. 用 Rand7() 实现 Rand10()
剑指 Offer 22. 链表中倒数第k个节点-快慢指针
极致体验,揭晓抖音背后的音视频技术
cartographer_ There is no problem compiling node, but running the bug that hangs directly
Special effects case collection: mouse planet small tail
编译原理 求first集 follow集 select集预测分析表 判断符号串是否符合文法定义(有源码!!!)
列錶的使用-增删改查
92. 反转链表 II-字节跳动高频题
MySQL进阶学习之SQL优化【插入,主键,排序,分组,分页,计数】
关于gcc输出typeid完整名的方法
Listen for click events other than an element
Kubernetes 服务发现 监控Endpoints