one_hot函数

import tensorflow as tf
import numpy as np

a =[[1,2,3,2,1],[1,2,3,1,1]]

b = tf.one_hot(a,depth=10,axis=-1)
print('b')
print(b)

print('c')
c = tf.one_hot(a,depth=10,axis=0)
print(c)

print('d')
d = tf.one_hot(a,depth=10,axis=1)
print(d)

print('e')
e = tf.one_hot(a,depth=10,axis=2)
print(e)


b
tf.Tensor(
[[[0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
  [0. 0. 1. 0. 0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]
  [0. 0. 1. 0. 0. 0. 0. 0. 0. 0.]
  [0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]]

 [[0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
  [0. 0. 1. 0. 0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]
  [0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
  [0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]]], shape=(2, 5, 10), dtype=float32)
c
tf.Tensor(
[[[0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0.]]

 [[1. 0. 0. 0. 1.]
  [1. 0. 0. 1. 1.]]

 [[0. 1. 0. 1. 0.]
  [0. 1. 0. 0. 0.]]

 [[0. 0. 1. 0. 0.]
  [0. 0. 1. 0. 0.]]

 [[0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0.]]

 [[0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0.]]

 [[0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0.]]

 [[0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0.]]

 [[0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0.]]

 [[0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0.]]], shape=(10, 2, 5), dtype=float32)
d
tf.Tensor(
[[[0. 0. 0. 0. 0.]
  [1. 0. 0. 0. 1.]
  [0. 1. 0. 1. 0.]
  [0. 0. 1. 0. 0.]
  [0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0.]]

 [[0. 0. 0. 0. 0.]
  [1. 0. 0. 1. 1.]
  [0. 1. 0. 0. 0.]
  [0. 0. 1. 0. 0.]
  [0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0.]
  [0. 0. 0. 0. 0.]]], shape=(2, 10, 5), dtype=float32)
e
tf.Tensor(
[[[0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
  [0. 0. 1. 0. 0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]
  [0. 0. 1. 0. 0. 0. 0. 0. 0. 0.]
  [0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]]

 [[0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
  [0. 0. 1. 0. 0. 0. 0. 0. 0. 0.]
  [0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]
  [0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
  [0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]]], shape=(2, 5, 10), dtype=float32)
import numpy as np
import tensorflow as tf


indices = [0, 1, 1]  # rank=1
depth = 8
a = tf.one_hot(indices, depth)  # rank=2,输出为[3,3]

indices=[0,2,-2,1]  #rank=1
depth=7
b=tf.one_hot(indices,depth,on_value=5.0,off_value=1.0,axis=-1)

print(a)
print(b)


结果:
tf.Tensor(
[[1. 0. 0. 0. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0. 0. 0. 0.]], shape=(3, 8), dtype=float32)
tf.Tensor(
[[5. 1. 1. 1. 1. 1. 1.]
 [1. 1. 5. 1. 1. 1. 1.]
 [1. 1. 1. 1. 1. 1. 1.]
 [1. 5. 1. 1. 1. 1. 1.]], shape=(4, 7), dtype=float32)
2022-01-26 23:03:21.608213: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x2181e053ce0 initialized for platform Host (this does not guarantee that XLA will be used). Devices:
2022-01-26 23:03:21.608562: I tensorflow/compiler/xla/service/service.cc:176]   StreamExecutor device (0): Host, Default Version
2022-01-26 23:03:21.608979: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1102] Device interconnect StreamExecutor with strength 1 edge matrix:
2022-01-26 23:03:21.609264: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1108]      

Process finished with exit code 0

one_hot(a,b)中,b代表向量的长度

                           a为列表,代表每个行向量中主元素(on_value)的值为1(若设定,则为设定值),其余元素(off_value)的值为0(若设定,则为设定值)。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值