MCPcopy Create free account
hub / github.com/apache/singa / Train

Function Train

examples/cpp/cifar10/vgg-parallel.cc:205–309  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

203}
204
205void Train(float lr, int num_epoch, string data_dir) {
206 Cifar10 data(data_dir);
207 Tensor train_x, train_y, test_x, test_y;
208 Tensor train_x_1, train_x_2, train_y_1, train_y_2;
209 {
210 auto train = data.ReadTrainData();
211 size_t nsamples = train.first.shape(0);
212 auto mtrain =
213 Reshape(train.first, Shape{nsamples, train.first.Size() / nsamples});
214 const Tensor &mean = Average(mtrain, 0);
215 SubRow(mean, &mtrain);
216 Tensor std = Square(mtrain);
217 std = Average(std, 0);
218 std = Sqrt(std);;
219 std += 1e-6f;
220 DivRow(std, &mtrain);
221
222 train_x = Reshape(mtrain, train.first.shape());
223 train_y = train.second;
224
225 LOG(INFO) << "Slicing training data...";
226 train_x_1 = Tensor(Shape{nsamples / 2, train.first.shape(1),
227 train.first.shape(2), train.first.shape(3)});
228 LOG(INFO) << "Copying first data slice...";
229 CopyDataToFrom(&train_x_1, train_x, train_x.Size() / 2);
230 train_x_2 = Tensor(Shape{nsamples / 2, train.first.shape(1),
231 train.first.shape(2), train.first.shape(3)});
232 LOG(INFO) << "Copying second data slice...";
233 CopyDataToFrom(&train_x_2, train_x, train_x.Size() / 2, 0,
234 train_x.Size() / 2);
235 train_y_1 = Tensor(Shape{nsamples / 2});
236 train_y_1.AsType(kInt);
237 LOG(INFO) << "Copying first label slice...";
238 CopyDataToFrom(&train_y_1, train_y, train_y.Size() / 2);
239 train_y_2 = Tensor(Shape{nsamples / 2});
240 train_y_2.AsType(kInt);
241 LOG(INFO) << "Copying second label slice...";
242 CopyDataToFrom(&train_y_2, train_y, train_y.Size() / 2, 0,
243 train_y.Size() / 2);
244
245 auto test = data.ReadTestData();
246 nsamples = test.first.shape(0);
247 auto mtest =
248 Reshape(test.first, Shape{nsamples, test.first.Size() / nsamples});
249 SubRow(mean, &mtest);
250 DivRow(std, &mtest);
251 test_x = Reshape(mtest, test.first.shape());
252 test_y = test.second;
253 }
254
255 CHECK_EQ(train_x.shape(0), train_y.shape(0));
256 CHECK_EQ(test_x.shape(0), test_y.shape(0));
257 LOG(INFO) << "Total Training samples = " << train_y.shape(0)
258 << ", Total Test samples = " << test_y.shape(0);
259 CHECK_EQ(train_x_1.shape(0), train_y_1.shape(0));
260 LOG(INFO) << "On net 1, Training samples = " << train_y_1.shape(0)
261 << ", Test samples = " << test_y.shape(0);
262 CHECK_EQ(train_x_2.shape(0), train_y_2.shape(0));

Callers 1

mainFunction · 0.70

Calls 15

AverageFunction · 0.85
SubRowFunction · 0.85
SquareFunction · 0.85
DivRowFunction · 0.85
CopyDataToFromFunction · 0.85
ReadTrainDataMethod · 0.80
shapeMethod · 0.80
ReadTestDataMethod · 0.80
CompileMethod · 0.80
TrainThreadMethod · 0.80
CreateNetFunction · 0.70

Tested by

no test coverage detected