Tensorflow中的张量和天花板上的Argmax [英] Argmax on a tensor and ceiling in Tensorflow

查看:85
本文介绍了Tensorflow中的张量和天花板上的Argmax的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在Tensorflow中有一个张量,其值如下:

Suppose I have a tensor in Tensorflow that its values are like:

A = [[0.7, 0.2, 0.1],[0.1, 0.4, 0.5]]

如何将这个张量更改为以下内容:

How can I change this tensor into the following:

B = [[1, 0, 0],[0, 0, 1]] 

换句话说,我只想保留最大值并将其替换为1.
任何帮助将不胜感激.

In other words I want to just keep the maximum and replace it with 1.
Any help would be appreciated.

推荐答案

我认为您可以使用单线解决方案:

I think that you can solve it with a one-liner:

import tensorflow as tf
import numpy as np

x_data = [[0.7, 0.2, 0.1],[0.1, 0.4, 0.5]]
# I am using hard-coded dimensions for simplicity
x = tf.placeholder(dtype=tf.float32, name="x", shape=(2,3))

session = tf.InteractiveSession()

session.run(tf.one_hot(tf.argmax(x, 1), 3), {x: x_data})

结果是您期望的结果:

Out[6]: 
array([[ 1.,  0.,  0.],
       [ 0.,  0.,  1.]], dtype=float32)

这篇关于Tensorflow中的张量和天花板上的Argmax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆