refactor: add type parameter to layer inputs
This commit is contained in:
+6
-6
@@ -11,7 +11,7 @@
|
||||
#include <random>
|
||||
#include <vector>
|
||||
|
||||
using network_type = cenet::network<cenet::tensor_input<1, 28, 28>,
|
||||
using network_type = cenet::network<cenet::tensor_input<float, 1, 28, 28>,
|
||||
cenet::conv2d_layer<8, 3, 1, 1>,
|
||||
cenet::relu_layer,
|
||||
cenet::maxpool_layer<>,
|
||||
@@ -25,8 +25,8 @@ using network_type = cenet::network<cenet::tensor_input<1, 28, 28>,
|
||||
static std::uniform_real_distribution<> dist(-1, 1);
|
||||
|
||||
struct data {
|
||||
cenet::tensor<double, 1, 28, 28> xs;
|
||||
cenet::mat<1, 10> ys;
|
||||
cenet::tensor<float, 1, 28, 28> xs;
|
||||
cenet::mat<1, 10, float> ys;
|
||||
};
|
||||
|
||||
using dataset = std::vector<data>;
|
||||
@@ -81,7 +81,7 @@ static dataset load_dataset(const char* labelsPath, const char* imagesPath) {
|
||||
std::uint8_t pixel = 0;
|
||||
images.read(reinterpret_cast<char*>(&pixel), sizeof(pixel));
|
||||
|
||||
set[i].xs[0][row][col] = static_cast<double>(pixel) / 255.0;
|
||||
set[i].xs[0][row][col] = static_cast<float>(pixel) / 255.0F;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -157,8 +157,8 @@ int main(void) {
|
||||
const auto& output = mnistNet->feed(validationSet[i].xs);
|
||||
|
||||
for (std::size_t j = 0; j < 10; ++j) {
|
||||
double diff = output.get()[0][j] - validationSet[i].ys[0][j];
|
||||
totalLoss += diff * diff;
|
||||
float diff = output.get()[0][j] - validationSet[i].ys[0][j];
|
||||
totalLoss += diff * diff;
|
||||
}
|
||||
|
||||
if (col_largest_idx(output.get()[0]) == col_largest_idx(validationSet[i].ys[0])) ++hits;
|
||||
|
||||
+9
-9
@@ -7,7 +7,7 @@
|
||||
static std::uniform_real_distribution<> dist(-1, 1);
|
||||
|
||||
int main(void) {
|
||||
static constexpr double s_trainData[4] = {
|
||||
static constexpr float s_trainData[4] = {
|
||||
0, // 0 ^ 0
|
||||
1, // 1 ^ 0
|
||||
1, // 0 ^ 1
|
||||
@@ -36,9 +36,9 @@ int main(void) {
|
||||
|
||||
while (gen <= 10'000'000) {
|
||||
for (std::size_t i = 0; i < 4; ++i) {
|
||||
cenet::mat<1, 2> xs;
|
||||
xs[0][0] = static_cast<double>(i & 1);
|
||||
xs[0][1] = static_cast<double>(i > 1);
|
||||
cenet::mat<1, 2, float> xs;
|
||||
xs[0][0] = static_cast<float>(i & 1);
|
||||
xs[0][1] = static_cast<float>(i > 1);
|
||||
|
||||
xorNet.train(xs, s_trainData[i], EPSILON);
|
||||
}
|
||||
@@ -46,12 +46,12 @@ int main(void) {
|
||||
double acc = 0.0;
|
||||
|
||||
for (std::size_t i = 0; i < 4; ++i) {
|
||||
cenet::mat<1, 2> xs;
|
||||
xs[0][0] = static_cast<double>(i & 1);
|
||||
xs[0][1] = static_cast<double>(i > 1);
|
||||
cenet::mat<1, 2, float> xs;
|
||||
xs[0][0] = static_cast<float>(i & 1);
|
||||
xs[0][1] = static_cast<float>(i > 1);
|
||||
|
||||
double yHat = xorNet.feed(xs).get()[0][0];
|
||||
double error = yHat - s_trainData[i];
|
||||
float yHat = xorNet.feed(xs).get()[0][0];
|
||||
float error = yHat - s_trainData[i];
|
||||
|
||||
if ((gen % 1000) == 0) {
|
||||
std::cout << xs[0][0] << " ^ " << xs[0][1] << " = " << yHat << " (error = " << error << ")\n";
|
||||
|
||||
Reference in New Issue
Block a user