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中一些自然语言工具的使用的入门教程
Apr 13 Python
python利用urllib和urllib2访问http的GET/POST详解
Sep 27 Python
使用Django和Python创建Json response的方法
Mar 26 Python
Django使用HttpResponse返回图片并显示的方法
May 22 Python
详谈Pandas中iloc和loc以及ix的区别
Jun 08 Python
Python数据报表之Excel操作模块用法分析
Mar 11 Python
python 搜索大文件的实例代码
Jul 08 Python
python 用所有标点符号分隔句子的示例
Jul 15 Python
python datetime中strptime用法详解
Aug 29 Python
余弦相似性计算及python代码实现过程解析
Sep 18 Python
Python3 Click模块的使用方法详解
Feb 12 Python
python tkinter 设置窗口大小不可缩放实例
Mar 04 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利用REFERER根居访问来地址进行页面跳转
2013/09/28 PHP
ThinkPHP惯例配置文件详解
2014/07/14 PHP
PHP中把数据库查询结果输出为json格式简单实例
2015/04/09 PHP
Yii2中使用asset压缩js,css文件的方法
2016/11/24 PHP
Thinkphp5+uploadify实现的文件上传功能示例
2018/05/26 PHP
一实用的实现table排序的Javascript类库
2007/09/12 Javascript
打豆豆小游戏 用javascript编写的[打豆豆]小游戏
2013/01/08 Javascript
使用js解决由border属性引起的div宽度问题
2013/11/26 Javascript
javascript实现存储hmtl字符串示例
2014/04/25 Javascript
使用JQuery中的trim()方法去掉前后空格
2016/09/16 Javascript
Javascript实现图片懒加载插件的方法
2016/10/20 Javascript
JS实现json的序列化和反序列化功能示例
2017/06/13 Javascript
JavaScript表单即时验证 验证不成功不能提交
2017/08/31 Javascript
ES6中Set和Map数据结构,Map与其它数据结构互相转换操作实例详解
2019/02/28 Javascript
微信小程序自定义可滑动顶部TabBar选项卡实现页面切换功能示例
2019/05/14 Javascript
深入了解JavaScript 私有化
2019/05/30 Javascript
JavaScript原型式继承实现方法
2019/11/06 Javascript
基于Python实现一个简单的银行转账操作
2016/03/06 Python
对python numpy数组中冒号的使用方法详解
2018/04/17 Python
python游戏开发之视频转彩色字符动画
2019/04/26 Python
python使用pygame模块实现坦克大战游戏
2020/03/25 Python
使用Python实现将list中的每一项的首字母大写
2019/06/11 Python
python装饰器常见使用方法分析
2019/06/26 Python
python pandas模块基础学习详解
2019/07/03 Python
关于TensorFlow新旧版本函数接口变化详解
2020/02/10 Python
详解python的super()的作用和原理
2020/10/29 Python
Python3使用Selenium获取session和token方法详解
2021/02/16 Python
浅析两列自适应布局的3种思路
2016/05/03 HTML / CSS
大学生党员自我批评
2014/02/14 职场文书
老公保证书范文
2014/04/29 职场文书
上海世博会志愿者口号
2014/06/17 职场文书
2014医学院领导班子对照检查材料思想汇报
2014/09/19 职场文书
2014年物业公司工作总结
2014/11/22 职场文书
最感人的道歉情书
2015/05/12 职场文书
2015年中职班主任工作总结
2015/05/25 职场文书
采购员工作总结范文
2015/08/12 职场文书