解决ValueError: Cannot feed value of shape (1,) for Tensor 'Placeholder_1:0', which has shape '(?, 1)'

我的问题出现在如下代码段:

feed_dict = {model.user_input: user_input[i],
             model.item_input_pos: item_input_pos[i],
             model.item_input_neg: item_input_neg[i]}
if output_adv:
    loss, output_pos, output_neg = sess.run([model.loss_adv, model.output_adv, model.output_neg_adv], feed_dict)
else:
    loss, output_pos, output_neg = sess.run([model.loss, model.output, model.output_neg], feed_dict)

核心问题就是两者shape无法匹配,通过修改代码加入reshape变为如下即可:

feed_dict = {model.user_input: user_input[i].reshape([1,-1]),
             model.item_input_pos: item_input_pos[i].reshape([1,-1]),
             model.item_input_neg: item_input_neg[i].reshape([1,-1])}
if output_adv:
    loss, output_pos, output_neg = sess.run([model.loss_adv, model.output_adv, model.output_neg_adv], feed_dict)
else:
    loss, output_pos, output_neg = sess.run([model.loss, model.output, model.output_neg], feed_dict)

你可能感兴趣的:(python)