本文整理汇总了Java中org.nd4j.linalg.ops.transforms.Transforms.pow方法的典型用法代码示例。如果您正苦于以下问题:Java Transforms.pow方法的具体用法?Java Transforms.pow怎么用?Java Transforms.pow使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.nd4j.linalg.ops.transforms.Transforms
的用法示例。
在下文中一共展示了Transforms.pow方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: lexicalSubstituteMult
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
/**
* Lexical Substitute task by Multiple Method
* @param word target word
* @param contexts list of given contexts
* @param average average the context vectors of given contexts
* @param top number of results return
* @return a list of {@link Pair}
*/
public List<Pair<String, Double>> lexicalSubstituteMult (String word, List<String> contexts, boolean average, Integer top) {
top = MoreObjects.firstNonNull(top, 10);
INDArray targetVec = getWordVector(word);
INDArray scores = wordPosSimilarity(targetVec);
for (String context : contexts) {
if (hasContext(context)) {
INDArray multScores = wordPosSimilarity(getContextVector(context));
if (average) multScores = Transforms.pow(multScores, 1.0 / contexts.size());
scores.muli(multScores);
}
}
List<Pair<String, Double>> list = new ArrayList<>(wordVocab.size());
for (int i = 0; i < wordVocab.size(); i++) { list.add(new Pair<>(wordVocab.get(i), scores.getDouble(i))); }
return list.stream().sorted((e1, e2) -> Double.valueOf(e2.getValue()).compareTo(Double.valueOf(e1.getValue()))).limit(top).collect(Collectors.toCollection(LinkedList::new));
}
示例2: reducedBasis
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
/**
* Return a reduced basis set that covers a certain fraction of the variance of the data
* @param variance The desired fractional variance (0 to 1), it will always be greater than the value.
* @return The basis vectors as columns, size <i>N</i> rows by <i>ndims</i> columns, where <i>ndims</i> is less than or equal to <i>N</i>
*/
public INDArray reducedBasis(double variance) {
INDArray vars = Transforms.pow(eigenvalues, -0.5, true);
double res = vars.sumNumber().doubleValue();
double total = 0.0;
int ndims = 0;
for (int i = 0; i < vars.columns(); i++) {
ndims++;
total += vars.getDouble(i);
if (total / res > variance)
break;
}
INDArray result = Nd4j.create(eigenvectors.rows(), ndims);
for (int i = 0; i < ndims; i++)
result.putColumn(i, eigenvectors.getColumn(i));
return result;
}
示例3: pca2
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
/**
* This method performs a dimensionality reduction, including principal components
* that cover a fraction of the total variance of the system. It does all calculations
* about the mean.
* @param in A matrix of datapoints as rows, where column are features with fixed number N
* @param variance The desired fraction of the total variance required
* @return The reduced basis set
*/
public static INDArray pca2(INDArray in, double variance) {
// let's calculate the covariance and the mean
INDArray[] covmean = covarianceMatrix(in);
// use the covariance matrix (inverse) to find "force constants" and then break into orthonormal
// unit vector components
INDArray[] pce = principalComponents(covmean[0]);
// calculate the variance of each component
INDArray vars = Transforms.pow(pce[1], -0.5, true);
double res = vars.sumNumber().doubleValue();
double total = 0.0;
int ndims = 0;
for (int i = 0; i < vars.columns(); i++) {
ndims++;
total += vars.getDouble(i);
if (total / res > variance)
break;
}
INDArray result = Nd4j.create(in.columns(), ndims);
for (int i = 0; i < ndims; i++)
result.putColumn(i, pce[0].getColumn(i));
return result;
}
示例4: vcovUpdate
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
private INDArray vcovUpdate(final INDArray var, final INDArray w)
{
INDArray fft = Transforms.pow(w, 2);
INDArray psi = fft.sum(0);
for (int i = 0; i < psi.columns(); i++)
{
psi
.getColumn(i)
.assign(max(var.getDouble(i) - psi.getDouble(i), PSEUDO_COUNT));
}
return Nd4j.diag(psi);
}
示例5: testPow1
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
@Test
public void testPow1() throws Exception {
val argX = Nd4j.create(3).assign(2.0);
val argY = Nd4j.create(new double[]{1.0, 2.0, 3.0});
val exp = Nd4j.create(new double[] {2.0, 4.0, 8.0});
val res = Transforms.pow(argX, argY);
assertEquals(exp, res);
}
示例6: estimateVariance
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
/**
* Estimate the variance of a single record with reduced # of dimensions.
* @param data A single record with the same <i>N</i> features as the constructing data set
* @param ndims The number of dimensions to include in calculation
* @return The fraction (0 to 1) of the total variance covered by the <i>ndims</i> basis set.
*/
public double estimateVariance(INDArray data, int ndims) {
INDArray dx = data.sub(mean);
INDArray v = eigenvectors.transpose().mmul(dx.reshape(dx.columns(), 1));
INDArray t2 = Transforms.pow(v, 2);
double fraction = t2.get(NDArrayIndex.interval(0, ndims)).sumNumber().doubleValue();
double total = t2.sumNumber().doubleValue();
return fraction / total;
}
示例7: generateGaussianSamples
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
/**
* Generates a set of <i>count</i> random samples with the same variance and mean and eigenvector/values
* as the data set used to initialize the PCA object, with same number of features <i>N</i>.
* @param count The number of samples to generate
* @return A matrix of size <i>count</i> rows by <i>N</i> columns
*/
public INDArray generateGaussianSamples(int count) {
INDArray samples = Nd4j.randn(count, eigenvalues.columns());
INDArray factors = Transforms.pow(eigenvalues, -0.5, true);
samples.muliRowVector(factors);
return Nd4j.tensorMmul(eigenvectors, samples, new int[][] {{1}, {1}}).transposei().addiRowVector(mean);
}
示例8: apply
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
@Override
public INDArray apply(INDArray input) {
return Transforms.pow(input.dup(),scaleBy);
}
示例9: add
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
/**
* Add rows of data to the statistics
*
* @param data the matrix containing multiple rows of data to include
* @param mask (optionally) the mask of the data, useful for e.g. time series
*/
public Builder add(@NonNull INDArray data, INDArray mask) {
data = DataSetUtil.tailor2d(data, mask);
// Using https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Parallel_algorithm
if (data == null) {
// Nothing to add. Either data is empty or completely masked. Just skip it, otherwise we will get
// null pointer exceptions.
return this;
}
INDArray mean = data.mean(0);
INDArray variance = data.var(false, 0);
int count = data.size(0);
if (runningMean == null) {
// First batch
runningMean = mean;
runningVariance = variance;
runningCount = count;
if (data.size(0) == 1) {
//Handle edge case: currently, reduction ops may return the same array
//But we don't want to modify this array in-place later
runningMean = runningMean.dup();
runningVariance = runningVariance.dup();
}
} else {
// Update running variance
INDArray deltaSquared = Transforms.pow(mean.subRowVector(runningMean), 2);
INDArray mB = variance.muli(count);
runningVariance.muli(runningCount).addiRowVector(mB)
.addiRowVector(deltaSquared
.muli((float) (runningCount * count) / (runningCount + count)))
.divi(runningCount + count);
// Update running count
runningCount += count;
// Update running mean
INDArray xMinusMean = data.subRowVector(runningMean);
runningMean.addi(xMinusMean.sum(0).divi(runningCount));
}
return this;
}
示例10: fit
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
/**
* Fit the given model
* @param iterator the data to iterate oer
*/
public void fit(DataSetIterator iterator) {
while (iterator.hasNext()) {
DataSet next = iterator.next();
runningTotal += next.numExamples();
batchCount = next.getFeatures().size(0);
if (mean == null) {
//start with the mean and std of zero
//column wise
mean = next.getFeatureMatrix().mean(0);
std = (batchCount == 1) ? Nd4j.zeros(mean.shape()) : Transforms.pow(next.getFeatureMatrix().std(0), 2);
std.muli(batchCount);
} else {
// m_newM = m_oldM + (x - m_oldM)/m_n;
// This only works if batch size is 1, m_newS = m_oldS + (x - m_oldM)*(x - m_newM);
INDArray xMinusMean = next.getFeatureMatrix().subRowVector(mean);
INDArray newMean = mean.add(xMinusMean.sum(0).divi(runningTotal));
// Using http://i.stanford.edu/pub/cstr/reports/cs/tr/79/773/CS-TR-79-773.pdf
// for a version of calc variance when dataset is partitioned into two sample sets
// Also described in https://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#Parallel_algorithm
// delta = mean_B - mean_A; A is data seen so far, B is the current batch
// M2 is the var*n
// M2 = M2_A + M2_B + delta^2 * nA * nB/(nA+nB)
INDArray meanB = next.getFeatureMatrix().mean(0);
INDArray deltaSq = Transforms.pow(meanB.subRowVector(mean), 2);
INDArray deltaSqScaled =
deltaSq.mul(((float) runningTotal - batchCount) * batchCount / (float) runningTotal);
INDArray mtwoB = Transforms.pow(next.getFeatureMatrix().std(0), 2);
mtwoB.muli(batchCount);
std = std.add(mtwoB);
std = std.add(deltaSqScaled);
mean = newMean;
}
}
std.divi(runningTotal);
std = Transforms.sqrt(std);
std.addi(Nd4j.scalar(Nd4j.EPS_THRESHOLD));
if (std.min(1) == Nd4j.scalar(Nd4j.EPS_THRESHOLD))
logger.info("API_INFO: Std deviation found to be zero. Transform will round upto epsilon to avoid nans.");
iterator.reset();
}