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自定义scrapy中间模块避免重复采集的方法
Apr 07 Python
python奇偶行分开存储实现代码
Mar 19 Python
python实现音乐下载器
Apr 15 Python
Python判断一个文件夹内哪些文件是图片的实例
Dec 07 Python
Python Matplotlib实现三维数据的散点图绘制
Mar 19 Python
pygame实现五子棋游戏
Oct 29 Python
python用Tkinter做自己的中文代码编辑器
Sep 07 Python
python实现b站直播自动发送弹幕功能
Feb 20 Python
解决python 输出到csv 出现多空行的情况
Mar 24 Python
tensorflow中的梯度求解及梯度裁剪操作
May 26 Python
详解Python+OpenCV绘制灰度直方图
Mar 22 Python
代码复现python目标检测yolo3详解预测
May 06 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核心代码分析require和include的区别
2011/01/02 PHP
ThinkPHP实现支付宝接口功能实例
2014/12/02 PHP
php实现Session存储到Redis
2015/11/11 PHP
PHP面向对象学习之parent::关键字
2017/01/18 PHP
javascript 打印页面代码
2009/03/24 Javascript
jquery中eq和get的区别与使用方法
2011/04/14 Javascript
javascript实现复制与粘贴操作实例
2014/10/16 Javascript
Javascript动态创建表格及删除行列的方法
2015/05/15 Javascript
JavaScript中解析JSON数据的三种方法
2015/07/03 Javascript
jQuery鼠标经过方形图片切换成圆边效果代码分享
2015/08/20 Javascript
jQuery Real Person验证码插件防止表单自动提交
2015/11/06 Javascript
跟我学习JScript的Bug与内存管理
2015/11/18 Javascript
jquery根据td给相同tr下其他td赋值的实现方法
2016/10/05 Javascript
jQuery实现背景滑动菜单
2016/12/02 Javascript
AngularJS服务service用法总结
2016/12/13 Javascript
JS获取多维数组中相同键的值实现方法示例
2017/01/06 Javascript
angular和BootStrap3实现购物车功能
2017/01/25 Javascript
jQuery实现弹窗居中效果类似alert()
2017/02/27 Javascript
JavaScript 基础表单验证示例(纯Js实现)
2017/07/20 Javascript
js实现日期显示的一些操作(实例讲解)
2017/07/27 Javascript
Angular-UI Bootstrap组件实现警报功能
2018/07/16 Javascript
详解Nuxt.js部署及踩过的坑
2018/08/07 Javascript
Vue自定义弹窗指令的实现代码
2018/08/13 Javascript
JavaScript WeakMap使用详解
2021/02/05 Javascript
python简单商城购物车实例代码
2018/03/15 Python
win10系统Anaconda和Pycharm的Tensorflow2.0之CPU和GPU版本安装教程
2019/12/03 Python
Python与C/C++的相互调用案例
2021/03/04 Python
HTML5 Notification(桌面提醒)功能使用实例
2014/03/17 HTML / CSS
十月份红领巾广播稿
2014/01/22 职场文书
标准单位租车协议书
2014/09/23 职场文书
建筑横幅标语
2014/10/09 职场文书
仓库管理员岗位职责
2015/02/03 职场文书
调任通知
2015/04/21 职场文书
2015年暑期社会实践总结
2015/07/13 职场文书
2015新员工工作总结范文
2015/10/15 职场文书
pytorch 实现在测试的时候启用dropout
2021/05/27 Python