当前位置: 代码迷 >> 综合 >> tf.gather和tf.softmax使用
  详细解决方案

tf.gather和tf.softmax使用

热度:35   发布时间:2024-03-06 05:53:37.0

tf.gather(Tensor, index)可以获取某个Tensor指定index的元素。

tf.nn.softmax(Tensor)可以对某个Tensor进行处理。它不改变Tensor的结构,其效果类似于某种归一化。

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
import numpy as npX_batch=  np.array([[[0,1,2],[9,8,7]],[[3,4,5],[0,0,0]],[[6,7,8],[6,5,4]],[[9,0,1],[3,2,1]],
])
X = tf.placeholder(dtype=tf.float16,shape=[None,2,3],name='x_int2')
softm = tf.nn.softmax(X)
hf = tf.gather(X,4-1)
with tf.Session() as sess:X_,hf_,Soft = sess.run([X,hf,softm],feed_dict={X:X_batch})print(X_)print('-----------------------')print(hf_)print('------------------')print(Soft)

输出:

[[[0. 1. 2.][9. 8. 7.]][[3. 4. 5.][0. 0. 0.]][[6. 7. 8.][6. 5. 4.]][[9. 0. 1.][3. 2. 1.]]]
-----------------------
[[9. 0. 1.][3. 2. 1.]]
------------------
[[[9.003e-02 2.448e-01 6.650e-01][6.650e-01 2.448e-01 9.003e-02]][[9.003e-02 2.448e-01 6.650e-01][3.333e-01 3.333e-01 3.333e-01]][[9.003e-02 2.448e-01 6.650e-01][6.650e-01 2.448e-01 9.003e-02]][[9.995e-01 1.234e-04 3.352e-04][6.650e-01 2.448e-01 9.003e-02]]]