tf.nn.in_top_k的用法

tf.nn.in_top_k组要是用于计算预测的结果和实际结果的是否相等,返回一个bool类型的张量,tf.nn.in_top_k(prediction, target, K):prediction就是表示你预测的结果,大小就是预测样本的数量乘以输出的维度,类型是tf.float32等。target就是实际样本类别的标签,大小就是样本数量的个数。K表示每个样本的预测结果的前K个最大的数里面是否含有target中的值。一般都是取1。

例如:

import tensorflow as tf;
 
A = [[0.8,0.6,0.3], [0.1,0.6,0.4]]
B = [1, 1]
out = tf.nn.in_top_k(A, B, 1)
with tf.Session() as sess:
	sess.run(tf.initialize_all_variables())
	print sess.run(out)

输出:

[False True]

解释:因为A张量里面的第一个元素的最大值的标签是0,第二个元素的最大值的标签是1.。但是实际的确是1和1.所以输出就是False 和True。如果把K改成2,那么第一个元素的前面2个最大的元素的位置是0,1,第二个的就是1,2。实际结果是1和1。包含在里面,所以输出结果就是True 和True.如果K的值大于张量A的列,那就表示输出结果都是true。

引用链接:https://blog.youkuaiyun.com/uestc_c2_403/article/details/73187915

@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
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) /tmp/ipykernel_553597/306262114.py in <module> 7 ## Training 8 # Epoch_list,Loss_list = model_train(batchsize,channel_SNR_db1,noise_init,nl_factor,eq_flag,norm_epsilon,earlystop_epoch) ----> 9 Epoch_list,Loss_list, Min_Distance_list = model_train(batchsize,channel_SNR_db1,noise_init,nl_factor,eq_flag,norm_epsilon,earlystop_epoch, min_distance_threshold=0.7,flags_schedule=[(1, 0), (0, 1), (1, 1)],iter_per_stage=50) /tmp/ipykernel_553597/4102420687.py in model_train(batchsize, channel_SNR, noise_init, nl_factor, eq_flag, epsilon, earlystop_epoch, min_distance_threshold, flags_schedule, iter_per_stage) 58 59 (batch_loss, batch_loss_Eq, NGMI, GMI, entropy_S, ---> 60 p_s, norm_constellation, x, min_distance) = train_step( 61 channel_SNR, noise_tf, GS_flag_now, PS_flag_now, eq_flag, epsilon, min_distance_threshold 62 ) ~/miniconda3/lib/python3.8/site-packages/tensorflow/python/util/traceback_utils.py in error_handler(*args, **kwargs) 151 except Exception as e: 152 filtered_tb = _process_traceback_frames(e.__traceback__) --> 153 raise e.with_traceback(filtered_tb) from None 154 finally: 155 del filtered_tb /tmp/__autograph_generated_file_jsnzuik.py in tf__train_step(inp_SNR, noise, GS_flag, PS_flag, eq_flag, epsilon, min_distance_threshold) 39 batch_size = ag__.converted_call(ag__.ld(tf).shape, (ag__.ld(p_s),), None, fscope)[0] 40 batch_indices = ag__.converted_call(ag__.ld(tf).tile, (ag__.converted_call(ag__.ld(tf).range, (ag__.ld(batch_size),), None, fscope)[:, ag__.ld(tf).newaxis, ag__.ld(tf).newaxis], [1, ag__.ld(M_int), ag__.ld(k)]), None, fscope) ---> 41 gather_indices = ag__.converted_call(ag__.ld(tf).stack, ([ag__.ld(batch_indices), ag__.converted_call(ag__.ld(tf).tile, (ag__.ld(topk_indices)[:, :, ag__.ld(tf).newaxis, :], [1, 1, ag__.ld(k), 1]), None, fscope)],), dict(axis=(- 1)), fscope) 42 neighbor_probs = ag__.converted_call(ag__.ld(tf).gather_nd, (ag__.ld(p_s), ag__.ld(gather_indices)), None, fscope) 43 neighbor_sum = ag__.converted_call(ag__.ld(tf).reduce_sum, (ag__.ld(neighbor_probs),), dict(axis=(- 1)), fscope) ValueError: in user code: File "/tmp/ipykernel_553597/675414708.py", line 77, in train_step * gather_indices = tf.stack([ ValueError: Shapes must be equal rank, but are 3 and 4 From merging shape 0 with other shapes. for '{{node stack_1}} = Pack[N=2, T=DT_INT32, axis=-1](Tile, Tile_1)' with input shapes: [1,8,3], [1,8,3,3].
最新发布
08-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值