Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Tensorflow中投影我的word2vec模型_Tensorflow_Gensim_Word2vec_Tensorboard - Fatal编程技术网

如何在Tensorflow中投影我的word2vec模型

如何在Tensorflow中投影我的word2vec模型,tensorflow,gensim,word2vec,tensorboard,Tensorflow,Gensim,Word2vec,Tensorboard,我不熟悉单词嵌入,想知道如何在Tensorflow中投影我的模型。我在看tensorflow网站,它只接受tsv文件(矢量/元数据),但不知道如何生成所需的tsv文件。我已经试着去查了一下,但找不到任何解决办法来重新命名这个。我会尝试以tsv文件格式保存我的模型吗?我需要做一些转换吗?任何帮助都将不胜感激 我已将模型保存为以下文件,并在需要时将其加载: word2vec.model word2vec.model.wv.vectors.npy 假设您正在尝试将一些预先训练好的Gensim word

我不熟悉单词嵌入,想知道如何在Tensorflow中投影我的模型。我在看tensorflow网站,它只接受tsv文件(矢量/元数据),但不知道如何生成所需的tsv文件。我已经试着去查了一下,但找不到任何解决办法来重新命名这个。我会尝试以tsv文件格式保存我的模型吗?我需要做一些转换吗?任何帮助都将不胜感激

我已将模型保存为以下文件,并在需要时将其加载:

word2vec.model

word2vec.model.wv.vectors.npy


假设您正在尝试将一些预先训练好的Gensim word嵌入到模型中,您可以直接使用以下代码来完成此操作

import numpy
import tensorflow as tf
from   gensim.models import KeyedVectors

# Load the word-vector model
wvec_fn = 'wvecs.kv'
wvecs = KeyedVectors.load(wvec_fn, mmap='r')
vec_size = wvecs.vector_size
vocab_size = len(wvecs.vocab)

# Create the embedding matrix where words are indexed alphabetically
embedding_mat = numpy.zeros(shape=(vocab_size, vec_size), dtype='int32')
for idx, word in enumerate(sorted(wvecs.vocab)):
    embedding_mat[idx] = wvecs.get_vector(word)

# Setup the embedding matrix for tensorflow
with tf.variable_scope("input_layer"):
    embedding_tf = tf.get_variable(
       "embedding", [vocab_size, vec_size],
        initializer=tf.constant_initializer(embedding_mat),
        trainable=False)

# Integrate this into your model
batch_size = 32     # just for example
seq_length = 20
input_data = tf.placeholder(tf.int32, [batch_size, seq_length])
inputs = tf.nn.embedding_lookup(embedding_tf, input_data)

如果您保存了一个模型而不仅仅是KeyedVectors,则可能需要修改代码以加载模型,然后使用
model.wv

干杯,我将尝试!所以它的代码运行良好,但我不知道它在做什么。我能请你解释一下你的流程吗?每一步上面都有一行评论。除此之外,我不知道还能说些什么。对于嵌入的更一般的解释,您可以尝试通过谷歌搜索和阅读一些博客。看一看。还有一些人也在做类似的事情。