本文整理汇总了Java中org.nd4j.linalg.api.ndarray.INDArray.put方法的典型用法代码示例。如果您正苦于以下问题:Java INDArray.put方法的具体用法?Java INDArray.put怎么用?Java INDArray.put使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.nd4j.linalg.api.ndarray.INDArray
的用法示例。
在下文中一共展示了INDArray.put方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: predict
import org.nd4j.linalg.api.ndarray.INDArray; //导入方法依赖的package包/类
public INDArray predict(INDArray x) {
INDArray y = output(x); // activate input data through learned networks
INDArray out = Nd4j.create(new double[x.rows() * nOut], new int[] { x.rows(), nOut });
for (int i = 0; i < x.rows(); i++) {
int argmax = -1;
double max = Double.MIN_VALUE;
for (int j = 0; j < nOut; j++) {
if (max < y.getDouble(i, j)) {
argmax = j;
max = y.getDouble(i, j);
}
}
out.put(i, argmax, Nd4j.scalar(1.0));
}
return out;
}
示例2: loadFeaturesFromString
import org.nd4j.linalg.api.ndarray.INDArray; //导入方法依赖的package包/类
/**
* Used post training to convert a String to a features INDArray that can be passed to the network output method
*
* @param reviewContents Contents of the review to vectorize
* @param maxLength Maximum length (if review is longer than this: truncate to maxLength). Use Integer.MAX_VALUE to not nruncate
* @return Features array for the given input String
*/
public INDArray loadFeaturesFromString(String reviewContents, int maxLength){
List<String> tokens = tokenizerFactory.create(reviewContents).getTokens();
List<String> tokensFiltered = new ArrayList<>();
for(String t : tokens ){
if(wordVectors.hasWord(t)) tokensFiltered.add(t);
}
int outputLength = Math.max(maxLength,tokensFiltered.size());
INDArray features = Nd4j.create(1, vectorSize, outputLength);
for( int j=0; j<tokens.size() && j<maxLength; j++ ){
String token = tokens.get(j);
INDArray vector = wordVectors.getWordVectorMatrix(token);
features.put(new INDArrayIndex[]{NDArrayIndex.point(0), NDArrayIndex.all(), NDArrayIndex.point(j)}, vector);
}
return features;
}
示例3: normalize
import org.nd4j.linalg.api.ndarray.INDArray; //导入方法依赖的package包/类
/** @return {@link INDArray} */
INDArray normalize(INDArray array) {
INDArray norm = array.norm2(1);
for (int i = 0; i < norm.size(0); i++) {
for (int j = 0; j < norm.size(1); j++) {
if (norm.getFloat(i) == 0) norm.put(i, j, 1.0);
}
}
return array.diviColumnVector(norm);
}
示例4: binomial
import org.nd4j.linalg.api.ndarray.INDArray; //导入方法依赖的package包/类
private INDArray binomial(INDArray x, Random rng) {
INDArray y = Nd4j.create(new double[x.rows() * x.columns()], new int[] { x.rows(), x.columns() });
for (int i = 0; i < x.rows(); i++) {
for (int j = 0; j < x.columns(); j++) { y.put(i, j, RandomGenerator.binomial(1, x.getDouble(i, j), rng)); }
}
return y;
}
示例5: getCorruptedInput
import org.nd4j.linalg.api.ndarray.INDArray; //导入方法依赖的package包/类
private INDArray getCorruptedInput(INDArray x, double corruptionLevel) {
INDArray corruptedInput = Nd4j.create(new double[x.rows() * x.columns()], new int[] { x.rows(), x.columns() });
// add masking noise
for (int i = 0; i < x.rows(); i++) {
for (int j = 0; j < x.columns(); j++) {
double rand_ = rng.nextDouble();
if (rand_ < corruptionLevel) { corruptedInput.put(i, j, Nd4j.scalar(0.0)); }
else { corruptedInput.put(i, j, x.getDouble(i, j)); }
}
}
return corruptedInput;
}
示例6: outputBinomial
import org.nd4j.linalg.api.ndarray.INDArray; //导入方法依赖的package包/类
public INDArray outputBinomial (INDArray X) {
INDArray out = output(X);
INDArray y = Nd4j.create(new double[out.rows() * out.columns()], new int[] { out.rows(), out.columns() });
for (int i = 0; i < out.rows(); i++) {
for (int j = 0; j < out.columns(); j++) {
double value = RandomGenerator.binomial(1, out.getDouble(i, j), rng);
y.put(i, j, Nd4j.scalar(value));
}
}
return y;
}
示例7: next
import org.nd4j.linalg.api.ndarray.INDArray; //导入方法依赖的package包/类
@Override
public MultiDataSet next(int num) {
int i = currentBatch * batchSize;
int currentBatchSize = Math.min(batchSize, corpus.size() - i - 1);
INDArray input = Nd4j.zeros(currentBatchSize, 1, rowSize);
INDArray prediction = Nd4j.zeros(currentBatchSize, dictSize, rowSize);
INDArray decode = Nd4j.zeros(currentBatchSize, dictSize, rowSize);
INDArray inputMask = Nd4j.zeros(currentBatchSize, rowSize);
// this mask is also used for the decoder input, the length is the same
INDArray predictionMask = Nd4j.zeros(currentBatchSize, rowSize);
for (int j = 0; j < currentBatchSize; j++) {
List<Double> rowIn = new ArrayList<>(corpus.get(i));
Collections.reverse(rowIn);
List<Double> rowPred = new ArrayList<>(corpus.get(i + 1));
rowPred.add(1.0); // add <eos> token
// replace the entire row in "input" using NDArrayIndex, it's faster than putScalar(); input is NOT made of
// one-hot vectors because of the embedding layer that accepts token indexes directly
input.put(new INDArrayIndex[] { NDArrayIndex.point(j), NDArrayIndex.point(0), NDArrayIndex.interval(0, rowIn.size()) },
Nd4j.create(ArrayUtils.toPrimitive(rowIn.toArray(new Double[0]))));
inputMask.put(new INDArrayIndex[] { NDArrayIndex.point(j), NDArrayIndex.interval(0, rowIn.size()) },
Nd4j.ones(rowIn.size()));
predictionMask.put(new INDArrayIndex[] { NDArrayIndex.point(j), NDArrayIndex.interval(0, rowPred.size()) },
Nd4j.ones(rowPred.size()));
// prediction (output) and decode ARE one-hots though, I couldn't add an embedding layer on top of the decoder
// and I'm not sure it's a good idea either
double predOneHot[][] = new double[dictSize][rowPred.size()];
double decodeOneHot[][] = new double[dictSize][rowPred.size()];
decodeOneHot[2][0] = 1; // <go> token
int predIdx = 0;
for (Double pred : rowPred) {
predOneHot[pred.intValue()][predIdx] = 1;
// put the same vals to decode with +1 offset except the last token that is <eos>
if (predIdx < rowPred.size() - 1) { decodeOneHot[pred.intValue()][predIdx + 1] = 1; }
++predIdx;
}
prediction.put(new INDArrayIndex[] { NDArrayIndex.point(j), NDArrayIndex.interval(0, dictSize),
NDArrayIndex.interval(0, rowPred.size()) }, Nd4j.create(predOneHot));
decode.put(new INDArrayIndex[] { NDArrayIndex.point(j), NDArrayIndex.interval(0, dictSize),
NDArrayIndex.interval(0, rowPred.size()) }, Nd4j.create(decodeOneHot));
++i;
}
++currentBatch;
return new org.nd4j.linalg.dataset.MultiDataSet(new INDArray[] { input, decode }, new INDArray[] { prediction },
new INDArray[] { inputMask, predictionMask }, new INDArray[] { predictionMask });
}