Tensorflow tf.dynamic_partition矩阵拆分示例(Python3)
作者:胡大炮的妖孽人生 时间:2021-07-10 10:35:25
先给出一个样例看看
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.]]
来源:https://blog.csdn.net/huplion/article/details/79600232
标签:Tensorflow,tf.dynamic,partition,矩阵拆分
0
投稿
猜你喜欢
Python面向对象类编写细节分析【类,方法,继承,超类,接口等】
2023-01-01 11:23:13
使用标准的表单字段名
2008-06-30 14:14:00
取巧的边框等高
2009-12-16 12:11:00
golang中interface接口的深度解析
2024-05-08 10:52:00
python神经网络Keras实现LSTM及其参数量详解
2023-02-09 14:02:22
Dojo Style Javascript 编程规范
2007-10-25 17:24:00
Python基于React-Dropzone实现上传组件的示例代码
2021-04-01 19:53:03
python re模块findall()函数实例解析
2022-07-07 13:38:40
Python+OpenCV内置方法实现行人检测
2023-10-19 12:52:47
python2.x实现人民币转大写人民币
2023-06-26 10:35:53
MySQL中二进制与重做日志文件的基本概念学习教程
2024-01-19 07:59:19
pytorch下tensorboard的使用程序示例
2021-10-27 01:01:16
python中set()函数简介及实例解析
2022-05-15 17:12:24
常用ASCII 码对照表
2007-08-21 14:35:00
和“登录”有关的事儿
2009-07-10 17:37:00
三表左连接查询的sql语句写法
2024-01-15 01:54:21
Mysql使用sum()函数返回null的问题详解
2024-01-17 03:20:04
javascript 常见汉字转换成拼音工具
2008-03-03 16:54:00
VSCode使用ssh密钥免密远程登录服务器的方法
2022-03-13 22:42:13
深入浅析Pytorch中stack()方法
2021-12-26 01:40:08