国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Python > 正文

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

2020-02-15 21:20:59
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

先給出一個(gè)樣例看看

import tensorflow as tfraw = 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 tflabel = 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')            
發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 左权县| 黄山市| 通州市| 黄陵县| 花莲市| 剑川县| 玛沁县| 怀柔区| 垦利县| 绥芬河市| 建平县| 西青区| 承德县| 和政县| 江津市| 武穴市| 鄂托克前旗| 连云港市| 丰镇市| 响水县| 阳信县| 永丰县| 郎溪县| 都兰县| 二手房| 北海市| 陇川县| 普洱| 黄龙县| 波密县| 板桥市| 康平县| 密山市| 重庆市| 池州市| 内乡县| 石城县| 霍山县| 克拉玛依市| 宁强县| 阳泉市|