input must have last dimension = k = 3 but is 2 for 'TopKV2_这种错误是怎么产生的
-> 1864 ? ? c_op = c_api.TF_FinishOperation(op_desc)
? ?1865 ? except errors.InvalidArgumentError as e:
InvalidArgumentError: input must have last dimension >= k = 3 but is 2 for 'TopKV2_5' (op: 'TopKV2') with input shapes: [10,2], [] and with computed input tensors: input[1] = <3>.
During handling of the above exception, another exception occurred:
import tensorflow as tf import numpy as np# 建立一個長度為10的向量,內部數據隨機生成。 a = tf.convert_to_tensor(np.random.random([10,5]))# 取出前5個最大的數據,默認從大到小進行排序。 b = tf.nn.top_k(a, 3)with tf.Session() as sess:#print(sess.run(a))print(sess.run(b))#print(sess.run(b[1]))TopKV2(values=array([[0.66026178, 0.54823469, 0.37898699],
? ? ? ?[0.79200312, 0.77568642, 0.74089808],
? ? ? ?[0.96763732, 0.63758324, 0.60687008],
? ? ? ?[0.80445416, 0.6118514 , 0.36231455],
? ? ? ?[0.78002845, 0.65835375, 0.64277663],
? ? ? ?[0.68795806, 0.59848445, 0.30689224],
? ? ? ?[0.75036776, 0.66124513, 0.46344809],
? ? ? ?[0.99914842, 0.83331088, 0.62411206],
? ? ? ?[0.76538444, 0.60968665, 0.59446372],
? ? ? ?[0.72230054, 0.4663702 , 0.41750983]]), indices=array([[3, 0, 2],
? ? ? ?[0, 4, 1],
? ? ? ?[2, 0, 4],
? ? ? ?[2, 4, 0],
? ? ? ?[2, 3, 1],
? ? ? ?[1, 2, 4],
? ? ? ?[3, 0, 1],
? ? ? ?[3, 4, 2],
? ? ? ?[4, 0, 2],
? ? ? ?[3, 0, 2]], dtype=int32))
tf.nn.top_k主要是最后一個維度中取前k個值;如果最后數據最后的一個維度小于k的數量,就會出現上述錯誤
總結
以上是生活随笔為你收集整理的input must have last dimension = k = 3 but is 2 for 'TopKV2_这种错误是怎么产生的的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 子元素是字典列表转成字典
- 下一篇: 业务需求、客户需求与功能需求