<jupyter_start><jupyter_text>27 November 2021<jupyter_code>from cnnmodel import CNN

import gym
import numpy as np
from scipy import stats
import torch
import torch.nn as nn
from torch.utils.data import DataLoader, Dataset
import torchvision.transforms as transforms
import torch.optim as optim
import gym


## Hyperparams
BATCH_SIZE = 32
LEARNING_RATE = 1E-4

class GameData(Dataset):
    def __init__(self, name = 'training_data.npz'):
        game = np.load(name)
        self.states = game['states'].astype(np.float32)
        self.actions = game['actions']
        self.n_samples = self.actions.shape[0]
#         print('Starting preprocess')
#         self.states, self.actions = preprocess(self.states, self.actions)
#         l, w, h, c = self.states.shape
#         self.states = self.states.reshape([l, c, w, h])
#         print('Initialization finished......')
        
    def __len__(self):
        return self.n_samples
    
    def __getitem__(self, index):
        return self.states[index], self.actions[index]

dataset = GameData()
dataloader = DataLoader(dataset= dataset, batch_size = BATCH_SIZE, shuffle = True, num_workers = 2)
transform = transforms.Compose(
        [transforms.ToTensor(),
        transforms.Normalize((0.5, 0.5, 0.5, 0.5), (0.5, 0.5, 0.5, 0.5))])
trainset = dataset
trainloader = torch.utils.data.DataLoader(trainset, batch_size=BATCH_SIZE,num_workers=2)

trainset = dataset
trainloader = torch.utils.data.DataLoader(trainset, batch_size=BATCH_SIZE,num_workers=2)

# Alterantive, use env.action_space.n
classes = tuple(np.arange(0, 6))
dataiter = iter(trainloader)
images, labels = dataiter.next()

net  = CNN(4, 6)
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)
print('Training starting................................')
for epoch in range(2):  # loop over the dataset multiple times
    running_loss = 0.0
    for i, data in enumerate(trainloader, 0):
        # get the inputs; data is a list of [inputs, labels]
        inputs, labels = data
        if i == 71:
            break
        # zero the parameter gradients
        optimizer.zero_grad()

        # forward + backward + optimize
        outputs = net(inputs)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()
        
        # print statistics
        running_loss += loss.item()
        if i % 10 == 0:    # print every 2000 mini-batches
            print('[%d, %5d] loss: %.3f' %
                (epoch + 1, i + 1, running_loss / 10))
            running_loss = 0.0

print('Finished Training')

env.observation_space.shape[0]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12951807228915663, 'fraction_numerical': 0.024096385542168676, 'mean_word_length': 2.8507246376811595, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '309535', 'license': 'CC0-1.0', 'n_tokens_mistral': 887, 'n_tokens_neox': 836, 'n_words': 215, 'path': 'architecture 1/supervised/Untitled.ipynb', 'repo_name': 'BongumusaSizwe/Surrogate-Learning', 'size': '2656'}