本文整理汇总了Java中org.nd4j.linalg.ops.transforms.Transforms.tanh方法的典型用法代码示例。如果您正苦于以下问题:Java Transforms.tanh方法的具体用法?Java Transforms.tanh怎么用?Java Transforms.tanh使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.nd4j.linalg.ops.transforms.Transforms
的用法示例。
在下文中一共展示了Transforms.tanh方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getBoWBigramModel
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
public INDArray getBoWBigramModel(String sentence, Language language) {
List<INDArray> wordEmbeddings = getWordEmbeddings(sentence, language);
INDArray featureVector = Nd4j.zeros(1, VEC_LENGTH);
for (int i = 1; i < wordEmbeddings.size(); i++) {
INDArray prev = wordEmbeddings.get(i-1) == null ? Nd4j.zeros(1, VEC_LENGTH) : wordEmbeddings.get(i-1);
INDArray curr = wordEmbeddings.get(i) == null ? Nd4j.zeros(1, VEC_LENGTH) : wordEmbeddings.get(i);
INDArray tanhSum = Transforms.tanh(prev.add(curr));
featureVector = featureVector.add(tanhSum);
}
return featureVector;
}
示例2: getSentence2VecBigramModel
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
public INDArray getSentence2VecBigramModel(String sentence, Language language) {
List<double[]> wordEmbeddings = getWordEmbeddings(sentence, language);
INDArray featureVector = Nd4j.zeros(1, VEC_LENGTH);
for (int i = 1; i < wordEmbeddings.size(); i++) {
INDArray prev = wordEmbeddings.get(i-1) == null ? Nd4j.zeros(1, VEC_LENGTH) : Nd4j.create(wordEmbeddings.get(i-1));
INDArray curr = wordEmbeddings.get(i) == null ? Nd4j.zeros(1, VEC_LENGTH) : Nd4j.create(wordEmbeddings.get(i));
INDArray tanhSum = Transforms.tanh(prev.add(curr));
featureVector = featureVector.add(tanhSum);
}
return featureVector;
}
示例3: sample
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
/**
* sample a sequence of integers from the model
* h is memory state, seed_ix is seed letter for first time step
* @param h
* @param seedIdx
* @param n
* @return
*/
private int[] sample(INDArray h, int seedIdx, int n) {
// Use a seed character to start the sampling from
INDArray x = oneHotEncoding(seedIdx);
int[] ixes = new int[n];
// Do forward pass
for (int t = 0; t < n; t++) {
// Input to hidden
INDArray dot1 = Wxh.mmul(x);
// Hidden layer to hidden
INDArray dot2 = Whh.mmul(h).add(bh);
// Hidden state step, squash with tanh to -1 to 1
h = Transforms.tanh(dot1.add(dot2));
// Output - Y
// Dot product between weights from h to y and hidden state, plus bias
INDArray y = Why.mmul(h).add(by);
// Normalised Probabilities - P
INDArray exp = Transforms.exp(y);
INDArray cumExp = Nd4j.sum(exp);
INDArray p = exp.div(cumExp);
int[] to_select = new int[vocabSize];
for (int i = 0; i < vocabSize; i++){
to_select[i] = i;
}
// Given the probabilities of the characters, pick "random characters" to generate the text
int idx = randChoice(to_select, p);
// Next character in the sequence
x = oneHotEncoding(idx);
// Store the chosen character
ixes[t] = idx;
}
return ixes;
}
示例4: map
import org.nd4j.linalg.ops.transforms.Transforms; //导入方法依赖的package包/类
@Override
public NDArrayWritable map(Writable w) {
NDArrayWritable n = (NDArrayWritable) w;
INDArray i = n.get();
if (i == null) {
return n;
}
NDArrayWritable o;
switch (mathFunction) {
case ABS:
o = new NDArrayWritable(Transforms.abs(i, DUP));
break;
case ACOS:
o = new NDArrayWritable(Transforms.acos(i, DUP));
break;
case ASIN:
o = new NDArrayWritable(Transforms.asin(i, DUP));
break;
case ATAN:
o = new NDArrayWritable(Transforms.atan(i, DUP));
break;
case CEIL:
o = new NDArrayWritable(Transforms.ceil(i, DUP));
break;
case COS:
o = new NDArrayWritable(Transforms.cos(i, DUP));
break;
case COSH:
//No cosh operation in ND4J
throw new UnsupportedOperationException("sinh operation not yet supported for NDArray columns");
case EXP:
o = new NDArrayWritable(Transforms.exp(i, DUP));
break;
case FLOOR:
o = new NDArrayWritable(Transforms.floor(i, DUP));
break;
case LOG:
o = new NDArrayWritable(Transforms.log(i, DUP));
break;
case LOG10:
o = new NDArrayWritable(Transforms.log(i, 10.0, DUP));
break;
case SIGNUM:
o = new NDArrayWritable(Transforms.sign(i, DUP));
break;
case SIN:
o = new NDArrayWritable(Transforms.sin(i, DUP));
break;
case SINH:
//No sinh op in ND4J
throw new UnsupportedOperationException("sinh operation not yet supported for NDArray columns");
case SQRT:
o = new NDArrayWritable(Transforms.sqrt(i, DUP));
break;
case TAN:
//No tan op in ND4J yet - but tan(x) = sin(x)/cos(x)
INDArray sinx = Transforms.sin(i, true);
INDArray cosx = Transforms.cos(i, true);
o = new NDArrayWritable(sinx.divi(cosx));
break;
case TANH:
o = new NDArrayWritable(Transforms.tanh(i, DUP));
break;
default:
throw new RuntimeException("Unknown function: " + mathFunction);
}
//To avoid threading issues...
Nd4j.getExecutioner().commit();
return o;
}