首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >AttributeError:在带有PyTorch的Azure中,'NoneType‘对象没有属性'state_dict’

AttributeError:在带有PyTorch的Azure中,'NoneType‘对象没有属性'state_dict’
EN

Stack Overflow用户
提问于 2022-08-10 07:11:02
回答 1查看 309关注 0票数 0

我是PyTorch的初学者,我一直试图在中建立一个PyTorch模型。代码在Google中运行良好,但在Azure ML笔记本中,我得到了以下错误:

代码语言:javascript
运行
复制
File /anaconda/envs/azureml_py38_PT_TF/lib/python3.8/site-packages/pytorch_lightning/trainer/training_io.py:268, in TrainerIOMixin.save_checkpoint(self, filepath, weights_only)
    267 def save_checkpoint(self, filepath, weights_only: bool = False):
--> 268     checkpoint = self.dump_checkpoint(weights_only)
    270     if self.is_global_zero:
    271         # do the actual save
    272         try:

File /anaconda/envs/azureml_py38_PT_TF/lib/python3.8/site-packages/pytorch_lightning/trainer/training_io.py:362, in TrainerIOMixin.dump_checkpoint(self, weights_only)
    360     # save native amp scaling
    361     if self.use_amp and NATIVE_AMP_AVALAIBLE and not self.use_tpu:
--> 362         checkpoint['native_amp_scaling_state'] = self.scaler.state_dict()
    364 # add the module_arguments and state_dict from the model
    365 model = self.get_model()

AttributeError: 'NoneType' object has no attribute 'state_dict'

模型代码:

代码语言:javascript
运行
复制
class EvaluationModel(pl.LightningModule):
  def __init__(self,learning_rate=1e-3,batch_size=1024,layer_count=10):
    super().__init__()
    self.batch_size = batch_size
    self.learning_rate = learning_rate
    layers = []
    for i in range(layer_count-1):
      layers.append((f"linear-{i}", nn.Linear(808, 808)))
      layers.append((f"relu-{i}", nn.ReLU()))
    layers.append((f"linear-{layer_count-1}", nn.Linear(808, 1)))
    self.seq = nn.Sequential(OrderedDict(layers))

  def forward(self, x):
    return self.seq(x)

  def training_step(self, batch, batch_idx):
    x, y = batch['binary'], batch['eval']
    y_hat = self(x)
    loss = F.l1_loss(y_hat, y)
    self.log("train_loss", loss)
    return loss

  def configure_optimizers(self):
    return torch.optim.Adam(self.parameters(), lr=self.learning_rate)

  def train_dataloader(self):
    dataset = EvaluationDataset(count=LABEL_COUNT)
    return DataLoader(dataset, batch_size=self.batch_size, num_workers=2, pin_memory=True)

configs = [
           {"layer_count": 4, "batch_size": 512},
          #  {"layer_count": 6, "batch_size": 1024},
           ]
for config in configs:
  version_name = f'{int(time.time())}-batch_size-{config["batch_size"]}-layer_count-{config["layer_count"]}'
  logger = pl.loggers.TensorBoardLogger("lightning_logs", name="chessml", version=version_name)
  trainer = pl.Trainer(gpus=1,precision=16,max_epochs=1,auto_lr_find=True,logger=logger)
  model = EvaluationModel(layer_count=config["layer_count"],batch_size=config["batch_size"],learning_rate=1e-3)
  # trainer.tune(model)
  # lr_finder = trainer.tuner.lr_find(model, min_lr=1e-6, max_lr=1e-3, num_training=25)
  # fig = lr_finder.plot(suggest=True)
  # fig.show()
  trainer.fit(model)
  break

模型代码来自https://towardsdatascience.com/train-your-own-chess-ai-66b9ca8d71e4

EN

回答 1

Stack Overflow用户

发布于 2022-08-22 10:31:31

从错误来看,您的检查点似乎存在一些问题。您能尝试使用torch.load API加载您的模型并查看它是否有效吗?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73302066

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档