# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: mmc
#     language: python
#     name: mmc
# ---

# +
# hide pakcage non critical warnings

import warnings

warnings.filterwarnings('ignore')

from IPython.core.display import display, HTML
display(HTML("<style>.container { width:80% !important; }</style>"))


# %load_ext autoreload
# %autoreload 2

# %matplotlib inline

# +
import matplotlib.pyplot as plt

import scipy.io as sio

import torch
import torch.nn as nn

import torch.nn.functional as F
import torch.nn.init as init

from custom_data import DCCPT_data

from config import cfg, get_data_dir, get_output_dir, AverageMeter, remove_files_in_dir
from convSDAE import convSDAE
from tensorboard_logger import Logger

import os
import random
import numpy as np

import data_params as dp
# -

import devkit.api as dk

net = convSDAE(dim=[1, 50, 50, 50, 10], output_padding=[0, 1, 0], numpen=4, dropout=0.2, slope=0)

net

import torch.optim as optim
import torch.optim.lr_scheduler as lr_scheduler

lr =0.0001

numlayers = 4
lr = 10
maxepoch = 2
stepsize = 10

for par in net.base[numlayers-1].parameters():
    par.requires_grad = True

for par in net.bbase[numlayers-1].parameters():
    par.requires_grad = True
for m in net.bbase[numlayers-1].modules():
    if isinstance(m, nn.BatchNorm2d):
        m.training = True

# setting up optimizer - the bias params should have twice the learning rate w.r.t. weights params
bias_params = filter(lambda x: ('bias' in x[0]) and (x[1].requires_grad), net.named_parameters())
bias_params = list(map(lambda x: x[1], bias_params))
nonbias_params = filter(lambda x: ('bias' not in x[0]) and (x[1].requires_grad), net.named_parameters())
nonbias_params = list(map(lambda x: x[1], nonbias_params))

# +
optimizer = optim.SGD([{'params': bias_params, 'lr': 2*lr}, {'params': nonbias_params}],
                      lr=lr, momentum=0.9, weight_decay=0.0, nesterov=True)

scheduler = lr_scheduler.StepLR(optimizer, step_size=200, gamma=0.1)
# -

from torch.utils.tensorboard import SummaryWriter


writer = SummaryWriter('fashion_mnist_experiment_1')


datadir = get_data_dir("cmnist")


trainset = DCCPT_data(root=datadir, train=True, h5=False)

trainloader = torch.utils.data.DataLoader(trainset, batch_size=256, shuffle=True)


dataiter = iter(trainloader)

dataiter.next()

images, labels = dataiter.next()


images.shape

torch.tensor(3)

labels

net(images, torch.tensor(1))

writer = SummaryWriter('fashion_mnist_experiment_1')


writer.add_graph(net, (images, torch.tensor(4)))
# writer.close()



StarcoderdataJupyterScriptsDedupFiltered