【问题标题】:Reading a random seed of a jupyter notebook读取 jupyter notebook 的随机种子
【发布时间】:2017-07-25 14:20:41
【问题描述】:

有没有办法在 jupyter 笔记本中读取随机数生成器的“状态”?

例如,如果我运行一个指定神经网络架构的单元,然后在没有指定种子的情况下使用一些数据对其进行训练,那么有没有办法可以读取用于运行它的种子?

【问题讨论】:

  • 据我所知你做不到,最简单的解决办法是在训练之前设置种子,这样你就知道种子了。 Here 类似问题

标签: python numpy jupyter random-seed


【解决方案1】:

您确实可以读取(并存储)RNG 的当前状态,但是每次使用它时都会发生变化,即,您无法在 之后执行您描述的操作已运行单元格。

这是一个示例(由于您已使用keras 标记问​​题,我假设您实际上对Numpy RNG 感兴趣,这是 Keras 中使用的那个):

import numpy as np
current_state = np.random.get_state()

# produce some random numbers:
a = np.random.randn(3)
a
# array([-0.44270351, 1.42933504, 2.11385353])

# Now, restoring the RNG state and producing again 3 random numbers, you get the same result:

np.random.set_state(current_state)
b = np.random.randn(3)
b
# array([-0.44270351, 1.42933504, 2.11385353])
a == b
# array([ True, True, True], dtype=bool)

【讨论】:

    猜你喜欢
    • 2021-10-09
    • 2018-12-27
    • 2014-01-29
    • 2022-01-15
    • 1970-01-01
    • 2021-07-28
    • 2016-08-12
    • 2016-10-07
    • 1970-01-01
    相关资源
    最近更新 更多