【问题标题】:Usage of tensorflow One Hot Encodingstensorflow One Hot Encodings 的使用
【发布时间】:2021-10-09 18:19:21
【问题描述】:

​我收到了这个函数的 AssertionError ... 我该如何解决这个问题

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")

【问题讨论】:

  • 请提供一些示例输入和错误。
  • 嗨!请提供完整的代码和错误详情,以帮助我们理解问题。

标签: python tensorflow one-hot-encoding


【解决方案1】:

根据结果的形状,您通常会遇到断言错误。
为此,您使用

one_hot = tf.reshape(one_hot, (depth,))

【讨论】:

    猜你喜欢
    • 2017-06-07
    • 2018-03-30
    • 1970-01-01
    • 2017-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-09
    • 2020-09-18
    相关资源
    最近更新 更多