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 相关文章推荐
利用Django框架中select_related和prefetch_related函数对数据库查询优化
Apr 01 Python
给Python的Django框架下搭建的BLOG添加RSS功能的教程
Apr 08 Python
django框架如何集成celery进行开发
May 24 Python
Python爬虫包BeautifulSoup简介与安装(一)
Jun 17 Python
python3 实现验证码图片切割的方法
Dec 07 Python
PyQt5 QTable插入图片并动态更新的实例
Jun 18 Python
python全栈要学什么 python全栈学习路线
Jun 28 Python
使用Python自动生成HTML的方法示例
Aug 06 Python
解决django后台管理界面添加中文内容乱码问题
Nov 15 Python
Python获取二维数组的行列数的2种方法
Feb 11 Python
python实现控制台输出彩色字体
Apr 05 Python
Python json格式化打印实现过程解析
Jul 21 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
PHP中替换换行符的几种方法小结
2012/10/15 PHP
php json与xml序列化/反序列化
2013/10/28 PHP
PHP 中使用ajax时一些常见错误总结整理
2017/02/27 PHP
ThinkPHP Where 条件中常用表达式示例(详解)
2017/03/31 PHP
php常用日期时间函数实例小结
2019/07/04 PHP
ext 代码生成器
2009/08/07 Javascript
NodeJs中的非阻塞方法介绍
2012/06/05 NodeJs
javascript-简单的计算器实现步骤分解(附图)
2013/05/30 Javascript
详解照片瀑布流效果(js,jquery分别实现与知识点总结)
2017/01/01 Javascript
详解Vue+axios+Node+express实现文件上传(用户头像上传)
2018/08/10 Javascript
详解基于mpvue微信小程序下载远程图片到本地解决思路
2019/05/16 Javascript
解决LayUI数据表格复选框不居中显示的问题
2019/09/25 Javascript
解决ant-design-vue中menu菜单无法默认展开的问题
2020/10/31 Javascript
JS前端基于canvas给图片添加水印
2020/11/11 Javascript
浅谈JSON5解决了JSON的两大痛点
2020/12/14 Javascript
[05:07]DOTA2英雄梦之声_第14期_暗影恶魔
2014/06/20 DOTA
[06:57]DOTA2-DPC中国联赛 正赛 Ehome vs PSG.LGD 选手采访
2021/03/11 DOTA
Python中的特殊语法:filter、map、reduce、lambda介绍
2015/04/14 Python
python爬虫获取新浪新闻教学
2018/12/23 Python
python 实现倒排索引的方法
2018/12/25 Python
python3.4 将16进制转成字符串的实例
2019/06/12 Python
Python实现网页截图(PyQT5)过程解析
2019/08/12 Python
Python要如何实现列表排序的几种方法
2020/02/21 Python
互斥锁解决 Python 中多线程共享全局变量的问题(推荐)
2020/09/28 Python
如何Tkinter模块编写Python图形界面
2020/10/14 Python
使用Python判断一个文件是否被占用的方法教程
2020/12/16 Python
详解Python openpyxl库的基本应用
2021/02/26 Python
马来西亚综合购物网站:Lazada马来西亚
2018/06/05 全球购物
第一范式(1NF)、第二范式(2NF)和第三范式(3NF)之间的区别是什么?
2016/04/28 面试题
应届生污水处理求职信
2013/11/06 职场文书
ktv总经理岗位职责
2014/02/17 职场文书
《问银河》教学反思
2014/02/19 职场文书
产品质量承诺书
2014/03/27 职场文书
幼儿教师寄语集锦
2014/04/03 职场文书
感恩之星事迹材料
2014/05/03 职场文书
大学生先进个人主要事迹材料
2015/11/04 职场文书