博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
tensorflow笔记1:基础函数、embedding_lookup
阅读量:6688 次
发布时间:2019-06-25

本文共 2353 字,大约阅读时间需要 7 分钟。

函数一: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

解决办法:

#之前的: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 withtf.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 tfimport numpy as npa = [[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)

 

 

转载于:https://www.cnblogs.com/lovychen/p/8385453.html

你可能感兴趣的文章
Micropython教程之TPYBoard DIY超声波测距仪实例演示
查看>>
我的友情链接
查看>>
mysqlbinlog 使用
查看>>
RHCS中GFS2共享存储扩容
查看>>
Excel如何把非打印区域设置成灰色
查看>>
条件语句整理
查看>>
Office365 SKU-1
查看>>
汉语国际传播思索
查看>>
TODO:排列组合问题:n个数中取m个
查看>>
27.chown更换所有者
查看>>
grep、egrep以及正则表达式的使用
查看>>
rsync加inotify实现无间隔文件同步
查看>>
系统最小化安装后,使用命令时提示“command not found”
查看>>
ffmpeg2.x开始支持opencl,编译测试
查看>>
python 抽象类分析
查看>>
DNS基本使用--主从服务器的搭建、主从同步、子域授权的实现
查看>>
centos 7
查看>>
java获取路径的方法
查看>>
IK中文分词_IK分词器配置文件讲解以及自定义词库
查看>>
One or more constraints have not been satisfied.
查看>>