use of java.util.LinkedList in project deeplearning4j by deeplearning4j.
the class LBFGS method postStep.
// Numerical Optimization (Nocedal & Wright) section 7.2
// s = parameters differences (old & current)
// y = gradient differences (old & current)
// gamma = initial Hessian approximation (i.e., equiv. to gamma*IdentityMatrix for Hessian)
// rho = scalar. rho_i = 1/(y_i \dot s_i)
@Override
public void postStep(INDArray gradient) {
INDArray previousParameters = (INDArray) searchState.get("oldparams");
INDArray parameters = model.params();
INDArray previousGradient = (INDArray) searchState.get(GRADIENT_KEY);
LinkedList<Double> rho = (LinkedList<Double>) searchState.get("rho");
LinkedList<INDArray> s = (LinkedList<INDArray>) searchState.get("s");
LinkedList<INDArray> y = (LinkedList<INDArray>) searchState.get("y");
double sy = Nd4j.getBlasWrapper().dot(previousParameters, previousGradient) + Nd4j.EPS_THRESHOLD;
double yy = Nd4j.getBlasWrapper().dot(previousGradient, previousGradient) + Nd4j.EPS_THRESHOLD;
INDArray sCurrent;
INDArray yCurrent;
if (s.size() >= m) {
//Optimization: Remove old (no longer needed) INDArrays, and use assign for re-use.
//Better to do this: fewer objects created -> less memory overall + less garbage collection
sCurrent = s.removeLast();
yCurrent = y.removeLast();
rho.removeLast();
sCurrent.assign(parameters).subi(previousParameters);
yCurrent.assign(gradient).subi(previousGradient);
} else {
//First few iterations. Need to allocate new INDArrays for storage (via copy operation sub)
sCurrent = parameters.sub(previousParameters);
yCurrent = gradient.sub(previousGradient);
}
//Most recent first
rho.addFirst(1.0 / sy);
//Most recent first. si = currParams - oldParams
s.addFirst(sCurrent);
//Most recent first. yi = currGradient - oldGradient
y.addFirst(yCurrent);
//assert (s.size()==y.size()) : "Gradient and parameter sizes are not equal";
if (s.size() != y.size())
throw new IllegalStateException("Gradient and parameter sizes are not equal");
//In general: have m elements in s,y,rho.
//But for first few iterations, have less.
int numVectors = Math.min(m, s.size());
double[] alpha = new double[numVectors];
// First work backwards, from the most recent difference vectors
Iterator<INDArray> sIter = s.iterator();
Iterator<INDArray> yIter = y.iterator();
Iterator<Double> rhoIter = rho.iterator();
//searchDir: first used as equivalent to q as per N&W, then later used as r as per N&W.
//Re-using existing array for performance reasons
INDArray searchDir = (INDArray) searchState.get(SEARCH_DIR);
searchDir.assign(gradient);
for (int i = 0; i < numVectors; i++) {
INDArray si = sIter.next();
INDArray yi = yIter.next();
double rhoi = rhoIter.next();
if (si.length() != searchDir.length())
throw new IllegalStateException("Gradients and parameters length not equal");
alpha[i] = rhoi * Nd4j.getBlasWrapper().dot(si, searchDir);
//q = q-alpha[i]*yi
Nd4j.getBlasWrapper().level1().axpy(searchDir.length(), -alpha[i], yi, searchDir);
}
//Use Hessian approximation initialization scheme
//searchDir = H0*q = (gamma*IdentityMatrix)*q = gamma*q
double gamma = sy / yy;
searchDir.muli(gamma);
//Reverse iterators: end to start. Java LinkedLists are doubly-linked,
// so still O(1) for reverse iteration operations.
sIter = s.descendingIterator();
yIter = y.descendingIterator();
rhoIter = rho.descendingIterator();
for (int i = 0; i < numVectors; i++) {
INDArray si = sIter.next();
INDArray yi = yIter.next();
double rhoi = rhoIter.next();
//beta = rho_i * y_i^T * r
double beta = rhoi * Nd4j.getBlasWrapper().dot(yi, searchDir);
//r = r + s_i * (alpha_i - beta)
Nd4j.getBlasWrapper().level1().axpy(gradient.length(), alpha[i] - beta, si, searchDir);
}
previousParameters.assign(parameters);
//Update gradient. Still in searchState map keyed by GRADIENT_KEY
previousGradient.assign(gradient);
}
use of java.util.LinkedList in project che by eclipse.
the class NodeStorage method remove.
public boolean remove(Node node) {
NodeDescriptor nodeDescriptor = idToNodeMap.get(getKeyProvider().getKey(node));
if (nodeDescriptor != null) {
Node parent = getParent(node);
List<Node> children = getAllChildren(node);
int visibleIndex = nodeDescriptor.getParent().getChildren().indexOf(nodeDescriptor);
nodeDescriptor.getParent().remove(nodeDescriptor);
if (visibleIndex != -1) {
fireEvent(new StoreRemoveEvent(visibleIndex, node, parent, children));
} else {
List<NodeDescriptor> descriptors = new LinkedList<>();
descriptors.add(nodeDescriptor);
for (int i = 0; i < descriptors.size(); i++) {
nodeDescriptor = descriptors.get(i);
descriptors.addAll(nodeDescriptor.getChildren());
idToNodeMap.remove(getKeyProvider().getKey(nodeDescriptor.getNode()));
}
}
return true;
}
return false;
}
use of java.util.LinkedList in project che by eclipse.
the class NodeStorage method removeChildren.
private void removeChildren(NodeDescriptor parent) {
if (parent.getChildren().size() != 0) {
List<NodeDescriptor> models = new LinkedList<>();
models.addAll(parent.getChildren());
parent.clear();
for (int i = 0; i < models.size(); i++) {
NodeDescriptor wrapper = models.get(i);
models.addAll(wrapper.getChildren());
List<Node> children = getAllChildren(wrapper.getNode());
idToNodeMap.remove(getKeyProvider().getKey(wrapper.getNode()));
if (wrapper.getParent() == parent) {
fireEvent(new StoreRemoveEvent(0, wrapper.getNode(), parent.getNode(), children));
}
}
}
}
use of java.util.LinkedList in project che by eclipse.
the class QueryManager method removeAll.
public void removeAll() {
synchronized (this) {
List old = fQueries;
fQueries = new LinkedList();
Iterator iter = old.iterator();
while (iter.hasNext()) {
ISearchQuery element = (ISearchQuery) iter.next();
fireRemoved(element);
}
}
}
use of java.util.LinkedList in project che by eclipse.
the class MavenProjectManager method update.
public void update(List<IProject> projects, boolean recursive) {
if (projects.isEmpty()) {
return;
}
mavenNotifier.start();
UpdateState state = new UpdateState();
Deque<MavenProject> stack = new LinkedList<>();
for (IProject project : projects) {
MavenProject mavenProject = findMavenProject(project);
if (mavenProject != null) {
internalUpdate(mavenProject, findParentProject(mavenProject), false, recursive, state, stack);
} else {
internalAddMavenProject(project, recursive, state, stack);
}
}
mavenNotifier.stop();
state.fireUpdate();
}
Aggregations