-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata.py
More file actions
142 lines (127 loc) · 4.5 KB
/
data.py
File metadata and controls
142 lines (127 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import numpy as np
import torch
from torch import nn, Tensor
from torch.utils.data import DataLoader, TensorDataset
from typing import Literal, Dict, Any, Union
class TaskGenerator:
"""
Generator for machine learning tasks with predefined datasets and hyperparameters.
This class provides a unified interface for generating datasets and baseline models
for specific tasks like XOR classification and sine function regression. It includes
hyperparameters optimized for each task and methods to create appropriate baseline
models.
"""
# Task-specific hyperparameters optimized for each problem
HYPERPARAMETERS = {
'none': {
'batch_size': 4,
'baseline_lr': 0.01,
'baseline_epochs': 10,
'input_size': 2,
'hidden_size': 5,
'output_size': 1,
'its': 1,
'complete_lr': 0.01,
'complete_epochs': 10,
},
'xor': {
'batch_size': 4,
'baseline_lr': 0.01,
'baseline_epochs': 10000,
'input_size': 2,
'hidden_size': 5,
'output_size': 1,
'its': 3,
'complete_lr': 0.01,
'complete_epochs': 1000,
},
'sine': {
'batch_size': 10,
'baseline_lr': 0.01,
'baseline_epochs': 1000,
'input_size': 2,
'hidden_size': 8,
'output_size': 1,
'its': 3,
'complete_lr': 0.01,
'complete_epochs': 40,
}
}
def __init__(
self,
name: Literal['xor', 'sine', 'none'],
device: Literal['cpu', 'cuda'] = 'cpu'
) -> None:
"""
Initialize a task generator for a specific task.
Args:
name: Name of the task to generate ('xor', 'sine', or 'none')
device: Device to place tensors on ('cpu' or 'cuda')
"""
self.name = name
self.device = device
# Get hyperparameters for this task
self.params = self.HYPERPARAMETERS[self.name]
self._init_data()
def _init_data(self) -> None:
"""
Initialize the dataset and dataloader for the specified task.
Creates task-specific input-output pairs:
- XOR: Creates truth table for XOR function with 4 samples
- Sine: Creates sine wave samples over [0, 3] range
"""
if self.name in ('none', 'xor'):
# Create XOR truth table: [[0,0], [0,1], [1,0], [1,1]]
x = torch.tensor(
[[0, 0], [0, 1], [1, 0], [1, 1]],
dtype=torch.float32,
device=self.device
)
# XOR outputs: [0, 1, 1, 0]
y = torch.unsqueeze(
torch.logical_xor(x[:, 0], x[:, 1]),
dim=1
).to(torch.float32).to(self.device)
elif self.name == 'sine':
x = torch.tensor(
np.arange(0, 3, 0.01).reshape(-1, 2),
dtype=torch.float32
).to(self.device)
y = torch.unsqueeze(
torch.sin(x).sum(1) / 2,
dim=1
).to(torch.float32).to(self.device)
# Create dataset and dataloader
self.dataset = TensorDataset(x, y)
self.dataloader = DataLoader(
self.dataset,
batch_size=self.params['batch_size'],
shuffle=True
)
def get_mlp_baseline(self) -> nn.Module:
"""
Create a baseline MLP model appropriate for the task.
Returns task-specific architectures:
- XOR: 2-layer MLP with sigmoid activation (2→2→1)
- Sine: 3-layer MLP with sigmoid activation (2→10→10→1)
Returns:
A PyTorch Sequential model configured for the task
"""
if self.name in ('none', 'xor'):
# Simple 2-layer network for XOR
return nn.Sequential(
nn.Linear(2, 2, bias=True),
nn.Sigmoid(),
nn.Linear(2, 1, bias=True),
nn.Sigmoid()
).to(self.device)
if self.name == 'sine':
# Deeper network for sine function approximation
return nn.Sequential(
nn.Linear(2, 10, bias=True),
nn.Sigmoid(),
nn.Linear(10, 10, bias=True),
nn.Sigmoid(),
nn.Linear(10, 1, bias=True),
nn.Sigmoid(),
).to(self.device)