Tensorflow tf.dynamic_partition矩陣拆分示例(Python3)
先給出一個(gè)樣例看看
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))
結(jié)果
[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]])]
再給出一個(gè)樣例
Py3代碼:
# one-hot 函數(shù)的樣例 import tensorflow as tf label = tf.placeholder(tf.int32,[None]) # 直接把 輸入的序列進(jìn)行One-Hot的結(jié)果 one_hot = tf.one_hot(label, 3, 1, 0) # 進(jìn)行轉(zhuǎn)置 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 # 按照每一維的大小進(jìn)行拆分 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] # 按照每一維大小進(jìn)行拆分 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] # 拼接以上兩維得到原來(lái)的結(jié)果 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結(jié)果:") print(one_hot_out, end='\n\n') print("以上的結(jié)果.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')
控制臺(tái)輸出:
原始的One-hot結(jié)果: [[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]] 以上的結(jié)果.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) 就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python實(shí)現(xiàn)簡(jiǎn)單HTML表格解析的方法
這篇文章主要介紹了Python實(shí)現(xiàn)簡(jiǎn)單HTML表格解析的方法,涉及Python基于libxml2dom模塊操作html頁(yè)面元素的技巧,需要的朋友可以參考下2015-06-06python:批量統(tǒng)計(jì)xml中各類目標(biāo)的數(shù)量案例
這篇文章主要介紹了python:批量統(tǒng)計(jì)xml中各類目標(biāo)的數(shù)量案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-03-03使用Python和Scribus創(chuàng)建一個(gè)RGB立方體的方法
這篇文章主要介紹了使用Python和Scribus創(chuàng)建一個(gè)RGB立方體的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07python數(shù)據(jù)分析之將爬取的數(shù)據(jù)保存為csv格式
Python內(nèi)置了CSV模塊,可直接通過該模塊實(shí)現(xiàn)csv文件的讀寫操作,在web應(yīng)用中導(dǎo)出數(shù)據(jù)是比較常見操作,下面這篇文章主要給大家介紹了關(guān)于python數(shù)據(jù)分析之將爬取的數(shù)據(jù)保存為csv格式的相關(guān)資料,需要的朋友可以參考下2022-06-06Python3之外部文件調(diào)用Django程序操作model等文件實(shí)現(xiàn)方式
這篇文章主要介紹了Python3之外部文件調(diào)用Django程序操作model等文件實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-04-04Python Pygame實(shí)戰(zhàn)之趣味籃球游戲的實(shí)現(xiàn)
這篇文章主要為大家分享了一個(gè)基于Python和Pygame實(shí)現(xiàn)的一個(gè)趣味籃球游戲,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Python有一定幫助,需要的可以參考一下2022-04-04python 常用日期處理-- datetime 模塊的使用
這篇文章主要介紹了python 如何對(duì)日期進(jìn)行處理,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下2020-09-09