Compare commits
2
Commits
41cd7dbc2f
...
490661ebc0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
490661ebc0
|
||
|
|
aa438bfac7
|
+5
-5
@@ -11,7 +11,7 @@
|
|||||||
#include <random>
|
#include <random>
|
||||||
#include <vector>
|
#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::conv2d_layer<8, 3, 1, 1>,
|
||||||
cenet::relu_layer,
|
cenet::relu_layer,
|
||||||
cenet::maxpool_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);
|
static std::uniform_real_distribution<> dist(-1, 1);
|
||||||
|
|
||||||
struct data {
|
struct data {
|
||||||
cenet::tensor<double, 1, 28, 28> xs;
|
cenet::tensor<float, 1, 28, 28> xs;
|
||||||
cenet::mat<1, 10> ys;
|
cenet::mat<1, 10, float> ys;
|
||||||
};
|
};
|
||||||
|
|
||||||
using dataset = std::vector<data>;
|
using dataset = std::vector<data>;
|
||||||
@@ -81,7 +81,7 @@ static dataset load_dataset(const char* labelsPath, const char* imagesPath) {
|
|||||||
std::uint8_t pixel = 0;
|
std::uint8_t pixel = 0;
|
||||||
images.read(reinterpret_cast<char*>(&pixel), sizeof(pixel));
|
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,7 +157,7 @@ int main(void) {
|
|||||||
const auto& output = mnistNet->feed(validationSet[i].xs);
|
const auto& output = mnistNet->feed(validationSet[i].xs);
|
||||||
|
|
||||||
for (std::size_t j = 0; j < 10; ++j) {
|
for (std::size_t j = 0; j < 10; ++j) {
|
||||||
double diff = output.get()[0][j] - validationSet[i].ys[0][j];
|
float diff = output.get()[0][j] - validationSet[i].ys[0][j];
|
||||||
totalLoss += diff * diff;
|
totalLoss += diff * diff;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+9
-9
@@ -7,7 +7,7 @@
|
|||||||
static std::uniform_real_distribution<> dist(-1, 1);
|
static std::uniform_real_distribution<> dist(-1, 1);
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
static constexpr double s_trainData[4] = {
|
static constexpr float s_trainData[4] = {
|
||||||
0, // 0 ^ 0
|
0, // 0 ^ 0
|
||||||
1, // 1 ^ 0
|
1, // 1 ^ 0
|
||||||
1, // 0 ^ 1
|
1, // 0 ^ 1
|
||||||
@@ -36,9 +36,9 @@ int main(void) {
|
|||||||
|
|
||||||
while (gen <= 10'000'000) {
|
while (gen <= 10'000'000) {
|
||||||
for (std::size_t i = 0; i < 4; ++i) {
|
for (std::size_t i = 0; i < 4; ++i) {
|
||||||
cenet::mat<1, 2> xs;
|
cenet::mat<1, 2, float> xs;
|
||||||
xs[0][0] = static_cast<double>(i & 1);
|
xs[0][0] = static_cast<float>(i & 1);
|
||||||
xs[0][1] = static_cast<double>(i > 1);
|
xs[0][1] = static_cast<float>(i > 1);
|
||||||
|
|
||||||
xorNet.train(xs, s_trainData[i], EPSILON);
|
xorNet.train(xs, s_trainData[i], EPSILON);
|
||||||
}
|
}
|
||||||
@@ -46,12 +46,12 @@ int main(void) {
|
|||||||
double acc = 0.0;
|
double acc = 0.0;
|
||||||
|
|
||||||
for (std::size_t i = 0; i < 4; ++i) {
|
for (std::size_t i = 0; i < 4; ++i) {
|
||||||
cenet::mat<1, 2> xs;
|
cenet::mat<1, 2, float> xs;
|
||||||
xs[0][0] = static_cast<double>(i & 1);
|
xs[0][0] = static_cast<float>(i & 1);
|
||||||
xs[0][1] = static_cast<double>(i > 1);
|
xs[0][1] = static_cast<float>(i > 1);
|
||||||
|
|
||||||
double yHat = xorNet.feed(xs).get()[0][0];
|
float yHat = xorNet.feed(xs).get()[0][0];
|
||||||
double error = yHat - s_trainData[i];
|
float error = yHat - s_trainData[i];
|
||||||
|
|
||||||
if ((gen % 1000) == 0) {
|
if ((gen % 1000) == 0) {
|
||||||
std::cout << xs[0][0] << " ^ " << xs[0][1] << " = " << yHat << " (error = " << error << ")\n";
|
std::cout << xs[0][0] << " ^ " << xs[0][1] << " = " << yHat << " (error = " << error << ")\n";
|
||||||
|
|||||||
@@ -14,7 +14,7 @@ struct layer_input {
|
|||||||
static constexpr std::uint32_t TENSOR_TAG = 0x544E5352;
|
static constexpr std::uint32_t TENSOR_TAG = 0x544E5352;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <std::size_t Features>
|
template <std::size_t Features, typename T = float>
|
||||||
class dense_input : public layer_input {
|
class dense_input : public layer_input {
|
||||||
template <typename Input, typename... Layers>
|
template <typename Input, typename... Layers>
|
||||||
friend class network;
|
friend class network;
|
||||||
@@ -26,7 +26,8 @@ private:
|
|||||||
|
|
||||||
static constexpr std::size_t FEATURES = Features;
|
static constexpr std::size_t FEATURES = Features;
|
||||||
|
|
||||||
using type = mat<1, FEATURES>;
|
using inner_type = T;
|
||||||
|
using type = mat<1, FEATURES, T>;
|
||||||
|
|
||||||
template <typename... Args, typename = std::enable_if_t<std::is_constructible_v<type, Args...>>>
|
template <typename... Args, typename = std::enable_if_t<std::is_constructible_v<type, Args...>>>
|
||||||
inner(Args&&... args)
|
inner(Args&&... args)
|
||||||
@@ -42,7 +43,7 @@ private:
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
template <std::size_t... Dims>
|
template <typename T, std::size_t... Dims>
|
||||||
class tensor_input : public layer_input {
|
class tensor_input : public layer_input {
|
||||||
template <typename Input, typename... Layers>
|
template <typename Input, typename... Layers>
|
||||||
friend class network;
|
friend class network;
|
||||||
@@ -52,7 +53,8 @@ private:
|
|||||||
struct inner final {
|
struct inner final {
|
||||||
static constexpr std::uint32_t TAG = TENSOR_TAG;
|
static constexpr std::uint32_t TAG = TENSOR_TAG;
|
||||||
|
|
||||||
using type = tensor<double, Dims...>;
|
using inner_type = T;
|
||||||
|
using type = tensor<T, Dims...>;
|
||||||
|
|
||||||
template <typename... Args, typename = std::enable_if_t<std::is_constructible_v<type, Args...>>>
|
template <typename... Args, typename = std::enable_if_t<std::is_constructible_v<type, Args...>>>
|
||||||
inner(Args&&... args)
|
inner(Args&&... args)
|
||||||
|
|||||||
+45
-34
@@ -26,7 +26,7 @@ protected:
|
|||||||
using input_inner_t = typename T::inner;
|
using input_inner_t = typename T::inner;
|
||||||
};
|
};
|
||||||
|
|
||||||
template <std::size_t Features>
|
template <std::size_t Features, typename T = float>
|
||||||
class dense_layer final : public layer_info {
|
class dense_layer final : public layer_info {
|
||||||
template <typename Input, typename... Layers>
|
template <typename Input, typename... Layers>
|
||||||
friend class network;
|
friend class network;
|
||||||
@@ -36,13 +36,13 @@ private:
|
|||||||
using input_type = input_type_s<NN, I>::type;
|
using input_type = input_type_s<NN, I>::type;
|
||||||
static_assert(input_type::TAG == layer_input::DENSE_TAG);
|
static_assert(input_type::TAG == layer_input::DENSE_TAG);
|
||||||
|
|
||||||
using output_type = input_inner_t<dense_input<Features>>;
|
using output_type = input_inner_t<dense_input<Features, T>>;
|
||||||
|
|
||||||
static constexpr std::size_t INPUT_FEATURES = input_type::FEATURES;
|
static constexpr std::size_t INPUT_FEATURES = input_type::FEATURES;
|
||||||
static constexpr std::size_t OUTPUT_FEATURES = Features;
|
static constexpr std::size_t OUTPUT_FEATURES = Features;
|
||||||
|
|
||||||
using weights_type = mat<INPUT_FEATURES, OUTPUT_FEATURES>;
|
using weights_type = mat<INPUT_FEATURES, OUTPUT_FEATURES, T>;
|
||||||
using biases_type = mat<1, OUTPUT_FEATURES>;
|
using biases_type = mat<1, OUTPUT_FEATURES, T>;
|
||||||
|
|
||||||
struct gradient final {
|
struct gradient final {
|
||||||
using parent_type = inner;
|
using parent_type = inner;
|
||||||
@@ -68,18 +68,18 @@ private:
|
|||||||
output_type output;
|
output_type output;
|
||||||
|
|
||||||
void dump(std::ostream& os) const {
|
void dump(std::ostream& os) const {
|
||||||
os.write(reinterpret_cast<const char*>(weights.data()), sizeof(double) * weights.size());
|
os.write(reinterpret_cast<const char*>(weights.data()), sizeof(T) * weights.size());
|
||||||
os.write(reinterpret_cast<const char*>(biases.data()), sizeof(double) * biases.size());
|
os.write(reinterpret_cast<const char*>(biases.data()), sizeof(T) * biases.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
void load(std::istream& is) {
|
void load(std::istream& is) {
|
||||||
is.read(reinterpret_cast<char*>(weights.data()), sizeof(double) * weights.size());
|
is.read(reinterpret_cast<char*>(weights.data()), sizeof(T) * weights.size());
|
||||||
is.read(reinterpret_cast<char*>(biases.data()), sizeof(double) * biases.size());
|
is.read(reinterpret_cast<char*>(biases.data()), sizeof(T) * biases.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Dist>
|
template <typename Dist>
|
||||||
void fill_random(const Dist& dist) {
|
void fill_random(const Dist& dist) {
|
||||||
double scale = std::sqrt(2.0 / static_cast<double>(INPUT_FEATURES));
|
T scale = std::sqrt(2.0 / static_cast<T>(INPUT_FEATURES));
|
||||||
weights = decltype(weights)::random(dist) * scale;
|
weights = decltype(weights)::random(dist) * scale;
|
||||||
biases = decltype(biases)::random(dist) * scale;
|
biases = decltype(biases)::random(dist) * scale;
|
||||||
}
|
}
|
||||||
@@ -103,14 +103,14 @@ private:
|
|||||||
|
|
||||||
for (std::size_t row = 0; row < INPUT_FEATURES; ++row) {
|
for (std::size_t row = 0; row < INPUT_FEATURES; ++row) {
|
||||||
for (std::size_t col = 0; col < OUTPUT_FEATURES; ++col) {
|
for (std::size_t col = 0; col < OUTPUT_FEATURES; ++col) {
|
||||||
double avgGrad = grad.weights[row][col] / divisor;
|
auto avgGrad = grad.weights[row][col] / divisor;
|
||||||
|
|
||||||
grad.momentumWeights[row][col] = beta1 * grad.momentumWeights[row][col] + (1.0 - beta1) * avgGrad;
|
grad.momentumWeights[row][col] = beta1 * grad.momentumWeights[row][col] + (1.0 - beta1) * avgGrad;
|
||||||
grad.varianceWeights[row][col] =
|
grad.varianceWeights[row][col] =
|
||||||
beta2 * grad.varianceWeights[row][col] + (1.0 - beta2) * (avgGrad * avgGrad);
|
beta2 * grad.varianceWeights[row][col] + (1.0 - beta2) * (avgGrad * avgGrad);
|
||||||
|
|
||||||
double momentumCorrected = grad.momentumWeights[row][col] / (1.0 - std::pow(beta1, grad.time));
|
auto momentumCorrected = grad.momentumWeights[row][col] / (1.0 - std::pow(beta1, grad.time));
|
||||||
double varianceCorrected = grad.varianceWeights[row][col] / (1.0 - std::pow(beta2, grad.time));
|
auto varianceCorrected = grad.varianceWeights[row][col] / (1.0 - std::pow(beta2, grad.time));
|
||||||
|
|
||||||
weights[row][col] -= (rate * momentumCorrected) / (std::sqrt(varianceCorrected) + epsilon);
|
weights[row][col] -= (rate * momentumCorrected) / (std::sqrt(varianceCorrected) + epsilon);
|
||||||
}
|
}
|
||||||
@@ -122,8 +122,8 @@ private:
|
|||||||
grad.momentumBiases[0][col] = beta1 * grad.momentumBiases[0][col] + (1.0 - beta1) * avgGrad;
|
grad.momentumBiases[0][col] = beta1 * grad.momentumBiases[0][col] + (1.0 - beta1) * avgGrad;
|
||||||
grad.varianceBiases[0][col] = beta2 * grad.varianceBiases[0][col] + (1.0 - beta2) * (avgGrad * avgGrad);
|
grad.varianceBiases[0][col] = beta2 * grad.varianceBiases[0][col] + (1.0 - beta2) * (avgGrad * avgGrad);
|
||||||
|
|
||||||
double momentumCorrected = grad.momentumBiases[0][col] / (1.0 - std::pow(beta1, grad.time));
|
auto momentumCorrected = grad.momentumBiases[0][col] / (1.0 - std::pow(beta1, grad.time));
|
||||||
double varianceCorrected = grad.varianceBiases[0][col] / (1.0 - std::pow(beta2, grad.time));
|
auto varianceCorrected = grad.varianceBiases[0][col] / (1.0 - std::pow(beta2, grad.time));
|
||||||
|
|
||||||
biases[0][col] -= (rate * momentumCorrected) / (std::sqrt(varianceCorrected) + epsilon);
|
biases[0][col] -= (rate * momentumCorrected) / (std::sqrt(varianceCorrected) + epsilon);
|
||||||
}
|
}
|
||||||
@@ -147,6 +147,7 @@ private:
|
|||||||
void reset() {}
|
void reset() {}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static float sigmoid(float in) { return 1 / (1 + std::exp(-in)); }
|
||||||
static double sigmoid(double in) { return 1 / (1 + std::exp(-in)); }
|
static double sigmoid(double in) { return 1 / (1 + std::exp(-in)); }
|
||||||
|
|
||||||
void dump(std::ostream& os) const {}
|
void dump(std::ostream& os) const {}
|
||||||
@@ -157,13 +158,13 @@ private:
|
|||||||
void fill_random(const Dist& dist) {}
|
void fill_random(const Dist& dist) {}
|
||||||
|
|
||||||
auto& forward(const input_type& input) {
|
auto& forward(const input_type& input) {
|
||||||
output.get().apply(input, [](double unusd, double in) { return sigmoid(in); });
|
output.get().apply(input, [](auto unusd, auto in) { return sigmoid(in); });
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Error>
|
template <typename Error>
|
||||||
auto backward(const input_type& input, gradient& grad, Error prevErr) const {
|
auto backward(const input_type& input, gradient& grad, Error prevErr) const {
|
||||||
return prevErr.hadamard(output.get().hadamard(1.0 - output.get()));
|
return prevErr.hadamard(output.get().hadamard(static_cast<output_type::inner_type>(1) - output.get()));
|
||||||
}
|
}
|
||||||
|
|
||||||
void apply_grad(gradient&, double, double) {}
|
void apply_grad(gradient&, double, double) {}
|
||||||
@@ -195,20 +196,24 @@ private:
|
|||||||
|
|
||||||
auto& forward(const input_type& input) {
|
auto& forward(const input_type& input) {
|
||||||
savedInput = input;
|
savedInput = input;
|
||||||
output.get().apply(input, [](double unusd, double in) { return in > 0.0 ? in : 0.0; });
|
output.get().apply(input, [](auto unusd, auto in) { return in > 0.0 ? in : 0.0; });
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename Error>
|
template <typename Error>
|
||||||
auto backward(const input_type& input, gradient& grad, Error prevErr) const {
|
auto backward(const input_type& input, gradient& grad, Error prevErr) const {
|
||||||
return prevErr.apply(savedInput, [](double in, double check) { return check > 0.0 ? in : 0.0; });
|
return prevErr.apply(savedInput, [](auto in, auto check) { return check > 0.0 ? in : 0.0; });
|
||||||
}
|
}
|
||||||
|
|
||||||
void apply_grad(gradient&, double, double) {}
|
void apply_grad(gradient&, double, double) {}
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
template <std::size_t OutChannels, std::size_t KernelSize, std::size_t Stride = 1, std::size_t Padding = 0>
|
template <std::size_t OutChannels,
|
||||||
|
std::size_t KernelSize,
|
||||||
|
std::size_t Stride = 1,
|
||||||
|
std::size_t Padding = 0,
|
||||||
|
typename T = float>
|
||||||
class conv2d_layer final : public layer_info {
|
class conv2d_layer final : public layer_info {
|
||||||
template <typename Input, typename... Layers>
|
template <typename Input, typename... Layers>
|
||||||
friend class network;
|
friend class network;
|
||||||
@@ -225,14 +230,19 @@ private:
|
|||||||
static constexpr std::size_t IN_HEIGHT = input_tensor::template dimension<1>();
|
static constexpr std::size_t IN_HEIGHT = input_tensor::template dimension<1>();
|
||||||
static constexpr std::size_t IN_WIDTH = input_tensor::template dimension<2>();
|
static constexpr std::size_t IN_WIDTH = input_tensor::template dimension<2>();
|
||||||
|
|
||||||
|
static_assert(Stride > 0, "Invalid stride passed to conv2d_layer");
|
||||||
|
static_assert(KernelSize > 0, "Invalid kernel size passed to conv2d_layer");
|
||||||
|
static_assert(IN_HEIGHT + (2 * Padding) >= KernelSize, "Invalid network configuration for conv2d_layer");
|
||||||
|
static_assert(IN_WIDTH + (2 * Padding) >= KernelSize, "Invalid network configuration for conv2d_layer");
|
||||||
|
|
||||||
static constexpr std::size_t OUT_CHANS = OutChannels;
|
static constexpr std::size_t OUT_CHANS = OutChannels;
|
||||||
static constexpr std::size_t OUT_HEIGHT = ((IN_HEIGHT - KernelSize + (2 * Padding)) / Stride) + 1;
|
static constexpr std::size_t OUT_HEIGHT = ((IN_HEIGHT - KernelSize + (2 * Padding)) / Stride) + 1;
|
||||||
static constexpr std::size_t OUT_WIDTH = ((IN_WIDTH - KernelSize + (2 * Padding)) / Stride) + 1;
|
static constexpr std::size_t OUT_WIDTH = ((IN_WIDTH - KernelSize + (2 * Padding)) / Stride) + 1;
|
||||||
|
|
||||||
using output_type = input_inner_t<tensor_input<OUT_CHANS, OUT_HEIGHT, OUT_WIDTH>>;
|
using output_type = input_inner_t<tensor_input<T, OUT_CHANS, OUT_HEIGHT, OUT_WIDTH>>;
|
||||||
|
|
||||||
using weights_type = tensor<double, OUT_CHANS, IN_CHANS, KernelSize, KernelSize>;
|
using weights_type = tensor<T, OUT_CHANS, IN_CHANS, KernelSize, KernelSize>;
|
||||||
using biases_type = tensor<double, OUT_CHANS>;
|
using biases_type = tensor<T, OUT_CHANS>;
|
||||||
|
|
||||||
struct gradient final {
|
struct gradient final {
|
||||||
using parent_type = inner;
|
using parent_type = inner;
|
||||||
@@ -282,7 +292,7 @@ private:
|
|||||||
for (std::size_t oc = 0; oc < OUT_CHANS; ++oc) {
|
for (std::size_t oc = 0; oc < OUT_CHANS; ++oc) {
|
||||||
for (std::size_t oy = 0; oy < OUT_HEIGHT; ++oy) {
|
for (std::size_t oy = 0; oy < OUT_HEIGHT; ++oy) {
|
||||||
for (std::size_t ox = 0; ox < OUT_WIDTH; ++ox) {
|
for (std::size_t ox = 0; ox < OUT_WIDTH; ++ox) {
|
||||||
double value = biases[oc];
|
auto value = biases[oc];
|
||||||
|
|
||||||
for (std::size_t ic = 0; ic < IN_CHANS; ++ic) {
|
for (std::size_t ic = 0; ic < IN_CHANS; ++ic) {
|
||||||
for (std::size_t ky = 0; ky < KernelSize; ++ky) {
|
for (std::size_t ky = 0; ky < KernelSize; ++ky) {
|
||||||
@@ -313,7 +323,7 @@ private:
|
|||||||
for (std::size_t oc = 0; oc < OUT_CHANS; ++oc) {
|
for (std::size_t oc = 0; oc < OUT_CHANS; ++oc) {
|
||||||
for (std::size_t oy = 0; oy < OUT_HEIGHT; ++oy) {
|
for (std::size_t oy = 0; oy < OUT_HEIGHT; ++oy) {
|
||||||
for (std::size_t ox = 0; ox < OUT_WIDTH; ++ox) {
|
for (std::size_t ox = 0; ox < OUT_WIDTH; ++ox) {
|
||||||
const double err = prevErr[oc][oy][ox];
|
const auto err = prevErr[oc][oy][ox];
|
||||||
|
|
||||||
grad.biases[oc] += err;
|
grad.biases[oc] += err;
|
||||||
|
|
||||||
@@ -361,9 +371,9 @@ private:
|
|||||||
grad.varianceWeights[oc][ic][ky][kx] =
|
grad.varianceWeights[oc][ic][ky][kx] =
|
||||||
beta2 * grad.varianceWeights[oc][ic][ky][kx] + (1.0 - beta2) * avgGrad * avgGrad;
|
beta2 * grad.varianceWeights[oc][ic][ky][kx] + (1.0 - beta2) * avgGrad * avgGrad;
|
||||||
|
|
||||||
double momentumCorrected =
|
auto momentumCorrected =
|
||||||
grad.momentumWeights[oc][ic][ky][kx] / (1.0 - std::pow(beta1, grad.time));
|
grad.momentumWeights[oc][ic][ky][kx] / (1.0 - std::pow(beta1, grad.time));
|
||||||
double varianceCorrected =
|
auto varianceCorrected =
|
||||||
grad.varianceWeights[oc][ic][ky][kx] / (1.0 - std::pow(beta2, grad.time));
|
grad.varianceWeights[oc][ic][ky][kx] / (1.0 - std::pow(beta2, grad.time));
|
||||||
|
|
||||||
weights[oc][ic][ky][kx] -=
|
weights[oc][ic][ky][kx] -=
|
||||||
@@ -372,13 +382,13 @@ private:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
double avgGrad = grad.biases[oc] / divisor;
|
auto avgGrad = grad.biases[oc] / divisor;
|
||||||
|
|
||||||
grad.momentumBiases[oc] = beta1 * grad.momentumBiases[oc] + (1.0 - beta1) * avgGrad;
|
grad.momentumBiases[oc] = beta1 * grad.momentumBiases[oc] + (1.0 - beta1) * avgGrad;
|
||||||
grad.varianceBiases[oc] = beta2 * grad.varianceBiases[oc] + (1.0 - beta2) * avgGrad * avgGrad;
|
grad.varianceBiases[oc] = beta2 * grad.varianceBiases[oc] + (1.0 - beta2) * avgGrad * avgGrad;
|
||||||
|
|
||||||
double momentumCorrected = grad.momentumBiases[oc] / (1.0 - std::pow(beta1, grad.time));
|
auto momentumCorrected = grad.momentumBiases[oc] / (1.0 - std::pow(beta1, grad.time));
|
||||||
double varianceCorrected = grad.varianceBiases[oc] / (1.0 - std::pow(beta2, grad.time));
|
auto varianceCorrected = grad.varianceBiases[oc] / (1.0 - std::pow(beta2, grad.time));
|
||||||
|
|
||||||
biases[oc] -= rate * momentumCorrected / (std::sqrt(varianceCorrected) + epsilon);
|
biases[oc] -= rate * momentumCorrected / (std::sqrt(varianceCorrected) + epsilon);
|
||||||
}
|
}
|
||||||
@@ -386,7 +396,7 @@ private:
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
template <std::size_t PoolSize = 2, std::size_t Stride = 2>
|
template <std::size_t PoolSize = 2, std::size_t Stride = 2, typename T = float>
|
||||||
class maxpool_layer : public layer_info {
|
class maxpool_layer : public layer_info {
|
||||||
template <typename Input, typename... Layers>
|
template <typename Input, typename... Layers>
|
||||||
friend class network;
|
friend class network;
|
||||||
@@ -397,6 +407,7 @@ private:
|
|||||||
static_assert(input_type::TAG == layer_input::TENSOR_TAG);
|
static_assert(input_type::TAG == layer_input::TENSOR_TAG);
|
||||||
|
|
||||||
using input_tensor = input_type::type;
|
using input_tensor = input_type::type;
|
||||||
|
static_assert(std::is_same_v<typename input_tensor::inner_type, T>);
|
||||||
static_assert(input_tensor::RANK == 3);
|
static_assert(input_tensor::RANK == 3);
|
||||||
|
|
||||||
static constexpr std::size_t IN_CHANS = input_tensor::template dimension<0>();
|
static constexpr std::size_t IN_CHANS = input_tensor::template dimension<0>();
|
||||||
@@ -407,7 +418,7 @@ private:
|
|||||||
static constexpr std::size_t OUT_HEIGHT = ((IN_HEIGHT - PoolSize) / Stride) + 1;
|
static constexpr std::size_t OUT_HEIGHT = ((IN_HEIGHT - PoolSize) / Stride) + 1;
|
||||||
static constexpr std::size_t OUT_WIDTH = ((IN_WIDTH - PoolSize) / Stride) + 1;
|
static constexpr std::size_t OUT_WIDTH = ((IN_WIDTH - PoolSize) / Stride) + 1;
|
||||||
|
|
||||||
using output_type = input_inner_t<tensor_input<OUT_CHANS, OUT_HEIGHT, OUT_WIDTH>>;
|
using output_type = input_inner_t<tensor_input<T, OUT_CHANS, OUT_HEIGHT, OUT_WIDTH>>;
|
||||||
output_type output;
|
output_type output;
|
||||||
|
|
||||||
struct gradient final {
|
struct gradient final {
|
||||||
@@ -425,7 +436,7 @@ private:
|
|||||||
for (std::size_t ch = 0; ch < OUT_CHANS; ++ch) {
|
for (std::size_t ch = 0; ch < OUT_CHANS; ++ch) {
|
||||||
for (std::size_t oy = 0; oy < OUT_HEIGHT; ++oy) {
|
for (std::size_t oy = 0; oy < OUT_HEIGHT; ++oy) {
|
||||||
for (std::size_t ox = 0; ox < OUT_WIDTH; ++ox) {
|
for (std::size_t ox = 0; ox < OUT_WIDTH; ++ox) {
|
||||||
double maxValue = input.get()[ch][oy * Stride][ox * Stride];
|
auto maxValue = input.get()[ch][oy * Stride][ox * Stride];
|
||||||
|
|
||||||
for (std::size_t ky = 0; ky < PoolSize; ++ky) {
|
for (std::size_t ky = 0; ky < PoolSize; ++ky) {
|
||||||
for (std::size_t kx = 0; kx < PoolSize; ++kx) {
|
for (std::size_t kx = 0; kx < PoolSize; ++kx) {
|
||||||
@@ -451,7 +462,7 @@ private:
|
|||||||
std::size_t maxY = oy * Stride;
|
std::size_t maxY = oy * Stride;
|
||||||
std::size_t maxX = ox * Stride;
|
std::size_t maxX = ox * Stride;
|
||||||
|
|
||||||
double maxValue = input.get()[ch][maxY][maxX];
|
auto maxValue = input.get()[ch][maxY][maxX];
|
||||||
|
|
||||||
for (std::size_t ky = 0; ky < PoolSize; ++ky) {
|
for (std::size_t ky = 0; ky < PoolSize; ++ky) {
|
||||||
for (std::size_t kx = 0; kx < PoolSize; ++kx) {
|
for (std::size_t kx = 0; kx < PoolSize; ++kx) {
|
||||||
@@ -491,7 +502,7 @@ private:
|
|||||||
|
|
||||||
static constexpr std::size_t FEATURES = input_tensor::SIZE;
|
static constexpr std::size_t FEATURES = input_tensor::SIZE;
|
||||||
|
|
||||||
using output_type = input_inner_t<dense_input<FEATURES>>;
|
using output_type = input_inner_t<dense_input<FEATURES, typename input_tensor::inner_type>>;
|
||||||
output_type output;
|
output_type output;
|
||||||
|
|
||||||
struct gradient final {
|
struct gradient final {
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ template <std::size_t M, std::size_t N, typename T = double, typename = std::ena
|
|||||||
class mat {
|
class mat {
|
||||||
public:
|
public:
|
||||||
using col_type = mat_col<N, T>;
|
using col_type = mat_col<N, T>;
|
||||||
|
using inner_type = T;
|
||||||
public:
|
public:
|
||||||
mat() { std::memset(m_items, 0, sizeof(m_items)); }
|
mat() { std::memset(m_items, 0, sizeof(m_items)); }
|
||||||
|
|
||||||
|
|||||||
@@ -108,7 +108,7 @@ public:
|
|||||||
public:
|
public:
|
||||||
void backprop(const input_type& input, const output_type& expected, learning_state& state) {
|
void backprop(const input_type& input, const output_type& expected, learning_state& state) {
|
||||||
feed(input);
|
feed(input);
|
||||||
auto outputErr = (get_output().get() - expected).hadamard(get_output()).hadamard(1.0 - get_output().get());
|
auto outputErr = get_output().get() - expected;
|
||||||
get_layer<0>().backward(input,
|
get_layer<0>().backward(input,
|
||||||
std::get<0>(state.m_layers),
|
std::get<0>(state.m_layers),
|
||||||
backprop_chain<sizeof...(Layers) - 1>(state, outputErr));
|
backprop_chain<sizeof...(Layers) - 1>(state, outputErr));
|
||||||
|
|||||||
@@ -21,6 +21,8 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
static constexpr std::size_t RANK = 1 + sizeof...(Rest);
|
static constexpr std::size_t RANK = 1 + sizeof...(Rest);
|
||||||
|
|
||||||
|
using inner_type = T;
|
||||||
public:
|
public:
|
||||||
template <typename InT, std::size_t InFirst, std::size_t... InRest>
|
template <typename InT, std::size_t InFirst, std::size_t... InRest>
|
||||||
class view {
|
class view {
|
||||||
|
|||||||
Reference in New Issue
Block a user