在tensorflow中,如何在进食前使用占位符的值?

时间:2017-06-15 03:31:06

标签: python tensorflow

TensorFlow新手,现在我需要在进食之前使用占位符中的值,例如:

tensor = tf.placeholder(tf.float32, [None, 3])
Mat_items = tf.Variable(tf.random_normal([10,10]))
Mat_users = tf.Variable(tf.random_normal([10,10]))
item_index = tensor[:, 0]
user_index = tensor[:, 1]
rating = tensor[:, 2]
Val = Mat_items[item_index, :]-Mat_users[user_index, :]

虽然 tensor 是一个占位符,有N行3列,第一列和第二列分别是Mat_items和Mat_users的索引, Mat_items Mat_users 需要对变量进行索引 运行它绝对会引发错误,因为item_index,user_index在进给之前都是 Tensor 而不是数字。 所以我想知道Tensorflow能否实现这一需求? 任何建议将不胜感激!:)

=============================================== ========================== 除了我的问题:
Val取决于Tensor中的某些列,就像第一列和第二列一样。所以当我创建我的图表时,我会编码

Val = Mat_items[item_index, :]-Mat_users[user_index, :]

item_index和user_index是tensor的切片,它们都是类型tensor。它会抛出错误。我不知道如何在TensorFlow中实现这个需求。

=============================================== ========================== 找到了解决方案:

tensor = tf.placeholder(tf.float32, [None, 3])
Mat_items = tf.Variable(tf.random_normal([10,10]))
Mat_users = tf.Variable(tf.random_normal([10,10]))
for each in range(batch_number):
    item_ind, user_ind = tensor[each, 2], tensor[each, 1]
    rating = tensor[each, 1]
    Val = Mat_item[item_ind, 0]*Mat_user[user_ind, 0]*rating

上面的代码似乎在构建gragh成本太多时间时工作,即使使用litte数据集(批量大小约1000并且只需要一个批次进行测试),构建图形大约需要78秒,我是不确定它是否正常?

1 个答案:

答案 0 :(得分:0)

您的问题似乎含糊不清,但我想您的问题是如何将值放入占位符。如果要从Val获取输出,则必须使用输入补充tensor,因为默认情况下它不包含任何值。 Val还依赖于tensor来计算其输出。您的输入必须与tensor的大小相同。 (在这种情况下,假设您的输入是随机噪声input_tensor = numpy.random.uniform(-1, 1, size =(None, 3)),其中None是您必须指定的值。

因此,在开始会话后,执行 output = sess.run(Val, feed_dict = {tensor: input_tensor}) output将是您的结果