如何将所有返回值全部print出来
时间: 2024-11-09 12:19:09 浏览: 12
解决使用python print打印函数返回值多一个None的问题
要在代码中打印出所有函数的返回值,可以在每个函数调用后立即使用 `print` 语句来输出返回值。以下是一些关键函数及其返回值的示例:
1. **`make_data_loader` 函数**:
```python
def make_data_loader(spec, tag=''):
...
return loader
train_loader = make_data_loader(config.get('train_dataset'), tag='train')
print("Train loader:", train_loader)
val_loader = make_data_loader(config.get('val_dataset'), tag='val')
print("Validation loader:", val_loader)
```
2. **`prepare_training` 函数**:
```python
def prepare_training():
...
return model, optimizer, epoch_start, lr_scheduler
model, optimizer, epoch_start, lr_scheduler = prepare_training()
print("Model:", model)
print("Optimizer:", optimizer)
print("Epoch start:", epoch_start)
print("LR Scheduler:", lr_scheduler)
```
3. **`train` 函数**:
```python
def train(train_loader, model, optimizer, epoch):
...
return train_loss.item()
train_loss_value = train(train_loader, model, optimizer, epoch)
print("Train loss:", train_loss_value)
```
4. **`main` 函数**:
```python
def main(config_, save_path):
...
for epoch in range(epoch_start, epoch_max + 1):
...
train_loss = train(train_loader, model, optimizer, epoch)
print("Train loss for epoch {}:".format(epoch), train_loss)
if (epoch_val is not None) and (epoch % epoch_val == 0):
...
val_res = eval_psnr(val_loader, model_, data_norm=config['data_norm'], eval_type=config.get('eval_type'), eval_bsize=config.get('eval_bsize'))
print("Validation PSNR for epoch {}:".format(epoch), val_res)
...
if __name__ == '__main__':
...
main(config, save_path)
```
通过在这些关键位置添加 `print` 语句,你可以确保所有重要的返回值都被打印出来,以便于调试和监控程序的运行情况。
阅读全文