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

 更新时间:2020年02月07日 10:38:49   作者:胡大炮的妖孽人生  
今天小编就为大家分享一篇Tensorflow tf.dynamic_partition矩阵拆分示例(Python3) ,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

先给出一个样例看看

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) 就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • Pandas 多层索引操作的实现

    Pandas 多层索引操作的实现

    本文主要介绍了Pandas 多层索引操作的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2025-02-02
  • 最新PyCharm从安装到PyCharm永久激活再到PyCharm官方中文汉化详细教程

    最新PyCharm从安装到PyCharm永久激活再到PyCharm官方中文汉化详细教程

    这篇文章涵盖了最新版PyCharm安装教程,最新版PyCharm永久激活码教程,PyCharm官方中文(汉化)版安装教程图文并茂非常详细,需要的朋友可以参考下
    2020-11-11
  • 详解python-opencv 常用函数

    详解python-opencv 常用函数

    这篇文章主要介绍了python-opencv 常用函数,主要包括读取图像保存图像和缩放图像的相关知识,需要的朋友可以参考下
    2022-08-08
  • Python中的自省(反射)详解

    Python中的自省(反射)详解

    这篇文章主要介绍了Python中的自省(反射)详解,本文讲解了通过访问对象的属性、访问对象的元数据、确定对象的类型等内容,需要的朋友可以参考下
    2015-06-06
  • Python字典遍历的陷阱

    Python字典遍历的陷阱

    这篇文章主要介绍了Python字典遍历的陷阱,我们都知道,Python中常常按照key、value的形式来遍历字典的items。若value是基本数据类型(int,float等),则是传的拷贝,是不能直接修改value的,下面来看看文章的详细内容吧
    2021-12-12
  • Pytorch加载数据集的方式总结及补充

    Pytorch加载数据集的方式总结及补充

    Pytorch自定义数据集方法,应该是用pytorch做算法的最基本的东西,下面这篇文章主要给大家介绍了关于Pytorch加载数据集的方式总结及补充,文中通过实例代码介绍的非常详细,需要的朋友可以参考下
    2022-11-11
  • Python模拟登录之滑块验证码的破解(实例代码)

    Python模拟登录之滑块验证码的破解(实例代码)

    这篇文章主要介绍了Python模拟登录之滑块验证码的破解(实例代码),代码简单易懂,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-11-11
  • python读取文本绘制动态速度曲线

    python读取文本绘制动态速度曲线

    这篇文章主要为大家详细介绍了python读取文本绘制动态速度曲线,多图同步显示,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-06-06
  • Python中使用zip函数的七重境界解析

    Python中使用zip函数的七重境界解析

    这篇文章主要介绍了Python中使用zip函数的七重境界,重点介绍了Python中功能强大的zip 函数的多种用法,并给出了相应的代码示例,需要的朋友可以参考下
    2022-12-12
  • Python爬虫框架Scrapy实战之批量抓取招聘信息

    Python爬虫框架Scrapy实战之批量抓取招聘信息

    网络爬虫又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者,是按照一定的规则,自动抓取万维网信息的程序或者脚本。这篇文章主要介绍Python爬虫框架Scrapy实战之批量抓取招聘信息,有需要的朋友可以参考下
    2015-08-08

最新评论