| 81 | } |
| 82 | |
| 83 | int main(int argc, char** argv) { |
| 84 | dynet::initialize(argc, argv); |
| 85 | |
| 86 | const unsigned HIDDEN = 8; |
| 87 | const unsigned ITERATIONS = 20; |
| 88 | ParameterCollection m; |
| 89 | SimpleSGDTrainer trainer(m); |
| 90 | XORModel model(HIDDEN, m); |
| 91 | |
| 92 | vector<dynet::real> x_values(2); // set x_values to change the inputs |
| 93 | dynet::real y_value; // set y_value to change the target output |
| 94 | |
| 95 | // Train the model |
| 96 | for (unsigned iter = 0; iter < ITERATIONS; ++iter) { |
| 97 | double loss = 0; |
| 98 | for (unsigned mi = 0; mi < 4; ++mi) { |
| 99 | bool x1 = mi % 2; |
| 100 | bool x2 = (mi / 2) % 2; |
| 101 | x_values[0] = x1 ? 1 : -1; |
| 102 | x_values[1] = x2 ? 1 : -1; |
| 103 | y_value = (x1 != x2) ? 1 : -1; |
| 104 | loss += model.Train(x_values, y_value, trainer); |
| 105 | } |
| 106 | loss /= 4; |
| 107 | cerr << "E = " << loss << endl; |
| 108 | } |
| 109 | |
| 110 | string outfile = "read-write.model"; |
| 111 | cerr << "Written model to File: " << outfile << endl; |
| 112 | WriteToFile(outfile, m); // Writing objects to file |
| 113 | |
| 114 | // New objects in which the written model will be read |
| 115 | ParameterCollection read_dynet_model; |
| 116 | XORModel read_model(HIDDEN, read_dynet_model); |
| 117 | |
| 118 | cerr << "Reading model from File: " << outfile << endl; |
| 119 | ReadFromFile(outfile, read_dynet_model); // Reading from file |
| 120 | cerr << "Output for the input: " << x_values[0] << " " << x_values[1] << endl; |
| 121 | cerr << read_model.Decode(x_values); // Checking output for sanity |
| 122 | } |
nothing calls this directly
no test coverage detected