本文整理汇总了C++中Net::InitWeights方法的典型用法代码示例。如果您正苦于以下问题:C++ Net::InitWeights方法的具体用法?C++ Net::InitWeights怎么用?C++ Net::InitWeights使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Net
的用法示例。
在下文中一共展示了Net::InitWeights方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: mexFunction
void mexFunction(int nLhs, mxArray* pLhs[], int nRhs, const mxArray* pRhs[]) {
mexAssert(nRhs == NARGIN, "Number of input arguments in wrong!");
mexAssert(nLhs == NARGOUT, "Number of output arguments is wrong!" );
Net net;
net.InitLayers(IN_L);
net.InitParams(IN_P);
net.InitWeights(NULL);
net.GetWeights(OUT_W);
}
示例2: mexFunction
void mexFunction(int nLhs, mxArray* pLhs[], int nRhs, const mxArray* pRhs[]) {
mexAssert(nRhs == NARGIN && nLhs == NARGOUT,
"Number of input and/or output arguments is not correct!" );
Net net;
net.InitLayers(IN_L);
net.InitWeights(IN_W);
net.InitParams(IN_P);
net.Classify(IN_X, OUT_Y);
}
示例3: mexFunction
void mexFunction(int nLhs, mxArray* pLhs[], int nRhs, const mxArray* pRhs[]) {
mexAssert(nRhs == NARGIN, "Number of input arguments in wrong!");
mexAssert(nLhs == NARGOUT, "Number of output arguments is wrong!" );
size_t seed = (size_t) mexGetScalar(IN_S);
Net net;
net.InitRand(seed);
net.InitLayers(IN_L);
net.InitWeights(NULL);
net.GetWeights(OUT_W);
}
示例4: mexFunction
void mexFunction(int nLhs, mxArray* pLhs[], int nRhs, const mxArray* pRhs[]) {
mexAssert(NARGIN_MIN <= nRhs && nRhs <= NARGIN_MAX, "Number of input arguments in wrong!");
mexAssert(nLhs == NARGOUT, "Number of output arguments is wrong!" );
mexAssert(mexIsCell(IN_L), "Layers must be the cell array");
mexAssert(mexGetNumel(IN_L) == 2, "Layers array must contain 2 cells");
mexAssert(mexIsCell(IN_W), "Weights must be the cell array");
mexAssert(mexGetNumel(IN_W) == 2, "Weights array must contain 2 cells");
Net net;
mxArray *mx_weights;
net.InitLayers(mexGetCell(IN_L, 1));
net.InitWeights(mexGetCell(IN_W, 1), mx_weights);
net.InitParams(IN_P);
net.ReadLabels(IN_Y);
const mxArray *mx_imweights = mexGetCell(IN_W, 0);
size_t train_num = net.labels_.size1();
mexAssert(train_num == mexGetNumel(mx_imweights),
"Weights and labels number must coincide");
bool is_multicoords = false;
if (mexIsCell(IN_X)) {
mexAssert(train_num == mexGetNumel(IN_X),
"Coordinates and labels number must coincide");
is_multicoords = true;
}
Params params_ = net.params_;
size_t numbatches = (size_t) ceil((ftype) train_num/params_.batchsize_);
Mat trainerror_(params_.numepochs_, numbatches);
Mat trainerror2_(params_.numepochs_, numbatches);
trainerror2_.assign(0);
std::vector<Net> imnets;
imnets.resize(params_.batchsize_);
for (size_t i = 0; i < params_.batchsize_; ++i) {
imnets[i].InitLayers(mexGetCell(IN_L, 0));
if (!is_multicoords) {
imnets[i].ReadData(IN_X);
} else {
imnets[i].ReadData(mexGetCell(IN_X, i)); // just to get pixels_num
}
}
size_t pixels_num = imnets[0].data_.size1();
Layer *firstlayer = net.layers_[0];
size_t dimens_num = firstlayer->outputmaps_;
mexAssert(imnets[0].layers_.back()->length_ == dimens_num,
"Final layer length must coincide with the number of outputmaps");
mexAssert(pixels_num == firstlayer->mapsize_[0] * firstlayer->mapsize_[1],
"Pixels number must coincide with the first layer elements number");
std::vector<size_t> pred_size(2);
pred_size[0] = 1; pred_size[1] = pixels_num * dimens_num;
Mat images_mat, labels_batch, pred_batch, pred_pixels;
std::vector< std::vector<Mat> > images, images_der;
for (size_t epoch = 0; epoch < params_.numepochs_; ++epoch) {
std::vector<size_t> randind(train_num);
for (size_t i = 0; i < train_num; ++i) {
randind[i] = i;
}
if (params_.shuffle_) {
std::random_shuffle(randind.begin(), randind.end());
}
std::vector<size_t>::const_iterator iter = randind.begin();
for (size_t batch = 0; batch < numbatches; ++batch) {
size_t batchsize = std::min(params_.batchsize_, (size_t)(randind.end() - iter));
std::vector<size_t> batch_ind = std::vector<size_t>(iter, iter + batchsize);
iter = iter + batchsize;
labels_batch = SubMat(net.labels_, batch_ind, 1);
net.UpdateWeights(epoch, false);
images_mat.resize(batchsize, pred_size[1]);
InitMaps(images_mat, pred_size, images);
// first pass
for (size_t m = 0; m < batchsize; ++m) {
imnets[m].InitWeights(mexGetCell(mx_imweights, batch_ind[m]));
if (is_multicoords) {
imnets[m].ReadData(mexGetCell(IN_X, batch_ind[m]));
}
imnets[m].InitActiv(imnets[m].data_);
imnets[m].Forward(pred_pixels, 1);
images[m][0].copy(Trans(pred_pixels).reshape(pred_size[0], pred_size[1]));
}
net.InitActiv(images_mat);
net.Forward(pred_batch, 1);
/*
for (int i = 0; i < 5; ++i) {
mexPrintMsg("pred_batch1", pred_batch(0, i));
}*/
// second pass
net.InitDeriv(labels_batch, trainerror_(epoch, batch));
net.Backward();
net.CalcWeights();
InitMaps(firstlayer->deriv_mat_, pred_size, images_der);
for (size_t m = 0; m < batchsize; ++m) {
imnets[m].layers_.back()->deriv_mat_ = Trans(images_der[m][0].reshape(dimens_num, pixels_num));
imnets[m].Backward();
}
// third pass
ftype loss2 = 0, curloss = 0, invind = 0;
//.........这里部分代码省略.........