本文整理汇总了C++中array::dims方法的典型用法代码示例。如果您正苦于以下问题:C++ array::dims方法的具体用法?C++ array::dims怎么用?C++ array::dims使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类array
的用法示例。
在下文中一共展示了array::dims方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: bagging
array bagging(array &train_feats, array &test_feats, array &train_labels,
int num_classes, int num_models, int sample_size)
{
int num_train = train_feats.dims(0);
int num_test = test_feats.dims(0);
array idx = floor(randu(sample_size, num_models) * num_train);
array labels_all = constant(0, num_test, num_classes);
array off = seq(num_test);
for (int i = 0; i < num_models; i++) {
array ii = idx(span, i);
array train_feats_ii = lookup(train_feats, ii, 0);
array train_labels_ii = train_labels(ii);
// Get the predicted results
array labels_ii = knn(train_feats_ii, test_feats, train_labels_ii);
array lidx = labels_ii * num_test + off;
labels_all(lidx) = labels_all(lidx) + 1;
}
array val, labels;
max(val, labels, labels_all, 1);
return labels;
}
示例2: train
array train(const array &X, const array &Y, double alpha = 0.1,
double lambda = 1.0, double maxerr = 0.01, int maxiter = 1000,
bool verbose = false) {
// Initialize parameters to 0
array Weights = constant(0, X.dims(1), Y.dims(1));
array J, dJ;
float err = 0;
for (int i = 0; i < maxiter; i++) {
// Get the cost and gradient
cost(J, dJ, Weights, X, Y, lambda);
err = max<float>(abs(J));
if (err < maxerr) {
printf("Iteration %4d Err: %.4f\n", i + 1, err);
printf("Training converged\n");
return Weights;
}
if (verbose && ((i + 1) % 10 == 0)) {
printf("Iteration %4d Err: %.4f\n", i + 1, err);
}
// Update the parameters via gradient descent
Weights = Weights - alpha * dJ;
}
printf("Training stopped after %d iterations\n", maxiter);
return Weights;
}
示例3: train
array train(const array &X, const array &Y,
double alpha = 0.1,
double maxerr = 0.05,
int maxiter = 1000, bool verbose = false)
{
// Initialize parameters to 0
array Weights = constant(0, X.dims(1), Y.dims(1));
for (int i = 0; i < maxiter; i++) {
array P = predict(X, Weights);
array err = Y - P;
float mean_abs_err = mean<float>(abs(err));
if (mean_abs_err < maxerr) break;
if (verbose && (i + 1) % 25 == 0) {
printf("Iter: %d, Err: %.4f\n", i + 1, mean_abs_err);
}
Weights = Weights + alpha * matmulTN(X, err);
}
return Weights;
}
示例4: cost
void cost(array &J, array &dJ, const array &Weights,
const array &X, const array &Y, double lambda = 1.0)
{
// Number of samples
int m = Y.dims(0);
// Make the lambda corresponding to Weights(0) == 0
array lambdat = constant(lambda, Weights.dims());
// No regularization for bias weights
lambdat(0, span) = 0;
// Get the prediction
array H = predict(X, Weights);
// Cost of misprediction
array Jerr = -sum(Y * log(H) + (1 - Y) * log(1 - H));
// Regularization cost
array Jreg = 0.5 * sum(lambdat * Weights * Weights);
// Total cost
J = (Jerr + Jreg) / m;
// Find the gradient of cost
array D = (H - Y);
dJ = (matmulTN(X, D) + lambdat * Weights) / m;
}
示例5: distance
// Calculate all the distances from testing set to training set
array distance(array train, array test)
{
const int feat_len = train.dims(1);
const int num_train = train.dims(0);
const int num_test = test.dims(0);
array dist = constant(0, num_train, num_test);
// Iterate over each attribute
for (int ii = 0; ii < feat_len; ii++) {
// Get a attribute vectors
array train_i = train(span, ii);
array test_i = test (span, ii).T();
// Tile the vectors to generate matrices
array train_tiled = tile(train_i, 1, num_test);
array test_tiled = tile( test_i, num_train, 1 );
// Add the distance for this attribute
dist = dist + abs(train_tiled - test_tiled);
dist.eval(); // Necessary to free up train_i, test_i
}
return dist;
}
示例6: alphaBlend
/**
* a - foregound image
* b - background image
* mask - mask map
* */
array alphaBlend(const array &a, const array &b, const array &mask)
{
array tiledMask;
if (mask.dims(2) != a.dims(2))
tiledMask = tile(mask, 1, 1, a.dims(2));
return a*tiledMask + (1.0f - tiledMask)*b;
}
示例7: matmul
array matmul(const array &a, const array &b, const array &c) {
int tmp1 = a.dims(0) * b.dims(1);
int tmp2 = b.dims(0) * c.dims(1);
if (tmp1 < tmp2) {
return matmul(matmul(a, b), c);
} else {
return matmul(a, matmul(b, c));
}
}
示例8: border
array border(const array& img, const int left, const int right,
const int top, const int bottom,
const float value = 0.0)
{
if((int)img.dims(0) < (top + bottom))
std::cerr << "input does not have enough rows" << std::endl;
if((int)img.dims(1) < (left + right))
std::cerr << "input does not have enough columns" << std::endl;
dim4 imgDims = img.dims();
array ret = constant(value, imgDims);
ret(seq(top, imgDims[0]-bottom), seq(left, imgDims[1]-right), span, span) =
img(seq(top, imgDims[0]-bottom), seq(left, imgDims[1]-right), span, span);
return ret;
}
示例9: blur
array blur(const array& img, const array mask = gaussianKernel(3,3))
{
array blurred = array(img.dims(), img.type());
for(int i = 0; i < (int)blurred.dims(2); i++)
blurred(span, span, i) = convolve(img(span, span, i), mask);
return blurred;
}
示例10: max
void max(array &val, array &idx, const array &in, const int dim)
{
af_array out = 0;
af_array loc = 0;
AF_THROW(af_imax(&out, &loc, in.get(), getFNSD(dim, in.dims())));
val = array(out);
idx = array(loc);
}
示例11: medianfilter
array medianfilter(const array &in, int window_width, int window_height)
{
array ret_val(in.dims());
ret_val(span, span, 0) = medfilt(in(span, span, 0), window_width, window_height);
ret_val(span, span, 1) = medfilt(in(span, span, 1), window_width, window_height);
ret_val(span, span, 2) = medfilt(in(span, span, 2), window_width, window_height);
return ret_val;
}
示例12: pick
/**
* randomization - controls % of total number of pixels in the image
* that will be effected by random noise
* repeat - # of times the process is carried out on the previous steps output
*/
array pick(const array &in, int randomization, int repeat)
{
int w = in.dims(0);
int h = in.dims(1);
float f = randomization / 100.0f;
int dim = (int)(f*w*h);
array ret_val = in.copy();
for (int i = 0; i<repeat; ++i) {
array idxs = (w*h) * randu(dim);
array rnd = getRandomNeighbor(ret_val, 1, 1);
array temp_src = moddims(rnd, w*h, 3);
array temp_dst = moddims(ret_val, w*h, 3);
temp_dst(idxs, span) = temp_src(idxs, span);
ret_val = moddims(temp_dst, in.dims());
}
return ret_val;
}
示例13: getRandomNeighbor
/**
* Retrieve a new image of same dimensions of the original image
* where each original image's pixel is replaced by randomly picked
* neighbor in the provided local neighborhood window
*/
array getRandomNeighbor(const array &in, int windW, int windH)
{
array rnd = 2.0f*randu(in.dims(0), in.dims(1)) - 1.0f;
array sx = seq(in.dims(0));
array sy = seq(in.dims(1));
array vx = tile(sx, 1, in.dims(1)) + floor(rnd*windW);
array vy = tile(sy.T(), in.dims(0), 1) + floor(rnd*windH);
array vxx = clamp(vx, 0, in.dims(0));
array vyy = clamp(vy, 0, in.dims(1));
array in2 = moddims(in, vx.elements(), 3);
return moddims(in2(vyy*in.dims(0) + vxx, span), in.dims());
}
示例14: hurl
/**
* randomization - controls % of total number of pixels in the image
* that will be effected by random noise
* repeat - # of times the process is carried out on the previous steps output
*/
array hurl(const array &in, int randomization, int repeat)
{
int w = in.dims(0);
int h = in.dims(1);
float f = randomization / 100.0f;
int dim = (int)(f*w*h);
array ret_val = in.copy();
array temp = moddims(ret_val, w*h, 3);
for (int i = 0; i<repeat; ++i) {
array idxs = (w*h) * randu(dim);
array rndR = 255.0f * randu(dim);
array rndG = 255.0f * randu(dim);
array rndB = 255.0f * randu(dim);
temp(idxs, 0) = rndR;
temp(idxs, 1) = rndG;
temp(idxs, 2) = rndB;
}
ret_val = moddims(temp, in.dims());
return ret_val;
}
示例15: segment_volume
array segment_volume(array A, int k) {
array I1 = A(span, span, k);
float mx = max<float>(I1);
float mn = min<float>(I1);
float u0 = 0.9 * mx;
float s0 = (mx - mn) / 2;
float u1 = 1.1 * mn;
float s1 = (mx - mn) / 2;
array L0 = gauss(I1, u0, s0);
array L11 = gauss(I1, u1, s1);
array L10;
array L12;
static array kernel = constant(1, 3, 3) / 9;
static array L11_old;
static array L12_old;
if (k == 0) {
L11 = convolve(L11, kernel);
L10 = L11;
} else {
L10 = L11_old;
L11 = L12_old;
}
if (k < A.dims(2) - 1) {
L12 = gauss(A(span, span, k + 1), u1, s1);
L12 = convolve(L12, kernel);
} else {
L12 = L11;
}
L11_old = L11;
L12_old = L12;
array L1 = (L10 + L11 + L12) / 3;
array S = (L0 > L1);
return S.as(A.type());
}