Tensorflow中Tensor对象的常用方法(持续更新)
Tensor是Tensorflow中重要的对象。下面是Tensor的常用方法,后面还会写一篇随笔记录Variable的用法。
1. 生成一个(常)Tensor对象
>>>A = tf.constant(4)
>>>B = tf.constant([[1, 2], [3, 4]))
>>>A
<tf.Tensor: id=76, shape=(), dtype=int32, numpy=4>
>>>B
<tf.Tensor: id=77, shape=(2, 2), dtype=int32, numpy=
array([[1, 2],
[3, 4]], dtype=int32)>
Tensor对象和ndarray对象看起来很像但也有差别,一个最大的差别是Tensor是不可变的(immutable)。这意味着你永远也无法随心所欲的对Tensor进行赋值,只能新创建一个新的Tensor。
2. 和Ndarray的互相转换
>>>B.numpy()
array([[1, 2],
[3, 4]], dtype=int32)
>>>np.array(B)
array([[1, 2],
[3, 4]], dtype=int32)
>>>D = np.array([[1, 2], [3, 4]])
>>>tf.convert_to_tensor(D, dtype='int32')
<tf.Tensor: id=79, shape=(2, 2), dtype=int32, numpy=
array([[1, 2],
[3, 4]], dtype=int32)>
Tensorflow2引入了叫做Eager execution的机制,让Tensor和ndarray具有一样的运算灵活性。除了以上的转换方式之外,任意的Tensorflow操作都可以生成(返回)Tensor对象。
3. 矩阵运算
>>>a = tf.constant([[1, 2],
[3, 4]])
>>>b = tf.constant([[1, 1],
[1, 1]]) # Could have also said `tf.ones([2,2])`
>>>print(tf.add(a, b), "\n")
tf.Tensor(
[[2 3]
[4 5]], shape=(2, 2), dtype=int32)
>>>print(tf.multiply(a, b), "\n")
tf.Tensor(
[[1 2]
[3 4]], shape=(2, 2), dtype=int32)
>>>print(tf.matmul(a, b), "\n")
tf.Tensor(
[[3 3]
[7 7]], shape=(2, 2), dtype=int32)
以上三个操作返回的都是Tensor对象,同时这三个操作可以使用'+', '*', '@'代替。
4. 三种常用的操作
>>>c = tf.constant([[4.0, 5.0], [10.0, 1.0]])
>>>print(tf.reduce_max(c)) # Find the largest value
tf.Tensor(10.0, shape=(), dtype=float32)
>>>print(tf.argmax(c)) # Find the index of the largest value
tf.Tensor([1 0], shape=(2,), dtype=int64)
>>>print(tf.nn.softmax(c)) # # Compute the softmax
tf.Tensor(
[[2.6894143e-01 7.3105860e-01]
[9.9987662e-01 1.2339458e-04]], shape=(2, 2), dtype=float32)
三种看名字就能看出功能的操作,其中tf.reduce_XX()是tensorflow中降维的操作。类似的操作:'reduce_all', 'reduce_any', 'reduce_logsumexp', 'reduce_max', 'reduce_mean', 'reduce_min', 'reduce_prod', 'reduce_sum'。
5. Dtype转换
>>>the_f64_tensor = tf.constant([2.2, 3.3, 4.4], dtype=tf.float64)
>>>the_f16_tensor = tf.cast(the_f64_tensor, dtype=tf.float16)
# Now, let's cast to an uint8 and lose the decimal precision
>>>the_u8_tensor = tf.cast(the_f16_tensor, dtype=tf.uint8)
>>>print(the_u8_tensor)
tf.Tensor([2 3 4], shape=(3,), dtype=uint8)
numpy中可以使用astype()来进行转换,Tensorflow中则使用tf.cast()方法来转化不同数据类型的Tensor。
6. 广播操作
Tensor的广播操作和numpy中基本完全一样,机制可以看这篇文章:https://jakevdp.github.io/PythonDataScienceHandbook/02.05-computation-on-arrays-broadcasting.html
Tensorflow中Tensor对象的常用方法(持续更新)的更多相关文章
- jQuery常用方法(持续更新) jQuery(转)
0.常用代码: 请容许我在1之前插入一个0,我觉得我有必要把最常用的代码放在第一位,毕竟大部分时间大家都是找代码的. (1)AJAX请求 $(function() { $('#send').click ...
- PHP 日常开发过程中的bug集合(持续更新中。。。)
PHP 日常开发过程中的bug集合(持续更新中...) 在日常php开发过程中,会遇到一些意想不到的bug,所以想着把这些bug记录下来,以免再犯! 1.字符串 '0.00'.'0.0'.'0' 是 ...
- 好用的函数,assert,random.sample,seaborn tsplot, tensorflow.python.platform flags 等,持续更新
python 中好用的函数,random.sample等,持续更新 random.sample random.sample的函数原型为:random.sample(sequence, k),从指定序列 ...
- Android中常用开发工具类—持续更新...
一.自定义ActionBar public class ActionBarTool { public static void setActionBarLayout(Activity act,Conte ...
- 获取tensorflow中tensor的值
tensorflow中的tensor值的获取: import tensorflow as tf #定义变量a a=tf.Variable([[[1,2,3],[4,5,6]],[[7,8,9],[10 ...
- Android开发中常用的库总结(持续更新)
这篇文章用来收集Android开发中常用的库,都是实际使用过的.持续更新... 1.消息提示的小红点 微信,微博消息提示的小红点. 开源库地址:https://github.com/stefanjau ...
- 对Tensorflow中tensor的理解
Tensor即张量,在tensorflow中所有的数据都通过张量流来传输,在看代码的时候,对张量的概念很不解,很容易和矩阵弄混,今天晚上查了点资料,并深入了解了一下,简单总结一下什么是张量的阶,以及张 ...
- 【python】实践中的总结——列表『持续更新中』
2016-04-03 21:02:50 python list的遍历 list[a::b] #从list[a] 开始,每b个得到一个元组,返回新的list 举个例子: >>> l ...
- 3.C#/.NET编程中的常见异常(持续更新)
1.Object reference not set to an instance of an object. 未将对象引用(引用)到对象的实例,说白了就是有个对象为null,但是你在用它点出来的各种 ...
随机推荐
- ShoneSharp语言(S#)的设计和使用介绍系列(6)— 字符串String
ShoneSharp语言(S#)的设计和使用介绍 系列(6)— 字符串String 作者:Shone 声明:原创文章欢迎转载,但请注明出处,https://www.cnblogs.com/ShoneS ...
- sklearn学习:为什么roc_auc_score()和auc()有不同的结果?
为什么roc_auc_score()和auc()有不同的结果? auc():计算ROC曲线下的面积.即图中的area roc_auc_score():计算AUC的值,即输出的AUC 最佳答案 AUC并 ...
- 痞子衡嵌入式:MCUBootUtility v2.3发布,这次不再放过任何一款Flash
-- 痞子衡的 NXP-MCUBootUtility 开源项目自2018年8月27日第一笔提交至今已有21个月,目前累计代码已近50000行.相信这个工具为大家开发 i.MXRT 项目提供了一些便利, ...
- 善意的投票&小M的作物 题解
善意的投票: 因为只有\(2\)种意愿,不妨让想睡午觉的和源点连边,让不想睡午觉的和汇点连边.对于每一对好朋友,在他们之间连边.那么只要源点和汇点还联通,就存在一对好友是冲突的,我们现在要做的就是删去 ...
- Excel常用小方法
Excel快捷键 Excel中处理工作表的快捷键 插入新工作表 Shift+F11或Alt+Shift+F1 移动到工作簿中的下一张工作表 Ctrl+PageDown 移动到工作簿中的上一张工作表 C ...
- GitHub+jsDelivr+PicGo 打造稳定快速、高效免费图床
标题: GitHub+jsDelivr+PicGo 打造稳定快速.高效免费图床 作者: 梦幻之心星 347369787@QQ.com 标签: [GitHub, 图床] 目录: 图床 日期: 2019- ...
- 八、【spring】web应用安全设计
内容 Spring Security 使用Servlet规范中的Filter保护Web应用 基于数据库和LDAP进行认证 关键词 8.1 理解Spring Security模块 Spring Secu ...
- 从0开始探究vue-双向绑定原理
理解 vue是一个非常优秀的框架,其优秀的双向绑定原理,mvvm模型,组件,路由解析器等,非常的灵活方便,也使开发者能够着重于数据处理,让开发者更清晰的设计自己的业务. 双向绑定,就是数据变化的时候, ...
- Java实现 LeetCode 757 设置交集大小至少为2(排序+滑动窗口)
757. 设置交集大小至少为2 一个整数区间 [a, b] ( a < b ) 代表着从 a 到 b 的所有连续整数,包括 a 和 b. 给你一组整数区间intervals,请找到一个最小的集合 ...
- Java实现 LeetCode 707 设计链表(环形链表)
707. 设计链表 设计链表的实现.您可以选择使用单链表或双链表.单链表中的节点应该具有两个属性:val 和 next.val 是当前节点的值,next 是指向下一个节点的指针/引用.如果要使用双向链 ...