|
2 | 2 | from sklearn.datasets import make_circles
|
3 | 3 | # Make 100 Samples
|
4 | 4 | n_samples = 10000
|
5 |
| -X,y = make_circles(n_samples,noise=0.069,random_state=42) |
| 5 | +X,y = make_circles(n_samples,noise=0.125,random_state=42) |
6 | 6 |
|
7 | 7 |
|
8 | 8 | import matplotlib.pyplot as plt
|
9 | 9 | import numpy as np
|
10 | 10 | import torch
|
| 11 | +device = 'cuda' if torch.cuda.is_available() else 'cpu' |
11 | 12 |
|
12 | 13 |
|
13 | 14 | X[:5],y[:5]
|
|
37 | 38 | X.dtype
|
38 | 39 |
|
39 | 40 |
|
40 |
| -X = torch.from_numpy(X).type(torch.float32) |
41 |
| -y = torch.from_numpy(y).type(torch.float32) |
| 41 | +X = torch.from_numpy(X).type(torch.float32).to(device) |
| 42 | +y = torch.from_numpy(y).type(torch.float32).to(device) |
42 | 43 |
|
43 | 44 |
|
44 | 45 | X[:2],y[:2]
|
|
47 | 48 | from sklearn.model_selection import train_test_split
|
48 | 49 |
|
49 | 50 |
|
50 |
| -X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.25) |
| 51 | +X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.25,random_state=42) |
51 | 52 |
|
52 | 53 |
|
| 54 | +len(X_train),len(y_test) |
53 | 55 |
|
54 | 56 |
|
| 57 | +import torch |
| 58 | +from torch import nn |
| 59 | + |
| 60 | +# Make device agnositic code |
| 61 | +device = 'cuda' if torch.cuda.is_available() else 'cpu' |
| 62 | + |
| 63 | + |
| 64 | +device |
| 65 | + |
| 66 | + |
| 67 | +class CircleModelV0(nn.Module): |
| 68 | + def __init__(self): |
| 69 | + super().__init__() |
| 70 | + self.layer_1 = nn.Linear(2,1024) # |
| 71 | + self.layer_2 = nn.Linear(1024,1) |
| 72 | + |
| 73 | + def forward(self,X): |
| 74 | + return self.layer_2(self.layer_1(X)) # x -> layer_1 -> layer_2 |
| 75 | + |
| 76 | + |
| 77 | +model_0 = CircleModelV0().to(device) |
| 78 | + |
| 79 | + |
| 80 | +model_0 |
| 81 | + |
| 82 | + |
| 83 | +list(model_0.parameters()) |
| 84 | + |
| 85 | + |
| 86 | +model_0 = nn.Sequential( |
| 87 | + nn.Linear(in_features=2,out_features=64), |
| 88 | + nn.Linear(64,1) |
| 89 | +).to(device) |
| 90 | + |
| 91 | + |
| 92 | +untrained_preds = model_0(X_test) |
| 93 | + |
| 94 | + |
| 95 | +untrained_preds[0],y_test[0] |
| 96 | + |
55 | 97 |
|
56 | 98 |
|
0 commit comments