本文整理汇总了Java中edu.uw.easysrl.util.Util.add方法的典型用法代码示例。如果您正苦于以下问题:Java Util.add方法的具体用法?Java Util.add怎么用?Java Util.add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类edu.uw.easysrl.util.Util
的用法示例。
在下文中一共展示了Util.add方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: make
import edu.uw.easysrl.util.Util; //导入方法依赖的package包/类
private void make() throws IOException {
final Map<String, Multiset<SRLLabel>> keyToRole = new HashMap<>();
for (final SRLLabel label : SRLFrame.getAllSrlLabels()) {
srlToOffset.put(label, HashMultiset.create());
}
final Iterator<Sentence> sentences = ParallelCorpusReader.READER.readCorpus(false);
while (sentences.hasNext()) {
final Sentence sentence = sentences.next();
final List<InputWord> words = sentence.getInputWords();
final List<Category> goldCategories = sentence.getLexicalCategories();
final Map<SRLDependency, CCGBankDependency> depMap = sentence.getCorrespondingCCGBankDependencies();
for (int wordIndex = 0; wordIndex < sentence.getLength(); wordIndex++) {
final Category goldCategory = goldCategories.get(wordIndex);
for (final Entry<SRLDependency, CCGBankDependency> dep : depMap.entrySet()) {
if (dep.getValue() != null && dep.getValue().getSentencePositionOfPredicate() == wordIndex) {
final int offset = dep.getValue().getSentencePositionOfArgument()
- dep.getValue().getSentencePositionOfPredicate();
for (int i = Math.min(offset, 0); i <= Math.max(offset, 0); i++) {
if (i != 0 && Math.abs(offset) <= maxDependencyLength) {
// For a word at -5, also at -4,-3,-2,-1
Util.add(srlToOffset, dep.getKey().getLabel(), i);
}
}
Util.add(categoryToArgumentToSRLs, goldCategory, dep.getValue().getArgNumber(), dep.getKey()
.getLabel());
final Preposition preposition = Preposition.fromString(dep.getKey().getPreposition());
final String key = makeKey(words.get(wordIndex).word, goldCategory, preposition, dep.getValue()
.getArgNumber());
Multiset<SRLLabel> roles = keyToRole.get(key);
if (roles == null) {
roles = HashMultiset.create();
roles.add(SRLFrame.NONE);
keyToRole.put(key, roles);
}
roles.add(dep.getKey().getLabel());
}
}
}
}
for (final Entry<String, Multiset<SRLLabel>> entry : keyToRole.entrySet()) {
this.keyToRole.put(entry.getKey(), new HashSet<>(entry.getValue()));
}
}
示例2: getValues
import edu.uw.easysrl.util.Util; //导入方法依赖的package包/类
@Override
public FunctionValues getValues(final double[] featureWeights) {
final Collection<Callable<FunctionValues>> tasks = new ArrayList<>();
for (final RemoteTrainer trainer : servers) {
tasks.add(new Callable<FunctionValues>() {
@Override
public FunctionValues call() throws Exception {
return trainer.getValues(featureWeights).toFunctionValues();
}
});
}
final ExecutorService executor = Executors.newFixedThreadPool(servers
.size());
List<Future<FunctionValues>> results;
try {
results = executor.invokeAll(tasks);
final double[] totalGradient = new double[featureWeights.length];
double totalLoss = 0.0;
for (final Future<FunctionValues> result : results) {
final FunctionValues values = result.get();
totalLoss += values.functionValue;
Util.add(totalGradient, values.gradient);
System.out.print(".");
}
executor.shutdown(); // always reclaim resources
for (int i = 0; i < featureWeights.length; i++) {
totalLoss = totalLoss
- (Math.pow(featureWeights[i], 2) / (2.0 * sigmaSquared));
totalGradient[i] = totalGradient[i]
- (featureWeights[i] / sigmaSquared);
}
totalLoss = -totalLoss;
for (int i = 0; i < featureWeights.length; i++) {
totalGradient[i] = -totalGradient[i];
}
System.out.println();
System.out.println("Iteration: " + (it++));
System.out.println("Loss = " + Util.twoDP(totalLoss));
return new FunctionValues(totalLoss, totalGradient);
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
}
示例3: getValues
import edu.uw.easysrl.util.Util; //导入方法依赖的package包/类
@Override
public IterationResult getValues(final double[] featureWeights) {
final Collection<Callable<IterationResult>> tasks = new ArrayList<>();
int totalCorrect = 0;
final int batchSize = trainingData.size() / numThreads;
for (final List<CachedTrainingExample<L>> batch : Lists.partition(trainingData, batchSize)) {
tasks.add(new Callable<IterationResult>() {
@Override
public IterationResult call() throws Exception {
return lossFunction.getValues(featureWeights, batch);
}
});
}
final ExecutorService executor = Executors.newFixedThreadPool(numThreads);
List<Future<IterationResult>> results;
try {
results = executor.invokeAll(tasks);
// FunctionValues total = new FunctionValues(0.0, new
// double[featureWeights.length]);
final double[] totalGradient = new double[featureWeights.length];
double totalLoss = 0.0;
for (final Future<IterationResult> result : results) {
final IterationResult values = result.get();
totalLoss += values.functionValue;
Util.add(totalGradient, values.gradient);
totalCorrect += values.correctPredictions;
}
executor.shutdown(); // always reclaim resources
System.out.println();
System.out.println("Training accuracy=" + Util.twoDP(100.0 * totalCorrect / trainingData.size()));
System.out.println("Loss=" + Util.twoDP(totalLoss));
return new IterationResult(totalCorrect, totalLoss, totalGradient);
} catch (InterruptedException | ExecutionException e) {
throw new RuntimeException(e);
}
}