void testDatasetEquality(LabeledData<int, int> const& set1, LabeledData<int, int> const& set2){ BOOST_REQUIRE_EQUAL(set1.numberOfBatches(),set2.numberOfBatches()); BOOST_REQUIRE_EQUAL(set1.numberOfElements(),set2.numberOfElements()); for(std::size_t i = 0; i != set1.numberOfBatches(); ++i){ BOOST_REQUIRE_EQUAL(set1.batch(i).input.size(),set1.batch(i).label.size()); BOOST_REQUIRE_EQUAL(set2.batch(i).input.size(),set2.batch(i).label.size()); } testSetEquality(set1.inputs(),set2.inputs()); testSetEquality(set1.labels(),set2.labels()); }
TEST(Csv, ReadCsv) { LabeledData<RealVector, uint32_t> data; EXPECT_EQ(0U, data.size()); EXPECT_FALSE(ReadCsv("null/null", &data)); ASSERT_TRUE(ReadCsv("testdata/data/cls.10.csv", &data, LAST_COLUMN)); const std::size_t kN = 10; EXPECT_EQ(kN, data.size()); uint32_t expected_labels_arr[kN] = {0, 1, 0, 1, 0, 1, 0, 1, 0, 1}; Data<uint32_t> expected_labels; ToData(expected_labels_arr, kN, &expected_labels); EXPECT_EQ(expected_labels, data.labels()); EXPECT_EQ(2U, data.input(0).size()); EXPECT_DOUBLE_EQ(2.4114, data.input(0)(0)); EXPECT_DOUBLE_EQ(-3.8901, data.input(0)(1)); ASSERT_TRUE(ReadCsv("testdata/data/cls.csv", &data)); EXPECT_EQ(1000U, data.size()); LabeledData<RealVector, double> data2; ASSERT_TRUE(ReadCsv("testdata/data/cls.10.csv", &data2, FIRST_COLUMN)); EXPECT_EQ(10U, data2.size()); EXPECT_DOUBLE_EQ(2.4114, data2.label(0)); EXPECT_EQ(2U, data2.input(0).size()); EXPECT_DOUBLE_EQ(-3.8901, data2.input(0)(0)); EXPECT_DOUBLE_EQ(0, data2.input(0)(1)); }
int main(){ //get problem data Problem problem(1.0); LabeledData<RealVector,unsigned int> training = problem.generateDataset(1000); LabeledData<RealVector,unsigned int> test = problem.generateDataset(100); std::size_t inputs=inputDimension(training); std::size_t outputs = numberOfClasses(training); std::size_t hiddens = 10; unsigned numberOfSteps = 1000; //create network and initialize weights random uniform FFNet<LogisticNeuron,LinearNeuron> network; network.setStructure(inputs,hiddens,outputs); initRandomUniform(network,-0.1,0.1); //create error function CrossEntropy loss; ErrorFunction error(training,&network,&loss); // loss for evaluation // The zeroOneLoss for multiclass problems assigns the class to the highest output ZeroOneLoss<unsigned int, RealVector> loss01; // evaluate initial network Data<RealVector> prediction = network(training.inputs()); cout << "classification error before learning:\t" << loss01.eval(training.labels(), prediction) << endl; //initialize Rprop IRpropPlus optimizer; optimizer.init(error); for(unsigned step = 0; step != numberOfSteps; ++step) optimizer.step(error); // evaluate solution found by training network.setParameterVector(optimizer.solution().point); // set weights to weights found by learning prediction = network(training.inputs()); cout << "classification error after learning:\t" << loss01(training.labels(), prediction) << endl; }