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的Django框架中为代码添加注释的方法
Jul 16 Python
完美解决Python2操作中文名文件乱码的问题
Jan 04 Python
Scrapy抓取京东商品、豆瓣电影及代码分享
Nov 23 Python
Python聊天室程序(基础版)
Apr 01 Python
Python实现爬虫设置代理IP和伪装成浏览器的方法分享
May 07 Python
Python3中在Anaconda环境下安装basemap包
Oct 21 Python
numpy数组做图片拼接的实现(concatenate、vstack、hstack)
Nov 08 Python
Python实现动态给类和对象添加属性和方法操作示例
Feb 29 Python
Python 中由 yield 实现异步操作
May 04 Python
Python读取JSON数据操作实例解析
May 18 Python
Python常用库Numpy进行矩阵运算详解
Jul 21 Python
pytorch 实现在测试的时候启用dropout
May 27 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数据库连接类~~做成一个分页类!
2006/11/25 PHP
PHP系统命令函数使用分析
2013/07/05 PHP
discuz免激活同步登入代码修改方法(discuz同步登录)
2013/12/24 PHP
腾讯CMEM的PHP扩展编译安装方法
2015/09/25 PHP
php+MySQL实现登录时验证登录名和密码是否正确
2016/05/10 PHP
关于jquery append() html时的小问题的解决方法
2010/12/16 Javascript
jquery、js调用iframe父窗口与子窗口元素的方法整理
2014/07/31 Javascript
jQuery实现瀑布流的取巧做法分享
2015/01/12 Javascript
JS实现让网页背景图片斜向移动的方法
2015/02/25 Javascript
javascript基于prototype实现类似OOP继承的方法
2015/12/16 Javascript
全面介绍javascript实用技巧及单竖杠
2016/07/18 Javascript
Vue.js每天必学之指令系统与自定义指令
2016/09/07 Javascript
完美解决js传递参数中加号和&号自动改变的方法
2016/10/11 Javascript
详解Vue自定义过滤器的实现
2017/01/10 Javascript
浅谈Vue内置component组件的应用场景
2018/03/27 Javascript
微信小程序 获取手机号 JavaScript解密示例代码详解
2020/05/14 Javascript
python动态监控日志内容的示例
2014/02/16 Python
python获得文件创建时间和修改时间的方法
2015/06/30 Python
Python 类的特殊成员解析
2018/06/20 Python
对pyqt5之menu和action的使用详解
2019/06/20 Python
django settings.py 配置文件及介绍
2019/07/15 Python
PyQt5.6+pycharm配置以及pyinstaller生成exe(小白教程)
2020/06/02 Python
python 下载m3u8视频的示例代码
2020/11/11 Python
台湾前三大B2C购物网站:MOMO购物网
2017/04/27 全球购物
全球第二大家装零售商:Lowe’s
2018/01/13 全球购物
印尼太阳百货公司网站:Matahari
2018/02/04 全球购物
银行办理业务介绍信
2014/01/18 职场文书
亮剑精神演讲稿
2014/05/23 职场文书
助人为乐模范事迹材料
2014/06/02 职场文书
公司优秀员工获奖感言
2014/08/14 职场文书
研讨会通知
2015/04/27 职场文书
开会迟到检讨书范文
2015/05/06 职场文书
预备党员入党感想
2015/08/10 职场文书
MySQL中的布尔值,怎么存储false或true
2021/06/04 MySQL
浅谈mysql返回Boolean类型的几种情况
2021/06/04 MySQL
CSS实现鼠标悬浮动画特效
2023/05/07 HTML / CSS