• tf.one_hot()


    1.tf.one_hot()

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

    作用:将input转化为one-hot类型数据输出,相当于将多个数值联合放在一起作为多个相同类型的向量,可用于表示各自的概率分布,通常用于分类任务中作为最后的FC层的输出,有时翻译成“独热”编码。

    1. indices表示输入的多个数值,通常是矩阵形式;
    2. depth表示输出的尺寸。由于one-hot类型数据长度为depth位,其中只用一位数字表示原输入数据,这里的on_value就是这个数字,默认值为1,one-hot数据的其他位用off_value表示,默认值为0。
    3. tf.one_hot()函数规定输入的元素indices从0开始,最大的元素值不能超过(depth - 1),因此能够表示depth个单位的输入。若输入的元素值超出范围,输出的编码均为 [0, 0 … 0, 0]。
    4. indices = 0 对应的输出是[1, 0 … 0, 0], indices = 1 对应的输出是[0, 1 … 0, 0], 依次类推,最大可能值的输出是[0, 0 … 0, 1]。

    bert源码:

    flat_input_ids = tf.reshape(input_ids, [-1]) #【batch_size*seq_length*input_num】
    if use_one_hot_embeddings:
      one_hot_input_ids = tf.one_hot(flat_input_ids, depth=vocab_size)
      output = tf.matmul(one_hot_input_ids, embedding_table)
    else:
      output = tf.gather(embedding_table, flat_input_ids)
  • 相关阅读:
    C# using
    Spring框架
    is
    pycharm破解197
    python安装197
    python3.7.0安装197
    centos7 minimal 安装mysql197
    centos7 minimal 安装 &网络配置197
    ruby安装卸载197
    redis安装 卸载 启动 关闭197
  • 原文地址:https://www.cnblogs.com/nxf-rabbit75/p/12096589.html
Copyright © 2020-2023  润新知