MCPcopy Index your code
hub / github.com/pytorch/pytorch / testSparse

Method testSparse

caffe2/python/optimizer_test_util.py:97–148  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

95 workspace.RunNet(model.net.Proto().name)
96
97 def testSparse(self):
98 # to test duplicated indices we assign two indices to each weight and
99 # thus each weight might count once or twice
100 DUPLICATION = 2
101 perfect_model = np.array([2, 6, 5, 0, 1]).astype(np.float32)
102 np.random.seed(123) # make test deterministic
103 data = np.random.randint(
104 2,
105 size=(20, perfect_model.size * DUPLICATION)).astype(np.float32)
106 label = np.dot(data, np.repeat(perfect_model, DUPLICATION))
107
108 model = cnn.CNNModelHelper("NCHW", name="test")
109 # imitate what model wrapper does
110 w = model.param_init_net.ConstantFill(
111 [], 'w', shape=[perfect_model.size], value=0.0)
112 model.params.append(w)
113 picked = model.net.Gather([w, 'indices'], 'gather')
114 out = model.ReduceFrontSum(picked, 'sum')
115
116 sq = model.SquaredL2Distance([out, 'label'])
117 loss = model.AveragedLoss(sq, "avg_loss")
118 grad_map = model.AddGradientOperators([loss])
119 self.assertIsInstance(grad_map['w'], core.GradientSlice)
120 optimizer = self.build_optimizer(model)
121
122 workspace.CreateBlob('indices')
123 workspace.CreateBlob('label')
124
125 for indices_type in [np.int32, np.int64]:
126 workspace.RunNetOnce(model.param_init_net)
127 workspace.CreateNet(model.net, True)
128 for _ in range(2000):
129 idx = np.random.randint(data.shape[0])
130 # transform into indices of binary features
131 indices = np.repeat(np.arange(perfect_model.size),
132 DUPLICATION)[data[idx] == 1]
133 if indices.size == 0:
134 continue
135 workspace.FeedBlob(
136 'indices',
137 indices.reshape((indices.size,)).astype(indices_type)
138 )
139 workspace.FeedBlob('label',
140 np.array(label[idx]).astype(np.float32))
141 workspace.RunNet(model.net.Proto().name)
142
143 np.testing.assert_allclose(
144 perfect_model,
145 workspace.FetchBlob('w'),
146 atol=1e-2
147 )
148 self.check_optimizer(optimizer)
149
150
151class LRModificationTestBase:

Callers

nothing calls this directly

Calls 13

astypeMethod · 0.80
randintMethod · 0.80
dotMethod · 0.80
arangeMethod · 0.80
rangeFunction · 0.50
seedMethod · 0.45
repeatMethod · 0.45
appendMethod · 0.45
AddGradientOperatorsMethod · 0.45
build_optimizerMethod · 0.45
reshapeMethod · 0.45
ProtoMethod · 0.45

Tested by

no test coverage detected