TensorFlow tf.one_hot()函数

tf.one_hot函数可以将输入数据转成onehot编码。至于什么是onehot编码?为什么要将输入转成onehot编码?请查看我的另一篇博文:https://blog.csdn.net/duanlianvip/article/details/93492855

一、参数

def one_hot(indices,
            depth,
            on_value=None,
            off_value=None,
            axis=None,
            dtype=None,
            name=None)

返回值:返回一个onehot编码的张量。

  1. indices:输入的张量[batch, features](待转onehot编码张量);
  2. depth:onehot编码的一个维度;
  3. on_value=None:默认值为1
  4. off_value=None:默认值为0
  5. axis=None:默认为-1
  6. dtype=None:输出张量的数据类型。若dtype缺省,则其根据on_value或off_value的数据类型自动保持一致;若dtype、on_value、off_value都缺省,则默认为tf.float32 
  7. name=None:本次one_hot操作的名称

注意:

  1. on_value、off_value、dtype三者数据类型应相同;
  2. 如果indices为标量(scalar),则输出一个长度为“depth”的向量;
  3. 如果indices为长度为“features”的向量(vector),则输出的矩阵维度为:
axis取值输出的矩阵维度
axis == -1features * depth
axis == 0depth * features

     4. 如果indices为一个维度为“[batch, features]”的矩阵,则输出的矩阵维度为:

axis取值输出的矩阵维度
axis == -1batch * features * depth
axis == 1batch * depth * features
axis == 0depth * batch * features

     5. tf.one_hot()函数规定输入的元素indices从0开始,最大的元素值不能超过(depth - 1),因此能够表示depth个单位的输入。若输入的元素值超出范围,输出的编码均为 [0, 0 … 0, 0]。

     6. indices = 0 对应的输出是[1, 0 … 0, 0], indices = 1 对应的输出是[0, 1 … 0, 0], 依次类推,最大可能值的输出是[0, 0 … 0, 1]。

二、实验验证

验证程序:

import tensorflow as tf
import numpy as np

a = tf.Variable(2)
b = tf.Variable([1, 2, 3, 4, 5])
c = tf.Variable([[1, 2], [5, 6], [7, 8]])

a_one_hot1 = tf.one_hot(a, 1)
a_one_hot2 = tf.one_hot(a, 4)
b_one_hot1 = tf.one_hot(b, 6, axis=-1)
b_one_hot2 = tf.one_hot(b, 6, axis=0)
b_one_hot3 = tf.one_hot(b, 6, on_value=5, off_value=2)
b_one_hot4 = tf.one_hot(b, 6, on_value='x', off_value='y')
b_one_hot5 = tf.one_hot(b, 6, on_value='x', off_value='y', dtype=tf.string)
c_one_hot1 = tf.one_hot(c, 8)
c_one_hot2 = tf.one_hot(c, 9,axis=-1)
c_one_hot3 = tf.one_hot(c, 9,axis=1)
c_one_hot4 = tf.one_hot(c, 9,axis=0)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    print(sess.run(a_one_hot1))
    print('------------------')
    print(sess.run(a_one_hot2))
    print('------------------')
    print(sess.run(b_one_hot1))
    print('------------------')
    print(sess.run(b_one_hot2))
    print('------------------')
    print(sess.run(b_one_hot3))
    print('------------------')
    print(sess.run(b_one_hot4))
    print('------------------')
    print(sess.run(b_one_hot5))
    print('------------------')
    print(sess.run(c_one_hot1))
    print('------------------')
    print(sess.run(c_one_hot2))
    print('------------------')
    print(sess.run(c_one_hot3))
    print('------------------')
    print(sess.run(c_one_hot4))

输出结果:

[0.] # 最大的元素值超过"depth-1"(1-1),所以显示为0
------------------
[0. 0. 1. 0.] # one_hot(2)
------------------
[[0. 1. 0. 0. 0. 0.] # shape:[5, 6]
 [0. 0. 1. 0. 0. 0.] 
 [0. 0. 0. 1. 0. 0.]
 [0. 0. 0. 0. 1. 0.]
 [0. 0. 0. 0. 0. 1.]]
------------------
[[0. 0. 0. 0. 0.] # shape:[6, 5]
 [1. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0.]
 [0. 0. 1. 0. 0.]
 [0. 0. 0. 1. 0.]
 [0. 0. 0. 0. 1.]]
------------------
[[2 5 2 2 2 2]
 [2 2 5 2 2 2]
 [2 2 2 5 2 2]
 [2 2 2 2 5 2]
 [2 2 2 2 2 5]]
------------------
[[b'y' b'x' b'y' b'y' b'y' b'y']
 [b'y' b'y' b'x' b'y' b'y' b'y']
 [b'y' b'y' b'y' b'x' b'y' b'y']
 [b'y' b'y' b'y' b'y' b'x' b'y']
 [b'y' b'y' b'y' b'y' b'y' b'x']]
------------------
[[b'y' b'x' b'y' b'y' b'y' b'y']
 [b'y' b'y' b'x' b'y' b'y' b'y']
 [b'y' b'y' b'y' b'x' b'y' b'y']
 [b'y' b'y' b'y' b'y' b'x' b'y']
 [b'y' b'y' b'y' b'y' b'y' b'x']]
------------------
[[[0. 1. 0. 0. 0. 0. 0. 0.] # shape:[3, 2, 8]
  [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.]]] # one_hot(8),由于8大于"depth-1",所以显示为0
------------------
[[[0. 1. 0. 0. 0. 0. 0. 0. 0.] # shape:[3, 2, 9]
  [0. 0. 1. 0. 0. 0. 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. 1. 0.]
  [0. 0. 0. 0. 0. 0. 0. 0. 1.]]]
------------------
[[[0. 0.] # shape:[3, 9, 2]
  [1. 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.]
  [1. 0.]
  [0. 1.]
  [0. 0.]
  [0. 0.]]

 [[0. 0.]
  [0. 0.]
  [0. 0.]
  [0. 0.]
  [0. 0.]
  [0. 0.]
  [0. 0.]
  [1. 0.]
  [0. 1.]]]
------------------
[[[0. 0.] # shape:[9, 3, 2]
  [0. 0.]
  [0. 0.]]

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

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

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

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

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

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

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

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

在人工智能的学习过程中,可繁可简,弄清每一个细节需要耗费大量时间精力,文中有不对的地方烦请指出,愿我们一起提高进步。

参考:

https://blog.csdn.net/nini_coded/article/details/79250600

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import time import tensorflow.compat.v1 as tf tf.disable_v2_behavior() from tensorflow.examples.tutorials.mnist import input_data import mnist_inference import mnist_train tf.compat.v1.reset_default_graph() EVAL_INTERVAL_SECS = 10 def evaluate(mnist): with tf.Graph().as_default() as g: #定义输入与输出的格式 x = tf.compat.v1.placeholder(tf.float32, [None, mnist_inference.INPUT_NODE], name='x-input') y_ = tf.compat.v1.placeholder(tf.float32, [None, mnist_inference.OUTPUT_NODE], name='y-input') validate_feed = {x: mnist.validation.images, y_: mnist.validation.labels} #直接调用封装好的函数来计算前向传播的结果 y = mnist_inference.inference(x, None) #计算正确率 correcgt_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correcgt_prediction, tf.float32)) #通过变量重命名的方式加载模型 variable_averages = tf.train.ExponentialMovingAverage(0.99) variable_to_restore = variable_averages.variables_to_restore() saver = tf.train.Saver(variable_to_restore) #每隔10秒调用一次计算正确率的过程以检测训练过程中正确率的变化 while True: with tf.compat.v1.Session() as sess: ckpt = tf.train.get_checkpoint_state(minist_train.MODEL_SAVE_PATH) if ckpt and ckpt.model_checkpoint_path: #load the model saver.restore(sess, ckpt.model_checkpoint_path) global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1] accuracy_score = sess.run(accuracy, feed_dict=validate_feed) print("After %s training steps, validation accuracy = %g" % (global_step, accuracy_score)) else: print('No checkpoint file found') return time.sleep(EVAL_INTERVAL_SECS) def main(argv=None): mnist = input_data.read_data_sets(r"D:\Anaconda123\Lib\site-packages\tensorboard\mnist", one_hot=True) evaluate(mnist) if __name__ == '__main__': tf.compat.v1.app.run()对代码进行改进
最新发布
05-26

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值