张量上的Argmax与Tensorflow中的天花板

张量上的Argmax与Tensorflow中的天花板,tensorflow,neural-network,keras,softmax,argmax,Tensorflow,Neural Network,Keras,Softmax,Argmax,假设Tensorflow中有一个张量,其值如下: A = [[0.7, 0.2, 0.1],[0.1, 0.4, 0.5]] 如何将此张量更改为以下值: B = [[1, 0, 0],[0, 0, 1]] 换句话说,我只想保留最大值,并将其替换为1。 如果有任何帮助,我们将不胜感激。我想您可以用一行代码来解决这个问题: import tensorflow as tf import numpy as np x_data = [[0.7, 0.2, 0.1],[0.1, 0.4, 0.5]]

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

A = [[0.7, 0.2, 0.1],[0.1, 0.4, 0.5]]
如何将此张量更改为以下值:

B = [[1, 0, 0],[0, 0, 1]] 
换句话说,我只想保留最大值,并将其替换为1。

如果有任何帮助,我们将不胜感激。

我想您可以用一行代码来解决这个问题:

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)

我认为你可以用一句话来解决这个问题:

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)

真聪明!谢西特很聪明!谢谢