首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >embedding_column计算邻域

embedding_column计算邻域
EN

Stack Overflow用户
提问于 2018-03-20 12:03:42
回答 1查看 342关注 0票数 1

我正在尝试使用最新的估值器API在tensorflow中实现类似于word2vec的模型。我遇到的问题是,当我试图评估模型时。模型本身应成为推荐系统的基础。现在,我想使用的评估指标是命中率指标,如下所示:

  • 对于每个输入,计算k邻域。
  • 确定预期的标签是否位于k邻域,如果是,则是成功的推荐。

现在我已经建立了这样的模型:

代码语言:javascript
复制
# Map Embeddings Ids to One Hot Tensors
ref_embedding_ids = tf.feature_column.categorical_column_with_identity(
    key='Reference',
    num_buckets=params['dict_size'],
    default_value=0
)

# Map One Hot Tensors to Dense Embeddings
ref_embeddings = tf.feature_column.embedding_column(
    ref_embedding_ids,
    dimension=params['embedding_size']
)
# Actually create the input to the model
input_layer = tf.feature_column.input_layer(features, feature_columns=[ref_embeddings])

正如在估值器API教程中所做的那样。现在,我理解了这段代码,input_layer已经只包含在功能字典中引用的带有键Reference的嵌入。这是很棒的,因为我们不需要将所有的嵌入保存在内存中。但是现在,如果我想在评估模式下计算邻域,我需要访问所有可能输入的嵌入向量来计算相似点。但是,我没有任何参考,因为管道是设置的,只有必要的部分是加载的。我已经尝试找出保存嵌入的变量的名称,并使用tf.get_variable显式加载该变量,但这也不起作用。那么,我的问题是,如何计算给定嵌入id的邻域?

此外,在能够计算邻域之后,我需要使用一个度量来继续计算整个评估集的度量,因为对于来自数据集的每一批,都会调用该函数。但我想这是一个不同的问题,我只是提到它的背景。

EN

回答 1

Stack Overflow用户

发布于 2018-07-30 21:25:12

我能够通过对cols_to_vars函数使用tf.feature_column.input_layer参数来实现这个功能。基本上,您需要将一个空字典传递给tf.feature_column.input_layer,它将被输入层中生成的变量填充。下面的示例只包含model_fn函数的模型作用域。希望能帮上忙。

代码语言:javascript
复制
def my_model_fn(features, labels, mode, params):

  with tf.name_scope('model'):
    num_vocabulary = len(params['vocabulary'])

    #create embedding vectors for history feature
    vocabulary_lookup = tf.contrib.lookup.index_table_from_tensor(
      name='vocabulary_lookup',
      mapping=params['vocabulary'],
      default_value=-1,
      num_oov_buckets=1
    )
    #print("lookup: {}".format(vocabulary_lookup))

    #create a bais matrix
    rank_biases = tf.get_variable(name='rank_biases', shape=[num_vocabulary])

    # input layer
    input_variable_ref = {}
    net = tf.feature_column.input_layer(features, params['feature_columns'], cols_to_vars=input_variable_ref)
    embedding_metrix = input_variable_ref[params['feature_columns'][0]][0]
    print("embedding metrix => {}".format(embedding_metrix))

    # hidden layers
    for units in params['hidden_units']:
      net = tf.layers.dense(net, units=units, activation=tf.nn.relu)

    # output layer
    with tf.name_scope('DNN_output'):
      logits = tf.layers.dense(net, params['out_layer_dim'], activation=None)
      #print("logits shape: {}".format(logits.shape))
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49383723

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档