you will be given two set of codes, code 1 is running without any bugs, code 2 is giving…
- User
you will be given two set of codes, code 1 is running without any bugs, code 2 is giving a bug File "/staging/users/tpadhi1/transformer-experiments/Hyper-Parameter-Tuning/utils/model.py", line 22, in forward return x + self.pe[:x.size(0), :] RuntimeError: The size of tensor a (100) must match the size of tensor b (128) at non-singleton dimension 2
your job is to tell what is the issue:
code1-----------------------------------------------------> from utils.data import Data from utils.model import TransAm from tqdm import tqdm
if name=='main':
""" Without Ray Tune """ # train_transformer(config) input_window = 100 # number of input steps output_window = 1 # number of prediction steps, in this model its fixed to one dataset = Data(input_window, output_window) train_data, val_data = dataset.get_data() # load model and optimizers config = {"feature_size": 128, "num_layers": 1, "dropout": 0.1, "nhead": 4, "lr_decay": 0.95, "epochs": 100, "lr": 0.0001, "batch_size": 32, "num_workers": 0, "device": "cuda" if torch.cuda.is_available() else "cpu"} model = TransAm(feature_size=config["feature_size"], num_layers=config["num_layers"], dropout=config["dropout"], nhead=config["nhead"]) device = "cuda" if torch.cuda.is_available else "cpu" model=model.to(device) optimizer = torch.optim.SGD(model.parameters(), lr=config["lr"]) num_train_epochs=config["epochs"] lr = config['lr'] lr_decay = config['lr_decay'] optimizer = torch.optim.AdamW(model.parameters(), lr=lr) scheduler = torch.optim.lr_scheduler.StepLR(optimizer, 1.0, gamma=lr_decay) print(f"Running {num_train_epochs} epochs.") # training parameters criterion = nn.MSELoss() start_time = time.time() best_val_loss = float("inf") epochs = config['epochs'] batch_size=config['batch_size'] checkpoint=None # Calculate the number of batches given the size of the training data and the batch size num_batches = (len(train_data)) // batch_size for epoch in tqdm(range(1, epochs + 1)): training_loss = 0 # zero out the loss before the start of every epoch for batch_idx in range(0, num_batches): data, targets = dataset.get_batch(train_data, batch_idx, batch_size) data, targets = data.to(device), targets.to(device) # print("data.shape", data.shape) # print("targets.shape", targets.shape) optimizer.zero_grad() output = model(data) # print(output.size(), targets.size()) loss = criterion(output, targets) loss.backward() training_loss += loss.item() torch.nn.utils.clip_grad_norm_(model.parameters(), 0.7) optimizer.step()code2: -----------------------------------------------------> from utils.data import Data from utils.model import TransAm
config = {"feature_size": 128, "num_layers": 1, "dropout": 0.1, "nhead": 4, "lr_decay": 0.95, "epochs": 100, "lr": 0.0001, "batch_size": 32, "num_workers": 0, "device": "cuda" if torch.cuda.is_available() else "cpu"}
- Assistant