tensorflow笔记1:基础函数、embedding_lookup

函数一:tf.nn.embedding_lookup()

ERROR:

I get this error: TypeError: Tensors in list passed to 'values' of 'ConcatV2' Op have types [float32, float64] that don't all match. 
is it because deprecated function in Tensorflow 1.0, or this is a problem with the script or a problem of deprecation can someone help

解决办法:https://stackoverflow.com/questions/43452873/bidirectional-dynamic-rnn-function-in-tensorflow-1-0

#之前的:

tf.nn.embedding_lookup(embeddings, encoder_inputs)

#修改后的:
#will cast you're embeddings to tf.float64, which was what was caussing the concat between float32 and float64 error. I solved it by replacing above with

tf.nn.embedding_lookup(embeddings, encoder_inputs)
tf.cast(encoder_inputs_embedded,tf.float32)

#and also casting the embeddings variable to float32 (assuming its a numpy array).

tf.nn.embedding_lookup函数的用法主要是选取一个张量里面索引对应的元素。tf.nn.embedding_lookup(tensor, id):tensor就是输入张量,id就是张量对应的索引,其他的参数不介绍。

例如:

import tensorflow as tf;  
import numpy as np;  
  
c = np.random.random([10,1])  
b = tf.nn.embedding_lookup(c, [1, 3])  
  
with tf.Session() as sess:  
    sess.run(tf.initialize_all_variables())  
    print sess.run(b)  
    print c  

输出:

[[ 0.77505197]
 [ 0.20635818]]
[[ 0.23976515]
 [ 0.77505197]
 [ 0.08798201]
 [ 0.20635818]
 [ 0.37183035]
 [ 0.24753178]
 [ 0.17718483]
 [ 0.38533808]
 [ 0.93345168]
 [ 0.02634772]]

分析:输出为张量的第一和第三个元素。

样例2:

  • 原型:tf.nn.embedding_lookup(params, ids, partition_strategy=’mod’, name=None, validate_indices=True, max_norm=None)
  • 在网上搜会发现基本都是假设ids只有一行,但是假如ids有若干行,会怎样?
  • 直接上代码:
# -*- coding= utf-8 -*-
import tensorflow as tf
import numpy as np

a = [[0.1, 0.2, 0.3], [1.1, 1.2, 1.3], [2.1, 2.2, 2.3], [3.1, 3.2, 3.3], [4.1, 4.2, 4.3]]
a = np.asarray(a)
idx1 = tf.Variable([0, 2, 3, 1], tf.int32)
idx2 = tf.Variable([[0, 2, 3, 1], [4, 0, 2, 2]], tf.int32)
out1 = tf.nn.embedding_lookup(a, idx1)
out2 = tf.nn.embedding_lookup(a, idx2)
init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    print sess.run(out1)
    print out1
    print '=================='
    print sess.run(out2)
    print out2

 

输出:

[[ 0.1  0.2  0.3]
 [ 2.1  2.2  2.3]
 [ 3.1  3.2  3.3]
 [ 1.1  1.2  1.3]]
Tensor("embedding_lookup:0", shape=(4, 3), dtype=float64) ================== [[[ 0.1 0.2 0.3] [ 2.1 2.2 2.3] [ 3.1 3.2 3.3] [ 1.1 1.2 1.3]] [[ 4.1 4.2 4.3] [ 0.1 0.2 0.3] [ 2.1 2.2 2.3] [ 2.1 2.2 2.3]]] Tensor("embedding_lookup_1:0", shape=(2, 4, 3), dtype=float64)

 

 

点赞