Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Word2vec gui #239

Merged
merged 2 commits into from May 5, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -118,7 +118,7 @@ public void preProcessLine(INDArray line) {
if(i > rho.size())
throw new IllegalStateException("I > rho size");
alpha.putScalar(i, rho.get(i) * Nd4j.getBlasWrapper().dot(gradient, s.get(i)));
if(alpha.data().dataType() == DataBuffer.DOUBLE)
if(alpha.data().dataType() == DataBuffer.Type.DOUBLE)
Nd4j.getBlasWrapper().axpy(-1.0 * alpha.getDouble(i), gradient, y.get(i));
else
Nd4j.getBlasWrapper().axpy(-1.0f * alpha.getFloat(i), gradient, y.get(i));
Expand All @@ -133,7 +133,7 @@ public void preProcessLine(INDArray line) {
if(i >= alpha.length())
break;
double beta = rho.get(i) * Nd4j.getBlasWrapper().dot(y.get(i),gradient);
if(alpha.data().dataType() == DataBuffer.DOUBLE)
if(alpha.data().dataType() == DataBuffer.Type.DOUBLE)
Nd4j.getBlasWrapper().axpy(alpha.getDouble(i) * beta, gradient, s.get(i));
else
Nd4j.getBlasWrapper().axpy(alpha.getFloat(i) * (float) beta, gradient, s.get(i));
Expand Down
Expand Up @@ -32,7 +32,7 @@ public class DefaultStepFunction implements StepFunction {
public void step(INDArray x, INDArray line, Object[] params) {
double alam = (double) params[0];
double oldAlam = (double) params[1];
if(x.data().dataType() == DataBuffer.DOUBLE) {
if(x.data().dataType() == DataBuffer.Type.DOUBLE) {
Nd4j.getBlasWrapper().axpy(alam - oldAlam, line, x);
}
else {
Expand Down
Expand Up @@ -55,8 +55,8 @@ public class ConvolutionDownSampleLayerTest {
@Test
public void testConvolution() throws Exception {
boolean switched = false;
if(Nd4j.dtype == DataBuffer.FLOAT) {
Nd4j.dtype = DataBuffer.DOUBLE;
if(Nd4j.dtype == DataBuffer.Type.FLOAT) {
Nd4j.dtype = DataBuffer.Type.DOUBLE;
switched = true;
}
MnistDataFetcher data = new MnistDataFetcher(true);
Expand All @@ -70,7 +70,7 @@ public void testConvolution() throws Exception {
ConvolutionDownSampleLayer c = LayerFactories.getFactory(n.getLayer()).create(n);

if(switched) {
Nd4j.dtype = DataBuffer.FLOAT;
Nd4j.dtype = DataBuffer.Type.FLOAT;
}

}
Expand Down
Expand Up @@ -58,7 +58,7 @@ public class MultiLayerTest {

@Test
public void testDbnFaces() {
Nd4j.dtype = DataBuffer.DOUBLE;
Nd4j.dtype = DataBuffer.Type.DOUBLE;
DataSetIterator iter = new LFWDataSetIterator(28,28);

DataSet next = iter.next();
Expand Down
Expand Up @@ -38,7 +38,7 @@ public class BarnesHutTsneTest {
@Test
public void testTsne() throws Exception {
Nd4j.ENFORCE_NUMERICAL_STABILITY = true;
Nd4j.factory().setDType(DataBuffer.DOUBLE);
Nd4j.factory().setDType(DataBuffer.Type.DOUBLE);
Nd4j.getRandom().setSeed(123);
BarnesHutTsne b = new BarnesHutTsne.Builder().stopLyingIteration(250)
.theta(0.5).learningRate(500).useAdaGrad(false)
Expand Down
Expand Up @@ -35,11 +35,10 @@ public class TsneTest {

@Test
public void testTsne() throws Exception {
Nd4j.dtype = DataBuffer.DOUBLE;
Nd4j.dtype = DataBuffer.Type.DOUBLE;
Tsne calculation = new Tsne.Builder().setMaxIter(1).usePca(false).setSwitchMomentumIteration(20)
.normalize(true).useAdaGrad(false).learningRate(500).perplexity(20).minGain(1e-1f)
.build();
Nd4j.getResourceManager().disable();
ClassPathResource resource = new ClassPathResource("/mnist2500_X.txt");
File f = resource.getFile();
INDArray data = Nd4j.readTxt(f.getAbsolutePath()," ");
Expand Down
Expand Up @@ -231,7 +231,7 @@ public void iterateSample(VocabWord w1, VocabWord w2,AtomicLong nextRandom,doub
//gradient
double g = useAdaGrad ? w1.getGradient(i, (1 - code - f)) : (1 - code - f) * alpha;

if(neu1e.data().dataType() == DataBuffer.FLOAT) {
if(neu1e.data().dataType() == DataBuffer.Type.FLOAT) {
Nd4j.getBlasWrapper().axpy((float) g, syn1, neu1e);
Nd4j.getBlasWrapper().axpy((float) g, l1, syn1);

Expand Down Expand Up @@ -281,18 +281,18 @@ else if (f < -MAX_EXP)
g = label * (useAdaGrad ? w1.getGradient(target, alpha) : alpha);
else
g = useAdaGrad ? w1.getGradient(target, label - expTable[(int)((f + MAX_EXP) * (expTable.length / MAX_EXP / 2))]) : (label - expTable[(int)((f + MAX_EXP) * (expTable.length / MAX_EXP / 2))]) * alpha;
if(syn0.data().dataType() == DataBuffer.DOUBLE)
if(syn0.data().dataType() == DataBuffer.Type.DOUBLE)
Nd4j.getBlasWrapper().axpy(g,neu1e,l1);
else
Nd4j.getBlasWrapper().axpy((float) g,neu1e,l1);

if(syn0.data().dataType() == DataBuffer.DOUBLE)
if(syn0.data().dataType() == DataBuffer.Type.DOUBLE)
Nd4j.getBlasWrapper().axpy(g,syn1Neg.slice(target),l1);
else
Nd4j.getBlasWrapper().axpy((float) g,syn1Neg.slice(target),l1);
}

if(syn0.data().dataType() == DataBuffer.DOUBLE)
if(syn0.data().dataType() == DataBuffer.Type.DOUBLE)
Nd4j.getBlasWrapper().axpy(1.0,neu1e,l1);

else
Expand Down Expand Up @@ -366,7 +366,7 @@ public void iterate(VocabWord w1, VocabWord w2) {
//gradient
double g = (1 - code - f) * (useAdaGrad ? w1.getGradient(i, alpha) : alpha);

if(syn0.data().dataType() == DataBuffer.DOUBLE) {
if(syn0.data().dataType() == DataBuffer.Type.DOUBLE) {
Nd4j.getBlasWrapper().axpy(g, syn1, neu1e);
Nd4j.getBlasWrapper().axpy(g, l1, syn1);
}
Expand All @@ -380,7 +380,7 @@ public void iterate(VocabWord w1, VocabWord w2) {



if(syn0.data().dataType() == DataBuffer.DOUBLE)
if(syn0.data().dataType() == DataBuffer.Type.DOUBLE)
Nd4j.getBlasWrapper().axpy(1.0,neu1e,l1);

else
Expand Down
Expand Up @@ -332,7 +332,7 @@ public INDArray randomTransformMatrix() {
binary.put(indices,block);
NDArrayIndex[] indices2 = new NDArrayIndex[]{interval(0,block.rows()),interval(numHidden,numHidden + block.columns())};
binary.put(indices2,randomTransformBlock());
if(binary.data().dataType() == DataBuffer.DOUBLE)
if(binary.data().dataType() == DataBuffer.Type.DOUBLE)
return Nd4j.getBlasWrapper().scal(scalingForInit,binary);
return Nd4j.getBlasWrapper().scal((float) scalingForInit,binary);
}
Expand All @@ -352,7 +352,7 @@ INDArray randomClassificationMatrix() {
INDArray ret = Nd4j.zeros(numOuts,numHidden + 1);
INDArray insert = Nd4j.rand(numOuts,numHidden,-range,range,rng);
ret.put(new NDArrayIndex[] {interval(0,numOuts),interval(0,numHidden)},insert);
if(ret.data().dataType() == (DataBuffer.DOUBLE))
if(ret.data().dataType() == (DataBuffer.Type.DOUBLE))
return Nd4j.getBlasWrapper().scal(scalingForInit,ret);
return Nd4j.getBlasWrapper().scal((float) scalingForInit, ret);

Expand Down Expand Up @@ -546,7 +546,7 @@ public INDArray getBinaryINDArray(String left, String right) {
double cost = 0.0f; // the regularization cost
for (MultiDimensionalMap.Entry<String, String, INDArray> entry : currentMatrices.entrySet()) {
INDArray D = derivatives.get(entry.getFirstKey(), entry.getSecondKey());
if(D.data().dataType() == DataBuffer.DOUBLE)
if(D.data().dataType() == DataBuffer.Type.DOUBLE)
D = Nd4j.getBlasWrapper().scal(scale,D).addi(Nd4j.getBlasWrapper().scal(regCost, entry.getValue()));
else
D = Nd4j.getBlasWrapper().scal((float) scale,D).addi(Nd4j.getBlasWrapper().scal((float) regCost, entry.getValue()));
Expand All @@ -566,7 +566,7 @@ public INDArray getBinaryINDArray(String left, String right) {
for (String s : currentMatrices.keySet()) {
INDArray D = derivatives.get(s);
INDArray vector = currentMatrices.get(s);
if(D.data().dataType() == DataBuffer.DOUBLE)
if(D.data().dataType() == DataBuffer.Type.DOUBLE)
D = Nd4j.getBlasWrapper().scal(scale,D).addi(Nd4j.getBlasWrapper().scal(regCost,vector));
else
D = Nd4j.getBlasWrapper().scal((float) scale,D).addi(Nd4j.getBlasWrapper().scal((float) regCost,vector));
Expand All @@ -587,7 +587,7 @@ public INDArray getBinaryINDArray(String left, String right) {
for (String s : vocabCache.words()) {
INDArray D = derivatives.get(s);
INDArray vector = currentMatrices.vector(s);
if(D.data().dataType() == DataBuffer.DOUBLE)
if(D.data().dataType() == DataBuffer.Type.DOUBLE)
D = Nd4j.getBlasWrapper().scal(scale,D).addi(Nd4j.getBlasWrapper().scal(regCost,vector));
else
D = Nd4j.getBlasWrapper().scal((float) scale,D).addi(Nd4j.getBlasWrapper().scal((float) regCost,vector));
Expand Down Expand Up @@ -732,7 +732,7 @@ private INDArray computeINDArrayDeltaDown(INDArray deltaFull, INDArray leftVecto
INDArray deltaINDArray = Nd4j.create(size * 2, 1);
INDArray fullVector = Nd4j.concat(0, leftVector, rightVector);
for (int slice = 0; slice < size; ++slice) {
if(deltaFull.data().dataType() == DataBuffer.DOUBLE) {
if(deltaFull.data().dataType() == DataBuffer.Type.DOUBLE) {
INDArray scaledFullVector = Nd4j.getBlasWrapper().scal(deltaFull.getScalar(slice).getDouble(0),fullVector);
deltaINDArray = deltaINDArray.add(Wt.slice(slice).add(Wt.slice(slice).transpose()).mmul(scaledFullVector));
}
Expand Down Expand Up @@ -824,7 +824,7 @@ private INDArray getDoubleTensorGradient(INDArray deltaFull, INDArray leftVector
INDArray Wt_df = Nd4j.create(size * 2, size * 2, size);
INDArray fullVector = Nd4j.concat(0,leftVector, rightVector);
for (int slice = 0; slice < size; ++slice) {
if(Wt_df.data().dataType() == DataBuffer.DOUBLE)
if(Wt_df.data().dataType() == DataBuffer.Type.DOUBLE)
Wt_df.putSlice(slice, Nd4j.getBlasWrapper().scal(deltaFull.getDouble(slice),fullVector).mmul(fullVector.transpose()));
else
Wt_df.putSlice(slice, Nd4j.getBlasWrapper().scal((float) deltaFull.getDouble(slice),fullVector).mmul(fullVector.transpose()));
Expand Down
Expand Up @@ -339,7 +339,7 @@ public void iterateSample(Word2VecWork work,VocabWord w1, VocabWord w2,double a
double g = (1 - code - f) * (useAdaGrad ? w1.getGradient(i, alpha) : alpha);


if(neu1e.data().dataType() == DataBuffer.DOUBLE) {
if(neu1e.data().dataType() == DataBuffer.Type.DOUBLE) {
Nd4j.getBlasWrapper().axpy(g, syn1, neu1e);
Nd4j.getBlasWrapper().axpy(g, l1, syn1);
}
Expand Down Expand Up @@ -380,12 +380,12 @@ else if (f < -MAX_EXP)
g = (label - 0) * (useAdaGrad ? w1.getGradient(target, alpha) : alpha);
else
g = useAdaGrad ? w1.getGradient(target, label - expTable[(int)((f + MAX_EXP) * (expTable.length / MAX_EXP / 2))]) : (label - expTable[(int)((f + MAX_EXP) * (expTable.length / MAX_EXP / 2))]) * alpha;
if(syn1Neg.data().dataType() == DataBuffer.DOUBLE)
if(syn1Neg.data().dataType() == DataBuffer.Type.DOUBLE)
Nd4j.getBlasWrapper().axpy(g,neu1e,l1);
else
Nd4j.getBlasWrapper().axpy((float) g,neu1e,l1);

if(syn1Neg.data().dataType() == DataBuffer.DOUBLE)
if(syn1Neg.data().dataType() == DataBuffer.Type.DOUBLE)
Nd4j.getBlasWrapper().axpy(g,syn1Neg,l1);
else
Nd4j.getBlasWrapper().axpy((float) g,syn1Neg,l1);
Expand All @@ -395,7 +395,7 @@ else if (f < -MAX_EXP)



if(neu1e.data().dataType() == DataBuffer.DOUBLE)
if(neu1e.data().dataType() == DataBuffer.Type.DOUBLE)
Nd4j.getBlasWrapper().axpy(1.0,neu1e,l1);

else
Expand Down
Expand Up @@ -66,7 +66,7 @@ public void testLoaderBinary() throws IOException {

@Test
public void testCurrentFile() throws Exception {
Nd4j.dtype = DataBuffer.FLOAT;
Nd4j.dtype = DataBuffer.Type.FLOAT;
String url = "https://s3.amazonaws.com/dl4j-distribution/GoogleNews-vectors-negative300.bin.gz";
String path = "GoogleNews-vectors-negative300.bin.gz";
File toDl = new File(path);
Expand Down
Expand Up @@ -79,7 +79,7 @@ public ByteBuffer toBytes() {
public void fromString(String s) {
String[] split = s.split(" ");
paramMessage = Nd4j.create(split.length);
if(Nd4j.dataType() == DataBuffer.DOUBLE) {
if(Nd4j.dataType() == DataBuffer.Type.DOUBLE) {
for(int i = 0 ;i < split.length; i++) {
paramMessage.putScalar(i,Double.valueOf(split[i]));
}
Expand Down
Expand Up @@ -338,7 +338,7 @@ public void iterateSample(Word2VecWork work,VocabWord w1, VocabWord w2,double a
double g = (1 - code - f) * (useAdaGrad ? w1.getGradient(i, alpha) : alpha);


if(neu1e.data().dataType() == DataBuffer.DOUBLE) {
if(neu1e.data().dataType() == DataBuffer.Type.DOUBLE) {
Nd4j.getBlasWrapper().axpy(g, syn1, neu1e);
Nd4j.getBlasWrapper().axpy(g, l1, syn1);
}
Expand Down Expand Up @@ -379,12 +379,12 @@ else if (f < -MAX_EXP)
g = (label - 0) * (useAdaGrad ? w1.getGradient(target, alpha) : alpha);
else
g = useAdaGrad ? w1.getGradient(target, label - expTable[(int)((f + MAX_EXP) * (expTable.length / MAX_EXP / 2))]) : (label - expTable[(int)((f + MAX_EXP) * (expTable.length / MAX_EXP / 2))]) * alpha;
if(syn1Neg.data().dataType() == DataBuffer.DOUBLE)
if(syn1Neg.data().dataType() == DataBuffer.Type.DOUBLE)
Nd4j.getBlasWrapper().axpy(g,neu1e,l1);
else
Nd4j.getBlasWrapper().axpy((float) g,neu1e,l1);

if(syn1Neg.data().dataType() == DataBuffer.DOUBLE)
if(syn1Neg.data().dataType() == DataBuffer.Type.DOUBLE)
Nd4j.getBlasWrapper().axpy(g,syn1Neg,l1);
else
Nd4j.getBlasWrapper().axpy((float) g,syn1Neg,l1);
Expand All @@ -394,7 +394,7 @@ else if (f < -MAX_EXP)



if(neu1e.data().dataType() == DataBuffer.DOUBLE)
if(neu1e.data().dataType() == DataBuffer.Type.DOUBLE)
Nd4j.getBlasWrapper().axpy(1.0,neu1e,l1);

else
Expand Down
Expand Up @@ -37,7 +37,7 @@ public SaxpyMap(Number alpha, int fromIndex, int toIndex, String from, String to

@Override
public InMemoryLookupTable apply(InMemoryLookupTable inMemoryLookupTable) {
if(inMemoryLookupTable.getSyn0().data().dataType() == DataBuffer.DOUBLE)
if(inMemoryLookupTable.getSyn0().data().dataType() == DataBuffer.Type.DOUBLE)
Nd4j.getBlasWrapper().axpy(alpha.doubleValue(),getFrom(inMemoryLookupTable),getTo(inMemoryLookupTable));
else
Nd4j.getBlasWrapper().axpy(alpha.floatValue(),getFrom(inMemoryLookupTable),getTo(inMemoryLookupTable));
Expand Down
Expand Up @@ -146,7 +146,7 @@ public void iterateSample(Word2VecParam param,VocabWord w1, VocabWord w2,double
double g = (1 - code - f) * (useAdaGrad ? w1.getGradient(i, alpha) : alpha);


if (neu1e.data().dataType() == DataBuffer.DOUBLE) {
if (neu1e.data().dataType() == DataBuffer.Type.DOUBLE) {
Nd4j.getBlasWrapper().axpy(g, syn1, neu1e);
Nd4j.getBlasWrapper().axpy(g, l1, syn1);
} else {
Expand Down Expand Up @@ -189,12 +189,12 @@ else if (f < -MAX_EXP)
g = label * (useAdaGrad ? w1.getGradient(target, alpha) : alpha);
else
g = useAdaGrad ? w1.getGradient(target, label - expTable[(int)((f + MAX_EXP) * (expTable.length / MAX_EXP / 2))]) : (label - expTable[(int)((f + MAX_EXP) * (expTable.length / MAX_EXP / 2))]) * alpha;
if(syn1Neg.data().dataType() == DataBuffer.DOUBLE)
if(syn1Neg.data().dataType() == DataBuffer.Type.DOUBLE)
Nd4j.getBlasWrapper().axpy(g,neu1e,l1);
else
Nd4j.getBlasWrapper().axpy((float) g,neu1e,l1);

if(syn1Neg.data().dataType() == DataBuffer.DOUBLE)
if(syn1Neg.data().dataType() == DataBuffer.Type.DOUBLE)
Nd4j.getBlasWrapper().axpy(g,syn1Neg,l1);
else
Nd4j.getBlasWrapper().axpy((float) g,syn1Neg,l1);
Expand All @@ -203,7 +203,7 @@ else if (f < -MAX_EXP)
}
}

if(neu1e.data().dataType() == DataBuffer.DOUBLE)
if(neu1e.data().dataType() == DataBuffer.Type.DOUBLE)
Nd4j.getBlasWrapper().axpy(1.0,neu1e,l1);

else
Expand Down
Expand Up @@ -185,7 +185,7 @@ public void iterateSample(VocabWord w1, VocabWord w2,double alpha) {
double g = (1 - code - f) * (useAdaGrad ? w1.getGradient(i, alpha) : alpha);


if (neu1e.data().dataType() == DataBuffer.DOUBLE) {
if (neu1e.data().dataType() == DataBuffer.Type.DOUBLE) {
Nd4j.getBlasWrapper().axpy(g, syn1, neu1e);
Nd4j.getBlasWrapper().axpy(g, l1, syn1);
} else {
Expand Down Expand Up @@ -226,19 +226,19 @@ else if (f < -MAX_EXP)
g = label * (useAdaGrad ? w1.getGradient(target, alpha) : alpha);
else
g = useAdaGrad ? w1.getGradient(target, label - expTable[(int)((f + MAX_EXP) * (expTable.length / MAX_EXP / 2))]) : (label - expTable[(int)((f + MAX_EXP) * (expTable.length / MAX_EXP / 2))]) * alpha;
if(syn1Neg.data().dataType() == DataBuffer.DOUBLE)
if(syn1Neg.data().dataType() == DataBuffer.Type.DOUBLE)
Nd4j.getBlasWrapper().axpy(g,neu1e,l1);
else
Nd4j.getBlasWrapper().axpy((float) g,neu1e,l1);

if(syn1Neg.data().dataType() == DataBuffer.DOUBLE)
if(syn1Neg.data().dataType() == DataBuffer.Type.DOUBLE)
Nd4j.getBlasWrapper().axpy(g,syn1Neg,l1);
else
Nd4j.getBlasWrapper().axpy((float) g,syn1Neg,l1);
}
}

if(neu1e.data().dataType() == DataBuffer.DOUBLE)
if(neu1e.data().dataType() == DataBuffer.Type.DOUBLE)
Nd4j.getBlasWrapper().axpy(1.0,neu1e,l1);

else
Expand Down
Expand Up @@ -316,7 +316,7 @@ public void iterateSample(VocabWord w1, VocabWord w2,double alpha) {
double g = (1 - code - f) * (useAdaGrad ? w1.getGradient(i, alpha) : alpha);


if (neu1e.data().dataType() == DataBuffer.DOUBLE) {
if (neu1e.data().dataType() == DataBuffer.Type.DOUBLE) {
Nd4j.getBlasWrapper().axpy(g, syn1, neu1e);
Nd4j.getBlasWrapper().axpy(g, l1, syn1);
} else {
Expand Down Expand Up @@ -357,19 +357,19 @@ else if (f < -MAX_EXP)
g = label * (useAdaGrad ? w1.getGradient(target, alpha) : alpha);
else
g = useAdaGrad ? w1.getGradient(target, label - expTable[(int)((f + MAX_EXP) * (expTable.length / MAX_EXP / 2))]) : (label - expTable[(int)((f + MAX_EXP) * (expTable.length / MAX_EXP / 2))]) * alpha;
if(syn1Neg.data().dataType() == DataBuffer.DOUBLE)
if(syn1Neg.data().dataType() == DataBuffer.Type.DOUBLE)
Nd4j.getBlasWrapper().axpy(g,neu1e,l1);
else
Nd4j.getBlasWrapper().axpy((float) g,neu1e,l1);

if(syn1Neg.data().dataType() == DataBuffer.DOUBLE)
if(syn1Neg.data().dataType() == DataBuffer.Type.DOUBLE)
Nd4j.getBlasWrapper().axpy(g,syn1Neg,l1);
else
Nd4j.getBlasWrapper().axpy((float) g,syn1Neg,l1);
}
}

if(neu1e.data().dataType() == DataBuffer.DOUBLE)
if(neu1e.data().dataType() == DataBuffer.Type.DOUBLE)
Nd4j.getBlasWrapper().axpy(1.0,neu1e,l1);

else
Expand Down