From 47102e143aed7b99fcf2420d0fa6d44b39ed1e24 Mon Sep 17 00:00:00 2001 From: "jijoong.moon" Date: Thu, 19 Mar 2020 13:34:48 +0900 Subject: [PATCH] Add namespace Tensors for Tensor for better readibility, using Tensors::Tensor **Self evaluation:** 1. Build test: [X]Passed [ ]Failed [ ]Skipped 2. Run test: [X]Passed [ ]Failed [ ]Skipped Signed-off-by: jijoong.moon --- Applications/Classification/jni/main.cpp | 10 +-- include/layers.h | 70 +++++++++--------- include/neuralnet.h | 6 +- include/tensor.h | 15 ++++ src/layers.cpp | 122 +++++++++++++++---------------- src/neuralnet.cpp | 18 ++--- src/tensor.cpp | 3 + 7 files changed, 129 insertions(+), 115 deletions(-) diff --git a/Applications/Classification/jni/main.cpp b/Applications/Classification/jni/main.cpp index dc21b15..95e972d 100644 --- a/Applications/Classification/jni/main.cpp +++ b/Applications/Classification/jni/main.cpp @@ -459,7 +459,7 @@ int main(int argc, char *argv[]) { while (true) { std::vector>> in, label; if (buf.getDatafromBuffer(BUF_TRAIN, in, label, MINI_BATCH, FEATURE_SIZE, 1, TOTAL_LABEL_SIZE)) { - NN.backwarding(Tensor(in), Tensor(label), i); + NN.backwarding(Tensors::Tensor(in), Tensors::Tensor(label), i); count++; progress = (((float)(count * MINI_BATCH)) / (TOTAL_LABEL_SIZE * TOTAL_TRAIN_DATA_SIZE)); int pos = barWidth * progress; @@ -489,9 +489,9 @@ int main(int argc, char *argv[]) { // if (buf.getDatafromBuffer(BUF_VAL, in, label, MINI_BATCH, FEATURE_SIZE, 1, TOTAL_LABEL_SIZE)) { if (buf.getDatafromBuffer(BUF_TRAIN, in, label, MINI_BATCH, FEATURE_SIZE, 1, TOTAL_LABEL_SIZE)) { for (int i = 0; i < MINI_BATCH; ++i) { - Tensor X = Tensor({in[i]}); - Tensor Y2 = Tensor({label[i]}); - Tensor Y = NN.forwarding(X, Y2); + Tensors::Tensor X = Tensors::Tensor({in[i]}); + Tensors::Tensor Y2 = Tensors::Tensor({label[i]}); + Tensors::Tensor Y = NN.forwarding(X, Y2); if (Y.argmax() == Y2.argmax()) right++; valloss += NN.getLoss(); @@ -527,7 +527,7 @@ int main(int argc, char *argv[]) { std::vector featureVector, resultVector; featureVector.resize(FEATURE_SIZE); getFeature(img, featureVector); - Tensor X = Tensor({featureVector}); + Tensors::Tensor X = Tensors::Tensor({featureVector}); cout << NN.forwarding(X).applyFunction(stepFunction) << endl; } /** diff --git a/include/layers.h b/include/layers.h index 49d8c47..9f4810c 100644 --- a/include/layers.h +++ b/include/layers.h @@ -133,14 +133,14 @@ class Layer { * @param[in] input Input Tensor taken by upper layer * @retval Output Tensor */ - virtual Tensor forwarding(Tensor input) = 0; + virtual Tensors::Tensor forwarding(Tensors::Tensor input) = 0; /** * @brief Forward Propation of neural Network * @param[in] input Input Tensor taken by upper layer * @retval Output Tensor */ - virtual Tensor forwarding(Tensor input, Tensor output) = 0; + virtual Tensors::Tensor forwarding(Tensors::Tensor input, Tensors::Tensor output) = 0; /** * @brief Back Propation of neural Network @@ -148,7 +148,7 @@ class Layer { * @param[in] iteration Epoch value for the ADAM Optimizer * @retval Output Tensor */ - virtual Tensor backwarding(Tensor input, int iteration) = 0; + virtual Tensors::Tensor backwarding(Tensors::Tensor input, int iteration) = 0; /** * @brief Initialize the layer @@ -203,13 +203,13 @@ class Layer { /** * @brief Input Tensor */ - Tensor Input; + Tensors::Tensor Input; /** * @brief Hidden Layer Tensor which store the * forwading result */ - Tensor hidden; + Tensors::Tensor hidden; /** * @brief Layer index @@ -294,7 +294,7 @@ class InputLayer : public Layer { * @param[in] iteration Epoch Number for ADAM * @retval */ - Tensor backwarding(Tensor input, int iteration) { return Input; }; + Tensors::Tensor backwarding(Tensors::Tensor input, int iteration) { return Input; }; /** * @brief foward propagation : return Input Tensor @@ -302,7 +302,7 @@ class InputLayer : public Layer { * @param[in] input input Tensor from lower layer. * @retval return Input Tensor */ - Tensor forwarding(Tensor input); + Tensors::Tensor forwarding(Tensors::Tensor input); /** * @brief foward propagation : return Input Tensor @@ -311,7 +311,7 @@ class InputLayer : public Layer { * @param[in] output label Tensor. * @retval return Input Tensor */ - Tensor forwarding(Tensor input, Tensor output) { return forwarding(input); }; + Tensors::Tensor forwarding(Tensors::Tensor input, Tensors::Tensor output) { return forwarding(input); }; /** * @brief Set Optimizer @@ -386,7 +386,7 @@ class FullyConnectedLayer : public Layer { * @param[in] input Input Tensor from upper layer * @retval Activation(W x input + B) */ - Tensor forwarding(Tensor input); + Tensors::Tensor forwarding(Tensors::Tensor input); /** * @brief foward propagation : return Input Tensor @@ -395,7 +395,7 @@ class FullyConnectedLayer : public Layer { * @param[in] output label Tensor. * @retval Activation(W x input + B) */ - Tensor forwarding(Tensor input, Tensor output) { return forwarding(input); }; + Tensors::Tensor forwarding(Tensors::Tensor input, Tensors::Tensor output) { return forwarding(input); }; /** * @brief back propagation @@ -404,7 +404,7 @@ class FullyConnectedLayer : public Layer { * @param[in] iteration Number of Epoch for ADAM * @retval dJdB x W Tensor */ - Tensor backwarding(Tensor input, int iteration); + Tensors::Tensor backwarding(Tensors::Tensor input, int iteration); /** * @brief set optimizer @@ -430,22 +430,22 @@ class FullyConnectedLayer : public Layer { void initialize(int b, int h, int w, int id, bool init_zero, weightIni_type wini); private: - Tensor Weight; - Tensor Bias; + Tensors::Tensor Weight; + Tensors::Tensor Bias; /** * @brief First Momentum Tensor for the ADAM */ - Tensor WM; + Tensors::Tensor WM; - Tensor BM; + Tensors::Tensor BM; /** * @brief Second Momentum Tensor for the ADAM */ - Tensor WV; + Tensors::Tensor WV; - Tensor BV; + Tensors::Tensor BV; }; /** @@ -481,7 +481,7 @@ class OutputLayer : public Layer { * @param[in] input Input Tensor from upper layer * @retval Activation(W x input + B) */ - Tensor forwarding(Tensor input); + Tensors::Tensor forwarding(Tensors::Tensor input); /** * @brief forward propagation with input and set loss @@ -489,7 +489,7 @@ class OutputLayer : public Layer { * @param[in] output Label Tensor * @retval Activation(W x input + B) */ - Tensor forwarding(Tensor input, Tensor output); + Tensors::Tensor forwarding(Tensors::Tensor input, Tensors::Tensor output); /** * @brief back propagation @@ -498,7 +498,7 @@ class OutputLayer : public Layer { * @param[in] iteration Number of Epoch for ADAM * @retval dJdB x W Tensor */ - Tensor backwarding(Tensor label, int iteration); + Tensors::Tensor backwarding(Tensors::Tensor label, int iteration); /** * @brief set optimizer @@ -535,12 +535,12 @@ class OutputLayer : public Layer { void copy(Layer *l); private: - Tensor Weight; - Tensor Bias; - Tensor WM; - Tensor WV; - Tensor BM; - Tensor BV; + Tensors::Tensor Weight; + Tensors::Tensor Bias; + Tensors::Tensor WM; + Tensors::Tensor WV; + Tensors::Tensor BM; + Tensors::Tensor BV; float loss; cost_type cost; }; @@ -578,7 +578,7 @@ class BatchNormalizationLayer : public Layer { * @param[in] input Input Tensor from upper layer * @retval Activation(W x input + B) */ - Tensor forwarding(Tensor input); + Tensors::Tensor forwarding(Tensors::Tensor input); /** * @brief foward propagation : return Input Tensor @@ -587,7 +587,7 @@ class BatchNormalizationLayer : public Layer { * @param[in] output label Tensor. * @retval Activation(W x input + B) */ - Tensor forwarding(Tensor input, Tensor output) { return forwarding(input); }; + Tensors::Tensor forwarding(Tensors::Tensor input, Tensors::Tensor output) { return forwarding(input); }; /** * @brief back propagation @@ -596,7 +596,7 @@ class BatchNormalizationLayer : public Layer { * @param[in] iteration Number of Epoch for ADAM * @retval dJdB x W Tensor */ - Tensor backwarding(Tensor input, int iteration); + Tensors::Tensor backwarding(Tensors::Tensor input, int iteration); /** * @brief set optimizer @@ -622,12 +622,12 @@ class BatchNormalizationLayer : public Layer { void initialize(int b, int h, int w, int id, bool init_zero, weightIni_type wini); private: - Tensor Weight; - Tensor Bias; - Tensor mu; - Tensor var; - Tensor gamma; - Tensor beta; + Tensors::Tensor Weight; + Tensors::Tensor Bias; + Tensors::Tensor mu; + Tensors::Tensor var; + Tensors::Tensor gamma; + Tensors::Tensor beta; float epsilon; }; } diff --git a/include/neuralnet.h b/include/neuralnet.h index f0dcf19..81dbc1c 100644 --- a/include/neuralnet.h +++ b/include/neuralnet.h @@ -121,7 +121,7 @@ class NeuralNetwork { * @param[in] input Input Tensor X * @retval Output Tensor Y */ - Tensor forwarding(Tensor input); + Tensors::Tensor forwarding(Tensors::Tensor input); /** * @brief forward propagation @@ -129,7 +129,7 @@ class NeuralNetwork { * @param[in] label Input Tensor Y2 * @retval Output Tensor Y */ - Tensor forwarding(Tensor input, Tensor output); + Tensors::Tensor forwarding(Tensors::Tensor input, Tensors::Tensor output); /** * @brief back propagation to update W & B @@ -137,7 +137,7 @@ class NeuralNetwork { * @param[in] expectedOutput Lable Tensor Y * @param[in] iteration Epoch Number for ADAM */ - void backwarding(Tensor input, Tensor expectedOutput, int iteration); + void backwarding(Tensors::Tensor input, Tensors::Tensor expectedOutput, int iteration); /** * @brief save W & B into file diff --git a/include/tensor.h b/include/tensor.h index 61a610a..c6cf050 100644 --- a/include/tensor.h +++ b/include/tensor.h @@ -37,10 +37,24 @@ extern "C" { #include /** + * @Namespace Namespace of Tensor + * @brief Namespace for Tensor + */ +namespace Tensors { + +typedef struct { + unsigned int batch; + unsigned int channel; + unsigned int height; + unsigned int width; +} TensorDim; + +/** * @class Tensor Class for Calculation * @brief Tensor Class for Calculation */ class Tensor { + public: /** * @brief Constructor of Tensor @@ -284,5 +298,6 @@ class Tensor { * @brief Overriding output stream */ std::ostream &operator<<(std::ostream &out, Tensor const &m); +} #endif diff --git a/src/layers.cpp b/src/layers.cpp index c5e2b01..6ebaac1 100644 --- a/src/layers.cpp +++ b/src/layers.cpp @@ -94,14 +94,14 @@ float ReluPrime(float x) { } } -Tensor softmaxPrime(Tensor x) { +Tensors::Tensor softmaxPrime(Tensors::Tensor x) { int batch = x.getBatch(); int width = x.getWidth(); int height = x.getHeight(); assert(height == 1); - Tensor PI = Tensor(batch, height, width); + Tensors::Tensor PI = Tensors::Tensor(batch, height, width); for (int k = 0; k < batch; ++k) { for (int i = 0; i < height; ++i) { @@ -121,12 +121,12 @@ Tensor softmaxPrime(Tensor x) { return PI; } -static Tensor WeightInitialization(unsigned int width, unsigned int height, Layers::weightIni_type init_type) { +static Tensors::Tensor WeightInitialization(unsigned int width, unsigned int height, Layers::weightIni_type init_type) { std::random_device rd; std::mt19937 gen(rd()); - Tensor W = Tensor(height, width); + Tensors::Tensor W = Tensors::Tensor(height, width); - if(init_type == Layers::WEIGHT_UNKNOWN) + if (init_type == Layers::WEIGHT_UNKNOWN) init_type = Layers::WEIGHT_XAVIER_NORMAL; switch (init_type) { case Layers::WEIGHT_LECUN_NORMAL: { @@ -187,7 +187,7 @@ static Tensor WeightInitialization(unsigned int width, unsigned int height, Laye namespace Layers { void Layer::setActivation(acti_type acti) { - if(acti == ACT_UNKNOWN){ + if (acti == ACT_UNKNOWN) { std::cout << "have to specify activation function" << std::endl; exit(0); } @@ -210,9 +210,7 @@ void Layer::setActivation(acti_type acti) { } } -void InputLayer::setOptimizer(Optimizer opt) { - this->opt = opt; -} +void InputLayer::setOptimizer(Optimizer opt) { this->opt = opt; } void InputLayer::copy(Layer *l) { InputLayer *from = static_cast(l); @@ -224,7 +222,7 @@ void InputLayer::copy(Layer *l) { this->hidden.copy(from->hidden); } -Tensor InputLayer::forwarding(Tensor input) { +Tensors::Tensor InputLayer::forwarding(Tensors::Tensor input) { Input = input; if (normalization) Input = Input.normalization(); @@ -247,7 +245,7 @@ void FullyConnectedLayer::initialize(int b, int h, int w, int id, bool init_zero this->init_zero = init_zero; this->bnfallow = false; - Bias = Tensor(1, w); + Bias = Tensors::Tensor(1, w); Weight = WeightInitialization(w, h, wini); if (init_zero) { @@ -260,18 +258,18 @@ void FullyConnectedLayer::initialize(int b, int h, int w, int id, bool init_zero void FullyConnectedLayer::setOptimizer(Optimizer opt) { this->opt = opt; if (opt.type == OPT_ADAM) { - WM = Tensor(height, width); - WV = Tensor(height, width); + WM = Tensors::Tensor(height, width); + WV = Tensors::Tensor(height, width); WM.setZero(); WV.setZero(); - BM = Tensor(1, width); - BV = Tensor(1, width); + BM = Tensors::Tensor(1, width); + BV = Tensors::Tensor(1, width); BM.setZero(); BV.setZero(); } } -Tensor FullyConnectedLayer::forwarding(Tensor input) { +Tensors::Tensor FullyConnectedLayer::forwarding(Tensors::Tensor input) { Input = input; hidden = Input.dot(Weight).add(Bias); @@ -304,18 +302,18 @@ void FullyConnectedLayer::copy(Layer *l) { this->Bias.copy(from->Bias); } -Tensor FullyConnectedLayer::backwarding(Tensor derivative, int iteration) { - Tensor dJdB; +Tensors::Tensor FullyConnectedLayer::backwarding(Tensors::Tensor derivative, int iteration) { + Tensors::Tensor dJdB; dJdB = derivative.multiply(hidden.applyFunction(activationPrime)); - Tensor dJdW = Input.transpose().dot(dJdB); + Tensors::Tensor dJdW = Input.transpose().dot(dJdB); if (opt.weight_decay.type == WEIGHT_DECAY_L2NORM) { dJdW = dJdW.add(Weight.multiply(opt.weight_decay.lambda)); } - Tensor ret = dJdB.dot(Weight.transpose()); + Tensors::Tensor ret = dJdB.dot(Weight.transpose()); float ll = opt.learning_rate; if (opt.decay_steps != -1) { @@ -356,7 +354,7 @@ void OutputLayer::initialize(int b, int h, int w, int id, bool init_zero, weight this->index = id; this->init_zero = init_zero; - Bias = Tensor(1, w); + Bias = Tensors::Tensor(1, w); this->cost = cost; this->bnfallow = false; @@ -372,7 +370,7 @@ void OutputLayer::initialize(int b, int h, int w, int id, bool init_zero, weight } } -Tensor OutputLayer::forwarding(Tensor input) { +Tensors::Tensor OutputLayer::forwarding(Tensors::Tensor input) { Input = input; hidden = input.dot(Weight).add(Bias); if (activation_type == ACT_SOFTMAX) { @@ -382,11 +380,11 @@ Tensor OutputLayer::forwarding(Tensor input) { } } -Tensor OutputLayer::forwarding(Tensor input, Tensor output) { +Tensors::Tensor OutputLayer::forwarding(Tensors::Tensor input, Tensors::Tensor output) { Input = input; hidden = input.dot(Weight).add(Bias); - Tensor Y2 = output; - Tensor Y = hidden; + Tensors::Tensor Y2 = output; + Tensors::Tensor Y = hidden; if (activation_type == ACT_SOFTMAX) { Y = Y.softmax(); @@ -398,14 +396,14 @@ Tensor OutputLayer::forwarding(Tensor input, Tensor output) { switch (cost) { case COST_CATEGORICAL: { - Tensor temp = ((Y2.multiply(-1.0).transpose().dot(Y.add(opt.epsilon).applyFunction(log_float))) - .subtract(Y2.multiply(-1.0).add(1.0).transpose().dot( - Y.multiply(-1.0).add(1.0).add(opt.epsilon).applyFunction(log_float)))); + Tensors::Tensor temp = ((Y2.multiply(-1.0).transpose().dot(Y.add(opt.epsilon).applyFunction(log_float))) + .subtract(Y2.multiply(-1.0).add(1.0).transpose().dot( + Y.multiply(-1.0).add(1.0).add(opt.epsilon).applyFunction(log_float)))); loss = (1.0 / Y.Mat2Vec().size()) * temp.Mat2Vec()[0]; } break; case COST_MSR: { - Tensor sub = Y2.subtract(Y); - Tensor l = (sub.multiply(sub)).sum().multiply(0.5); + Tensors::Tensor sub = Y2.subtract(Y); + Tensors::Tensor l = (sub.multiply(sub)).sum().multiply(0.5); std::vector t = l.Mat2Vec(); for (int i = 0; i < l.getBatch(); i++) { lossSum += t[i]; @@ -414,7 +412,7 @@ Tensor OutputLayer::forwarding(Tensor input, Tensor output) { loss = lossSum / (float)l.getBatch(); } break; case COST_ENTROPY: { - Tensor l; + Tensors::Tensor l; if (activation_type == ACT_SIGMOID) { l = (Y2.multiply(Y.applyFunction(log_float)) .add((Y2.multiply(-1.0).add(1.0)).multiply((Y.multiply(-1.0).add(1.0)).applyFunction(log_float)))) @@ -472,28 +470,28 @@ void OutputLayer::copy(Layer *l) { void OutputLayer::setOptimizer(Optimizer opt) { this->opt = opt; if (opt.type == OPT_ADAM) { - WM = Tensor(height, width); - WV = Tensor(height, width); + WM = Tensors::Tensor(height, width); + WV = Tensors::Tensor(height, width); WM.setZero(); WV.setZero(); - BM = Tensor(1, width); - BV = Tensor(1, width); + BM = Tensors::Tensor(1, width); + BV = Tensors::Tensor(1, width); BM.setZero(); BV.setZero(); } } -Tensor OutputLayer::backwarding(Tensor label, int iteration) { +Tensors::Tensor OutputLayer::backwarding(Tensors::Tensor label, int iteration) { float lossSum = 0.0; - Tensor Y2 = label; - Tensor Y; + Tensors::Tensor Y2 = label; + Tensors::Tensor Y; if (activation_type == ACT_SOFTMAX) Y = hidden.softmax(); else Y = hidden.applyFunction(activation); - Tensor ret; - Tensor dJdB; + Tensors::Tensor ret; + Tensors::Tensor dJdB; float ll = opt.learning_rate; if (opt.decay_steps != -1) { @@ -503,17 +501,17 @@ Tensor OutputLayer::backwarding(Tensor label, int iteration) { switch (cost) { case COST_CATEGORICAL: { dJdB = Y.subtract(Y2); - Tensor temp = ((Y2.multiply(-1.0).transpose().dot(Y.add(opt.epsilon).applyFunction(log_float))) - .subtract(Y2.multiply(-1.0).add(1.0).transpose().dot( - Y.multiply(-1.0).add(1.0).add(opt.epsilon).applyFunction(log_float)))); + Tensors::Tensor temp = ((Y2.multiply(-1.0).transpose().dot(Y.add(opt.epsilon).applyFunction(log_float))) + .subtract(Y2.multiply(-1.0).add(1.0).transpose().dot( + Y.multiply(-1.0).add(1.0).add(opt.epsilon).applyFunction(log_float)))); loss = (1.0 / Y.Mat2Vec().size()) * temp.Mat2Vec()[0]; if (opt.weight_decay.type == WEIGHT_DECAY_L2NORM) { loss += opt.weight_decay.lambda * 0.5 * (Weight.l2norm()); } } break; case COST_MSR: { - Tensor sub = Y2.subtract(Y); - Tensor l = (sub.multiply(sub)).sum().multiply(0.5); + Tensors::Tensor sub = Y2.subtract(Y); + Tensors::Tensor l = (sub.multiply(sub)).sum().multiply(0.5); std::vector t = l.Mat2Vec(); for (int i = 0; i < l.getBatch(); i++) { lossSum += t[i]; @@ -530,7 +528,7 @@ Tensor OutputLayer::backwarding(Tensor label, int iteration) { } } break; case COST_ENTROPY: { - Tensor l; + Tensors::Tensor l; if (activation_type == ACT_SIGMOID) { dJdB = Y.subtract(Y2).multiply(1.0 / Y.getWidth()); l = (Y2.multiply(Y.applyFunction(log_float)) @@ -562,7 +560,7 @@ Tensor OutputLayer::backwarding(Tensor label, int iteration) { break; } - Tensor dJdW = Input.transpose().dot(dJdB); + Tensors::Tensor dJdW = Input.transpose().dot(dJdB); if (opt.weight_decay.type == WEIGHT_DECAY_L2NORM) { dJdW = dJdW.add(Weight.multiply(opt.weight_decay.lambda)); @@ -604,18 +602,16 @@ void BatchNormalizationLayer::initialize(int b, int h, int w, int id, bool init_ this->index = id; this->init_zero = init_zero; - this->gamma = Tensor(batch, w); - this->beta = Tensor(batch, w); + this->gamma = Tensors::Tensor(batch, w); + this->beta = Tensors::Tensor(batch, w); beta.setZero(); gamma.setZero(); } -void BatchNormalizationLayer::setOptimizer(Optimizer opt) { - this->opt = opt; -} +void BatchNormalizationLayer::setOptimizer(Optimizer opt) { this->opt = opt; } -Tensor BatchNormalizationLayer::forwarding(Tensor input) { - Tensor temp; +Tensors::Tensor BatchNormalizationLayer::forwarding(Tensors::Tensor input) { + Tensors::Tensor temp; hidden = input; @@ -625,28 +621,28 @@ Tensor BatchNormalizationLayer::forwarding(Tensor input) { var = temp.multiply(temp).sum(0).multiply(1.0 / batch); - Tensor hath = temp.divide(var.add(0.001).applyFunction(sqrt_float)); + Tensors::Tensor hath = temp.divide(var.add(0.001).applyFunction(sqrt_float)); hidden = hath; - Tensor ret = hath.multiply(gamma).add(beta).applyFunction(activation); + Tensors::Tensor ret = hath.multiply(gamma).add(beta).applyFunction(activation); return ret; } -Tensor BatchNormalizationLayer::backwarding(Tensor derivative, int iteration) { - Tensor dbeta; - Tensor dgamma; - Tensor hath = hidden; - Tensor dy = derivative.multiply(hath.multiply(gamma).add(beta).applyFunction(activationPrime)); +Tensors::Tensor BatchNormalizationLayer::backwarding(Tensors::Tensor derivative, int iteration) { + Tensors::Tensor dbeta; + Tensors::Tensor dgamma; + Tensors::Tensor hath = hidden; + Tensors::Tensor dy = derivative.multiply(hath.multiply(gamma).add(beta).applyFunction(activationPrime)); dbeta = dy.sum(0); dgamma = (Input.subtract(mu).divide(var.add(0.001).applyFunction(sqrt_float)).multiply(dy).sum(0)); - Tensor Temp = + Tensors::Tensor Temp = (dy.multiply(batch).subtract(dy.sum(0))) .subtract(Input.subtract(mu).divide(var.add(0.001)).multiply(dy.multiply(Input.subtract(mu)).sum(0))); - Tensor dh = Temp.multiply(1.0 / batch).multiply(var.add(0.001).applyFunction(sqrt_float)).multiply(gamma); + Tensors::Tensor dh = Temp.multiply(1.0 / batch).multiply(var.add(0.001).applyFunction(sqrt_float)).multiply(gamma); float ll = opt.learning_rate; if (opt.decay_steps != -1) { diff --git a/src/neuralnet.cpp b/src/neuralnet.cpp index 7137fb1..b8718bb 100644 --- a/src/neuralnet.cpp +++ b/src/neuralnet.cpp @@ -363,8 +363,8 @@ void NeuralNetwork::finalize() { /** * @brief forward propagation using layers object which has layer */ -Tensor NeuralNetwork::forwarding(Tensor input) { - Tensor X = input; +Tensors::Tensor NeuralNetwork::forwarding(Tensors::Tensor input) { + Tensors::Tensor X = input; for (unsigned int i = 0; i < layers.size(); i++) { X = layers[i]->forwarding(X); } @@ -374,9 +374,9 @@ Tensor NeuralNetwork::forwarding(Tensor input) { /** * @brief forward propagation using layers object which has layer */ -Tensor NeuralNetwork::forwarding(Tensor input, Tensor output) { - Tensor X = input; - Tensor Y2 = output; +Tensors::Tensor NeuralNetwork::forwarding(Tensors::Tensor input, Tensors::Tensor output) { + Tensors::Tensor X = input; + Tensors::Tensor Y2 = output; for (unsigned int i = 0; i < layers.size(); i++) { X = layers[i]->forwarding(X, Y2); } @@ -388,10 +388,10 @@ Tensor NeuralNetwork::forwarding(Tensor input, Tensor output) { * Call backwarding function of layer in reverse order * No need to call at first Input Layer (No data to be updated) */ -void NeuralNetwork::backwarding(Tensor input, Tensor expected_output, int iteration) { - Tensor Y2 = expected_output; - Tensor X = input; - Tensor Y = forwarding(X); +void NeuralNetwork::backwarding(Tensors::Tensor input, Tensors::Tensor expected_output, int iteration) { + Tensors::Tensor Y2 = expected_output; + Tensors::Tensor X = input; + Tensors::Tensor Y = forwarding(X); for (unsigned int i = layers.size() - 1; i > 0; i--) { Y2 = layers[i]->backwarding(Y2, iteration); diff --git a/src/tensor.cpp b/src/tensor.cpp index afb5ee1..56362d9 100644 --- a/src/tensor.cpp +++ b/src/tensor.cpp @@ -32,6 +32,8 @@ #include #endif +namespace Tensors{ + Tensor::Tensor(int height, int width) { this->height = height; this->width = width; @@ -711,3 +713,4 @@ Tensor Tensor::standardization() const { return result; } +} -- 2.7.4