Why I can't reproduce my results

本文详细介绍了PyTorch中CPU与GPU随机种子的独立性及设置方法,包括使用多个GPU时如何设置torch.cuda.manual_seed_all,以及使用cudnn时如何确保结果的确定性。同时,列举了在不同函数中设置随机种子的例子,以及容易忽视的随机种子保存与加载问题。

1. Pytorch's CPU's and GPU's seeds are independent. So set/save/load them independently

https://discuss.pytorch.org/t/are-gpu-and-cpu-random-seeds-independent/142

2. If you use randomness on severall gpus, you need to set torch.cuda.manual_seed_all(seed).

If you use cudnn, you need to set torch.backends.cudnn.deterministic=True.

3. There are many lines that set random seeds in different functions. E.g.,

linear_layer_init(seed=1)

sample_next_word(seed=10)

4. Different random generators

import random as rng

import numpy.random as rng

import torch; torch.cuda.manual_seed()

5. Forget to save random seed before testing the model on test/dev sets (which also do sampling or shuffle or simply setting seeds)

6. Forget to test immediately after loading the model before resuming training (because testing(random sample involved, performance showed) is always at the end of each epoch)

### GridWorld 的概念及环境重现方法 GridWorld 是一个用于 AP 计算机科学课程的案例研究程序,旨在帮助学生理解面向对象编程的核心概念,例如类、继承和多态性[^1]。它提供了一个可视化的网格世界(grid world),其中可以放置各种“角色”(actors),如虫子(Bug)、爬行者(Critter)等,并观察它们的行为。 #### 1. 下载与安装 要使用 GridWorld,首先需要从官方提供的链接下载 GridWorld 的代码包[^1]: - 下载地址:[GridWorld 官方下载页面](http://www.collegeboard.com/student/testing/ap/compsci_a/case.html)。 - 解压缩后,找到 `GridWorldCode` 文件夹,其中包含所有必要的源代码和文档。 #### 2. 配置开发环境 为了运行 GridWorld,需要确保以下工具已正确安装: - **Java 开发工具包 (JDK)**:GridWorld 使用 Java 编写,因此需要安装 JDK[^2]。 - **集成开发环境 (IDE)**:推荐使用 Eclipse 或 IntelliJ IDEA 等支持 Java 的 IDE。 将 `projects/firstProject` 文件夹中的 `BugRunner.java` 源文件复制到你的项目目录下,并在 IDE 中导入该项目。 #### 3. 运行示例程序 运行 GridWorld 的第一个示例程序 `BugRunner.java`。此程序展示了如何创建一个简单的 Bug 并观察其行为。运行命令如下[^3]: ```bash java BugRunner ``` 如果一切配置正确,你将看到一个可视化窗口,显示 Bug 在网格中移动的过程。 #### 4. 学习扩展功能 GridWorld 的一大特点是允许用户通过继承现有类来创建自定义角色。例如,可以通过扩展 `Critter` 类实现新的行为[^4]。以下是扩展 `Critter` 类的一个简单示例: ```java public class MyCritter extends Critter { public void act() { if (canMove()) { move(); } else { turn(); } } } ``` 将上述代码保存为 `MyCritter.java`,并将其添加到项目中。然后修改主程序以实例化 `MyCritter` 并观察其行为。 --- ### 示例代码 以下是一个完整的 GridWorld 示例程序,演示如何创建和运行一个自定义角色: ```java import info.gridworld.actor.Bug; import info.gridworld.actor.Critter; import info.gridworld.grid.Location; import info.gridworld.world.World; public class CustomBugRunner { public static void main(String[] args) { World<Bug> world = new World<>(); world.show(); Bug bug = new Bug(); world.add(new Location(5, 5), bug); } } ``` ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值