Tensorflow tf.dynamic_partition矩阵拆分示例(Python3)
作者:胡大炮的妖孽人生 发布时间:2021-07-10 10:35:25
标签:Tensorflow,tf.dynamic,partition,矩阵拆分
先给出一个样例看看
import tensorflow as tf
raw = tf.constant([1, 2, 3, 4, 5, 6, 6, 5, 4, 3, 2, 1])
'''
拆成 [1,2] [3,4] [5,6] [6,5] [4,3] [2,1]
'''
result_1 = tf.dynamic_partition(tf.reshape(raw, [6,2]),[0, 1, 2, 3, 4, 5], 6)
'''
拆成 [1, 2, 3, 4, 5, 6] [6, 5, 4, 3, 2, 1]
'''
result_2 = tf.dynamic_partition(tf.reshape(raw, [2, 6]), [0, 1], 2)
'''
拆成 [1] [2] [3] [4] [5] [6] [6] [5] [4] [3] [2] [1]
'''
result_3 = tf.dynamic_partition(tf.reshape(raw, [12, 1]), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], 12)
with tf.Session() as sess:
print(sess.run(result_1))
print(sess.run(result_2))
print(sess.run(result_3))
结果
[array([[1, 2]]), array([[3, 4]]), array([[5, 6]]), array([[6, 5]]), array([[4, 3]]), array([[2, 1]])]
[array([[1, 2, 3, 4, 5, 6]]), array([[6, 5, 4, 3, 2, 1]])]
[array([[1]]), array([[2]]), array([[3]]), array([[4]]), array([[5]]), array([[6]]), array([[6]]), array([[5]]), array([[4]]), array([[3]]), array([[2]]), array([[1]])]
再给出一个样例
Py3代码:
# one-hot 函数的样例
import tensorflow as tf
label = tf.placeholder(tf.int32,[None])
# 直接把 输入的序列进行One-Hot的结果
one_hot = tf.one_hot(label, 3, 1, 0)
# 进行转置
one_hot_new = tf.transpose(one_hot, perm=[1,0])
one_hot_new = tf.cast(one_hot_new, tf.float32)
# one_hot_new[2] = one_hot_new[2] * 1.5
# 按照每一维的大小进行拆分
one_hot_new_1 = tf.dynamic_partition(one_hot_new, [0, 1, 1], 2)[0]
one_hot_new_2 = tf.dynamic_partition(one_hot_new, [1, 0, 1], 2)[0]
one_hot_new_3 = tf.dynamic_partition(one_hot_new, [1, 1, 0], 2)[0]
# 按照每一维大小进行拆分
one_hot_1 = tf.dynamic_partition(one_hot_new, [0, 1, 2], 3)[0]
one_hot_2 = tf.dynamic_partition(one_hot_new, [0, 1, 2], 3)[1]
one_hot_3 = tf.dynamic_partition(one_hot_new, [0, 1, 2], 3)[2]
# one_hot_new_3 = tf.dynamic_partition(one_hot_new, [0, 0, 1], 2)[2]
# 拼接以上两维得到原来的结果
one_hot_new = tf.concat([one_hot_new_1, one_hot_new_2], axis=0)
if __name__ == '__main__':
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
one_hot_out, one_hot_new_out, one_hot_new_1_out, one_hot_new_2_out, one_hot_new_3_out, one_hot_1_out, one_hot_2_out, one_hot_3_out = sess.run([one_hot, one_hot_new, one_hot_new_1, one_hot_new_2, one_hot_new_3, one_hot_1, one_hot_2, one_hot_3], feed_dict={label: [0, 1, 1, 2, 2, 0, 0, 1, 2, 2, 0, 2]})
print("原始的One-hot结果:")
print(one_hot_out, end='\n\n')
print("以上的结果.T:")
print("方法一拆分:")
print(one_hot_new_out, end='\n\n')
print("拆分(1)维:")
print(one_hot_new_1_out, end='\n\n')
print("拆分 (2)维:")
print(one_hot_new_2_out, end='\n\n')
print("拆分 (3)维:")
print(one_hot_new_3_out, end='\n\n')
print("方法二拆分:")
print("拆分(1)维:")
print(one_hot_1_out, end='\n\n')
print("拆分 (2)维:")
print(one_hot_2_out, end='\n\n')
print("拆分 (3)维:")
print(one_hot_3_out, end='\n\n')
控制台输出:
原始的One-hot结果:
[[1 0 0]
[0 1 0]
[0 1 0]
[0 0 1]
[0 0 1]
[1 0 0]
[1 0 0]
[0 1 0]
[0 0 1]
[0 0 1]
[1 0 0]
[0 0 1]]
以上的结果.T:
方法一拆分:
[[ 1. 0. 0. 0. 0. 1. 1. 0. 0. 0. 1. 0.]
[ 0. 1. 1. 0. 0. 0. 0. 1. 0. 0. 0. 0.]]
拆分(1)维:
[[ 1. 0. 0. 0. 0. 1. 1. 0. 0. 0. 1. 0.]]
拆分 (2)维:
[[ 0. 1. 1. 0. 0. 0. 0. 1. 0. 0. 0. 0.]]
拆分 (3)维:
[[ 0. 0. 0. 1. 1. 0. 0. 0. 1. 1. 0. 1.]]
方法二拆分:
拆分(1)维:
[[ 1. 0. 0. 0. 0. 1. 1. 0. 0. 0. 1. 0.]]
拆分 (2)维:
[[ 0. 1. 1. 0. 0. 0. 0. 1. 0. 0. 0. 0.]]
拆分 (3)维:
[[ 0. 0. 0. 1. 1. 0. 0. 0. 1. 1. 0. 1.]]
来源:https://blog.csdn.net/huplion/article/details/79600232


猜你喜欢
- format()函数"""测试 format()函数"""def t
- 双向链表的基本操作的实现,供大家参考,具体内容如下在之前的博客中介绍了三种链表,分别是单链表、单向循环链表以及双向链表。本篇博客将用Pyth
- 原则, 以datetime为中心, 起点或中转, 转化为目标对象, 涵盖了大多数业务场景中需要的日期转换处理步骤:1. 掌握几种对象及其关系
- 今天发现有一个备份的mysql数据文件夹异常变大,一查发现是多了三个文件:ibdata1 ib_logfile0 ib_logfile1,前
- 1.创建一个项目django-admin.py startproject HelloWorld2.进入HelloWorld项目,在manag
- 大家都知道,不同字符编码,其在内存占用的字节数不一样。如ASCII编码字符占用1个字节,U
- < % Response.CharSet="gb2312" tblna
- 在对数值进行格式化的时候,一个常见的问题是按照千分位格式化,网上对这个问题已经有很多种解决方法了,还可以利用Array.prototype.
- ERROR 1819 (HY000): Your password does not satisfy the current policy
- Python是支持可视化编程,即编写gui程序,你可以用它来编写自己喜欢的桌面程序。使用wxPython来做界面非常的简单,只是不能像C#一
- 最近使用Python调用百度的REST API实现语音识别,但是百度要求音频文件的压缩方式只能是pcm(不压缩)、wav、opus、spee
- 一、静态方法(staticmethod)和类方法(classmethod)类方法:有个默认参数cls,并且可以直接用类名去调用,可以与类属×
- 一、石头剪刀布游戏目标:创建一个命令行游戏,游戏者可以在石头、剪刀和布之间进行选择,与计算机PK。如果游戏者赢了,得分就会添加,直到结束游戏
- python版本要求在3.3.x,需要mysql connector for python第三方库支持不适用所有的zencart导入到mag
- if exists (select * from dbo.sysobjects where id = object_id(N'[db
- 导语轻松瘦 | 和闺蜜减肥的日常,谁说闺蜜是减肥路上的一座山?哈喽!大家好!我是木木子吖~小编有一个闺蜜,还是同一所学校读书毕业的,这体重在
- 发送电子邮件在即时通信软件如此发达的今天,电子邮件仍然是互联网上使用最为广泛的应用之一,公司向应聘者发出录用通知、网站向用户发送一个激活账号
- 本文实例讲述了python通过wxPython打开一个音频文件并播放的方法。分享给大家供大家参考。具体如下:这段代码片段使用wx.lib.f
- MySQL去重的方法整理【初级】有极少的重复行使用distinct查出来,然后手动一行一行删除。【中级】按照单个字段的重复去重例如:对id字
- RSS是 Really Simple Syndication的缩写(对rss2.0而言,是这三个词的缩写,对rss1.0而言则是RDF Si