root@image-ubuntu:/home/zhoumeixu/credit-textclassify-deeplearning# nvidia-smi
Fri Jul 14 08:00:24 2017
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 375.51 Driver Version: 375.51 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
|===============================+======================+======================|
| 0 Tesla M60 Off | 0000:00:02.0 Off | Off |
| N/A 44C P0 39W / 150W | 2452MiB / 8123MiB | 17% Default |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: GPU Memory |
| GPU PID Type Process name Usage |
|=============================================================================|
| 0 76510 C java 2444MiB |
+-----------------------------------------------------------------------------+
1 预定 满 加 h 其他数字 免费 吃
1 属于 派对 场 不是 特别 商务 就是 定 位置 是个 非常 麻烦 事 基本 每 一天 都是 人满为患 是个 放松 好地方 过来 玩 可以 找我 预定 联系方式 看 我 名字
1 长宁 v show 很多 亮点 你 可能 您 不 太 清楚 1 满 底 消 不收 一分 计时 房费 2 生日 免费 布置 送 生日蛋糕 限 7月 22号 前 活动内容 3 专业 接待 经理 全程 接待 您 4 周一 会员卡 折优惠 5 房间 各种 主题 可供 您 选择 6 周五 12点 以后 底 消减 一半 7 5 星级 厨师 推出 99种 小吃 8 商务区 房间 享受 管家 服务 9 评出 场所 5 大 优点 送 鸡尾酒 10 大厅 走道 专 设有 拍照 区域
1 佳 企鹅 一0 死死 一六 儿 0六三
package com.dianping.deeplearning.rnn;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Random;
import org.apache.commons.lang.StringUtils;
import org.deeplearning4j.models.embeddings.wordvectors.WordVectors;
import org.deeplearning4j.text.tokenization.tokenizer.preprocessor.CommonPreprocessor;
import org.deeplearning4j.text.tokenization.tokenizerfactory.DefaultTokenizerFactory;
import org.deeplearning4j.text.tokenization.tokenizerfactory.TokenizerFactory;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.dataset.DataSet;
import org.nd4j.linalg.dataset.api.DataSetPreProcessor;
import org.nd4j.linalg.dataset.api.iterator.DataSetIterator;
import org.nd4j.linalg.factory.Nd4j;
import org.nd4j.linalg.indexing.INDArrayIndex;
import org.nd4j.linalg.indexing.NDArrayIndex;
public class SentimentIterator implements DataSetIterator {
private final WordVectors wordVectors;
private final int batSize;
private final int truncateLength;
private final int vectorSize;
private int cursor = 0;
private List<String> positiveList=new ArrayList<>();
private List<String> negativeList=new ArrayList<>();
private final TokenizerFactory tokenizerFactory;
public SentimentIterator(String path, WordVectors wordVectors,
int batchSize, int truncateLength) {
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(new InputStreamReader(
new FileInputStream(path)));
String line = bufferedReader.readLine();
while (line != null) {
String[] lines = line.split("\t");
if (lines.length > 1) {
String label = lines[0];
String content = lines[1];
if (StringUtils.isNotBlank(content)) {
if ("1".equalsIgnoreCase(label)) {
positiveList.add(content);
} else if ("0".equalsIgnoreCase(label)) {
negativeList.add(content);
}
}
}
line = bufferedReader.readLine();
}
bufferedReader.close();
} catch (Exception e) {
e.printStackTrace();
}
this.batSize = batchSize;
this.wordVectors = wordVectors;
this.vectorSize = wordVectors.getWordVector(wordVectors.vocab()
.wordAtIndex(0)).length;
this.truncateLength = truncateLength;
tokenizerFactory = new DefaultTokenizerFactory();
tokenizerFactory.setTokenPreProcessor(new CommonPreprocessor());
}
private DataSet nextDataSet(int num) throws IOException {
List<String> reviews = new ArrayList<>(num);
boolean[] positive = new boolean[num];
for (int i = 0; i < num && cursor < totalExamples(); i++) {
if (cursor % 2 == 0) {
int posReviewNumber = cursor / 2;
String review = null;
if (posReviewNumber < positiveList.size() - 1) {
review = positiveList.get(posReviewNumber);
} else {
Random randn = new Random();
int randint = randn.nextInt(positiveList.size());
review = positiveList.get(randint);
}
positive[i] = true;
reviews.add(review);
} else {
int negReviewNumber = cursor / 2;
String review = null;
if (negReviewNumber < negativeList.size() - 1) {
review = negativeList.get(negReviewNumber);
} else {
Random randn = new Random();
int randint = randn.nextInt(negativeList.size());
review = negativeList.get(randint);
}
reviews.add(review);
positive[i] = false;
}
cursor++;
}
List<List<String>> allTokens = new ArrayList<>(reviews.size());
int maxLength = 0;
for (String s : reviews) {
List<String> tokens = tokenizerFactory.create(s).getTokens();
List<String> tokensFiltered = new ArrayList<>();
for (String t : tokens) {
if (wordVectors.hasWord(t))
tokensFiltered.add(t);
}
allTokens.add(tokensFiltered);
maxLength = Math.max(maxLength, tokensFiltered.size());
}
if (maxLength > truncateLength)
maxLength = truncateLength;
INDArray features = Nd4j.create(reviews.size(), vectorSize, maxLength);
INDArray labels = Nd4j.create(reviews.size(), 2, maxLength); // Two
// labels:
// positive
// or
// negative
// Because we are dealing with reviews of different lengths and only one
// output at the final time step: use padding arrays
// Mask arrays contain 1 if data is present at that time step for that
// example, or 0 if data is just padding
INDArray featuresMask = Nd4j.zeros(reviews.size(), maxLength);
INDArray labelsMask = Nd4j.zeros(reviews.size(), maxLength);
int[] temp = new int[2];
for (int i = 0; i < reviews.size(); i++) {
List<String> tokens = allTokens.get(i);
temp[0] = i;
// Get word vectors for each word in review, and put them in the
// training data
for (int j = 0; j < tokens.size() && j < maxLength; j++) {
String token = tokens.get(j);
INDArray vector = wordVectors.getWordVectorMatrix(token);
features.put(new INDArrayIndex[] { NDArrayIndex.point(i),
NDArrayIndex.all(), NDArrayIndex.point(j) }, vector);
temp[1] = j;
featuresMask.putScalar(temp, 1.0); // Word is present (not
// padding) for this example
// + time step -> 1.0 in
// features mask
}
int idx = (positive[i] ? 0 : 1);
int lastIdx = Math.min(tokens.size(), maxLength);
labels.putScalar(new int[] { i, idx, lastIdx - 1 }, 1.0); // Set
// label:
// [0,1]
// for
// negative,
// [1,0]
// for
// positive
labelsMask.putScalar(new int[] { i, lastIdx - 1 }, 1.0); // Specify
// that
// an
// output
// exists
// at
// the
// final
// time
// step
// for
// this
// example
}
return new DataSet(features, labels, featuresMask, labelsMask);
}
@Override
public DataSet next(int num) {
if (cursor >= negativeList.size() + positiveList.size())
throw new NoSuchElementException();
try {
return nextDataSet(num);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public boolean hasNext() {
return cursor < numExamples();
}
@Override
public DataSet next() {
return next(batSize);
}
@Override
public void remove() {
}
@Override
public int totalExamples() {
return positiveList.size() + negativeList.size();
}
@Override
public int inputColumns() {
return vectorSize;
}
@Override
public int totalOutcomes() {
return 2;
}
@Override
public boolean resetSupported() {
return true;
}
@Override
public boolean asyncSupported() {
return true;
}
@Override
public void reset() {
cursor = 0;
Collections.shuffle(negativeList);
Collections.shuffle(positiveList);
}
@Override
public int batch() {
return batSize;
}
@Override
public int cursor() {
return cursor;
}
@Override
public int numExamples() {
return totalExamples();
}
@Override
public void setPreProcessor(DataSetPreProcessor preProcessor) {
}
@Override
public DataSetPreProcessor getPreProcessor() {
throw new UnsupportedOperationException("Not implemented");
}
@Override
public List<String> getLabels() {
return Arrays.asList("1", "0");
}
}
package com.dianping.deeplearning.rnn;
import org.deeplearning4j.eval.Evaluation;
import org.deeplearning4j.models.embeddings.loader.WordVectorSerializer;
import org.deeplearning4j.models.embeddings.wordvectors.WordVectors;
import org.deeplearning4j.nn.api.OptimizationAlgorithm;
import org.deeplearning4j.nn.conf.GradientNormalization;
import org.deeplearning4j.nn.conf.MultiLayerConfiguration;
import org.deeplearning4j.nn.conf.NeuralNetConfiguration;
import org.deeplearning4j.nn.conf.Updater;
import org.deeplearning4j.nn.conf.layers.GravesLSTM;
import org.deeplearning4j.nn.conf.layers.RnnOutputLayer;
import org.deeplearning4j.nn.multilayer.MultiLayerNetwork;
import org.deeplearning4j.nn.weights.WeightInit;
import org.deeplearning4j.optimize.listeners.ScoreIterationListener;
import org.deeplearning4j.parallelism.ParallelWrapper;
import org.nd4j.jita.conf.CudaEnvironment;
import org.nd4j.linalg.activations.Activation;
import org.nd4j.linalg.api.buffer.DataBuffer;
import org.nd4j.linalg.api.buffer.util.DataTypeUtil;
import org.nd4j.linalg.api.ndarray.INDArray;
import org.nd4j.linalg.dataset.DataSet;
import org.nd4j.linalg.dataset.api.iterator.DataSetIterator;
import org.nd4j.linalg.factory.Nd4j;
import org.nd4j.linalg.lossfunctions.LossFunctions;
public class TrainAdxRnnModelWithGPU {
private static String basepath = "/home/zhoumeixu/model/";
//private static String basepath = "adx/";
public static void main(String[] args) {
int batchSize = 48; // Number of examples in each minibatch
int nEpochs = 35; // 训练次数
int truncateLength = 256; // 文本最大长度
WordVectors wordVectors = WordVectorSerializer.readWord2VecModel(basepath + "word2vec.model");
DataSetIterator train = getDataSetIterator(basepath + "rnnsenec.txt",wordVectors, batchSize, truncateLength);
DataSetIterator test = getDataSetIterator(basepath + "rnnsenectest.txt", wordVectors, batchSize,truncateLength);
System.out.println("。。。。。。。gpu初始化即将开始。。。。。。。。。");
DataTypeUtil.setDTypeForContext(DataBuffer.Type.FLOAT);
CudaEnvironment.getInstance().getConfiguration().allowMultiGPU(true)
.setMaximumDeviceCache(2L * 1024L * 1024L * 1024L).allowCrossDeviceAccess(true);
System.out.println("。。。。。。。。。gpu初始化即将结束。。。。。。。。。。");
int outputs = train.getLabels().size();
int inputNeurons = wordVectors.getWordVector(wordVectors.vocab()
.wordAtIndex(0)).length; // 15 in our case
MultiLayerConfiguration conf = new NeuralNetConfiguration.Builder()
.optimizationAlgo(
OptimizationAlgorithm.STOCHASTIC_GRADIENT_DESCENT)
.iterations(1)
.updater(Updater.RMSPROP)
.regularization(true)
.l2(1e-5)
.weightInit(WeightInit.XAVIER)
.gradientNormalization(
GradientNormalization.ClipElementWiseAbsoluteValue)
.gradientNormalizationThreshold(1.0)
.learningRate(0.0018)
.list()
.layer(0,
new GravesLSTM.Builder().nIn(inputNeurons).nOut(200)
.activation(Activation.SOFTSIGN).build())
.layer(1,
new RnnOutputLayer.Builder()
.activation(Activation.SOFTMAX)
.lossFunction(LossFunctions.LossFunction.MCXENT)
.nIn(200).nOut(outputs).build())
.pretrain(false).backprop(true).build();
MultiLayerNetwork net = new MultiLayerNetwork(conf);
net.init();
/* ParallelWrapper pw = new ParallelWrapper.Builder<>(net)
.prefetchBuffer(16 * Nd4j.getAffinityManager().getNumberOfDevices())
.reportScoreAfterAveraging(true)
.averagingFrequency(10)
.useLegacyAveraging(false)
.useMQ(true)
.workers(Nd4j.getAffinityManager().getNumberOfDevices())
.build();
*/
ParallelWrapper pw = new ParallelWrapper.Builder(net)
.prefetchBuffer(24)
.workers(4)
.averagingFrequency(3)
.reportScoreAfterAveraging(true)
.useLegacyAveraging(true)
.build();
// 设置没两百步观察数据情况
net.setListeners(new ScoreIterationListener(200));
System.out.println("Starting training");
for (int i = 0; i < nEpochs; i++) {
pw.fit(train);
train.reset();
System.out.println("Epoch " + i + " complete. Starting evaluation:");
Evaluation evaluation = new Evaluation();
while (test.hasNext()) {
DataSet t = test.next();
INDArray features = t.getFeatureMatrix();
INDArray lables = t.getLabels();
// System.out.println("labels : " + lables);
INDArray inMask = t.getFeaturesMaskArray();
INDArray outMask = t.getLabelsMaskArray();
INDArray predicted = net.output(features, false);
// System.out.println("predicted : " + predicted);
evaluation.evalTimeSeries(lables, predicted, outMask);
}
test.reset();
System.out.println(evaluation.stats());
}
}
public static DataSetIterator getDataSetIterator(String path,
WordVectors wordVectors, int batchSize, int truncateLength) {
DataSetIterator dataSetIterator = new SentimentIterator(path,
wordVectors, batchSize, truncateLength);
return dataSetIterator;
}
}
07:58:12.231 [ParallelWrapper trainer 3] DEBUG o.n.j.c.CudaAffinityManager - Mapping thread [1869] to device [0], out of [1] devices...
07:58:12.232 [ParallelWrapper trainer 0] DEBUG o.n.j.c.CudaAffinityManager - Mapping thread [1866] to device [0], out of [1] devices...
07:58:12.910 [ParallelWrapper trainer 0] INFO org.nd4j.nativeblas.Nd4jBlas - Number of threads used for BLAS: 0
07:58:16.646 [ParallelWrapper trainer 0] INFO o.d.o.l.ScoreIterationListener - Score at iteration 0 is 0.6893714558848892
07:58:27.803 [main] INFO o.d.parallelism.ParallelWrapper - Averaged score: 0.6904921923723948
07:58:37.580 [main] INFO o.d.parallelism.ParallelWrapper - Averaged score: 0.6918042188376753
07:58:47.585 [main] INFO o.d.parallelism.ParallelWrapper - Averaged score: 0.6902696776475821
07:58:58.407 [main] INFO o.d.parallelism.ParallelWrapper - Averaged score: 0.6904408029170671
07:59:09.717 [main] INFO o.d.parallelism.ParallelWrapper - Averaged score: 0.6896152988017
07:59:22.807 [main] INFO o.d.parallelism.ParallelWrapper - Averaged score: 0.6888589759877908
07:59:40.347 [main] INFO o.d.parallelism.ParallelWrapper - Averaged score: 0.6892489896929257
08:00:00.643 [main] INFO o.d.parallelism.ParallelWrapper - Averaged score: 0.6895330264029644
08:00:21.219 [main] INFO o.d.parallelism.ParallelWrapper - Averaged score: 0.6878439507341991
08:00:45.554 [main] INFO o.d.parallelism.ParallelWrapper - Averaged score: 0.6887204976666248
08:01:07.273 [main] INFO o.d.parallelism.ParallelWrapper - Averaged score: 0.6873620491523212
08:01:29.412 [main] INFO o.d.parallelism.ParallelWrapper - Averaged score: 0.6879257711235788
08:01:50.624 [main] INFO o.d.parallelism.ParallelWrapper - Averaged score: 0.6893160108520883
08:02:16.107 [main] INFO o.d.parallelism.ParallelWrapper - Averaged score: 0.6871896758883935