Tensorflow 的tf.one_hot()功能:dense to one hot

本篇博客详细介绍了Tensorflow中的tf.one_hot()函数,用于将稠密编码的数据转换为独热编码。文章涵盖了函数的参数设置,包括indices、depth、on_value、off_value、axis和dtype,并强调了数据类型匹配的重要性。通过该函数,可以在指定轴上创建一个独热维度的张量,这对于分类问题尤其有用。
部署运行你感兴趣的模型镜像
import tensorflow as tf
indices = [[3], [5], [0], [7]]
indices = tf.concat(0, indices)
indices = tf.reshape(indice, (4, 1))
a = tf.one_hot(indices, depth=10, on_value=None, off_value=None, axis=None, dtype=None, name=None)
print ("a is : ")
print a
b = tf.reshape(a, (4, 10))
print ("a is : ")
print b

'''
a is : 
Tensor("one_hot:0", shape=(4, 1, 10), dtype=float32)
a is : 
Tensor("Reshape_1:0", shape=(4, 10), dtype=float32)
'''
Args:
  • indices: A Tensor of indices.
  • depth: A scalar defining the depth of the one hot dimension.
  • on_value: A scalar defining the value to fill in output when indices[j] = i. (default: 1)
  • off_value: A scalar defining the value to fill in output when indices[j] != i. (default: 0)
  • axis: The axis to fill (default: -1, a new inner-most axis).
  • dtype: The data type of the output tensor.
Returns:
  • output: The one-hot tensor.
Raises:
  • TypeError: If dtype of either on_value or off_value don't match dtype
  • TypeError: If dtype of on_value and off_value don't match one another

您可能感兴趣的与本文相关的镜像

TensorFlow-v2.15

TensorFlow-v2.15

TensorFlow

TensorFlow 是由Google Brain 团队开发的开源机器学习框架,广泛应用于深度学习研究和生产环境。 它提供了一个灵活的平台,用于构建和训练各种机器学习模型

D:\programer\anaconda\envs\myenv\python.exe "D:\22threenext\人工智能\experiment4\digimal image2\cgan_tensorflow1.py" Extracting ./MNIST_data/train-images-idx3-ubyte.gz WARNING:tensorflow:From D:\22threenext\人工智能\experiment4\digimal image2\cgan_tensorflow1.py:11: read_data_sets (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version. Instructions for updating: Please use alternatives such as official/mnist/dataset.py from tensorflow/models. WARNING:tensorflow:From D:\programer\anaconda\envs\myenv\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py:260: maybe_download (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version. Instructions for updating: Please write your own downloading logic. WARNING:tensorflow:From D:\programer\anaconda\envs\myenv\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py:262: extract_images (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version. Instructions for updating: Please use tf.data to implement this functionality. WARNING:tensorflow:From D:\programer\anaconda\envs\myenv\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py:267: extract_labels (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version. Instructions for updating: Please use tf.data to implement this functionality. WARNING:tensorflow:From D:\programer\anaconda\envs\myenv\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py:110: dense_to_one_hot (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version. Instructions for updating: Please use tf.one_hot on tensors. Extracting ./MNIST_data/train-labels-idx1-ubyte.gz Extracting ./MNIST_data/t10k-images-idx3-ubyte.gz WARNING:tensorflow:From D:\programer\anaconda\envs\myenv\lib\site-packages\tensorflow\contrib\learn\python\learn\datasets\mnist.py:290: DataSet.__init__ (from tensorflow.contrib.learn.python.learn.datasets.mnist) is deprecated and will be removed in a future version. Instructions for updating: Please use alternatives such as official/mnist/dataset.py from tensorflow/models. Extracting ./MNIST_data/t10k-labels-idx1-ubyte.gz WARNING:tensorflow:From D:\22threenext\人工智能\experiment4\digimal image2\cgan_tensorflow1.py:65: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead. WARNING:tensorflow:From D:\22threenext\人工智能\experiment4\digimal image2\cgan_tensorflow1.py:22: The name tf.random_normal is deprecated. Please use tf.random.normal instead. WARNING:tensorflow:From D:\programer\anaconda\envs\myenv\lib\site-packages\tensorflow\python\ops\nn_impl.py:180: add_dispatch_support.<locals>.wrapper (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version. Instructions for updating: Use tf.where in 2.0, which has the same broadcast rule as np.where WARNING:tensorflow:From D:\22threenext\人工智能\experiment4\digimal image2\cgan_tensorflow1.py:96: The name tf.train.AdamOptimizer is deprecated. Please use tf.compat.v1.train.AdamOptimizer instead. WARNING:tensorflow:From D:\22threenext\人工智能\experiment4\digimal image2\cgan_tensorflow1.py:100: The name tf.Session is deprecated. Please use tf.compat.v1.Session instead. 2025-06-02 13:47:24.646890: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 WARNING:tensorflow:From D:\22threenext\人工智能\experiment4\digimal image2\cgan_tensorflow1.py:101: The name tf.global_variables_initializer is deprecated. Please use tf.compat.v1.global_variables_initializer instead. Iter: 0 D loss: 1.244 G_loss: 2.327 Traceback (most recent call last): File "D:\22threenext\人工智能\experiment4\digimal image2\cgan_tensorflow1.py", line 137, in <module> psnr, ssim = calculate_psnr_ssim(real_images, generated_images_resized) File "D:\22threenext\人工智能\experiment4\digimal image2\cgan_tensorflow1.py", line 55, in calculate_psnr_ssim ssim = tf.image.ssim(real_images, generated_images, max_val=1.0) File "D:\programer\anaconda\envs\myenv\lib\site-packages\tensorflow\python\ops\image_ops_impl.py", line 3123, in ssim _, _, checks = _verify_compatible_image_shapes(img1, img2) File "D:\programer\anaconda\envs\myenv\lib\site-packages\tensorflow\python\ops\image_ops_impl.py", line 2825, in _verify_compatible_image_shapes shape1 = img1.get_shape().with_rank_at_least(3) AttributeError: 'numpy.ndarray' object has no attribute 'get_shape' 进程已结束,退出代码为 1 为什么你给的代码运行后异常退出
最新发布
06-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值