【问题标题】:Creating a Tensor using a pre-populated pd dataframe?使用预先填充的 pd 数据框创建张量?
【发布时间】:2016-12-15 12:32:04
【问题描述】:

我正在尝试创建一个基于项目的协同过滤推荐引擎。由于流过的数据量很大,我不得不使用 TensorFlow。但是,即使在文档和互联网上花费数小时后,我也无法弄清楚如何使用预先填充的 ndarray 创建张量。

我正在尝试使用用户项操作转置 ndarray 以将信息记录在张量中。我调试了多个错误,包括形状不匹配,只是为了找到一个我找不到任何信息的新错误。下面是代码。非常感谢任何帮助/建议。

user_item_matrix = tf.Variable(np.zeros(shape = (num_usr,num_prd)),dtype=tf.int32)
init_op = tf.initialize_all_variables()
sess = tf.Session()
sess.run(init_op)
for index,row in user_item_action.iterrows():
    update_row = usr_ht[row['user_id']]
    update_col = prd_ht[row['product_id']]
# Updating row slices of the tensor
update = [0]*num_prd
update[update_col] = row['action_score']
user_item_matrix = sess.run(tf.scatter_add(user_item_matrix,update_row,np.transpose(update)))
sess.close()

TypeError: Input 'ref' of 'ScatterAdd' Op 需要左值输入

user_item_action.head(): user_id product_id action_score 1354 76864 196823 10 2626 23364 234437 10 6422 8055 231014 10 9877 81965 200476 10 13334 88132 240015 10

【问题讨论】:

  • 如果代码中不使用这个变量,为什么要显示user_action_item.head()?有错别字吗?
  • 已更正,感谢指出。

标签: python numpy machine-learning tensorflow recommendation-engine


【解决方案1】:

不要将sess.run 的结果分配给user_item_matrixsess.run 返回已更改变量的,如果您为 user_item_matrix 分配一个常规值,它将不再是 TF 变量,您将无法再分配它。

【讨论】:

  • 嘿 Sygi,感谢您的洞察力。我有一种预感,这就是问题所在,但是我不确定如何更新 user_item_matrix 呢?似乎是第22条的情况。你能帮我解决这个问题吗?
  • sess.run(scatter_add(user_item_matrix, ... 确实更新user_item_matrix变量。
猜你喜欢
  • 2011-09-17
  • 1970-01-01
  • 1970-01-01
  • 2021-06-30
  • 1970-01-01
  • 2017-03-29
  • 2012-11-03
  • 2018-07-17
  • 2013-03-08
相关资源
最近更新 更多