Skip to content

Instantly share code, notes, and snippets.

View andreaschandra's full-sized avatar
💼
work at anywhere

Andreas Chandra andreaschandra

💼
work at anywhere
View GitHub Profile
# import modules
import torch.nn as nn
import torch.optim as optim
# define optimizer
# using previous model that we have built
# using Adam optimizer and learning rate = 0.001
optimizer = optim.Adam(model.parameters(), lr=1e-3)
print(optimizer)
import torch
from torch.utils.data import Dataset, DataLoader
# create dataset class
class Iris(Dataset):
def __init__(self, x_array, y_array):
self.x = x_array
self.y = y_array
def __getitem__(self, idx):
import torch
import torch.nn as nn
# declare Linear class
fc = nn.Linear(in_features=4, out_features=10)
print(fc)
> Linear(in_features=4, out_features=10, bias=True)
# Create a convolutional neural network
import torch
x = torch.rand(10)
print(x)
> tensor([0.2791, 0.7676, 0.5146, 0.5865, 0.5029, 0.5618, 0.2659, 0.9412, 0.4960,
0.1228])
# apply softmax
torch.softmax(x, dim=0)
import torch
# create a tensor 1D
torch.rand(10)
> tensor([0.5998, 0.7840, 0.1017, 0.5188, 0.7417, 0.3671, 0.7304, 0.4467, 0.7782,
0.0533])
# create a tensor 2D
torch.rand(4,4)
import time
import concurrent.futures
from itertools import compress
start = time.perf_counter()
storage = []
tokens = [True] * 2
def get_free_token(tokens):
for epoch in range(1, 101):
running_loss = 0
running_accuracy = 0
running_loss_val = 0
running_accuracy_val = 0
start_time = time.time()
# dataset class is assigned to dd variable
dd.set_split('train')
dataset = DataLoader(dd, batch_size=64, shuffle=True, collate_fn=padded)
def generate_model(args, num_labels):
config = AutoConfig.from_pretrained(
args.model_name_or_path,
num_labels=num_labels,
finetuning_task=args.task_name,
)
tokenizer = AutoTokenizer.from_pretrained(
args.model_name_or_path,
do_lower_case=args.do_lower_case
class DisasterDataset():
def __init__(self, data_path, eval_path, tokenizer):
d_data = pd.read_table(data_path, sep=',')
d_eval = pd.read_table(eval_path, sep=',')
row, col = d_data.shape
d_train = d_data[:int(row * 0.8)]
d_test = d_data[int(row*0.8):]
d_train.reset_index(drop=True, inplace=True)
import time
import pickle
import multiprocessing as mp
from multiprocessing import Manager
def my_worker(result):
name = mp.current_process().name
print(name, 'Starting')
result.append(name+' Starting')
time.sleep(1)