tf.nn.embedding_lookup中关于partition_strategy参数详解
来源:互联网 发布:网络随身听收音机 编辑:程序博客网 时间:2024/04/28 03:51
- tfnnembedding_lookup
- 数学上的原理
- API介绍
- 简单示例
- 程序
- 注解
- partition_strategy参数的示例
- mod案例1
- mod案例2
- div案例1
- div案例2
- 参考资料
tf.nn.embedding_lookup
embedding_lookup常用于NLP中将one-hot编码转换我对应的向量编码。
数学上的原理
数学上的原理假设一共有
应用中一般将物体嵌入到一个低维空间
从id(索引)找到对应的One-hot encoding,然后红色的weight就直接对应了输出节点的值(注意这里没有activation function),也就是对应的embedding向量。
API介绍
API介绍依据inputs_ids来寻找embedding_params中对应的元素.
embedding_lookup( params, # embedding_params 对应的转换向量 ids, # inputs_ids,标记着要查询的id partition_strategy='mod', #分割方式 name=None, validate_indices=True, # deprecated max_norm=None )
embedding_lookup中的partition_strategy参数比较难理解(this function is hard to understand, until you get the point!),下面会有特别的解释。
简单示例
简单示例下面我们通过一个常见的案例来解释embedding_lookup的用法:
程序
# coding:utf8import tensorflow as tfimport numpy as npinput_ids = tf.placeholder(dtype=tf.int32, shape=[None])_input_ids = tf.placeholder(dtype=tf.int32, shape=[3, 2])embedding_param = tf.Variable(np.identity(8, dtype=np.int32)) # 生成一个8x8的单位矩阵input_embedding = tf.nn.embedding_lookup(embedding_param, input_ids)_input_embedding = tf.nn.embedding_lookup(embedding_param, _input_ids)sess = tf.InteractiveSession()sess.run(tf.global_variables_initializer())print('embedding:')print(embedding_param.eval())var1 = [1, 2, 6, 4, 2, 5, 7]print('\n var1:')print(var1)print('\nprojecting result:')print(sess.run(input_embedding, feed_dict={input_ids: var1}))var2 = [[1, 4], [6, 3], [2, 5]]print('\n _var2:')print(var2)print('\n _projecting result:')print(sess.run(_input_embedding, feed_dict={_input_ids: var2}))'''输出:embedding:[[1 0 0 0 0 0 0 0] [0 1 0 0 0 0 0 0] [0 0 1 0 0 0 0 0] [0 0 0 1 0 0 0 0] [0 0 0 0 1 0 0 0] [0 0 0 0 0 1 0 0] [0 0 0 0 0 0 1 0] [0 0 0 0 0 0 0 1]] var1:[1, 2, 6, 4, 2, 5, 7]projecting result:[[0 1 0 0 0 0 0 0] [0 0 1 0 0 0 0 0] [0 0 0 0 0 0 1 0] [0 0 0 0 1 0 0 0] [0 0 1 0 0 0 0 0] [0 0 0 0 0 1 0 0] [0 0 0 0 0 0 0 1]] _var2:[[1, 4], [6, 3], [2, 5]] _projecting result:[[[0 1 0 0 0 0 0 0] [0 0 0 0 1 0 0 0]] [[0 0 0 0 0 0 1 0] [0 0 0 1 0 0 0 0]] [[0 0 1 0 0 0 0 0] [0 0 0 0 0 1 0 0]]]'''
注解
- embedding_param参数是一个8*8的单位矩阵(这个这是由一个tensor构成的params,即len(params)=1,partition_strategy只在len(params)>1时才作用)。
embedding_param= # embedding_param只由一个tensor组成 故len(embedding_param) = 1[[1 0 0 0 0 0 0 0] [0 1 0 0 0 0 0 0] [0 0 1 0 0 0 0 0] [0 0 0 1 0 0 0 0] [0 0 0 0 1 0 0 0] [0 0 0 0 0 1 0 0] [0 0 0 0 0 0 1 0] [0 0 0 0 0 0 0 1]]
- 我们ids为var1,照着此id从embedding_param取对应的行元素.
var1 = [1, 2, 6, 4, 2, 5, 7] # 1即取第2行 --> [0 1 0 0 0 0 0 0] # 2即取第3行 --> [0 0 1 0 0 0 0 0] # etc.
- 我们ids为var2,照着此id从embedding_param取对应的行元素
var2 = [[1, 4], [6, 3], [2, 5]] ''' [1, 4] 即取2,5行 [[0 1 0 0 0 0 0 0] [0 0 0 0 1 0 0 0]] 后面同理 '''
partition_strategy参数的示例
关于partition_strategy参数的示例看api迷迷糊糊的,就看下面的四个例子,就会明白这个函数的操作方法了~
‘mod’案例1
# coding:utf8 import tensorflow as tf import numpy as np def test_embedding_lookup(): a = np.arange(12).reshape(3, 4) b = np.arange(12, 16).reshape(1, 4) c = np.arange(16, 28).reshape(3, 4) print(a) print('\n') print(b) print('\n') print(c) print('\n') a = tf.Variable(a) b = tf.Variable(b) c = tf.Variable(c) t = tf.nn.embedding_lookup([a, b, c], partition_strategy='mod', ids=[0, 3, 6, 1, 2, 5, 8]) init = tf.global_variables_initializer() sess = tf.Session() sess.run(init) m = sess.run(t) print(m) test_embedding_lookup()
''' 分析: 这里我们注意到params是由[a, b, c]这三个tensor组成。即len(params)=3,且a,b,c这三个tensor的第一维度分别为3,1,3。 在把这个三个tensor组合过程中,我们按照partition_strategy='mod'策略分割。即每个tensor的元素之间相差len(params).这里分割方式为[a, b, c] == [[0,3,6], [1,4,7], [2,5,8]] 这里程序还不知道4和7是找不到对应的元素的,在获取元素时候会报错 a=[[ 0 1 2 3] = [0, 3, 6] --> [0 1 2 3] = 0 [ 4 5 6 7] --> [4 5 6 7] = 3 [ 8 9 10 11]] --> [8 9 10 11] = 6 b=[[12 13 14 15]] = [1, 4, 7] --> [12 13 14 15] = 1 --> 运行时报错 = 4 --> 运行时报错 = 7 c = etc.. 输出: [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] [[12 13 14 15]] [[16 17 18 19] [20 21 22 23] [24 25 26 27]] [[ 0 1 2 3] # 0 [ 4 5 6 7] # 3 [ 8 9 10 11] # 6 [12 13 14 15] # 1 [16 17 18 19] # 2 [20 21 22 23] # 5 [24 25 26 27]] # 8 '''
‘mod’案例2
# coding:utf8 import tensorflow as tf import numpy as np def test_embedding_lookup(): a = np.arange(12).reshape(3, 4) b = np.arange(12, 16).reshape(1, 4) c = np.arange(16, 28).reshape(3, 4) print(a) print('\n') print(b) print('\n') print(c) print('\n') a = tf.Variable(a) b = tf.Variable(b) c = tf.Variable(c) t = tf.nn.embedding_lookup([a, c, b], partition_strategy='mod', ids=[0, 3, 6, 1, 4, 7, 2]) init = tf.global_variables_initializer() sess = tf.Session() sess.run(init) m = sess.run(t) print(m) test_embedding_lookup()
''' 分析: 这里我们把params从[a, b, c]改为[a, c, b]这三个tensor组成。a,c,b这三个tensor的第一维度分别为3,3,1。 在把这个三个tensor组合过程中,依旧是每个tensor的元素之间相差len(params).这里分割方式为[a, c, b] == [[0,3,6], [1,4,7], [2,5,8]] 这里程序还不知道4和7是找不到对应的元素的,在获取元素时候会报错 a=[[ 0 1 2 3] = [0, 3, 6] --> [0 1 2 3] = 0 [ 4 5 6 7] --> [4 5 6 7] = 3 [ 8 9 10 11]] --> [8 9 10 11] = 6 c=[[16 17 18 19] = [1, 4, 7] --> [16 17 18 19] = 1 [20 21 22 23] --> [20 21 22 23] = 4 [24 25 26 27]] --> [24 25 26 27] = 7 b=[[12 13 14 15]] = [2, 5, 8] --> [12 13 14 15] = 2 --> 运行时报错 = 5 --> 运行时报错 = 8 输出: [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] [[12 13 14 15]] [[16 17 18 19] [20 21 22 23] [24 25 26 27]] [[ 0 1 2 3] # 0 [ 4 5 6 7] # 3 [ 8 9 10 11] # 6 [16 17 18 19] # 1 [20 21 22 23] # 4 [24 25 26 27] # 7 [12 13 14 15]] # 2 '''
‘div’案例1
# coding:utf8 import tensorflow as tf import numpy as np def test_embedding_lookup(): a = np.arange(12).reshape(3, 4) b = np.arange(12, 16).reshape(1, 4) c = np.arange(16, 28).reshape(3, 4) print(a) print('\n') print(b) print('\n') print(c) print('\n') a = tf.Variable(a) b = tf.Variable(b) c = tf.Variable(c) t = tf.nn.embedding_lookup([a, b, c], partition_strategy='div', ids=[0, 1, 2, 3, 5, 6]) init = tf.global_variables_initializer() sess = tf.Session() sess.run(init) m = sess.run(t) print(m) test_embedding_lookup()
''' 分析: 这里我们把params依旧是[a, b, c],三个tensor的第一维度分别为3,1,3。 在把这个三个tensor组合过程中,这我们按照partition_strategy='div'策略分割。即每个tensor的元素之间相差1.如果不够等分的话,前面(max_id+1)%len(params)多分一个元素。这里一共7个元素,分为3组,即3、2、2分配。 这里分割方式为[a, b, c] == [[0,1,2], [3,4], [5,6]] 这里程序还不知道4和7是找不到对应的元素的,在获取元素时候会报错 a=[[ 0 1 2 3] = [0, 1, 2] --> [0 1 2 3] = 0 [ 4 5 6 7] --> [4 5 6 7] = 1 [ 8 9 10 11]] --> [8 9 10 11] = 2 b=[[12 13 14 15]] = [3, 4] --> [12 13 14 15] = 3 --> 运行时报错 = 4 c=[[16 17 18 19] = [5, 6] --> [16 17 18 19] = 5 [20 21 22 23] --> [20 21 22 23] = 6 [24 25 26 27]] --> [24 25 26 27] = 这个是找不到的了 输出: [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] [[12 13 14 15]] [[16 17 18 19] [20 21 22 23] [24 25 26 27]] [[ 0 1 2 3] # 0 [ 4 5 6 7] # 1 [ 8 9 10 11] # 2 [12 13 14 15] # 3 [16 17 18 19] # 5 [20 21 22 23]] # 6 '''
‘div’案例2
# coding:utf8 import tensorflow as tf import numpy as np def test_embedding_lookup(): a = np.arange(12).reshape(3, 4) b = np.arange(12, 16).reshape(1, 4) c = np.arange(16, 28).reshape(3, 4) print(a) print('\n') print(b) print('\n') print(c) print('\n') a = tf.Variable(a) b = tf.Variable(b) c = tf.Variable(c) t = tf.nn.embedding_lookup([a, c, b], partition_strategy='div', ids=[0, 1, 2, 3, 4, 5]) init = tf.global_variables_initializer() sess = tf.Session() sess.run(init) m = sess.run(t) print(m) test_embedding_lookup()
''' 分析: 这里我们把params改为[a, c, b],三个tensor的第一维度分别为3,3,1。 在把这个三个tensor组合过程中,这我们按照partition_strategy='div'策略分割。这里一共7个元素,分为3组,即3、2、2分配。 这里分割方式为[a, c, b] == [[0,1,2], [3,4], [5,6]] 这里程序还不知道4和7是找不到对应的元素的,在获取元素时候会报错 a=[[ 0 1 2 3] = [0, 1, 2] --> [0 1 2 3] = 0 [ 4 5 6 7] --> [4 5 6 7] = 1 [ 8 9 10 11]] --> [8 9 10 11] = 2 c=[[16 17 18 19] = [3, 4] --> [16 17 18 19] = 3 [20 21 22 23] --> [20 21 22 23] = 4 [24 25 26 27]] --> [24 25 26 27] = 这个是找不到的了 b=[[12 13 14 15]] = [5, 6] --> [12 13 14 15] = 5 --> 运行时报错 = 6 输出: [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] [[12 13 14 15]] [[16 17 18 19] [20 21 22 23] [24 25 26 27]] [[ 0 1 2 3] # 0 [ 4 5 6 7] # 1 [ 8 9 10 11] # 2 [16 17 18 19] # 3 [20 21 22 23] # 4 [16 17 18 19]] # 5 '''
参考资料
https://stackoverflow.com/questions/34870614/what-does-tf-nn-embedding-lookup-function-do/41922877#41922877?newreg=5119f86ea49b43aa8988a833294ceb3e
https://www.zhihu.com/question/52250059
- tf.nn.embedding_lookup中关于partition_strategy参数详解
- TensorFlow中 tf.nn.embedding_lookup
- tf.nn.embedding_lookup()的参数形式
- tf.nn.embedding_lookup理解
- tf.nn.embedding_lookup()笔记
- tf.nn.embedding_lookup
- tf.nn.embedding_lookup函数
- tf.nn.embedding_lookup()的使用
- Tensorflow学习---tf.nn.embedding_lookup
- tf.nn.embedding_lookup简单介绍
- tensorflow中embedding_lookup, tf.gather以及tf.nn.embedding_lookup_sparse的理解
- tensorflow tf.nn.embedding_lookup(embeddings, train_inputs)解释
- tf.nn.embedding_lookup函数的用法
- Tensorflow-tf.nn.embedding_lookup函数原理
- tf.nn.embedding_lookup TensorFlow embedding_lookup 函数最简单实例
- tf.nn.max_pool参数含义和用法
- Tensorflow -- tf.nn.conv2d() 函数详解
- tf.nn
- 【C++ STL】深入解析神秘的 --- 仿函数
- 利用矢量计算快速判定一点在直线的哪一侧
- onkeypress事件
- 概率机器人 第二章 递归状态估计(贝叶斯滤波)
- 页面布局的总结
- tf.nn.embedding_lookup中关于partition_strategy参数详解
- 4.14 leetcode -14 zigzag-conversion
- 嵌入到POS机中的手持端车牌识别
- phaser 背景音乐以追加的形式无限循环 (4)
- Echart自定义悬浮tooltip 设置字体颜色
- 文件上传功能实现
- linux的tcp/udp网络编程模型
- 动态规划——吹气球
- sparkStreaming 连接数据库 --设计模式