tf.convert_to_tensor
参考 tf.convert_to_tensor - 云+社区 - 腾讯云
将给定值转换为张量。
tf.convert_to_tensor(value,dtype=None,dtype_hint=None,name=None
)
该函数将各种类型的Python对象转换为张量对象。它接受张量对象、数字数组、Python列表和Python标量。
例:
import numpy as npdef my_func(arg):arg = tf.convert_to_tensor(arg, dtype=tf.float32)return tf.matmul(arg, arg) + arg# The following calls are equivalent.
value_1 = my_func(tf.constant([[1.0, 2.0], [3.0, 4.0]]))
value_2 = my_func([[1.0, 2.0], [3.0, 4.0]])
value_3 = my_func(np.array([[1.0, 2.0], [3.0, 4.0]], dtype=np.float32))
这个函数在用Python编写新操作时非常有用(如上面示例中的my_func)。所有标准的Python op构造函数都将此函数应用于它们的每个张量值输入,这使得这些ops除了接受张量对象外,还可以接受numpy数组、Python列表和标量。
参数:
value
:类型具有注册张量转换函数的对象。- dtype:返回张量的可选元素类型。如果缺少,则从值的类型推断类型。
- dtype_hint:返回张量的可选元素类型,当dtype为None时使用。在某些情况下,调用者在转换为张量时可能没有考虑到dtype,因此dtype_hint可以用作软首选项。如果不能转换为dtype_hint,则此参数没有效果。
- name:创建新张量时使用的可选名称。
返回值:
- 一个基于值的张量。
可能产生的异常:
TypeError
: If no conversion function is registered forvalue
todtype
.RuntimeError
: If a registered conversion function returns an invalid value.ValueError
: If thevalue
is a tensor not of givendtype
in graph mode.
tf.convert_to_tensor相关推荐
- tensorflow 之 最近用到的几个小操作tf.reshape,tf.convert_to_tensor,tf.where
1.tf.reshape() #调整tensor的形状 img = .... img_res = tf.reshape(img, [-1]) 着重记录是shape=[-1]时,相当于flatten操作 ...
- tf.convert_to_tensor()
这是个很有用的函数,我们经常需要将python的数据类型转换成TensorFlow可用的tensor数据类型,所以仔细研究一下这个函数还是很有必要的. 参考官方说明文档 format:convert_ ...
- tf.convert_to_tensor的用法
tf.convert_to_tensor用于将不同数据变成张量:比如可以让数组变成张量.也可以让列表变成张量. 例如: import tensorflow as tf; import numpy as ...
- 深度学习原理与框架-CNN在文本分类的应用 1.tf.nn.embedding_lookup(根据索引数据从数据中取出数据) 2.saver.restore(加载sess参数)...
1. tf.nn.embedding_lookup(W, X) W的维度为[len(vocabulary_list), 128], X的维度为[?, 8],组合后的维度为[?, 8, 128] 代码说 ...
- tf.keras.layers.MaxPool2D 最大池化层 示例
tf.keras.layers.MaxPool2D 最大池化层 示例 import tensorflow as tf import numpy as np inputs = np.random.ran ...
- tf.keras.layers.MaxPool2D 示例 池化层
tf.keras.layers.MaxPool2D 示例 池化层 import tensorflow as tf import numpy as np inputs = np.random.randi ...
- tf.keras.activations.softmax 激活函数 示例
文章目录 softmax 将值的向量转换为概率分布 创建数组 计算公式 计算过程 tf.keras.activations.softmax实现 numpy实现 softmax 将值的向量转换为概率分布 ...
- TF之LSTM:基于Tensorflow框架采用PTB数据集建立LSTM网络的自然语言建模
TF之LSTM:基于Tensorflow框架采用PTB数据集建立LSTM网络的自然语言建模 目录 关于PTB数据集 代码实现 关于PTB数据集 PTB (Penn Treebank Dataset)文 ...
- TF之RNN:实现利用scope.reuse_variables()告诉TF想重复利用RNN的参数的案例
TF之RNN:实现利用scope.reuse_variables()告诉TF想重复利用RNN的参数的案例 目录 输出结果 代码设计 输出结果 后期更新-- 代码设计 import tensorflow ...
最新文章
- jQuery EasyUI使用教程之创建可折叠面板
- Delphi中高级DLL的编写和调用
- POJ 2251 Dungeon Master
- CentOS系统更换yum源(repomd.xml not found解决方案)
- [蓝桥杯2019初赛]数列求值-模拟+数论
- GridView 中 Bind和Eval的区别详解
- vs vb连接mysql_详细介绍VS2010 VB ACCESS数据库的连接(如有不对,敬请改正)
- 手把手带你入坑迁移学习(by 当过黑客的CTO大叔)
- [Music]乡村摇滚:Any man of mine
- 好烦,一封报警邮件,大量服务节点 redis 响应超时,又得要捉“虫”!
- mysql 覆盖索引_MySQL 覆盖索引
- 远程控制电脑的实现方法
- 2串口两串口三串口多串口3串口转WiFi透传模块实现多通道与服务器透传
- 读书笔记(穷查理宝典)
- 如何在word中批量编辑所有图片的大小?
- Visual Studio 2005 Service Pack 1 (SP1)
- SQL总结 学期前8周学习内容
- 【李佳辉_周报_2022.10.23】
- 时间最优轨迹规划(3-5-3次多项式)
- 分治策略时间复杂度计算