python中运行结果为false-如何在Python中将True切换为False

我需要做什么操作才能始终将False值变为True或将True值变为False?

换句话说,我该怎么做才能切换给定变量的布尔值?

new_dict = {}

for i in range(1, 101):

new_dict[i] = False

i = 2

while i < 101:

for x in range(1, 101):

if new_dict[x] % i == 0:

a = new_dict[x]

new_dict[x] = not a

i += 1

for a in new_dict:

print "Light #%d --> %r" % (a, new_dict[a])

以下输出仅为True。从这一点来说,我明白我出于某种原因所做的并不是将其他所有值都改为False。为什么会这样?

有谁知道为什么?

Light #1 --> True

Light #2 --> True

Light #3 --> True

Light #4 --> True

Light #5 --> True

Light #6 --> True

Light #7 --> True

Light #8 --> True

Light #9 --> True

Light #10 --> True

Light #11 --> True

Light #12 --> True

Light #13 --> True

Light #14 --> True

Light #15 --> True

Light #16 --> True

Light #17 --> True

Light #18 --> True

Light #19 --> True

Light #20 --> True

Light #21 --> True

Light #22 --> True

Light #23 --> True

Light #24 --> True

Light #25 --> True

Light #26 --> True

Light #27 --> True

Light #28 --> True

Light #29 --> True

Light #30 --> True

Light #31 --> True

Light #32 --> True

Light #33 --> True

Light #34 --> True

Light #35 --> True

Light #36 --> True

Light #37 --> True

Light #38 --> True

Light #39 --> True

Light #40 --> True

Light #41 --> True

Light #42 --> True

Light #43 --> True

Light #44 --> True

Light #45 --> True

Light #46 --> True

Light #47 --> True

Light #48 --> True

Light #49 --> True

Light #50 --> True

Light #51 --> True

Light #52 --> True

Light #53 --> True

Light #54 --> True

Light #55 --> True

Light #56 --> True

Light #57 --> True

Light #58 --> True

Light #59 --> True

Light #60 --> True

Light #61 --> True

Light #62 --> True

Light #63 --> True

Light #64 --> True

Light #65 --> True

Light #66 --> True

Light #67 --> True

Light #68 --> True

Light #69 --> True

Light #70 --> True

Light #71 --> True

Light #72 --> True

Light #73 --> True

Light #74 --> True

Light #75 --> True

Light #76 --> True

Light #77 --> True

Light #78 --> True

Light #79 --> True

Light #80 --> True

Light #81 --> True

Light #82 --> True

Light #83 --> True

Light #84 --> True

Light #85 --> True

Light #86 --> True

Light #87 --> True

Light #88 --> True

Light #89 --> True

Light #90 --> True

Light #91 --> True

Light #92 --> True

Light #93 --> True

Light #94 --> True

Light #95 --> True

Light #96 --> True

Light #97 --> True

Light #98 --> True

Light #99 --> True

Light #100 --> True

谢谢,我非常感谢所有帮助人员!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Python代码中将对比损失函数改为CB-loss(Class-Balanced Loss),你可以按照以下步骤进行操作: 1. 导入所需的库和模块: ```python import tensorflow as tf ``` 2. 定义新的对比损失函数。CB-loss是基于对比损失函数的改进版本,它通过考虑类别不平衡问题来提高模型性能。下面是一个示例的CB-loss实现: ```python def cb_loss(y_true, y_pred, samples_per_cls, no_of_classes, beta=0.99): effective_num = 1.0 - tf.pow(beta, samples_per_cls) weights = (1.0 - beta) / effective_num weights = weights / tf.reduce_sum(weights) * no_of_classes y_pred = tf.clip_by_value(y_pred, 1e-7, 1.0 - 1e-7) loss = -tf.reduce_sum(y_true * tf.math.log(y_pred) * weights, axis=-1) return loss ``` 这里的 `samples_per_cls` 是一个包含每个类别样本数量的列表,`no_of_classes` 是总共的类别数量,`beta` 是一个用于调整权重的参数。 3. 在模型使用新定义的CB-loss函数。假设你有一个已经定义好的模型 `model`: ```python model.compile(loss=lambda y_true, y_pred: cb_loss(y_true, y_pred, samples_per_cls, no_of_classes), optimizer='adam') ``` 在这里,我们使用了 lambda 函数将 `cb_loss` 函数作为损失函数传递给 `model.compile()`。 以上是一个简单的示例,演示了如何在Python代码中将对比损失函数改为CB-loss。你可以根据自己的需求定义自己的CB-loss函数,并在模型编译时将其设置为损失函数。记得根据你的具体需求传递正确的参数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值