当前位置: 首页>>代码示例>>Java>>正文


Java Pair.getSecond方法代码示例

本文整理汇总了Java中tberg.murphy.tuple.Pair.getSecond方法的典型用法代码示例。如果您正苦于以下问题:Java Pair.getSecond方法的具体用法?Java Pair.getSecond怎么用?Java Pair.getSecond使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在tberg.murphy.tuple.Pair的用法示例。


在下文中一共展示了Pair.getSecond方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: computeMarginalsScaling

import tberg.murphy.tuple.Pair; //导入方法依赖的package包/类
public static Pair<NodeMarginals,StationaryEdgeMarginals> computeMarginalsScaling(final StationaryLattice lattice, final StationaryStateProjector nodeMarginalsStateProjector, final boolean viterbiEmissionOnly, int numThreads) {
		final NodeMarginalsScaling projectedNodeMarginals = new NodeMarginalsScaling(new StationaryLatticeWrapper(lattice), nodeMarginalsStateProjector);
		final StationaryEdgeMarginalsScaling edgeMarginals = (viterbiEmissionOnly ? null : new StationaryEdgeMarginalsScaling(lattice));
		BetterThreader.Function<Integer,Object> func = new BetterThreader.Function<Integer,Object>(){public void call(Integer d, Object ignore){
			Pair<float[][],float[]> alphasAndScales = doPassScaling(new StationaryLatticeWrapper(lattice), false, viterbiEmissionOnly, d);
			float[][] alphas = alphasAndScales.getFirst();
			float [] alphaLogScales = alphasAndScales.getSecond();
			Pair<float[][],float[]> betasAndScales = doPassScaling(new StationaryLatticeWrapper(lattice), true, viterbiEmissionOnly, d);
			float[][] betas = betasAndScales.getFirst();
			float [] betaLogScales = betasAndScales.getSecond();
			projectedNodeMarginals.incrementExpectedCounts(alphas, alphaLogScales, betas, betaLogScales, d, viterbiEmissionOnly);
			if (!viterbiEmissionOnly) edgeMarginals.incrementExpectedCounts(alphas, alphaLogScales, betas, betaLogScales, d);
		}};
		BetterThreader<Integer,Object> threader = new BetterThreader<Integer,Object>(func, numThreads);
		for (int d=0; d<lattice.numSequences(); ++d) threader.addFunctionArgument(d);
		threader.run();
//		System.out.printf("Estimated node marginals size: %.3fgb\n", projectedNodeMarginals.estimateMemoryUsage());
//		if (!viterbiEmissionOnly) System.out.printf("Estimated edge marginals size: %.3fgb\n", edgeMarginals.estimateMemoryUsage());
		return Pair.makePair((NodeMarginals) projectedNodeMarginals, (StationaryEdgeMarginals) edgeMarginals);
	}
 
开发者ID:tberg12,项目名称:murphy,代码行数:21,代码来源:ForwardBackward.java

示例2: computeMarginalsScaling

import tberg.murphy.tuple.Pair; //导入方法依赖的package包/类
public static Pair<NodeMarginals,StationaryEdgeMarginals> computeMarginalsScaling(final StationaryLattice lattice, final StationaryStateProjector nodeMarginalsStateProjector, final boolean viterbiEmissionOnly, int numThreads) {
		final NodeMarginalsScaling projectedNodeMarginals = new NodeMarginalsScaling(new StationaryLatticeWrapper(lattice), nodeMarginalsStateProjector);
		final StationaryEdgeMarginalsScaling edgeMarginals = (viterbiEmissionOnly ? null : new StationaryEdgeMarginalsScaling(lattice));
		BetterThreader.Function<Integer,Object> func = new BetterThreader.Function<Integer,Object>(){public void call(Integer d, Object ignore){
			Pair<double[][],double[]> alphasAndScales = doPassScaling(new StationaryLatticeWrapper(lattice), false, viterbiEmissionOnly, d);
			double[][] alphas = alphasAndScales.getFirst();
			double [] alphaLogScales = alphasAndScales.getSecond();
			Pair<double[][],double[]> betasAndScales = doPassScaling(new StationaryLatticeWrapper(lattice), true, viterbiEmissionOnly, d);
			double[][] betas = betasAndScales.getFirst();
			double [] betaLogScales = betasAndScales.getSecond();
			projectedNodeMarginals.incrementExpectedCounts(alphas, alphaLogScales, betas, betaLogScales, d, viterbiEmissionOnly);
			if (!viterbiEmissionOnly) edgeMarginals.incrementExpectedCounts(alphas, alphaLogScales, betas, betaLogScales, d);
		}};
		BetterThreader<Integer,Object> threader = new BetterThreader<Integer,Object>(func, numThreads);
		for (int d=0; d<lattice.numSequences(); ++d) threader.addFunctionArgument(d);
		threader.run();
//		System.out.printf("Estimated node marginals size: %.3fgb\n", projectedNodeMarginals.estimateMemoryUsage());
//		if (!viterbiEmissionOnly) System.out.printf("Estimated edge marginals size: %.3fgb\n", edgeMarginals.estimateMemoryUsage());
		return Pair.makePair((NodeMarginals) projectedNodeMarginals, (StationaryEdgeMarginals) edgeMarginals);
	}
 
开发者ID:tberg12,项目名称:murphy,代码行数:21,代码来源:ForwardBackward.java

示例3: test

import tberg.murphy.tuple.Pair; //导入方法依赖的package包/类
public static void test(DifferentiableFunction func, double[] x, double relEps, double delInitial, double delMin) {
	double[] nextX = a.copy(x);
	Pair<Double,double[]> valAndGrad = func.calculate(x);
	double baseVal = valAndGrad.getFirst();
	double[] grad = valAndGrad.getSecond();
	for (int i=0; i<x.length; ++i) {
		double delta = delInitial;
		boolean ok = false;
		double empDeriv = 0.0;
		while (delta > delMin && !ok) {
			nextX[i] += delta;
			double nextVal = func.calculate(nextX).getFirst();
			empDeriv = (nextVal - baseVal) / delta;
			if (close(empDeriv, grad[i], relEps)) {
				System.out.printf("Gradient ok for dim %d, delta %f, calculated %f, empirical: %f\n", i, delta, grad[i], empDeriv);
				ok = true;
			}
			nextX[i] -= delta;
			if (!ok) delta /= 2.0;
		}
		if (!ok) System.out.printf("Empirical gradient step-size underflow dim %d, delta %.12f, calculated %.12f, empirical: %.12f\n", i, delta, grad[i], empDeriv);
	}
}
 
开发者ID:tberg12,项目名称:murphy,代码行数:24,代码来源:EmpiricalGradientTester.java

示例4: minimize

import tberg.murphy.tuple.Pair; //导入方法依赖的package包/类
public double[] minimize(List<DifferentiableFunction> functions, double[] initial, boolean verbose, Callback iterCallbackFunction) {
	Random rand = new Random(0);
	double[] guess = a.copy(initial);
	double update = 0;
	for (int epoch=0; epoch<epochs; ++epoch) {
		double valSum = 0.0;
		double[] gradSum = new double[guess.length];
		int[] indices = a.shuffle(a.enumerate(0, functions.size()), rand);
		for (int funcIndex : indices) {
			DifferentiableFunction func = functions.get(funcIndex);
			Pair<Double,double[]> valAndGrad = func.calculate(guess);
			valSum += valAndGrad.getFirst();
			double[] grad = valAndGrad.getSecond();
			double learningRate = startLearningRate + update/(epochs*functions.size()) * (endLearningRate - startLearningRate);
			a.combi(guess, 1.0, grad, -learningRate);
			a.combi(gradSum, 1.0, grad, 1.0);
			update++;
		}
		if (verbose) System.out.println(String.format("[SGDMinimizer.minimize] Epoch %d ended with value %.6f", epoch, valSum));
		if (iterCallbackFunction != null) iterCallbackFunction.callback(guess, epoch, valSum, gradSum);
	}
	return guess;
}
 
开发者ID:tberg12,项目名称:murphy,代码行数:24,代码来源:SGDMinimizer.java

示例5: lineSearch

import tberg.murphy.tuple.Pair; //导入方法依赖的package包/类
private static Pair<double[],Double> lineSearch(DifferentiableFunction function, double[] initial, double[] direction, double initialStepSize, double stepSizeMultiplier) {
	double stepSize = initialStepSize;
	Pair<Double,double[]> initialValAndGrad = function.calculate(initial);
	double val = initialValAndGrad.getFirst();
	final double[] grad = initialValAndGrad.getSecond();
	double initialDirectionalDerivative = a.innerProd(grad, direction);
	double gradMax = a.max(a.abs(grad));
	double[] guess = null;
	double guessValue = 0.0;
	boolean sufficientDecreaseObtained = false;
	while (!sufficientDecreaseObtained) {
		guess = a.comb(initial, 1.0, direction, stepSize);
		guessValue = function.calculate(guess).getFirst();
		double sufficientDecreaseValue = val + LINE_SEARCH_SUFF_DECR * initialDirectionalDerivative * stepSize;
		sufficientDecreaseObtained = (guessValue <= sufficientDecreaseValue + EPS);
		if (!sufficientDecreaseObtained) {
			if (stepSize < EPS && stepSize * gradMax < EPS) {
				System.out.printf("[LBFGSMinimizer.minimize]: Line search step size underflow: %.15f, %.15f, %.15f, %.15f, %.15f, %.15f\n", stepSize, initialDirectionalDerivative, gradMax, guessValue, sufficientDecreaseValue, val);
				return null;
			}
			stepSize *= stepSizeMultiplier;
		}
	}
	return Pair.makePair(guess, stepSize);
}
 
开发者ID:tberg12,项目名称:murphy,代码行数:26,代码来源:LBFGSMinimizer.java

示例6: minimize

import tberg.murphy.tuple.Pair; //导入方法依赖的package包/类
public float[] minimize(List<DifferentiableFunction> functions, float[] initial, boolean verbose, Callback iterCallbackFunction) {
  Random rand = new Random(0);
  float[] guess = a.copy(initial);
  double update = 0;
  for (int epoch=0; epoch<epochs; ++epoch) {
    double valSum = 0.0;
    int[] indices = a.shuffle(a.enumerate(0, functions.size()), rand);
    for (int funcIndex : indices) {
      DifferentiableFunction func = functions.get(funcIndex);
      Pair<Double,CounterInterface<Integer>> valAndGrad = func.calculate(guess);
      valSum += valAndGrad.getFirst();
      CounterInterface<Integer> grad = valAndGrad.getSecond();
      double learningRate = startLearningRate + update/(epochs*functions.size()) * (endLearningRate - startLearningRate);
      for (Map.Entry<Integer,Double> entry : grad.entries()) {
        guess[entry.getKey()] += -learningRate * entry.getValue();
      }
      update++;
    }
    if (verbose) System.out.println(String.format("[SGDMinimizer.minimize] Epoch %d ended with value %.6f", epoch, valSum));
    if (iterCallbackFunction != null) iterCallbackFunction.callback(guess, epoch, valSum);
  }
  return guess;
}
 
开发者ID:tberg12,项目名称:murphy,代码行数:24,代码来源:SGDMinimizer.java

示例7: preprocessDatum

import tberg.murphy.tuple.Pair; //导入方法依赖的package包/类
private static ProcDatum preprocessDatum(Datum datum, float[][] spectralAtoms) {
	Pair<float[][],Float> spectAndSecondsPerFrame = computeSpect(datum.wave);
	float[][] spect = spectAndSecondsPerFrame.getFirst();
	float secondsPerFrame = spectAndSecondsPerFrame.getSecond();
	float[][] activations = null;
	if (Main.nmfType == NMFType.KL) {
		activations = (Main.useGpu ? NMFUtilOpenCL.nmfKLGPU(spectralAtoms, false, a.onesFloat(spect.length, spectralAtoms.length), true, spect, Main.nmfNumIters, (float) Main.nmfSilenceEps, (float) Main.nmfMinEps).getSecond() : NMFUtilOpenCL.nmfKL(spectralAtoms, false, a.onesFloat(spect.length, spectralAtoms.length), true, spect, Main.nmfNumIters, (float) Main.nmfSilenceEps, (float) Main.nmfMinEps).getSecond());
	} else if (Main.nmfType == NMFType.Beta) {
		activations = (Main.useGpu ? NMFUtilOpenCL.nmfBetaGPU(spectralAtoms, false, a.onesFloat(spect.length, spectralAtoms.length), true, spect, Main.nmfNumIters, (float) Main.nmfSilenceEps, (float) Main.nmfMinEps, (float) Main.nmfBeta).getSecond() : NMFUtilOpenCL.nmfBeta(spectralAtoms, false, a.onesFloat(spect.length, spectralAtoms.length), true, spect, Main.nmfNumIters, (float) Main.nmfSilenceEps, (float) Main.nmfMinEps, (float) Main.nmfBeta).getSecond());
	} else if (Main.nmfType == NMFType.LogNormal) {
		float startStepSize = 1e-7f;
		float endStepSize = 1e-7f;
		float init = 1e-5f;
		activations = (Main.useGpu ? NMFUtilOpenCL.nmfLogNormalExpGradGPU(spectralAtoms, false, a.scale(a.onesFloat(spect.length, spectralAtoms.length), init), true, spect, startStepSize, endStepSize, Main.nmfNumIters, (float) Main.nmfSilenceEps, (float) Main.nmfMinEps, (float) Main.nmfC).getSecond() : NMFUtilOpenCL.nmfLogNormalExpGrad(spectralAtoms, false, a.scale(a.onesFloat(spect.length, spectralAtoms.length), init), true, spect, startStepSize, endStepSize, Main.nmfNumIters, (float) Main.nmfSilenceEps, (float) Main.nmfMinEps, (float) Main.nmfC).getSecond());
	}  else {
		activations = (Main.useGpu ? NMFUtilOpenCL.nmfL2GPU(spectralAtoms, false, a.onesFloat(spect.length, spectralAtoms.length), true, spect, Main.nmfNumIters, (float) Main.nmfSilenceEps, (float) Main.nmfMinEps).getSecond() : NMFUtilOpenCL.nmfL2(spectralAtoms, false, a.onesFloat(spect.length, spectralAtoms.length), true, spect, Main.nmfNumIters, (float) Main.nmfSilenceEps, (float) Main.nmfMinEps).getSecond());
	}
	
	if (ACTIVATIONS_POW) {
		a.powi(activations, POW);
	}
	if (ACTIVATIONS_DIVIDE_BY_MAX) {
		divideByMax(activations);
	}
	if (ACTIVATIONS_STRETCH_TO_UNIT) {
		stretchToUnit(activations);
	}
	if (ACTIVATIONS_LOG) {
		logSpace(activations);
		stretchToUnit(activations);
	}
	if (ACTIVATIONS_SCALE) {
		a.scalei(activations, SCALE);
	}
	if (ACTIVATIONS_BUCKET) {
		bucket(activations);
	}
	return new ProcDatum(datum, spect, activations, secondsPerFrame);
}
 
开发者ID:tberg12,项目名称:klavier,代码行数:40,代码来源:Preprocessing.java

示例8: Clustering

import tberg.murphy.tuple.Pair; //导入方法依赖的package包/类
public Clustering(File corpusRoot) {
    knownChords = new HashSet<Chord>();
    List<List<Chord>> sequences = loadSongs(corpusRoot);
    featIndexer = new HashMapIndexer<String>();
    chordIndexer = new HashMapIndexer<Chord>();
    System.out.println("loaded activations");

    Pair<int[][][], double[][][]> chordFeatures = computeChordFeatures();
    chordFeatIndices = chordFeatures.getFirst();
    chordFeatValues = chordFeatures.getSecond();
    System.out.println("computed chord features");

    Pair<int[][][][], double[][][][]> emissionFeatures = computeEmissionFeatures(sequences);
    emissionFeatIndices = emissionFeatures.getFirst();
    emissionFeatValues = emissionFeatures.getSecond();
    System.out.println("computed emission features");
    //System.out.println(sequences);
    //System.out.println(emissionFeatIndices.length);
    //System.out.println(emissionFeatIndices[0].length);
    //System.out.println(emissionFeatIndices[0][0].length);
    //System.out.println(emissionFeatIndices[0][0][0].length);

    Pair<int[][][], double[][][]> transitionFeatures = computeTransitionFeatures(sequences);
    transitionFeatIndices = transitionFeatures.getFirst();
    transitionFeatValues = transitionFeatures.getSecond();
    System.out.println("computed transition features");
}
 
开发者ID:tberg12,项目名称:klavier,代码行数:28,代码来源:Clustering.java

示例9: IMSLPIndepTransitionReader

import tberg.murphy.tuple.Pair; //导入方法依赖的package包/类
public IMSLPIndepTransitionReader(File corpusRoot) {
    loadSongs(corpusRoot);

    //int[] lengthCounts = new int[1000];

    //for (boolean[][] m : activations) {
    //    int offset = 0;
    //    int counter = 0;
    //    //System.out.println(m.length);
    //    for (int pitch = 0; pitch < m[0].length; pitch++) {
    //        while (offset < m.length && m[offset][pitch]) {
    //            offset++; }
    //        while (offset + counter < m.length && !m[offset + counter][pitch]) {
    //            counter++; }
    //        //while (m[offset++][pitch]) { if (offset >= m.length) break; }
    //        //while (m[offset + counter++][pitch]) { if (offset + counter >= m.length) break; }
    //        counter /= 10;
    //        counter = Math.min(counter, lengthCounts.length-1);
    //        lengthCounts[counter]++;
    //        offset = offset + counter;
    //        counter = 0;
    //    }
    //}

    //System.out.println(a.toString(lengthCounts));
    //System.exit(1);

    transitionScores = computeTransitionScores();
    Pair<double[], double[]> meansAndVars = computeStats();
    widthMeans = meansAndVars.getFirst();
    widthVars = meansAndVars.getSecond();
}
 
开发者ID:tberg12,项目名称:klavier,代码行数:33,代码来源:IMSLPIndepTransitionReader.java

示例10: decode

import tberg.murphy.tuple.Pair; //导入方法依赖的package包/类
public static Pair<TransitionState[],int[]> decode(EmissionModel emissionModel, TransitionModel transitionModel, int beamSize) {
  GeneralPriorityQueue<SparseSemiMarkovDP.BeamState>[] alphas = doForwardPass(emissionModel, transitionModel, beamSize);
  Pair<TransitionState[],int[]> statesAndWidths = followBackpointers(alphas, emissionModel);
  TransitionState[] decodeStates = statesAndWidths.getFirst();
  int[] decodeWidths = statesAndWidths.getSecond();
  return Pair.makePair(decodeStates, decodeWidths);
}
 
开发者ID:tberg12,项目名称:murphy,代码行数:8,代码来源:SparseSemiMarkovDP.java

示例11: addNullBackpointers

import tberg.murphy.tuple.Pair; //导入方法依赖的package包/类
private static Collection<BeamState> addNullBackpointers(Collection<Pair<TransitionState,Double>> without) {
  List<BeamState> with = new ArrayList<BeamState>();
  for (Pair<TransitionState,Double> startPair : without) {
    BeamState beamState = new BeamState(startPair.getFirst());
    beamState.score = startPair.getSecond();
    beamState.backPointer = Pair.makePair(-1, null);
    with.add(beamState);
  }
  return with;
}
 
开发者ID:tberg12,项目名称:murphy,代码行数:11,代码来源:SparseSemiMarkovDP.java

示例12: followBackpointers

import tberg.murphy.tuple.Pair; //导入方法依赖的package包/类
private static Pair<TransitionState[],int[]> followBackpointers(GeneralPriorityQueue<SparseSemiMarkovDP.BeamState>[] alphas, EmissionModel emissionModel) {
  List<TransitionState> transStateDecodeList = new ArrayList<TransitionState>();
  List<Integer> widthsDecodeList = new ArrayList<Integer>();
  TransitionState bestFinalTs = null;
  double bestFinalScore = Double.NEGATIVE_INFINITY;
  for (BeamState beamState : alphas[emissionModel.sequenceLength()].getObjects()) {
    double score = beamState.score + beamState.transState.endScore();
    if (score > bestFinalScore) {
      bestFinalScore = score;
      bestFinalTs = beamState.transState;
    }
  }

  int currentT = emissionModel.sequenceLength();
  TransitionState currentTs = bestFinalTs;
  while (true) {
    Pair<Integer,TransitionState> backpointer = alphas[currentT].getObject(new BeamState(currentTs)).backPointer;
    int width =  currentT - backpointer.getFirst();
    transStateDecodeList.add(currentTs);
    widthsDecodeList.add(width);
    currentT = backpointer.getFirst();
    currentTs = backpointer.getSecond();
    if (currentT == 0) {
      break;
    }
  }

  Collections.reverse(transStateDecodeList);
  Collections.reverse(widthsDecodeList);
  int[] widthsDecode = a.toIntArray(widthsDecodeList);
  return Pair.makePair(transStateDecodeList.toArray(new TransitionState[0]), widthsDecode);
}
 
开发者ID:tberg12,项目名称:murphy,代码行数:33,代码来源:SparseSemiMarkovDP.java

示例13: minimize

import tberg.murphy.tuple.Pair; //导入方法依赖的package包/类
public double[] minimize(List<DifferentiableFunction> functions, double[] initial, boolean verbose, Callback iterCallbackFunction) {
	Random rand = new Random(0);
	double[] guess = a.copy(initial);
	double[] sqrGradSum = new double[guess.length];
	a.addi(sqrGradSum, delta);
	final double r = eta * regConstant;
	for (int epoch=0; epoch<epochs; ++epoch) {
		double epochValSum = 0.0;
		double[] epochGradSum = new double[guess.length];
		for (int funcIndex : a.shuffle(a.enumerate(0, functions.size()), rand)) {
			DifferentiableFunction func = functions.get(funcIndex);
			Pair<Double,double[]> valAndGrad = func.calculate(guess);
			epochValSum += valAndGrad.getFirst();
			double[] grad = valAndGrad.getSecond();
			a.combi(epochGradSum, 1.0, grad, 1.0);
		
			double[] sqrGrad = a.sqr(grad);
			a.combi(sqrGradSum, 1.0, sqrGrad, 1.0);
			
			for (int i=0; i<guess.length; ++i) {
				double s = Math.sqrt(sqrGradSum[i]);
				guess[i] = (s * guess[i] - eta * grad[i]) / (r + s);
			}
		}
		if (verbose) System.out.println(String.format("[AdaGradMinimizer.minimize] Epoch %d ended with value %.6f", epoch, epochValSum + regConstant * a.innerProd(guess, guess)));
		if (iterCallbackFunction != null) iterCallbackFunction.callback(guess, epoch, epochValSum, epochGradSum);
	}
	return guess;
}
 
开发者ID:tberg12,项目名称:murphy,代码行数:30,代码来源:AdaGradL2Minimizer.java

示例14: minimize

import tberg.murphy.tuple.Pair; //导入方法依赖的package包/类
public double[] minimize(List<DifferentiableFunction> functions, double[] initial, boolean verbose, Callback iterCallbackFunction) {
	Random rand = new Random(0);
	double[] guess = a.copy(initial);
	double[] sqrGradSum = new double[guess.length];
	a.addi(sqrGradSum, delta);
	final double r = eta * regConstant;
	for (int epoch=0; epoch<epochs; ++epoch) {
		double epochValSum = 0.0;
		double[] epochGradSum = new double[guess.length];
		for (int funcIndex : a.shuffle(a.enumerate(0, functions.size()), rand)) {
			DifferentiableFunction func = functions.get(funcIndex);
			Pair<Double,double[]> valAndGrad = func.calculate(guess);
			epochValSum += valAndGrad.getFirst();
			double[] grad = valAndGrad.getSecond();
			a.combi(epochGradSum, 1.0, grad, 1.0);
		
			double[] sqrGrad = a.sqr(grad);
			a.combi(sqrGradSum, 1.0, sqrGrad, 1.0);
			
			for (int i=0; i<guess.length; ++i) {
				double s = Math.sqrt(sqrGradSum[i]);
				double xHalf = guess[i] - (eta/s)*grad[i];
				double x = Math.abs(xHalf) - (r/s);
				if (x > 0) {
					guess[i] = (xHalf > 0 ? 1.0 : -1.0) * x;
				} else {
					guess[i] = 0.0;
				}
			}
		}
		if (verbose) System.out.println(String.format("[AdaGradMinimizer.minimize] Epoch %d ended with value %.6f", epoch, epochValSum + regConstant * a.sum(a.abs(guess))));
		if (iterCallbackFunction != null) iterCallbackFunction.callback(guess, epoch, epochValSum, epochGradSum);
	}
	return guess;
}
 
开发者ID:tberg12,项目名称:murphy,代码行数:36,代码来源:AdaGradL1Minimizer.java

示例15: buildSpectralAtomsAndEnvelopes

import tberg.murphy.tuple.Pair; //导入方法依赖的package包/类
private static Pair<Pair<float[][],float[][]>,Float> buildSpectralAtomsAndEnvelopes(String notesBasePath, String notesPaths, String instrName) {
		JaggedMatrixAverager spectralAtomsAvg = new JaggedMatrixAverager(PitchEventIO.N_MIDI_PITCH_IDS);
		JaggedMatrixAverager envelopesAvg = new JaggedMatrixAverager(PitchEventIO.N_MIDI_PITCH_IDS);
		float secondsPerFrame = 0.0f;
		for (String notesPath : notesPaths.trim().split(":")) {
			List<Datum> data = DatasetIO.readLabeledData(notesBasePath+"/"+instrName+"/"+notesPath, Float.POSITIVE_INFINITY);
			System.out.println(notesBasePath+"/"+instrName+"/"+notesPath+" num: "+data.size());
			for (Datum datum : data) {
				Pair<float[][],Float> spectAndSecondsPerFrame = computeSpect(datum.wave);
				float[][] spect = spectAndSecondsPerFrame.getFirst();
				secondsPerFrame = spectAndSecondsPerFrame.getSecond();
				for (int i=0; i<datum.events.size(); ++i) {
					int noteIndex = datum.events.get(i).noteIndex;
					if (noteIndex >= 0 && noteIndex < PitchEventIO.N_MIDI_PITCH_IDS) {

						List<float[]> subSpectList = new ArrayList<float[]>();
						for (int t : PitchEventUtil.framesByOverlap(datum.events.get(i).onsetSec + ((float) Main.initSpectEnvNotesOffsetMs * 1e-3f), datum.events.get(i).offsetSec + ((float) Main.initSpectEnvNotesOffsetMs * 1e-3f), secondsPerFrame, 0, spect.length-1)) {
//						for (int t : PitchEventUtil.framesByCenterWithinInterval(datum.events.get(i).onsetSec + ((float) Main.notesOffsetMs * 1e-3f), datum.events.get(i).offsetSec + ((float) Main.notesOffsetMs * 1e-3f), secondsPerFrame, 0, spect.length-1)) {
							subSpectList.add(spect[t]);
						}
						if (subSpectList.isEmpty()) continue;
						float[][] subSpect = subSpectList.toArray(new float[0][]);

						Pair<float[][],float[][]> atomAndEnvelope = null;
						if (Main.nmfType == NMFType.KL) {
							atomAndEnvelope = NMFUtilOpenCL.nmfKL(subSpect, 1, 20, (float) Main.nmfSilenceEps, (float) Main.nmfMinEps);
						} else if (Main.nmfType == NMFType.Beta) {
							atomAndEnvelope = NMFUtilOpenCL.nmfBeta(subSpect, 1, 20, (float) Main.nmfSilenceEps, (float) Main.nmfMinEps, (float) Main.nmfBeta);
						} else if (Main.nmfType == NMFType.LogNormal) {
							int iters = 2000;
							float startStepSize = 1e-8f;
							float endStepSize = 1e-8f;
							float init = 1e-5f;
							atomAndEnvelope = NMFUtilOpenCL.nmfLogNormalExpGrad(a.scale(a.onesFloat(1, subSpect[0].length), init), true, a.scale(a.onesFloat(subSpect.length, 1), init), true, subSpect, startStepSize, endStepSize, iters, (float) Main.nmfSilenceEps, (float) Main.nmfMinEps, (float) Main.nmfC);
							while (a.hasinf(atomAndEnvelope.getFirst()) || a.hasnan(atomAndEnvelope.getFirst()) || a.hasinf(atomAndEnvelope.getSecond()) || a.hasnan(atomAndEnvelope.getSecond())) {
								startStepSize *= 0.8;
								endStepSize *= 0.8;
								atomAndEnvelope = NMFUtilOpenCL.nmfLogNormalExpGrad(a.scale(a.onesFloat(1, subSpect[0].length), init), true, a.scale(a.onesFloat(subSpect.length, 1), init), true, subSpect, startStepSize, endStepSize, iters, (float) Main.nmfSilenceEps, (float) Main.nmfMinEps, (float) Main.nmfC);
							}
						} else {
							atomAndEnvelope = NMFUtilOpenCL.nmfL2(subSpect, 1, 20, (float) Main.nmfSilenceEps, (float) Main.nmfMinEps);
						}
						float[] atom = atomAndEnvelope.getFirst()[0];
						float[] envelope = a.transpose(atomAndEnvelope.getSecond())[0];

//						a.scalei(atom, 1.0f / a.max(atom));
//						a.scalei(atom, 1.0f / a.sum(atom));
						a.scalei(atom, 1.0f / (float) Math.sqrt(a.sum(a.sqr(atom))));
						spectralAtomsAvg.observe(noteIndex, atom);

						a.scalei(envelope, 1.0f / a.max(envelope));
						envelopesAvg.observe(noteIndex, envelope);
					}
				}
			}
		}
		return Pair.makePair(Pair.makePair(spectralAtomsAvg.getAverage(), envelopesAvg.getAverage()), secondsPerFrame);
	}
 
开发者ID:tberg12,项目名称:klavier,代码行数:59,代码来源:Preprocessing.java


注:本文中的tberg.murphy.tuple.Pair.getSecond方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。