本文整理匯總了Java中org.nd4j.linalg.factory.Nd4j.zeros方法的典型用法代碼示例。如果您正苦於以下問題:Java Nd4j.zeros方法的具體用法?Java Nd4j.zeros怎麽用?Java Nd4j.zeros使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.nd4j.linalg.factory.Nd4j
的用法示例。
在下文中一共展示了Nd4j.zeros方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: readTestData
import org.nd4j.linalg.factory.Nd4j; //導入方法依賴的package包/類
private static List<TrainingData> readTestData(String fn) {
int[] shape = { 3, 1 };
List<TrainingData> trainingDataSet = new ArrayList<>();
try {
CSVReader reader = new CSVReader(new FileReader(fn));
String[] row;
while ((row = reader.readNext()) != null) {
int type = Integer.parseInt(row[0]);
double f1 = Double.parseDouble(row[1]);
double f2 = Double.parseDouble(row[2]);
double f3 = Double.parseDouble(row[3]);
TrainingData trainingData = new TrainingData();
trainingData.input = Nd4j.create(new double[] { f1, f2, f3 }, shape);
trainingData.output = Nd4j.zeros(shape);
trainingData.output.putScalar(type, (double) 1);
trainingDataSet.add(trainingData);
}
} catch (java.io.IOException e) {
}
return trainingDataSet;
}
示例2: NDMatrix
import org.nd4j.linalg.factory.Nd4j; //導入方法依賴的package包/類
/**
* Constructor. Fill the matrix with given value.
*/
public NDMatrix(int rows, int cols, double value) {
if (value == 0.0)
A = Nd4j.zeros(rows, cols);
else if (value == 1.0)
A = Nd4j.ones(rows, cols);
else
A = Nd4j.zeros(rows, cols).addi(value);
}
示例3: storeDims
import org.nd4j.linalg.factory.Nd4j; //導入方法依賴的package包/類
/**
* Store the mean/std values for width/height of each (populated) omr shape.
*
* @throws IOException on IO error
*/
private void storeDims ()
throws IOException
{
INDArray dimStats = Nd4j.zeros(4, SHAPE_COUNT);
logger.info("Symbol dimensions for populated shapes:");
for (Entry<OmrShape, DistributionStats.Builder> entry : dimMap.entrySet()) {
OmrShape shape = entry.getKey();
DistributionStats.Builder builder = entry.getValue();
DistributionStats stats = builder.build();
INDArray means = stats.getMean();
INDArray stds = stats.getStd();
int index = shape.ordinal();
double meanWidth = means.getDouble(0);
double stdWidth = stds.getDouble(0);
double meanHeight = means.getDouble(1);
double stdHeight = stds.getDouble(1);
dimStats.putScalar(new int[]{0, index}, meanWidth);
dimStats.putScalar(new int[]{1, index}, stdWidth);
dimStats.putScalar(new int[]{2, index}, meanHeight);
dimStats.putScalar(new int[]{3, index}, stdHeight);
logger.info(
String.format(
"%27s width{mean:%.2f std:%.2f} height{mean:%.2f std:%.2f}",
shape,
meanWidth,
stdWidth,
meanHeight,
stdHeight));
}
Nd4j.saveBinary(dimStats, DIMS_PATH.toFile());
}
示例4: sampleCharactersFromNetwork
import org.nd4j.linalg.factory.Nd4j; //導入方法依賴的package包/類
/** Generate a sample from the network, given an (optional, possibly null) initialization. Initialization
* can be used to 'prime' the RNN with a sequence you want to extend/continue.<br>
* Note that the initalization is used for all samples
* @param initialization String, may be null. If null, select a random character as initialization for all samples
* @param charactersToSample Number of characters to sample from network (excluding initialization)
* @param net MultiLayerNetwork with one or more GravesLSTM/RNN layers and a softmax output layer
* @param iter CharacterIterator. Used for going from indexes back to characters
*/
private static String[] sampleCharactersFromNetwork(String initialization, MultiLayerNetwork net,
CharacterIterator iter, Random rng, int charactersToSample, int numSamples ){
//Set up initialization. If no initialization: use a random character
if( initialization == null ){
initialization = String.valueOf(iter.getRandomCharacter());
}
//Create input for initialization
INDArray initializationInput = Nd4j.zeros(numSamples, iter.inputColumns(), initialization.length());
char[] init = initialization.toCharArray();
for( int i=0; i<init.length; i++ ){
int idx = iter.convertCharacterToIndex(init[i]);
for( int j=0; j<numSamples; j++ ){
initializationInput.putScalar(new int[]{j,idx,i}, 1.0f);
}
}
StringBuilder[] sb = new StringBuilder[numSamples];
for( int i=0; i<numSamples; i++ ) sb[i] = new StringBuilder(initialization);
//Sample from network (and feed samples back into input) one character at a time (for all samples)
//Sampling is done in parallel here
net.rnnClearPreviousState();
INDArray output = net.rnnTimeStep(initializationInput);
output = output.tensorAlongDimension(output.size(2)-1,1,0); //Gets the last time step output
for( int i=0; i<charactersToSample; i++ ){
//Set up next input (single time step) by sampling from previous output
INDArray nextInput = Nd4j.zeros(numSamples,iter.inputColumns());
//Output is a probability distribution. Sample from this for each example we want to generate, and add it to the new input
for( int s=0; s<numSamples; s++ ){
double[] outputProbDistribution = new double[iter.totalOutcomes()];
for( int j=0; j<outputProbDistribution.length; j++ ) outputProbDistribution[j] = output.getDouble(s,j);
int sampledCharacterIdx = sampleFromDistribution(outputProbDistribution,rng);
nextInput.putScalar(new int[]{s,sampledCharacterIdx}, 1.0f); //Prepare next time step input
sb[s].append(iter.convertIndexToCharacter(sampledCharacterIdx)); //Add sampled character to StringBuilder (human readable output)
}
output = net.rnnTimeStep(nextInput); //Do one time step of forward pass
}
String[] out = new String[numSamples];
for( int i=0; i<numSamples; i++ ) out[i] = sb[i].toString();
return out;
}
示例5: main
import org.nd4j.linalg.factory.Nd4j; //導入方法依賴的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));
}
}
示例6: getPar2HierVector
import org.nd4j.linalg.factory.Nd4j; //導入方法依賴的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;
}
}
示例7: createGoal
import org.nd4j.linalg.factory.Nd4j; //導入方法依賴的package包/類
private Goal createGoal(
final TeamColor teamColor,
final CardinalDirection direction,
final GeometryFieldSize geometryData
) {
switch (direction) {
case NORTH:
return new Goal.State(
Nd4j.create(
new float[]{
geometryData.getGoalWidth(),
geometryData.getGoalDepth(),
((geometryData.getFieldLength() + geometryData.getGoalDepth()) / 2f),
0
}),
teamColor,
direction);
case SOUTH:
return new Goal.State(
Nd4j.create(
new float[]{
geometryData.getGoalWidth(),
geometryData.getGoalDepth(),
-1f * ((geometryData.getFieldLength() + geometryData.getGoalDepth()) / 2f),
0
}),
teamColor,
direction);
case EAST:
return new Goal.State(
Nd4j.create(
new float[]{
geometryData.getGoalWidth(),
geometryData.getGoalDepth(),
0,
((geometryData.getFieldWidth() + geometryData.getGoalWidth()) / 2f)
}),
teamColor,
direction);
case WEST:
return new Goal.State(
Nd4j.create(
new float[]{
geometryData.getGoalWidth(),
geometryData.getGoalDepth(),
0,
-1f * ((geometryData.getFieldWidth() + geometryData.getGoalWidth()) / 2f)
}),
teamColor,
direction);
default:
return new Goal.State(Nd4j.zeros(1, 4), TeamColor.NONE, CardinalDirection.NORTH);
}
}
示例8: aggregate
import org.nd4j.linalg.factory.Nd4j; //導入方法依賴的package包/類
@Override
public SeldonMessage aggregate(List<SeldonMessage> outputs, PredictiveUnitState state){
if (outputs.size()==0){
throw new APIException(APIException.ApiExceptionType.ENGINE_INVALID_COMBINER_RESPONSE, String.format("Combiner received no inputs"));
}
int[] shape = PredictorUtils.getShape(outputs.get(0).getData());
if (shape == null){
throw new APIException(APIException.ApiExceptionType.ENGINE_INVALID_COMBINER_RESPONSE, String.format("Combiner cannot extract data shape"));
}
if (shape.length!=2){
throw new APIException(APIException.ApiExceptionType.ENGINE_INVALID_COMBINER_RESPONSE, String.format("Combiner received data that is not 2 dimensional"));
}
INDArray currentSum = Nd4j.zeros(shape[0],shape[1]);
SeldonMessage.Builder respBuilder = SeldonMessage.newBuilder();
for (Iterator<SeldonMessage> i = outputs.iterator(); i.hasNext();)
{
DefaultData inputData = i.next().getData();
int[] inputShape = PredictorUtils.getShape(inputData);
if (inputShape == null){
throw new APIException(APIException.ApiExceptionType.ENGINE_INVALID_COMBINER_RESPONSE, String.format("Combiner cannot extract data shape"));
}
if (inputShape.length!=2){
throw new APIException(APIException.ApiExceptionType.ENGINE_INVALID_COMBINER_RESPONSE, String.format("Combiner received data that is not 2 dimensional"));
}
if (inputShape[0] != shape[0]){
throw new APIException(APIException.ApiExceptionType.ENGINE_INVALID_COMBINER_RESPONSE, String.format("Expected batch length %d but found %d",shape[0],inputShape[0]));
}
if (inputShape[1] != shape[1]){
throw new APIException(APIException.ApiExceptionType.ENGINE_INVALID_COMBINER_RESPONSE, String.format("Expected batch length %d but found %d",shape[1],inputShape[1]));
}
INDArray inputArr = PredictorUtils.getINDArray(inputData);
currentSum = currentSum.add(inputArr);
}
currentSum = currentSum.div((float)outputs.size());
DefaultData newData = PredictorUtils.updateData(outputs.get(0).getData(), currentSum);
respBuilder.setData(newData);
respBuilder.setMeta(outputs.get(0).getMeta());
respBuilder.setStatus(outputs.get(0).getStatus());
return respBuilder.build();
}
示例9: zeros
import org.nd4j.linalg.factory.Nd4j; //導入方法依賴的package包/類
/** @return {@link INDArray} */
public INDArray zeros () {
return Nd4j.zeros(1, layerSize);
}
示例10: next
import org.nd4j.linalg.factory.Nd4j; //導入方法依賴的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 });
}