【TensorFlow】lookup emb

直接看代码吧~

tensorflow学习笔记--embedding_lookup()用法_STHSF的博客-CSDN博客

#!/usr/bin/env/python
# coding=utf-8
import tensorflow as tf
import numpy as np


tf.__version__ # '1.15.0'


input_ids = tf.placeholder(dtype=tf.int32, shape=[None, None])
input_ids 
# <tf.Tensor 'Placeholder:0' shape=(?, ?) dtype=int32>

embedding = tf.Variable(np.identity(5, dtype=np.int32))
embedding
# <tf.Variable 'Variable:0' shape=(5, 5) dtype=int32_ref>

input_embedding = tf.nn.embedding_lookup(embedding, input_ids)
input_embedding
# <tf.Tensor 'embedding_lookup/Identity:0' shape=(?, ?, 5) dtype=int32>



with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print("embedding")
    print(embedding.eval())
    


    input_embedding = tf.nn.embedding_lookup(embedding, input_ids)
    print('-'*10)
    print(input_embedding.shape)
    print(sess.run(input_embedding, feed_dict={input_ids:[[1, 2], [2, 1], [3, 3]]}))
    

    print('-'*10)
    print(input_embedding.get_shape().as_list())


embedding输出:
[[1 0 0 0 0]
 [0 1 0 0 0]
 [0 0 1 0 0]
 [0 0 0 1 0]
 [0 0 0 0 1]]

----------
(?, ?, 5)
[[[0 1 0 0 0]
  [0 0 1 0 0]]  # [1, 2],从embeding中直接查找index=1,index=2的向量

 [[0 0 1 0 0]
  [0 1 0 0 0]]  # [2, 1],从embeding中直接查找index=2,index=1的向量

 [[0 0 0 1 0]
  [0 0 0 1 0]]] # [3, 3],从embeding中直接查找index=3,index=3的向量
----------

[None, None, 5]


 embedding层_tensorflow中的Embedding操作详解_weixin_39835321的博客-CSDN博客

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值