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


Java INDArray.tensorAlongDimension方法代码示例

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


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

示例1: 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


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