tf.nn.embedding

    xiaoxiao2021-03-25  87

    import tensorflow as tf import numpy as np #错误的方法: embedding = [[1,0,0], [0,1,0], [0,0,1]] sentwordId = [0, 2] nn = tf.nn.embedding_lookup(embedding, sentwordId) with tf.Session() as sess: sess.run(tf.initialize_all_variables()) print(sess.run(nn))

    输出结果如下:不是想要的结果

    第一种方法:

    import tensorflow as tf import numpy as np embedding = tf.Variable(np.identity(5,dtype=np.int32)) senId = tf.placeholder(dtype=tf.int32,shape = None) nn = tf.nn.embedding_lookup(embedding, senId) with tf.Session() as sess: sess.run(tf.initialize_all_variables()) print(sess.run(nn,feed_dict={senId:[0,2]}))

    第二种方法:

    import tensorflow as tf import numpy as np embedd = [[1,0,0], [0,1,0], [0,0,1]] embedding = np.array(embedd) sentId = np.array([0,2]) nn = tf.nn.embedding_lookup(embedding, sentId) with tf.Session() as sess: print(sess.run(nn))

    第三种方法:

    import tensorflow as tf import numpy as np embedd = [[1, 0, 0], [0, 1, 0], [0, 0, 1]] embedding = np.array(embedd) sentId = tf.placeholder(tf.int32,shape=None) nn = tf.nn.embedding_lookup(embedding, sentId) with tf.Session() as sess: print(sess.run(nn,feed_dict={sentId:[0, 2]}))

    第四种方法:

    import tensorflow as tf import numpy as np embedd = [[1, 0, 0], [0, 1, 0], [0, 0, 1]] embedding = tf.placeholder(dtype=tf.int32, shape=[3,3]) sentId = tf.placeholder(dtype=tf.int32, shape=None) nn = tf.nn.embedding_lookup(embedding, sentId) with tf.Session() as sess: print(sess.run(nn, feed_dict={embedding:embedd, sentId:[0,2]}))

    转载请注明原文地址: https://ju.6miu.com/read-24133.html

    最新回复(0)