Python 特征列预训练嵌入

Python 特征列预训练嵌入,python,tensorflow,tensorflow-estimator,Python,Tensorflow,Tensorflow Estimator,如何在tf.feature\u column.embedding\u column中使用预先训练好的嵌入 我使用了pre\u-trained嵌入tf.feature\u column.embedded\u column。但它不起作用。错误是 错误是: ValueError:如果指定,则初始值设定项必须可调用。嵌入列名称:itemx 这是我的密码: 我试过initializer=lambda w:w。如下所示: itemx_emb = tf.feature_column.embedding_col

如何在
tf.feature\u column.embedding\u column
中使用预先训练好的嵌入

我使用了
pre\u-trained
嵌入
tf.feature\u column.embedded\u column
。但它不起作用。错误是

错误是:

ValueError:如果指定,则初始值设定项必须可调用。嵌入列名称:itemx

这是我的密码:

我试过initializer=lambda w:w。如下所示:

itemx_emb = tf.feature_column.embedding_column(itemx_vocab,
                                               dimension=emb_size,
                                               initializer=lambda w:W,
                                               trainable=False)
它报告错误:

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

我在这里也遇到了一个问题

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

好的~~~~这是当前的偿付能力。实际上从这里开始

代码:


您还可以将数组包装成如下函数:

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
                                                       )

这是一种令人讨厌的方法,但确实有效

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
                                                       )