本文整理汇总了Java中org.nd4j.linalg.api.ndarray.INDArray.rows方法的典型用法代码示例。如果您正苦于以下问题:Java INDArray.rows方法的具体用法?Java INDArray.rows怎么用?Java INDArray.rows使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.nd4j.linalg.api.ndarray.INDArray
的用法示例。
在下文中一共展示了INDArray.rows方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: nd4JExample
import org.nd4j.linalg.api.ndarray.INDArray; //导入方法依赖的package包/类
public void nd4JExample() {
double[] A = {
0.1950, 0.0311,
0.3588, 0.2203,
0.1716, 0.5931,
0.2105, 0.3242};
double[] B = {
0.0502, 0.9823, 0.9472,
0.5732, 0.2694, 0.916};
INDArray aINDArray = Nd4j.create(A,new int[]{4,2},'c');
INDArray bINDArray = Nd4j.create(B,new int[]{2,3},'c');
INDArray cINDArray;
cINDArray = aINDArray.mmul(bINDArray);
for(int i=0; i<cINDArray.rows(); i++) {
System.out.println(cINDArray.getRow(i));
}
}
开发者ID:PacktPublishing,项目名称:Machine-Learning-End-to-Endguide-for-Java-developers,代码行数:22,代码来源:MathExamples.java
示例2: getTopN
import org.nd4j.linalg.api.ndarray.INDArray; //导入方法依赖的package包/类
/**
* Get top N elements
*
* @param vec the vec to extract the top elements from
* @param N the number of elements to extract
* @return the indices and the sorted top N elements
*/
private List<Double> getTopN(INDArray vec, int N) {
BasicModelUtils.ArrayComparator comparator = new BasicModelUtils.ArrayComparator();
PriorityQueue<Double[]> queue = new PriorityQueue<>(vec.rows(), comparator);
for (int j = 0; j < vec.length(); j++) {
final Double[] pair = new Double[] {vec.getDouble(j), (double) j};
if (queue.size() < N) {
queue.add(pair);
} else {
Double[] head = queue.peek();
if (comparator.compare(pair, head) > 0) {
queue.poll();
queue.add(pair);
}
}
}
List<Double> lowToHighSimLst = new ArrayList<>();
while (!queue.isEmpty()) {
double ind = queue.poll()[1];
lowToHighSimLst.add(ind);
}
return Lists.reverse(lowToHighSimLst);
}
示例3: 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;
}
示例4: getDoubles
import org.nd4j.linalg.api.ndarray.INDArray; //导入方法依赖的package包/类
private static double[][] getDoubles(INDArray matrix) {
double[][] data = new double[matrix.rows()][matrix.columns()];
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[0].length; j++) {
data[i][j] = matrix.getDouble(i, j);
}
}
return data;
}
示例5: isTransitive
import org.nd4j.linalg.api.ndarray.INDArray; //导入方法依赖的package包/类
private boolean isTransitive(double[] ilpSolution, int n) {
double[][] adjacencyMatrix = new double[n][n];
int k = 0;
for (int i = 0; i < n; i++) {
adjacencyMatrix[i][i] = 1;
for (int j = i + 1; j < n; j++) {
adjacencyMatrix[i][j] = ilpSolution[k];
adjacencyMatrix[j][i] = ilpSolution[k];
k++;
}
}
INDArray m = new NDArray(adjacencyMatrix);
INDArray m2 = m.mmul(m);
System.out.println(m);
for (int i = 0; i < m.rows(); i++) {
for (int j = 0; j < m.columns(); j++) {
if (m2.getDouble(i, j) > 0 && m.getDouble(i, j) == 0) {
System.out.println(i + " " + j + " " + m2.getDouble(i, j) + " " + m.getDouble(i, j));
return false;
}
}
}
return true;
}
示例6: Evaluation
import org.nd4j.linalg.api.ndarray.INDArray; //导入方法依赖的package包/类
public Evaluation (INDArray logits, INDArray labels) {
this.logits = logits;
this.labels = labels;
this.examples = labels.rows();
this.patterns = labels.columns();
this.confusionMatrix = new int[patterns][patterns];
this.precision = new double[patterns];
this.recall = new double[patterns];
this.accuracy = 0.0;
}
示例7: 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;
}
示例8: 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;
}
示例9: 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;
}
示例10: main
import org.nd4j.linalg.api.ndarray.INDArray; //导入方法依赖的package包/类
/**
* The main method.
* @param args No arguments are used.
*/
public static void main(final String args[]) throws IOException, InterruptedException {
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
.seed(123)
.iterations(1)
.optimizationAlgo(OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
.learningRate(0.1)
// .useDropConnect(false)
// .biasInit(0)
.miniBatch(false)
// .updater(Updater.SGD)
.list()
.layer(0, new DenseLayer.Builder().nIn(2).nOut(2)
// .weightInit(WeightInit.DISTRIBUTION).dist(new UniformDistribution(0,1))
.activation(Activation.SIGMOID)
.build())
.layer(1, new OutputLayer.Builder(LossFunctions.LossFunction.MSE)
// .weightInit(WeightInit.DISTRIBUTION).dist(new UniformDistribution(0,1))
.activation(Activation.SIGMOID)
.nIn(2).nOut(1).build())
.pretrain(false).backprop(true).build();
MultiLayerNetwork model = new MultiLayerNetwork(conf);
model.init();
model.setListeners(new ScoreIterationListener(1)); //Print score every 10 parameter updates
DoublesDataSetIterator iterator = new DoublesDataSetIterator(Arrays.asList(
makePair(XOR_INPUT[0],XOR_IDEAL[0]),
makePair(XOR_INPUT[1],XOR_IDEAL[1]),
makePair(XOR_INPUT[2],XOR_IDEAL[2]),
makePair(XOR_INPUT[3],XOR_IDEAL[3]))
,1);
for (int n = 0; n < 10000; n++) {
model.fit(iterator);
}
Evaluation eval = model.evaluate(iterator);
List<Prediction> predictionErrors = eval.getPredictionErrors();
System.out.println("\n\n+++++ Prediction Errors +++++");
if (predictionErrors != null) {
for (Prediction p : predictionErrors) {
System.out.printf("Predicted class: %d, Actual class: %d\t%s%n", p.getPredictedClass(), p.getActualClass(), p.getRecordMetaData(RecordMetaData.class));
}
}
//Print the evaluation statistics
System.out.println(eval.stats());
INDArray data = Nd4j.zeros(2, 2);
data.putScalar(0,0,0d);
data.putScalar(0,1,1d);
data.putScalar(1,0,1d);
data.putScalar(1,1,1d);
INDArray output = model.output(data);
for (int i=0;i<data.rows();i++) {
System.out.println(data.getRow(i) +" -> "+ output.getRow(i));
}
}
示例11: getPar2HierVector
import org.nd4j.linalg.api.ndarray.INDArray; //导入方法依赖的package包/类
/**
* base case: on a leaf hv = pv
* on a non-leaf node with n children: hv = pv + k centroids of the n hv
*/
private static INDArray getPar2HierVector(WeightLookupTable<VocabWord> lookupTable, PatriciaTrie<String> trie, String node,
int k, Map<String, INDArray> hvs, Method method) {
if (hvs.containsKey(node)) {
return hvs.get(node);
}
INDArray hv = lookupTable.vector(node);
String[] split = node.split(REGEX);
Collection<String> descendants = new HashSet<>();
if (split.length == 2) {
String separator = ".";
String prefix = node.substring(0, node.indexOf(split[1])) + separator;
SortedMap<String, String> sortedMap = trie.prefixMap(prefix);
for (Map.Entry<String, String> entry : sortedMap.entrySet()) {
if (prefix.lastIndexOf(separator) == entry.getKey().lastIndexOf(separator)) {
descendants.add(entry.getValue());
}
}
} else {
descendants = Collections.emptyList();
}
if (descendants.size() == 0) {
// just the pv
hvs.put(node, hv);
return hv;
} else {
INDArray chvs = Nd4j.zeros(descendants.size(), hv.columns());
int i = 0;
for (String desc : descendants) {
// child hierarchical vector
INDArray chv = getPar2HierVector(lookupTable, trie, desc, k, hvs, method);
chvs.putRow(i, chv);
i++;
}
double[][] centroids;
if (chvs.rows() > k) {
centroids = Par2HierUtils.getTruncatedVT(chvs, k);
} else if (chvs.rows() == 1) {
centroids = Par2HierUtils.getDoubles(chvs.getRow(0));
} else {
centroids = Par2HierUtils.getTruncatedVT(chvs, 1);
}
switch (method) {
case CLUSTER:
INDArray matrix = Nd4j.zeros(centroids.length + 1, hv.columns());
matrix.putRow(0, hv);
for (int c = 0; c < centroids.length; c++) {
matrix.putRow(c + 1, Nd4j.create(centroids[c]));
}
hv = Nd4j.create(Par2HierUtils.getTruncatedVT(matrix, 1));
break;
case SUM:
for (double[] centroid : centroids) {
hv.addi(Nd4j.create(centroid));
}
break;
}
hvs.put(node, hv);
return hv;
}
}