text stringlengths 1 93.6k |
|---|
if os.path.exists(output_file):
|
with open(output_file, 'r') as file:
|
for line in file:
|
existing_data.append(json.loads(line))
|
existing_data.append(evaluation)
|
# Write all data back to the file
|
with open(output_file, 'w') as file:
|
for item in existing_data:
|
json.dump(item, file)
|
file.write('\n')
|
if __name__ == "__main__":
|
main()
|
# <FILESEP>
|
from Data import ToyDataset
|
from periodic_activations import SineActivation, CosineActivation
|
import torch
|
from torch.utils.data import DataLoader
|
from Pipeline import AbstractPipelineClass
|
from torch import nn
|
from Model import Model
|
class ToyPipeline(AbstractPipelineClass):
|
def __init__(self, model):
|
self.model = model
|
def train(self):
|
loss_fn = nn.CrossEntropyLoss()
|
dataset = ToyDataset()
|
dataloader = DataLoader(dataset, batch_size=2048, shuffle=False)
|
optimizer = torch.optim.Adam(self.model.parameters(), lr=1e-3)
|
num_epochs = 100
|
for ep in range(num_epochs):
|
for x, y in dataloader:
|
optimizer.zero_grad()
|
y_pred = self.model(x.unsqueeze(1).float())
|
loss = loss_fn(y_pred, y)
|
loss.backward()
|
optimizer.step()
|
print("epoch: {}, loss:{}".format(ep, loss.item()))
|
def preprocess(self, x):
|
return x
|
def decorate_output(self, x):
|
return x
|
if __name__ == "__main__":
|
pipe = ToyPipeline(Model("sin", 42))
|
pipe.train()
|
#pipe = ToyPipeline(Model("cos", 12))
|
#pipe.train()
|
# <FILESEP>
|
# ---------------------------------------------------------------
|
# Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
#
|
# This work is licensed under the NVIDIA Source Code License
|
# for DiffPure. To view a copy of this license, see the LICENSE file.
|
# ---------------------------------------------------------------
|
import sys
|
import argparse
|
from typing import Any
|
import torch
|
import torch.nn as nn
|
import torchvision.models as models
|
from torch.utils.data import DataLoader
|
import torchvision.transforms as transforms
|
from robustbench import load_model
|
import data
|
def compute_n_params(model, return_str=True):
|
tot = 0
|
for p in model.parameters():
|
w = 1
|
for x in p.shape:
|
w *= x
|
tot += w
|
if return_str:
|
if tot >= 1e6:
|
return '{:.1f}M'.format(tot / 1e6)
|
else:
|
return '{:.1f}K'.format(tot / 1e3)
|
else:
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.