while (true) {
std::vector<std::vector<std::vector<float>>> 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;
// 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();
std::vector<float> 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;
}
/**
* @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
* @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
/**
* @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
* @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
* @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
* @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
* @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
* @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
* @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
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;
};
/**
* @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
* @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
* @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
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;
};
* @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
* @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
* @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
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;
};
}
* @param[in] input Input Tensor X
* @retval Output Tensor Y
*/
- Tensor forwarding(Tensor input);
+ Tensors::Tensor forwarding(Tensors::Tensor input);
/**
* @brief forward propagation
* @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
* @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
#include <vector>
/**
+ * @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
* @brief Overriding output stream
*/
std::ostream &operator<<(std::ostream &out, Tensor const &m);
+}
#endif
}
}
-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) {
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: {
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);
}
}
}
-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<InputLayer *>(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();
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) {
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);
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) {
this->index = id;
this->init_zero = init_zero;
- Bias = Tensor(1, w);
+ Bias = Tensors::Tensor(1, w);
this->cost = cost;
this->bnfallow = false;
}
}
-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) {
}
}
-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();
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<float> t = l.Mat2Vec();
for (int i = 0; i < l.getBatch(); i++) {
lossSum += t[i];
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))))
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) {
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<float> t = l.Mat2Vec();
for (int i = 0; i < l.getBatch(); i++) {
lossSum += t[i];
}
} 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))
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));
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;
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) {
/**
* @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);
}
/**
* @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);
}
* 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);
#include <helper_cuda.h>
#endif
+namespace Tensors{
+
Tensor::Tensor(int height, int width) {
this->height = height;
this->width = width;
return result;
}
+}