python - tensorflow One Hot Encodings 的使用
问题描述
我收到此功能的 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")
</p>
解决方案
大多数情况下,根据结果的形状,您会遇到断言错误。
为此,您使用
one_hot = tf.reshape(one_hot, (depth,))
推荐阅读
- javascript - Firebase如何链接使用phoneNumber创建的帐户
- python - Python 使用文件字节访问 zip 文件并遍历每个文件以将它们保存到数据帧中返回未找到文件的错误
- c++ - 带有引用的对象的 STL 容器
- git - 自动解决樱桃挑选冲突是否可能?
- microsoft-graph-api - MS Graph - SharePoint 错误:未添加成员
- python - 循环三个列表以创建组合输出?
- spring-boot - 无法使用自定义过滤器在 Spring Security 中实现会话限制
- algorithm - 压缩无序的哈希集合
- android - BouncyCastle:Android:不支持的类文件主要版本 59。无法转换 bcprov-jdk15on-1.67.jar (org.bouncycastle:bcprov-jdk15on:1.67)
- python - 不能在 python 中限制 randint