特征列预训练嵌入

特征列预训练嵌入

本文介绍了特征列预训练嵌入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用带有 tf.feature_column.embedding_column 的预训练嵌入.

我在 tf.feature_column.embedding_column 中使用了 pre_trained 嵌入.但它不起作用.错误是

错误是:

ValueError:如果指定,初始化程序必须是可调用的.column_name 的嵌入:itemx

这是我的代码:

weight, vocab_size, emb_size = _create_pretrained_emb_from_txt(FLAGS.vocab,FLAGS.pre_emb)W = tf.Variable(tf.constant(0.0, shape=[vocab_size, emb_size]),可训练=假,名称=W")embedding_placeholder = tf.placeholder(tf.float32, [vocab_size, emb_size])embedding_init = W.assign(embedding_placeholder)sess = tf.Session()sess.run(embedding_init, feed_dict={embedding_placeholder: weight})itemx_vocab = tf.feature_column.categorical_column_with_vocabulary_file(键='itemx',词汇文件=标志.vocabx)itemx_emb = tf.feature_column.embedding_column(itemx_vocab,尺寸=emb_size,初始值设定项=W,可训练=假)

我试过初始化器 = lambda w:W.像这样:

itemx_emb = tf.feature_column.embedding_column(itemx_vocab,尺寸=emb_size,初始值设定项 = lambda w:W,可训练=假)

它报告错误:

TypeError: () 得到了一个意外的关键字参数 'dtype'

解决方案

我也在这里提出一个问题 https://github.com/tensorflow/tensorflow/issues/20663

最后我找到了解决它的正确方法.虽然.我不清楚为什么上面的答案无效!!如果你知道这个问题,谢谢你给我一些建议!!

好的~~~~这是当前的解决方案.实际上从这里Feature Columns Embedding lookup

代码:

itemx_vocab = tf.feature_column.categorical_column_with_vocabulary_file(键='itemx',词汇文件=标志.vocabx)embedding_initializer_x = tf.contrib.framework.load_embedding_initializer(ckpt_path='model.ckpt',embedding_tensor_name='w_in',new_vocab_size=itemx_vocab.vocabulary_size,embedding_dim=emb_size,old_vocab_file='FLAGS.vocab_emb',new_vocab_file=标志.vocabx)itemx_emb = tf.feature_column.embedding_column(itemx_vocab,维度=128,初始值设定项=嵌入_初始值设定项_x,可训练=假)

How to use pre-trained embedding with tf.feature_column.embedding_column.

I used pre_trained embedding in tf.feature_column.embedding_column. But it doesn't work. Error is

ValueError: initializer must be callable if specified. Embedding of column_name: itemx

weight, vocab_size, emb_size = _create_pretrained_emb_from_txt(FLAGS.vocab,
FLAGS.pre_emb)

W = tf.Variable(tf.constant(0.0, shape=[vocab_size, emb_size]),
                trainable=False, name="W")
embedding_placeholder = tf.placeholder(tf.float32, [vocab_size, emb_size])
embedding_init = W.assign(embedding_placeholder)

sess = tf.Session()
sess.run(embedding_init, feed_dict={embedding_placeholder: weight})

itemx_vocab = tf.feature_column.categorical_column_with_vocabulary_file(
    key='itemx',
    vocabulary_file=FLAGS.vocabx)

itemx_emb = tf.feature_column.embedding_column(itemx_vocab,
                                               dimension=emb_size,
                                               initializer=W,
                                               trainable=False)

I have tried initializer = lambda w:W. like this:

itemx_emb = tf.feature_column.embedding_column(itemx_vocab,
                                               dimension=emb_size,
                                               initializer=lambda w:W,
                                               trainable=False)

TypeError: () got an unexpected keyword argument 'dtype'

解决方案

I also take a issue here https://github.com/tensorflow/tensorflow/issues/20663

finally I got a right way with to solve it. although. i'm not clear why answer above is not effective!! if you know the question, Thanks to give some suggestion to me!!

ok~~~~here is current solvement. Actually from here Feature Columns Embedding lookup

itemx_vocab = tf.feature_column.categorical_column_with_vocabulary_file(
    key='itemx',
    vocabulary_file=FLAGS.vocabx)

embedding_initializer_x = tf.contrib.framework.load_embedding_initializer(
    ckpt_path='model.ckpt',
    embedding_tensor_name='w_in',
    new_vocab_size=itemx_vocab.vocabulary_size,
    embedding_dim=emb_size,
    old_vocab_file='FLAGS.vocab_emb',
    new_vocab_file=FLAGS.vocabx
)
itemx_emb = tf.feature_column.embedding_column(itemx_vocab,
                                               dimension=128,
                                               initializer=embedding_initializer_x,
                                               trainable=False)

这篇关于特征列预训练嵌入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 16:41