当前位置: 首页>>代码示例>>Java>>正文


Java INDArray.putScalar方法代码示例

本文整理汇总了Java中org.nd4j.linalg.api.ndarray.INDArray.putScalar方法的典型用法代码示例。如果您正苦于以下问题:Java INDArray.putScalar方法的具体用法?Java INDArray.putScalar怎么用?Java INDArray.putScalar使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.nd4j.linalg.api.ndarray.INDArray的用法示例。


在下文中一共展示了INDArray.putScalar方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: clipMatrix

import org.nd4j.linalg.api.ndarray.INDArray; //导入方法依赖的package包/类
/**
 * Clip the values within a matrix to -5 to 5 range, to avoid exploding gradients
 * @param matrix
 * @return
 */
private static INDArray clipMatrix(INDArray matrix) {
    NdIndexIterator iter = new NdIndexIterator(matrix.shape());
    while (iter.hasNext()) {
        int[] nextIndex = iter.next();
        double nextVal = matrix.getDouble(nextIndex);
        if (nextVal < -5) {
            nextVal = -5;
        }
        if (nextVal > 5) {
            nextVal = 5;
        }
        matrix.putScalar(nextIndex, nextVal);
    }
    return matrix;
}
 
开发者ID:guilherme-pombo,项目名称:JavaRNN,代码行数:21,代码来源:CharRNN.java

示例2: fromText

import org.nd4j.linalg.api.ndarray.INDArray; //导入方法依赖的package包/类
private static Pair<List<String>, INDArray> fromText(String wordFilePath) throws IOException {
	BufferedReader reader = new BufferedReader(Common.asReaderUTF8Lenient(new FileInputStream(new File(wordFilePath))));
	String fstLine = reader.readLine();
	int vocabSize = Integer.parseInt(fstLine.split(" ")[0]);
	int layerSize = Integer.parseInt(fstLine.split(" ")[1]);
	List<String> wordVocab = Lists.newArrayList();
	INDArray wordVectors = Nd4j.create(vocabSize, layerSize);
	int n = 1;
	String line;
	while ((line = reader.readLine()) != null) {
		String[] values = line.split(" ");
		wordVocab.add(values[0]);
		Preconditions.checkArgument(layerSize == values.length - 1, "For file '%s', on line %s, layer size is %s, but found %s values in the word vector",
				wordFilePath, n, layerSize, values.length - 1); // Sanity check
		for (int d = 1; d < values.length; d++) wordVectors.putScalar(n - 1, d - 1, Float.parseFloat(values[d]));
		n++;
	}
	return new Pair<>(wordVocab, wordVectors);
}
 
开发者ID:IsaacChanghau,项目名称:Word2VecfJava,代码行数:20,代码来源:WordVectorSerializer.java

示例3: next

import org.nd4j.linalg.api.ndarray.INDArray; //导入方法依赖的package包/类
public DataSet next(int num) {
    if( exampleStartOffsets.size() == 0 ) throw new NoSuchElementException();

    int currMinibatchSize = Math.min(num, exampleStartOffsets.size());
    //Allocate space:
    //Note the order here:
    // dimension 0 = number of examples in minibatch
    // dimension 1 = size of each vector (i.e., number of characters)
    // dimension 2 = length of each time series/example
    //Why 'f' order here? See http://deeplearning4j.org/usingrnns.html#data section "Alternative: Implementing a custom DataSetIterator"
    INDArray input = Nd4j.create(new int[]{currMinibatchSize,validCharacters.length,exampleLength}, 'f');
    INDArray labels = Nd4j.create(new int[]{currMinibatchSize,validCharacters.length,exampleLength}, 'f');

    for( int i=0; i<currMinibatchSize; i++ ){
        int startIdx = exampleStartOffsets.removeFirst();
        int endIdx = startIdx + exampleLength;
        int currCharIdx = charToIdxMap.get(fileCharacters[startIdx]);	//Current input
        int c=0;
        for( int j=startIdx+1; j<endIdx; j++, c++ ){
            int nextCharIdx = charToIdxMap.get(fileCharacters[j]);		//Next character to predict
            input.putScalar(new int[]{i,currCharIdx,c}, 1.0);
            labels.putScalar(new int[]{i,nextCharIdx,c}, 1.0);
            currCharIdx = nextCharIdx;
        }
    }

    return new DataSet(input,labels);
}
 
开发者ID:alextavgen,项目名称:TrumpHallucinator,代码行数:29,代码来源:CharacterIterator.java

示例4: storeDims

import org.nd4j.linalg.api.ndarray.INDArray; //导入方法依赖的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());
}
 
开发者ID:Audiveris,项目名称:omr-dataset-tools,代码行数:39,代码来源:Features.java

示例5: next

import org.nd4j.linalg.api.ndarray.INDArray; //导入方法依赖的package包/类
@Override
public DataSet next(int num) {
    if (exampleStartOffsets.size() == 0) throw new NoSuchElementException();
    int actualMiniBatchSize = Math.min(num, exampleStartOffsets.size());
    INDArray input = Nd4j.create(new int[] {actualMiniBatchSize, VECTOR_SIZE, exampleLength}, 'f');
    INDArray label;
    if (category.equals(PriceCategory.ALL)) label = Nd4j.create(new int[] {actualMiniBatchSize, VECTOR_SIZE, exampleLength}, 'f');
    else label = Nd4j.create(new int[] {actualMiniBatchSize, predictLength, exampleLength}, 'f');
    for (int index = 0; index < actualMiniBatchSize; index++) {
        int startIdx = exampleStartOffsets.removeFirst();
        int endIdx = startIdx + exampleLength;
        StockData curData = train.get(startIdx);
        StockData nextData;
        for (int i = startIdx; i < endIdx; i++) {
            int c = i - startIdx;
            input.putScalar(new int[] {index, 0, c}, (curData.getOpen() - minArray[0]) / (maxArray[0] - minArray[0]));
            input.putScalar(new int[] {index, 1, c}, (curData.getClose() - minArray[1]) / (maxArray[1] - minArray[1]));
            input.putScalar(new int[] {index, 2, c}, (curData.getLow() - minArray[2]) / (maxArray[2] - minArray[2]));
            input.putScalar(new int[] {index, 3, c}, (curData.getHigh() - minArray[3]) / (maxArray[3] - minArray[3]));
            input.putScalar(new int[] {index, 4, c}, (curData.getVolume() - minArray[4]) / (maxArray[4] - minArray[4]));
            nextData = train.get(i + 1);
            if (category.equals(PriceCategory.ALL)) {
                label.putScalar(new int[] {index, 0, c}, (nextData.getOpen() - minArray[1]) / (maxArray[1] - minArray[1]));
                label.putScalar(new int[] {index, 1, c}, (nextData.getClose() - minArray[1]) / (maxArray[1] - minArray[1]));
                label.putScalar(new int[] {index, 2, c}, (nextData.getLow() - minArray[2]) / (maxArray[2] - minArray[2]));
                label.putScalar(new int[] {index, 3, c}, (nextData.getHigh() - minArray[3]) / (maxArray[3] - minArray[3]));
                label.putScalar(new int[] {index, 4, c}, (nextData.getVolume() - minArray[4]) / (maxArray[4] - minArray[4]));
            } else {
                label.putScalar(new int[]{index, 0, c}, feedLabel(nextData));
            }
            curData = nextData;
        }
        if (exampleStartOffsets.size() == 0) break;
    }
    return new DataSet(input, label);
}
 
开发者ID:IsaacChanghau,项目名称:StockPrediction,代码行数:37,代码来源:StockDataSetIterator.java

示例6: sampleCharactersFromNetwork

import org.nd4j.linalg.api.ndarray.INDArray; //导入方法依赖的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;
}
 
开发者ID:alextavgen,项目名称:TrumpHallucinator,代码行数:55,代码来源:GravelLSTMforTrump.java

示例7: 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));
        }
    }
 
开发者ID:neo4j-contrib,项目名称:neo4j-ml-procedures,代码行数:66,代码来源:DL4JXORHelloWorld.java

示例8: run

import org.nd4j.linalg.api.ndarray.INDArray; //导入方法依赖的package包/类
public void run() throws Exception {
    final File file = new File(inputFile);

    if (!file.exists() || !file.isFile()) {
        System.err.format("unable to access file %s\n", inputFile);
        System.exit(2);
    }

    // Open file
    BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file)));

    // Initialize RestTemplate
    RestTemplate restTemplate = new RestTemplate();

    if (textAsJson) {
        List<HttpMessageConverter<?>> converters = restTemplate.getMessageConverters();
        converters.add(new ExtendedMappingJackson2HttpMessageConverter());
        restTemplate.setMessageConverters(converters);
    }

    // Read each line
    String line = null;
    while ((line = br.readLine()) != null) {
        // Check if label indicator is up front
        String label = null;
        if (line.matches("^\\d:\\s.*")) {
            label = line.substring(0, 1);
        }

        // Just in case
        line = StringUtils.removePattern(line, "^\\d:\\s");
        String[] fields = line.split(",");

        // Maybe strip quotes
        for (int i = 0; i < fields.length; i++) {
            final String field = fields[i];
            if (field.matches("^\".*\"$")) {
                fields[i] = field.substring(1, field.length() - 1);
            }
        }

        int[] shape = (isSequential) ?
                new int[] { 1, 1, fields.length} :
                new int[] { 1, fields.length};

        INDArray array = Nd4j.create(shape);

        for (int i=0; i<fields.length; i++) {
            // TODO: catch NumberFormatException
            Double d = Double.parseDouble(fields[i]);
            int[] idx = (isSequential) ?
                    new int[]{0, 0, i} :
                    new int[]{0, i};

            array.putScalar(idx, d);
        }

        Inference.Request request = new Inference.Request(Nd4jBase64.base64String(array));
        final Object response = restTemplate.postForObject(
                inferenceEndpoint,
                request,
                Inference.Response.Classify.class);

        System.out.format("Inference response: %s\n", response.toString());
        if (label != null) {
            System.out.format("  Label expected: %s\n", label);
        }
    }

    br.close();
}
 
开发者ID:SkymindIO,项目名称:SKIL_Examples,代码行数:72,代码来源:ModelServerDirectInferenceExample.java


注:本文中的org.nd4j.linalg.api.ndarray.INDArray.putScalar方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。