当前位置:网站首页>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) '''
边栏推荐
- tf.constant用法
- dataframe整理:datetime格式分拆;删除特定行;分组整合。
- els 注册窗口类、创建窗口类、显示窗口
- 离线数据仓库从0到1-阶段二软件安装
- Use VRRP technology to realize gateway equipment redundancy, with detailed configuration experiments
- canvas——矩形的绘制——柱状图的制作
- Classic interview questions -- three characteristics of OOP language
- 阿里二面:千万级数据量的表,快速查询如何进行?
- els 初始化窗口类
- [noip2001 popularization group] packing problem
猜你喜欢

Looking at the next step of BAIC bluevale through the 8billion fund-raising, product upgrading and building core capabilities are the key words

LeetCode·

Execution process behind shell commands

经典面试问题——OOP语言的三大特征

Sentinel vs Hystrix 到底怎么选?

MPLS basic experiment configuration

easyExcel设置行隐藏,解决setHidden(true)失效问题

Small test (I)

Functions and usage of snownlp Library

Cloud native guide what is cloud native infrastructure
随机推荐
Service gateway (zuul)
C语言预处理指令以及Makefile脚本讲解
大厂面试都面试些啥,看了不亏(一)
els 窗口设置、WM_CREATE、WM_PAINT
MPLS basic experiment configuration
Configuration and use of virtualservice, gateway and destinationrule of istio III
What are the methods of array sorting in JS
实现一个方法,找出数组中的第k大和第m大的数字相加之和
Win11 method of changing disk drive letter
QT signal transmission between multi-level objects signal transmission between multi-level nested class objects
Get twice the result with half the effort: learn the web performance test case design model
2020 AF-RCNN: An anchor-free convolutional neural network for multi-categoriesagricultural pest det
2022-07-21 study notes of group 4 self-cultivation class (every day)
Illustration leetcode - 5. Longest palindrome substring (difficulty: medium)
Hcip day 8 notes sorting (OSPF routing control, Appendix E, anti ring, shortest path calculation, republication))
Jsd-2204-cool shark Mall (Management Commodity module) -day02
2022-07-21 第四小组 多态
Course notes of single chip microcomputer principle and interface technology for migrant workers majoring in electronic information engineering
Cloud native guide what is cloud native infrastructure
Execution process behind shell commands