Tensorflow tf.dynamic_partition矩阵拆分示例(Python3)


Posted in Python onFebruary 07, 2020

先给出一个样例看看

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.]]

以上这篇Tensorflow tf.dynamic_partition矩阵拆分示例(Python3) 就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python实现定时播放mp3
Mar 29 Python
python3新特性函数注释Function Annotations用法分析
Jul 28 Python
K-means聚类算法介绍与利用python实现的代码示例
Nov 13 Python
Python使用matplotlib的pie函数绘制饼状图功能示例
Jan 08 Python
python opencv之SIFT算法示例
Feb 24 Python
python实现log日志的示例代码
Apr 28 Python
Python 实现引用其他.py文件中的类和类的方法
Apr 29 Python
python BlockingScheduler定时任务及其他方式的实现
Sep 19 Python
详解python tkinter模块安装过程
Jan 06 Python
python多进程 主进程和子进程间共享和不共享全局变量实例
Apr 25 Python
python自动化调用百度api解决验证码
Apr 13 Python
Python下载商品数据并连接数据库且保存数据
Mar 31 Python
Python reshape的用法及多个二维数组合并为三维数组的实例
Feb 07 #Python
tensorflow 利用expand_dims和squeeze扩展和压缩tensor维度方式
Feb 07 #Python
Tensorflow进行多维矩阵的拆分与拼接实例
Feb 07 #Python
Tensorflow训练模型越来越慢的2种解决方案
Feb 07 #Python
详解python itertools功能
Feb 07 #Python
Python中itertools的用法详解
Feb 07 #Python
Python转换itertools.chain对象为数组的方法
Feb 07 #Python
You might like
windows7配置Nginx+php+mysql的详细教程
2016/09/04 PHP
php 实现一个字符串加密解密的函数实例代码
2016/11/01 PHP
PHP SESSION机制的理解与实例
2019/03/22 PHP
PHP模版引擎原理、定义与用法实例
2019/03/29 PHP
extjs之去除s.gif的影响
2010/12/25 Javascript
js导出table数据到excel即导出为EXCEL文档的方法
2013/10/10 Javascript
JavaScript实现自动生成网页元素功能(按钮、文本等)
2015/11/21 Javascript
Vue.js报错Failed to resolve filter问题的解决方法
2016/05/25 Javascript
JS 动态加载js文件和css文件 同步/异步的两种简单方式
2016/09/23 Javascript
Easyui笔记2:实现datagrid多行删除的示例代码
2017/01/14 Javascript
Ajax基础知识详解
2017/02/17 Javascript
强大的JavaScript响应式图表Chartist.js的使用
2017/09/13 Javascript
jquery实现Ajax请求的几种常见方式总结
2019/05/28 jQuery
手把手教你 CKEDITOR 4 扩展插件制作
2019/06/18 Javascript
echarts实现折线图的拖拽效果
2019/12/19 Javascript
基于JavaScript的数据结构队列动画实现示例解析
2020/08/06 Javascript
[01:32:22]DOTA2-DPC中国联赛 正赛 Ehome vs VG BO3 第一场 2月5日
2021/03/11 DOTA
Python字符串格式化的方法(两种)
2017/09/19 Python
PyCharm更改字体和界面样式的方法步骤
2019/09/27 Python
Django REST Framework之频率限制的使用
2019/09/29 Python
Python 余弦相似度与皮尔逊相关系数 计算实例
2019/12/23 Python
Python使用文件操作实现一个XX信息管理系统的示例
2020/07/02 Python
Pycharm新手使用教程(图文详解)
2020/09/17 Python
科沃斯机器人官网商城:Ecovacs
2016/08/29 全球购物
全球知名的珠宝首饰品牌:Kay Jewelers
2018/02/11 全球购物
祖国在我心中演讲稿
2014/01/15 职场文书
社区学雷锋活动策划方案
2014/01/30 职场文书
难忘的一课教学反思
2014/04/30 职场文书
中等生评语大全
2014/05/04 职场文书
募捐倡议书怎么写
2014/05/14 职场文书
市级青年文明号申报材料
2014/05/26 职场文书
基层党建工作汇报材料
2014/08/15 职场文书
出纳岗位职责
2015/01/31 职场文书
河童之夏观后感
2015/06/11 职场文书
银行培训心得体会范文
2016/01/09 职场文书
JavaScript 防篡改对象的用法示例
2021/04/24 Javascript