update save and load

This commit is contained in:
2023-05-31 16:16:24 +08:00
parent 41c70802fd
commit fe2c327f4e
12 changed files with 252 additions and 44 deletions
+18 -18
View File
@@ -113,7 +113,7 @@ class QAgent(Agent):
def diagnostics(self):
return {'value': self.last_move_value}
def save(self, filename):
def save(self, file_path):
torch.save({
'model': self.model.state_dict(),
'encoder': {
@@ -121,11 +121,11 @@ class QAgent(Agent):
'board_width': self.encoder.board_width,
'board_height': self.encoder.board_height,
}
}, filename)
}, file_path)
@classmethod
def load(cls, filename):
checkpoint = torch.load(filename)
def load(cls, file_path):
checkpoint = torch.load(file_path)
# Assuming `model` is a PyTorch model here and `encoder` is a DLGo encoder
model = Model() # The Model class should be defined appropriately
@@ -139,17 +139,17 @@ class QAgent(Agent):
return cls(model, encoder)
def load_q_agent(filename):
# 我假设 Model 类是预先定义好的 PyTorch 模型,你需要根据实际情况替换或实现这个模型。
checkpoint = torch.load(filename)
# Assuming `model` is a PyTorch model here and `encoder` is a DLGo encoder
model = Model() # The Model class should be defined appropriately
model.load_state_dict(checkpoint['model'])
encoder_info = checkpoint['encoder']
encoder = encoders.get_encoder_by_name(
encoder_info['name'],
(encoder_info['board_width'], encoder_info['board_height']))
return QAgent(model, encoder)
# def load_q_agent(file_path):
# # 我假设 Model 类是预先定义好的 PyTorch 模型,你需要根据实际情况替换或实现这个模型。
# checkpoint = torch.load(file_path)
#
# # Assuming `model` is a PyTorch model here and `encoder` is a DLGo encoder
# model = Model() # The Model class should be defined appropriately
# model.load_state_dict(checkpoint['model'])
#
# encoder_info = checkpoint['encoder']
# encoder = encoders.get_encoder_by_name(
# encoder_info['name'],
# (encoder_info['board_width'], encoder_info['board_height']))
#
# return QAgent(model, encoder)