特征列预训练嵌入
Feature Column Pre-trained Embedding
如何使用 tf.feature_column.embedding_column
的预训练嵌入。
我在 tf.feature_column.embedding_column
中使用了 pre_trained
嵌入。但它不起作用。错误是
The error is :
ValueError:如果指定,初始化程序必须是可调用的。 column_name 的嵌入:itemx
Here's my code:
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)
我试过 initializer = lambda w:W。像这样:
itemx_emb = tf.feature_column.embedding_column(itemx_vocab,
dimension=emb_size,
initializer=lambda w:W,
trainable=False)
it reports the error:
TypeError: () 得到了一个意外的关键字参数 'dtype'
我这里也提个问题https://github.com/tensorflow/tensorflow/issues/20663
终于找到了解决问题的正确方法。虽然。我不清楚为什么上面的回答无效!!如果你知道这个问题,谢谢你给我一些建议!!
ok~~~~这是当前的解决方案。实际上从这里 Feature Columns Embedding lookup
code:
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)
您还可以将数组包装成这样的函数:
some_matrix = np.array([[0,1,2],[0,2,3],[5,6,7]])
def custom_init(shape, dtype):
return some_matrix
embedding_feature = tf.feature_column.embedding_column(itemx_vocab,
dimension=3,
initializer=custom_init
)
这是一种笨拙的方式,但可以完成工作。
如何使用 tf.feature_column.embedding_column
的预训练嵌入。
我在 tf.feature_column.embedding_column
中使用了 pre_trained
嵌入。但它不起作用。错误是
The error is :
ValueError:如果指定,初始化程序必须是可调用的。 column_name 的嵌入:itemx
Here's my code:
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)
我试过 initializer = lambda w:W。像这样:
itemx_emb = tf.feature_column.embedding_column(itemx_vocab,
dimension=emb_size,
initializer=lambda w:W,
trainable=False)
it reports the error:
TypeError: () 得到了一个意外的关键字参数 'dtype'
我这里也提个问题https://github.com/tensorflow/tensorflow/issues/20663
终于找到了解决问题的正确方法。虽然。我不清楚为什么上面的回答无效!!如果你知道这个问题,谢谢你给我一些建议!!
ok~~~~这是当前的解决方案。实际上从这里 Feature Columns Embedding lookup
code:
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)
您还可以将数组包装成这样的函数:
some_matrix = np.array([[0,1,2],[0,2,3],[5,6,7]])
def custom_init(shape, dtype):
return some_matrix
embedding_feature = tf.feature_column.embedding_column(itemx_vocab,
dimension=3,
initializer=custom_init
)
这是一种笨拙的方式,但可以完成工作。