55np .random .seed (42 )
66
77
8- def create_shapes (num_samples = 300 ,
9- num_shapes = 1 ,
10- num_classes = 1 ,
11- outliers = 0.0 ,
12- ** kwargs ):
8+ def create_shapes (num_samples = 300 , num_shapes = 1 , num_classes = 1 , outliers = 0.0 , ** kwargs ):
139
14- keys = [' centers' , ' cluster_std' , ' random_state' , ' noise' , ' factor' ]
10+ keys = [" centers" , " cluster_std" , " random_state" , " noise" , " factor" ]
1511 for key in keys :
16- if key not in kwargs .keys () and key != ' factor' :
12+ if key not in kwargs .keys () and key != " factor" :
1713 kwargs [key ] = None
18- if key not in kwargs .keys () and key == ' factor' :
14+ if key not in kwargs .keys () and key == " factor" :
1915 kwargs [key ] = 0.8
2016
2117 outlier = int (outliers * num_samples )
2218 num_samples = num_samples - outlier
2319
2420 types = {
25- 'blobs' :
26- make_blobs (n_samples = num_samples ,
27- centers = kwargs ['centers' ],
28- cluster_std = kwargs ['cluster_std' ],
29- random_state = kwargs ['random_state' ]),
30- 'circle' :
31- make_circles (n_samples = num_samples ,
32- noise = kwargs ['noise' ],
33- random_state = kwargs ['random_state' ],
34- factor = kwargs ['factor' ]),
35- 'moon' :
36- make_moons (n_samples = num_samples ,
37- noise = kwargs ['noise' ],
38- random_state = kwargs ['random_state' ])
21+ "blobs" : make_blobs (
22+ n_samples = num_samples ,
23+ centers = kwargs ["centers" ],
24+ cluster_std = kwargs ["cluster_std" ],
25+ random_state = kwargs ["random_state" ],
26+ ),
27+ "circle" : make_circles (
28+ n_samples = num_samples ,
29+ noise = kwargs ["noise" ],
30+ random_state = kwargs ["random_state" ],
31+ factor = kwargs ["factor" ],
32+ ),
33+ "moon" : make_moons (
34+ n_samples = num_samples , noise = kwargs ["noise" ], random_state = kwargs ["random_state" ]
35+ ),
3936 }
4037
41- if kwargs [' kind' ] is None :
42- kwargs [' kind' ] = ' moon'
43- if not any (map (lambda shape : shape in kwargs [' kind' ], types .keys ())):
44- msg = ' kind must be in [{}, {}, {}]' .format (* types .keys ())
38+ if kwargs [" kind" ] is None :
39+ kwargs [" kind" ] = " moon"
40+ if not any (map (lambda shape : shape in kwargs [" kind" ], types .keys ())):
41+ msg = " kind must be in [{}, {}, {}]" .format (* types .keys ())
4542 raise ValueError (msg )
4643
4744 dataset , targets = [], []
4845 # TODO: extend for multiple shapes per class
4946 for j in range (num_shapes ):
50- dataset .append (types [kwargs [' kind' ]])
47+ dataset .append (types [kwargs [" kind" ]])
5148
5249 datasets , targets = zip (* dataset )
5350 datasets = np .asarray (datasets )
@@ -56,23 +53,24 @@ def create_shapes(num_samples=300,
5653
5754
5855class Shapes (torch .utils .data .TensorDataset ):
59- def __init__ (self ,
60- num_samples : int = 300 ,
61- num_shapes : int = 1 ,
62- num_classes : int = 1 ,
63- outliers = 0.0 ,
64- ** kwargs ):
65- x , y = create_shapes (num_samples , num_shapes , num_classes , outliers ,
66- ** kwargs )
56+ def __init__ (
57+ self ,
58+ num_samples : int = 300 ,
59+ num_shapes : int = 1 ,
60+ num_classes : int = 1 ,
61+ outliers = 0.0 ,
62+ ** kwargs ,
63+ ):
64+ x , y = create_shapes (num_samples , num_shapes , num_classes , outliers , ** kwargs )
6765 self .data = torch .squeeze (torch .Tensor (x ))
6866 print (self .data .shape )
6967 self .target = torch .squeeze (torch .LongTensor (y ))
7068 super ().__init__ (self .data , self .target )
7169
7270
73- if __name__ == ' __main__' :
71+ if __name__ == " __main__" :
7472 # Configuration
75- kind = ' blobs'
73+ kind = " blobs"
7674 num_samples = 1000
7775 num_shapes = 1
7876 num_classes = 2
@@ -85,12 +83,12 @@ def __init__(self,
8583 prototypes_per_class = 1
8684
8785 kwargs = {
88- ' kind' : kind ,
89- ' centers' : center ,
90- ' cluster_std' : cluster_std ,
91- ' random_state' : random_state ,
92- ' noise' : noise ,
93- ' factor' : factor ,
86+ " kind" : kind ,
87+ " centers" : center ,
88+ " cluster_std" : cluster_std ,
89+ " random_state" : random_state ,
90+ " noise" : noise ,
91+ " factor" : factor ,
9492 }
9593
9694 # Dataset
0 commit comments