python - 最大值的二元向量

标签 python tensorflow

如果我有一个像这样的向量

[2,5,8,1]

如何将其转换为二进制向量,其中除了最大元素将表示为 1 之外,所有元素均为 0?

[0,0,1,0]

我希望这可以在 Tensorflow 中优雅地完成。

最佳答案

这里:

X = np.array([2,5,8,1])
one_hot = tf.one_hot(indices=tf.argmax(X), depth=tf.shape(X)[0])

关于python - 最大值的二元向量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54493814/

相关文章:

java - 仅使用默认库在 jython 中解码 json

python - 将适用于 Python 的 Google App Engine SDK 与 Python 3 结合使用

python - 使用 cuda 10 安装 pip tensorflow-gpu 后出错

python - 张量 'embedding_input' 具有无效形状 '[None, None]'

python - Tensorflow + google ml-engne 本地预测 : How CSV quotes should be processed?

python - 具有 2 个组件的 pca 输出 1 个特征

python - Pandas 和 Python 数据框和条件移位函数

Python UTF-8 无法在 32 位机器上解码字节

Tensorflow:损失减少,但精度稳定

python - Tensorflow/Keras/BERT 多类文本分类准确率