我试图在tensorflow中连接不同的模糊嵌入:
因此,我想连接三个嵌入:
现在我要最终矢量为356模糊形状:
concat( [ 300 dim , 50 dim , 6 dim ] ) ---> 356 dim 在numpy中,我可以很容易地用np.column_stack实现这一点:
first_embedding = np.random.randint(10,20,[10,300])
second_embedding = np.random.randint(10,20,[10,50])
third_embedding = np.random.randint(10,20,[10,6])
concat = np.column_stack((first_embedding,second_embedding,third_embedding))
print(concat.shape)产出:
(10, 356)我不能在tensorflow中做同样的事情,所以如果我在tensorflow中创建三个嵌入:
import tensorflow as tf
tf.reset_default_graph()
sentences = tf.placeholder(tf.int32,
shape=[None,None]
)
sentences_sec = tf.placeholder(tf.int32,
shape=[None,None]
)
sentences_third = tf.placeholder(tf.int32,
shape=[None,None]
)
Word_embedding = tf.get_variable(name="Word_embedding",
shape=[24,300],
initializer=tf.constant_initializer(np.array(load_embedding_matrix_1)),
trainable=False
)
first_embedding_loopup= tf.nn.embedding_lookup(Word_embedding,sentences)
Word_embedding_second = tf.get_variable(name="Word_embedding_2",
shape=[24,50],
initializer=tf.constant_initializer(np.array(load_embedding_matrix_2)),
trainable=False
)
second_embedding_loopup= tf.nn.embedding_lookup(Word_embedding_second,sentences_sec)
word_sentences_third = tf.get_variable(name="Word_embedding_3",
shape=[24,6],
initializer=tf.constant_initializer(np.array(load_embedding_matrix_3)),
trainable=False
)
third_embedding_loopup = tf.nn.embedding_lookup(Word_embedding_third,sentences_third)我试着使用tf.concat,但是所有的模糊应该是一样的,
如果有人能给我一些建议的话,我会非常感激的,我怎样才能从np.column_stack那里得到和我一样的形状呢?
谢谢!
发布于 2018-12-28 20:40:26
您需要指定轴来正确连接单词向量。下面是一个示例:
import tensorflow as tf
tf.enable_eager_execution()
a = tf.get_variable(name='a', initializer=tf.zeros_initializer(), shape=(512, 24, 300))
b = tf.get_variable(name='b', initializer=tf.zeros_initializer(), shape=(512, 24, 50))
c = tf.get_variable(name='c', initializer=tf.zeros_initializer(), shape=(512, 24, 6))
print(tf.concat(values=[a, b, c], axis=-1).numpy().shape)
>>> (512, 24, 356)https://stackoverflow.com/questions/53964008
复制相似问题