【问题标题】:tf.random.set_seed() TypeError: 'int' object is not callabletf.random.set_seed() TypeError: 'int' object is not callable
【发布时间】:2021-10-22 10:21:38
【问题描述】:

tf.random.set_seed(7)in TF 版本:2.6.0 产生 TypeError: 'int' object is not callable

这个tf.compat.v1.set_random_seed(7) 在 Google Colab 的 TF 2.6.0 中为我工作,但不知道为什么我不能使用 tf.random.set_seed(7) 来完成它

【问题讨论】:

  • 你试过喂它lambda:7吗?
  • @Bey 你能举个例子吗?
  • 看起来 set_seed 需要一个函数,那么为什么不将它包装在一个返回 7 的函数中呢?
  • 我也试过@Bey。
  • 我刚刚在 2.6 中运行它没有问题。但无论如何,您都应该迁移到 tf.random.Generator API。 tf.random.uniform 和 tf.random.normal 不是很随机。当您的随机源不是随机的时,很难调试。 tensorflow.org/guide/random_numbers >警告:TF 1.x 中的旧 RNG,例如 tf.random.uniform 和 tf.random.normal 尚未弃用,但强烈建议不要使用。

标签: python tensorflow google-colaboratory


【解决方案1】:

TF 2.6

如果您使用 set_seed(using assign) 例如 tf.random.set_seed = 42,然后尝试在同一个 colab notebook 中使用 tf.random.set_seed(123)(可调用),则会发生此错误。请参阅下面的场景。

使用assign设置种子

tf.random.set_seed = 123
print(tf.random.uniform([1]))  # generates 'A1'
print(tf.random.uniform([1]))  # generates 'A2'

输出:

tf.Tensor([0.48962688], shape=(1,), dtype=float32)
tf.Tensor([0.42140496], shape=(1,), dtype=float32)

现在在下一个单元格中,如果您尝试像下面这样调用函数,则会发生错误。

tf.random.set_seed(123)
print(tf.random.uniform([1]))  # generates 'A1'
print(tf.random.uniform([1]))  # generates 'A2'

输出:

    TypeError                                 Traceback (most recent call last)
        <ipython-input-25-6ad9ca1b84ce> in <module>()
    ----> 1 tf.random.set_seed(123)
          2 print(tf.random.uniform([1]))  # generates 'A1'
          3 print(tf.random.uniform([1]))  # generates 'A2'
TypeError: 'int' object is not callable

现在,如果你重新启动笔记本并只运行 tf.random.set_seed(123) 函数,它就可以正常工作了。

tf.random.set_seed(123)
print(tf.random.uniform([1]))  # generates 'A1'
print(tf.random.uniform([1]))  # generates 'A2'

输出:

tf.Tensor([0.12615311], shape=(1,), dtype=float32)
tf.Tensor([0.88968754], shape=(1,), dtype=float32)

解决方案是确保在使用 tf.random.set_seed() 函数之前不要为 set_seed 赋值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-06
    • 2013-03-30
    • 2015-01-05
    • 2018-07-23
    • 2018-03-31
    • 2018-04-14
    • 2019-11-11
    • 1970-01-01
    相关资源
    最近更新 更多