本文整理汇总了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;
}