当前位置:网站首页>Tf.constant usage
Tf.constant usage
2022-07-26 03:22:00 【phac123】
The function prototype
tf.constant(
value,
dtype=None,
shape=None,
name='Const',
verify_shape=False
)
- The first value is required , It can be a number , It can also be a list ; In order to view the results, you must create a session , And use the value function eval() To view the created tensor Value ; The last four parameters can be written or not
- The second parameter represents the data type , It can generally be tf.float32, tf.float64 etc.
- The third parameter represents the tensor “ shape ”, That is, the dimension and the size of each dimension . If the third parameter is specified , When the first parameter value When it's numbers , All elements of the tensor are filled with this number ;( And when the first parameter value When it's a list , Note that the length of the list must be less than or equal to the third parameter shape Size ( That is, the product of the dimensions ), Otherwise, an error will be reported ;); This is because the function generates a shape The tensor of size , And then use value The values in this list are filled one by one shape The elements in . Here the list size is 7, and shape The size is 2*3=6, Cannot fill correctly , So something went wrong . And if the list size is smaller than shape size , Then the remaining tensor elements will be filled with the last element of the list :
- Fourth parameter name It can be anything , Mainly string .
- Fifth parameter verify_shape The default is False, If it's changed to True Words mean inspection value The shape and shape of shape Is it consistent with , If it does not match, an error will be reported .
Code
import tensorflow._api.v2.compat.v1 as tf
tf.disable_v2_behavior()
''' tf.constant( value, dtype=None, shape=None, name='Const', verify_shape=False ) '''
# The first parameter is a value
tmp = tf.constant(1)
sess = tf.Session()
with sess.as_default():
print(tmp.eval())
# The first parameter is a list
tmp = tf.constant([1, 2])
sess = tf.Session()
with sess.as_default():
print(tmp.eval())
# Specify the second parameter , Specify the data type
tmp = tf.constant([1, 2], dtype = tf.float32)
sess = tf.Session()
with sess.as_default():
print(tmp.eval())
# Specify the third parameter : For specifying tensors " shape "
tmp = tf.constant(3, shape=[2, 2])
sess = tf.Session()
with sess.as_default():
print(tmp.eval())
# Specify the fourth parameter : character string
tmp = tf.constant([2, 3], dtype = tf.float32, shape=[2,2], name = "LiQuan")
print(tmp)
Output :
''' [1. 2.] [[3 3] [3 3]] Tensor("LiQuan:0", shape=(2, 2), dtype=float32) '''
边栏推荐
- "Xiao Deng's view" the value brought by Siem to enterprises (II)
- YOLOv3: An Incremental Improvement
- ByteDance (Tiktok) software test monthly salary 23K post, technical two-sided interview questions are newly released
- Completion report of communication software development and Application
- Canvas -- drawing of rectangle -- making of histogram
- Intensive reading of the paper -yolov1:you only look once:unified, real time object detection
- Use VRRP technology to realize gateway equipment redundancy, with detailed configuration experiments
- Cloud native guide what is cloud native infrastructure
- 78. Subset
- 小测(一)
猜你喜欢

HCIP第十四天
![[tensorflow & pytorch] image data enhancement API](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[tensorflow & pytorch] image data enhancement API

Offline data warehouse from 0 to 1 - phase I resource purchase configuration

Intensive reading of the paper -yolov1:you only look once:unified, real time object detection

Why did Mr. Cao, a productionist in the field of high-end tea, choose Ruifeng L6 max?

ENVI_ Idl: create HDF5 file and write data (take writing GeoTIFF file to HDF file as an example) + detailed parsing

Matlab simulation of vertical handover between MTD SCDMA and TD LTE dual networks

UDP和TCP可以使用同一个端口吗?

【无标题】

PXE efficient batch network installation
随机推荐
2022-07-21 第四小组 修身课 学习笔记(every day)
LoRa和NB-IOT可用用在哪些地方
Chen Yili, China Academy of communications technology: cost reduction and efficiency increase are the greatest value of Enterprise Cloud native applications
Classic interview questions -- three characteristics of OOP language
小测(一)
经典面试问题——OOP语言的三大特征
MPLS basic experiment configuration
els 初始化窗口类
2022-07-21 group 4 polymorphism
Sentinel vs Hystrix 到底怎么选?
Installation and operation of orb-slam2 under ROS
爆肝出了4W字的Redis面试教程
Service gateway (zuul)
QT signal transmission between multi-level objects signal transmission between multi-level nested class objects
78. 子集
UDP和TCP可以使用同一个端口吗?
ue4如何进行静态渲染?5个步骤生成静态渲染
记一次SQL优化
堆内存与栈内存的区别?
使用anaconda配置gpu版本的tensorflow(30系列以下显卡)