tf.nn.top_k

博客提到评估操作对测量神经网络性能有用,因其不可微分,常用于评估阶段。还详细介绍了tf.nn.top_k函数,包括输入参数(input、k、name)和输出参数(values、indices),并给出了使用该函数的代码示例。

评估操作对于测量神经网络的性能是有用的。 由于它们是不可微分的,所以它们通常只是被用在评估阶段

tf.nn.top_k(input, k, name=None)

这个函数的作用是返回 input 中每行最大的 k 个数,并且返回它们所在位置的索引。


输入参数:
input: 一个张量,数据类型必须是以下之一:float32、float64、int32、int64、uint8、int16、int8。数据维度是 batch_size 乘上 x 个类别。
k: 一个整型,必须 >= 1。在每行中,查找最大的 k 个值。
name: 为这个操作取个名字。
输出参数:
一个元组 Tensor ,数据元素是 (values, indices),具体如下:
values: 一个张量,数据类型和 input 相同。数据维度是 batch_size 乘上 k 个最大值。
indices: 一个张量,数据类型是 int32 。每个最大值在 input 中的索引位置。
---------------------
作者:Never-Giveup
来源:CSDN
原文:https://blog.csdn.net/qq_36653505/article/details/81105894
版权声明:本文为博主原创文章,转载请附上博文链接!

input = tf.constant(np.random.rand(3,4))
k = 2
output = tf.nn.top_k(input, k)
with tf.Session() as sess:
       print(sess.run(input))
       print(sess.run(output))

 

 

[[0.61950464 0.34474213 0.79035374 0.15015998]
 [0.17963278 0.30331155 0.9208411  0.90382958]
 [0.20007082 0.89997606 0.03721232 0.24253472]]

TopKV2(values=array([[0.79035374, 0.61950464],
       [0.9208411 , 0.90382958],
       [0.89997606, 0.24253472]]), 
indices=array([[2, 0], [2, 3], [1, 3]], dtype=int32))

转载于:https://www.cnblogs.com/hapyygril/p/10824158.html

pooled = [] box_to_level = [] for i, level in enumerate(range(2, 6)): ix = tf.where(tf.equal(roi_level, level)) level_boxes = tf.gather_nd(boxes, ix) # Box indicies for crop_and_resize. box_indices = tf.cast(ix[:, 0], tf.int32) # Keep track of which box is mapped to which level box_to_level.append(ix) # Stop gradient propogation to ROI proposals level_boxes = tf.stop_gradient(level_boxes) box_indices = tf.stop_gradient(box_indices) # Crop and Resize # From Mask R-CNN paper: "We sample four regular locations, so # that we can evaluate either max or average pooling. In fact, # interpolating only a single value at each bin center (without # pooling) is nearly as effective." # # Here we use the simplified approach of a single value per bin, # which is how it's done in tf.crop_and_resize() # Result: [batch * num_boxes, pool_height, pool_width, channels] pooled.append(tf.image.crop_and_resize( feature_maps[i], level_boxes, box_indices, self.pool_shape, method="bilinear")) # Pack pooled features into one tensor pooled = tf.concat(pooled, axis=0) # Pack box_to_level mapping into one array and add another # column representing the order of pooled boxes box_to_level = tf.concat(box_to_level, axis=0) box_range = tf.expand_dims(tf.range(tf.shape(box_to_level)[0]), 1) box_to_level = tf.concat([tf.cast(box_to_level, tf.int32), box_range], axis=1) # Rearrange pooled features to match the order of the original boxes # Sort box_to_level by batch then box index # TF doesn't have a way to sort by two columns, so merge them and sort. sorting_tensor = box_to_level[:, 0] * 100000 + box_to_level[:, 1] ix = tf.nn.top_k(sorting_tensor, k=tf.shape( box_to_level)[0]).indices[::-1] ix = tf.gather(box_to_level[:, 2], ix) pooled = tf.gather(pooled, ix) # Re-add the batch dimension pooled = tf.expand_dims(pooled, 0) return pooled ,详细解释这段代码
最新发布
11-21
@tf.function def train_step(inp_SNR, noise, GS_flag, PS_flag, eq_flag, epsilon=1e-12, min_distance_threshold=0.5): loss = 0 with tf.GradientTape() as tape: # 原始前向传播计算 s_logits = logit_model(inp_SNR) # batch_size = tf.shape(inp_SNR)[0] # s_logits = tf.zeros((batch_size, M), dtype=tf.float32) s = s_model(s_logits) soft_bits = soft_bit_encoder(s) hard_bits = hard_decision_on_bit(soft_bits) enc = Trans_model_bit(hard_bits) # 生成完整星座图 bit_set = tf.math.mod(tf.bitwise.right_shift(tf.expand_dims(symbol_set, 1), tf.range(bitlen)), 2) bit_set = tf.reverse(bit_set, axis=[-1]) constellation = Trans_model_bit(bit_set) constellation = tf.expand_dims(constellation, 0) # 归一化处理 p_s = tf.nn.softmax(s_logits) magnitudes = tf.abs(constellation) max_mag = tf.reduce_max(magnitudes) norm_factor = 1.30793 / tf.maximum(max_mag, epsilon) norm_constellation = r2c(norm_factor) * constellation x = r2c(norm_factor) * enc # === 星座点最小距离约束 === points = tf.squeeze(tf.stack([tf.math.real(norm_constellation), tf.math.imag(norm_constellation)], axis=-1)) diff = tf.expand_dims(points, 1) - tf.expand_dims(points, 0) # [M, M, 2] distances = tf.norm(diff, axis=-1) # [M, M] mask = tf.eye(tf.shape(distances)[0], dtype=tf.bool) valid_distances = tf.where(mask, tf.ones_like(distances)*1e10, distances) min_distance = tf.reduce_min(valid_distances) distance_penalty = tf.nn.relu(min_distance_threshold - min_distance) * 50.0 # === 新增:概率分布可逆性约束 === # 1. 计算初始均匀分布的熵(基准值) num_constellation_points = tf.cast(tf.shape(constellation)[1], tf.float32) # 使用换底公式计算log2: log2(x) = ln(x)/ln(2) uniform_entropy = tf.math.log(num_constellation_points) / tf.math.log(2.0) # 均匀分布的熵 # 2. 计算当前分布的熵 current_entropy = -tf.reduce_sum(p_s * tf.math.log(p_s) / tf.math.log(2.0)) # 以2为底的熵 # 3. 熵约束惩罚 entropy_ratio = current_entropy / uniform_entropy entropy_penalty = tf.nn.relu(0.9 - entropy_ratio) * 200.0 # 4. 概率下限约束 min_prob = tf.reduce_min(p_s) prob_floor_penalty = tf.nn.relu(epsilon - min_prob) * 200.0 # === 原始损失计算 === Tx = upsample_pulse_shaping(x, Fs, h_rrc, fa, fc) Rx = Tx + noise y = Model_Eq(Rx) entropy_S = -p_norm(p_s, p_s, lambda x: log2(x)) GMI = GMIcal_tf(x, tf.squeeze(y), M, norm_constellation, hard_bits_out, p_s) NGMI = 1 - (entropy_S - GMI) / bitlen loss_NGMI = tf.nn.relu(NGMI_th - NGMI) loss_Eq = tf.reduce_mean(tf.square(tf.abs(x - y))) # === 修改后的损失函数(添加所有惩罚项) === loss = (loss_Eq * eq_flag * 0.5 - GMI + loss_NGMI * 100 + distance_penalty + entropy_penalty # 新增:熵约束惩罚 + prob_floor_penalty) # 新增:概率下限惩罚 # # 梯度计算与更新 # variables = [] # if PS_flag == 1: # variables.extend(logit_model.trainable_variables) # variables.extend(s_model.trainable_variables) # if GS_flag == 1: # variables.extend(Trans_model_bit.trainable_variables) # if eq_flag == 1: # variables.extend(Model_Eq.trainable_variables) variables = (logit_model.trainable_variables * PS_flag + s_model.trainable_variables + Trans_model_bit.trainable_variables * GS_flag + Model_Eq.trainable_variables * eq_flag) gradients = tape.gradient(loss, variables) optimizer.apply_gradients(zip(gradients, variables)) # 保持原始返回值结构不变 return loss, loss_Eq, NGMI, GMI, tf.reduce_mean(entropy_S), p_s, norm_constellation, x, min_distance 新增约束条件,一个点与其相邻三个点的概率和不能超过4/M。当前代码可以正常运行,修改时只修改必要地方
08-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值