python
1 import torch.nn as nn (1) 2 from nn import build_model (2)
(1)は通っているのですが、(2)が通りません.原因を教えていただきたいです
以下のようなエラーが出ます
ModuleNotFoundError: No module named 'nn'
全体のコードも示しておきます
python
1import logging 2import os 3import pickle 4 5import torch 6import torch.nn as nn 7import torch.optim as optim 8 9from config import Config 10from nn import build_model 11from tokenizer import Tokenizer 12from utils import (DialogDataset, one_cycle, evaluate, 13 seed_everything, BalancedDataLoader, 14 make_train_data_from_txt, make_itf) 15 16logging.basicConfig(level=logging.INFO) 17 18if __name__ == '__main__': 19 logging.info('*** Initializing ***') 20 21 if not os.path.isdir(Config.data_dir): 22 os.mkdir(Config.data_dir) 23 24 seed_everything(Config.seed) 25 device = torch.device(Config.device) 26 27 start_epoch = 0 28 tokenizer = Tokenizer.from_pretrained(Config.model_name) 29 30 logging.info('Preparing training data') 31 if Config.use_pickle: 32 with open(f'{Config.pickle_path}', 'rb') as f: 33 train_data = pickle.load(f) 34 else: 35 train_data = make_train_data_from_txt(Config, tokenizer) 36 itf = make_itf(train_data, Config.vocab_size) 37 dataset = DialogDataset(train_data, tokenizer) 38 39 logging.info('Define Models') 40 model = build_model(Config).to(device) 41 model.unfreeze() 42 43 logging.info('Define Loss and Optimizer') 44 criterion = nn.CrossEntropyLoss(reduction='none') 45 optimizer = optim.AdamW(model.parameters(), lr=Config.lr, betas=Config.betas, eps=1e-9) 46 47 if Config.load: 48 state_dict = torch.load(f'{Config.data_dir}/{Config.fn}.pth') 49 start_epoch = 10 50 print(f'Start Epoch: {start_epoch}') 51 model.load_state_dict(state_dict['model']) 52 optimizer.load_state_dict(state_dict['opt']) 53 54 logging.info('Start Training') 55 for epoch in range(start_epoch, Config.n_epoch): 56 one_cycle(epoch, Config, model, optimizer, criterion, 57 BalancedDataLoader(dataset, tokenizer.pad_token_id), 58 tokenizer, device) 59 evaluate(Config, 'おはよーーー', tokenizer, model, device) 60```
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。