我遇到此功能的断言错误...我该如何解决这个问题
def one_hot_matrix(label, depth=6):
one_hot = tf.one_hot(label, depth, axis = 0)
one_hot = tf.reshape(one_hot, (-1,1))
return one_hot
def one_hot_matrix_test(target):
label = tf.constant(1)
depth = 4
result = target(label, depth)
print("Test 1:",result)
assert result.shape[0] == depth, "Use the parameter depth"
assert np.allclose(result, [0., 1. ,0., 0.] ), "Wrong output. Use tf.one_hot"
label_2 = [2]
result = target(label_2, depth)
print("Test 2:", result)
assert result.shape[0] == depth, "Use the parameter depth"
assert np.allclose(result, [0., 0. ,1., 0.] ), "Wrong output. Use tf.reshape as instructed"
print("\033[92mAll test passed")
根据结果的形状,大多数情况下你会遇到断言错误。
为此请确保您使用
one_hot = tf.reshape(one_hot, (depth,))
这就是答案:
one_hot=tf.reshape(tf.one_hot(label, depth, axis = 0), [4,])
注意shape属性,小数点后没有值
one_hot = tf.reshape(tf.one_hot(label, depth, axis=0), shape=[depth,])