private void stochasticUpdateStep(Pair<Integer, Set<Integer>> wordPlusContexts, int s) { double eta = learningRateDecay(s); int wordIndex = wordPlusContexts.getFirst(); // actual center word // Set h vector equal to the kth row of weight matrix W1. h = x' * W = W[k,:] = v(input) RealVector h = W1.getRowVector(wordIndex); // 1xN row vector for (int contextWordIndex : wordPlusContexts.getSecond()) { Set<Integer> negativeContexts; if (sampleUnigram) { negativeContexts = negativeSampleContexts(wordIndex, noiseSampler); } else { negativeContexts = negativeSampleContexts(wordIndex); } // wordIndex is the input word // negativeContexts is the k negative contexts // contextWordIndex is 1 positive context // First update the output vectors for 1 positive context RealVector vPrime_j = W2.getColumnVector(contextWordIndex); // Nx1 column vector double u = h.dotProduct(vPrime_j); // u_j = vPrime(output) * v(input) double t_j = 1.0; // t_j := 1{j == contextWordIndex} double scale = sigmoid(u) - t_j; scale = eta * scale; RealVector gradientOut2Hidden = h.mapMultiply(scale); vPrime_j = vPrime_j.subtract(gradientOut2Hidden); W2.setColumnVector(contextWordIndex, vPrime_j); // Next backpropagate the error to the hidden layer and update the input vectors RealVector v_I = h; u = h.dotProduct(vPrime_j); scale = sigmoid(u) - t_j; scale = eta * scale; RealVector gradientHidden2In = vPrime_j.mapMultiply(scale); v_I = v_I.subtract(gradientHidden2In); h = v_I; W1.setRowVector(wordIndex, v_I); // Repeat update process for k negative contexts t_j = 0.0; // t_j := 1{j == contextWordIndex} for (int negContext : negativeContexts) { vPrime_j = W2.getColumnVector(negContext); u = h.dotProduct(vPrime_j); scale = sigmoid(u) - t_j; scale = eta * scale; gradientOut2Hidden = h.mapMultiply(scale); vPrime_j = vPrime_j.subtract(gradientOut2Hidden); W2.setColumnVector(negContext, vPrime_j); // Backpropagate the error to the hidden layer and update the input vectors v_I = h; u = h.dotProduct(vPrime_j); scale = sigmoid(u) - t_j; scale = eta * scale; gradientHidden2In = vPrime_j.mapMultiply(scale); v_I = v_I.subtract(gradientHidden2In); h = v_I; W1.setRowVector(wordIndex, v_I); } } }
private static Intersection getIntersection(Ray ray, SphereObject obj, Model model) { RealMatrix transform = obj.getTransform(); final RealMatrix transformInverse = obj.getTransformInverse(); ray = ray.transform(transformInverse); Vector3D c = VectorUtils.toVector3D(obj.getCenter()); Vector3D p0 = VectorUtils.toVector3D(ray.getP0()); Vector3D p1 = VectorUtils.toVector3D(ray.getP1()); float a = (float) p1.dotProduct(p1); Vector3D p0c = p0.subtract(c); float b = (float) p1.dotProduct(p0c) * 2.0f; float cc = (float) (p0c.dotProduct(p0c)) - obj.getSize() * obj.getSize(); Double t = quadraticEquationRoot1(a, b, cc); if (t == null || t < EPSILON) { return new Intersection(false); } Intersection result = new Intersection(true); result.setObject(obj); final Vector3D p = p0.add(p1.scalarMultiply(t)); RealVector pv = VectorUtils.toRealVector(p); pv.setEntry(3, 1.0); RealVector pt = transform.preMultiply(pv); result.setP(VectorUtils.toVector3D(pt)); RealVector nv = pv.subtract(obj.getCenter()); final RealVector nvt = transformInverse.transpose().preMultiply(nv); result.setN(VectorUtils.toVector3D(nvt).normalize()); result.setDistance(t); return result; }