深入浅出TensorFlow2函数——tf.constant
分类目录:《深入浅出TensorFlow2函数》总目录
相关文章:
· 深入浅出TensorFlow2函数——tf.constant
· 深入浅出TensorFlow2函数——tf.Tensor
· 深入浅出Pytorch函数——torch.tensor
· 深入浅出Pytorch函数——torch.as_tensor
· 深入浅出PaddlePaddle函数——paddle.to_tensor
语法
tf.constant(value, dtype=None, shape=None, name='Const'
)
参数
value
:输出张量的常数值。dtype
:输出张量元素的类型。shape
:[可选] 张量的形状。name
:[可选] 张量的名称。
返回值
一个常数张量。
实例
# Constant 1-D Tensor from a python list.
tf.constant([1, 2, 3, 4, 5, 6])
<tf.Tensor: shape=(6,), dtype=int32,numpy=array([1, 2, 3, 4, 5, 6], dtype=int32)>
# Or a numpy array
a = np.array([[1, 2, 3], [4, 5, 6]])
tf.constant(a)
<tf.Tensor: shape=(2, 3), dtype=int64, numpy=array([[1, 2, 3],[4, 5, 6]])>
函数实现
@tf_export("constant", v1=[])
def constant(value, dtype=None, shape=None, name="Const"):"""Creates a constant tensor from a tensor-like object.Note: All eager `tf.Tensor` values are immutable (in contrast to`tf.Variable`). There is nothing especially _constant_ about the valuereturned from `tf.constant`. This function is not fundamentally different from`tf.convert_to_tensor`. The name `tf.constant` comes from the `value` beingembedded in a `Const` node in the `tf.Graph`. `tf.constant` is usefulfor asserting that the value can be embedded that way.If the argument `dtype` is not specified, then the type is inferred fromthe type of `value`.>>> # Constant 1-D Tensor from a python list.>>> tf.constant([1, 2, 3, 4, 5, 6])<tf.Tensor: shape=(6,), dtype=int32,numpy=array([1, 2, 3, 4, 5, 6], dtype=int32)>>>> # Or a numpy array>>> a = np.array([[1, 2, 3], [4, 5, 6]])>>> tf.constant(a)<tf.Tensor: shape=(2, 3), dtype=int64, numpy=array([[1, 2, 3],[4, 5, 6]])>If `dtype` is specified, the resulting tensor values are cast to the requested`dtype`.>>> tf.constant([1, 2, 3, 4, 5, 6], dtype=tf.float64)<tf.Tensor: shape=(6,), dtype=float64,numpy=array([1., 2., 3., 4., 5., 6.])>If `shape` is set, the `value` is reshaped to match. Scalars are expanded tofill the `shape`:>>> tf.constant(0, shape=(2, 3))<tf.Tensor: shape=(2, 3), dtype=int32, numpy=array([[0, 0, 0],[0, 0, 0]], dtype=int32)>>>> tf.constant([1, 2, 3, 4, 5, 6], shape=[2, 3])<tf.Tensor: shape=(2, 3), dtype=int32, numpy=array([[1, 2, 3],[4, 5, 6]], dtype=int32)>`tf.constant` has no effect if an eager Tensor is passed as the `value`, iteven transmits gradients:>>> v = tf.Variable([0.0])>>> with tf.GradientTape() as g:... loss = tf.constant(v + v)>>> g.gradient(loss, v).numpy()array([2.], dtype=float32)But, since `tf.constant` embeds the value in the `tf.Graph` this fails forsymbolic tensors:>>> with tf.compat.v1.Graph().as_default():... i = tf.compat.v1.placeholder(shape=[None, None], dtype=tf.float32)... t = tf.constant(i)Traceback (most recent call last):...TypeError: ...`tf.constant` will create tensors on the current device. Inputs which arealready tensors maintain their placements unchanged.Related Ops:* `tf.convert_to_tensor` is similar but:* It has no `shape` argument.* Symbolic tensors are allowed to pass through.>>> with tf.compat.v1.Graph().as_default():... i = tf.compat.v1.placeholder(shape=[None, None], dtype=tf.float32)... t = tf.convert_to_tensor(i)* `tf.fill`: differs in a few ways:* `tf.constant` supports arbitrary constants, not just uniform scalarTensors like `tf.fill`.* `tf.fill` creates an Op in the graph that is expanded at runtime, so itcan efficiently represent large tensors.* Since `tf.fill` does not embed the value, it can produce dynamicallysized outputs.Args:value: A constant value (or list) of output type `dtype`.dtype: The type of the elements of the resulting tensor.shape: Optional dimensions of resulting tensor.name: Optional name for the tensor.Returns:A Constant Tensor.Raises:TypeError: if shape is incorrectly specified or unsupported.ValueError: if called on a symbolic tensor."""return _constant_impl(value, dtype, shape, name, verify_shape=False,allow_broadcast=True)
深入浅出TensorFlow2函数——tf.constant相关推荐
- 深入浅出TensorFlow2函数——tf.reshape
分类目录:<深入浅出TensorFlow2函数>总目录 语法 tf.reshape(tensor, shape, name=None ) 参数 返回值 返回一个新的形状为shape的tf. ...
- 深入浅出TensorFlow2函数——tf.data.Dataset.batch
分类目录:<深入浅出TensorFlow2函数>总目录 语法 batch(batch_size, drop_remainder=False, num_parallel_calls=None ...
- 深入浅出TensorFlow2函数——tf.keras.layers.Embedding
分类目录:<深入浅出TensorFlow2函数>总目录 语法 tf.keras.layers.Embedding(input_dim, output_dim, embeddings_ini ...
- 深入浅出TensorFlow2函数——tf.keras.layers.Dense
分类目录:<深入浅出TensorFlow2函数>总目录 tf.keras.layers.Dense实现操作:output = activation(dot(input, kernel) + ...
- 深入浅出PaddlePaddle函数——paddle.Tensor
分类目录:<深入浅出PaddlePaddle函数>总目录 相关文章: · 深入浅出TensorFlow2函数--tf.Tensor · 深入浅出Pytorch函数--torch.Tenso ...
- 深入浅出Pytorch函数——torch.arange
分类目录:<深入浅出Pytorch函数>总目录 相关文章: · 深入浅出TensorFlow2函数--tf.range · 深入浅出Pytorch函数--torch.arange · 深入 ...
- 深入浅出Pytorch函数——torch.exp
分类目录:<深入浅出Pytorch函数>总目录 相关文章: · 深入浅出TensorFlow2函数--tf.exp · 深入浅出TensorFlow2函数--tf.math.exp · 深 ...
- 深入浅出PaddlePaddle函数——paddle.arange
分类目录:<深入浅出PaddlePaddle函数>总目录 相关文章: · 深入浅出TensorFlow2函数--tf.range · 深入浅出Pytorch函数--torch.arange ...
- 深入浅出PaddlePaddle函数——paddle.numel
分类目录:<深入浅出PaddlePaddle函数>总目录 相关文章: · 深入浅出TensorFlow2函数--tf.size · 深入浅出Pytorch函数--torch.numel · ...
最新文章
- 【OpenCV 4开发详解】分割图像——Mean-Shift分割算法
- python 提交表单登录不成功_Python http requests模拟登录与提交表单的实现问题
- ## Hive分析疫情数据
- python实现八皇后问题(百练OJ:2754:八皇后)
- 3、常用关键字,变量赋值,多个变量赋值,标准数据类型,数字,字符串,列表,元组,字典,数据类型转换
- 工作十余年,还是一直被问 委托和事件 有什么区别? 真是够了
- 深度学习之 soft-NMS
- 科学的分析猪八戒到底是什么猪,黑猪还是白猪?
- C#中的Dictionary字典类介绍(转载)
- 【如意影视】运营级+完整类库+解析线路+无限增加或删减解析接口+如意可视化播放器1.1
- Git ~ 添加远程仓库 ~Git
- 一个好看的CSS样式表格
- yapi 权限_yapi接口管理平台手册
- 海南自贸区电信行业环境分析
- 电力LED时钟系统解决方案实现精确时间同步
- Ubuntu20.04、22.04安装nvidia显卡驱动——超详细、最简单
- Python中参数前面的星号
- 医保是不是只有住院才能在单位报销,什么样的病才能报销
- React 设置网页title
- 整理的金蝶云苍穹初级练习题
热门文章
- js获得当前日期时间
- 最小二乘法matlab流程图,最小二乘法 MATLAB
- 2017安徽省计算机一级试题,2017年计算机一级练习试题「附答案解析」
- 【Linux】解决外置光驱出现Cannot find kickstart file on CDROM
- python相关性系数显著性检验_Excel/SPSS相关性及显著性水平分析
- <微机原理>[汇编语言]-[实验九]电子时钟
- 早报:华为Mate30发布会结束,各项参数汇总,你是否会选择入手
- python cmdb 马哥_简易的CMDB服务端
- gradle全局代理
- MySQL数据库删除数据后自增ID不连续的问题