hughperkins / DeepCL

OpenCL library to train deep convolutional neural networks
Mozilla Public License 2.0
866 stars 200 forks source link

Predicting use kgsgo filter #47

Closed ghost closed 8 years ago

ghost commented 8 years ago

Hello, i'm a noob at this domain, after i train a filter 'weights.dat' with kgsgov2 dataset, i'm wondering how to predict (generate) a move from some board state(maybe i'm missing some document that explain how it work.), i've tried using testLoadSgf.py to make a binary file as input file for prediction, and it's appearing:

ManifestLoaderv1 checking format for move1.dat matched: 0 GenericLoader::getDimensions trainFilepath: move1.dat headstringGO Something went wrong: Filetype of move1.dat not recognised

please help, thanks! Deryk.

hughperkins commented 8 years ago

Well... you just need to create an appropriate input tensor, and send that in, using the python api, or the c++ api, or via the commandline. What language are you using for your prediction? If python, then you would do something like:

(adapted from https://github.com/hughperkins/DeepCL/blob/master/python/test_lowlevel.py)

inputTensor = array.array('f', [0] * (numPlanes * boardSize * boardSize))
# ... populate the inputTensor somehow ... , and then
net.forward(inputTensor)
lastLayer = net.getLastLayer()
predictions = lastLayer.getLabels()
ghost commented 8 years ago

Thank you for your response. For now I use commandline to do prediction, i tried make a DAT file as inputTensor with single SGF file from kgsgo-dataset-preprocessorv2. and add header mlv2-n=1-numplanes=7-imagewidth=19-imageheight=19-datatype=int-bpp=1 to the DAT file, Which n i assume it to 1 because I make the DAT file with single SGF file and one move only. After I do prediction with commandline. it shows n entries data, each entry has 361 numfield.

My question is the 361 numfield that possible is the probability of the SGF's next move on go game? Thank you for your help!

hughperkins commented 8 years ago

Ok, the n is indeed the number of examples in the file. From src/loaders/Kgsv2Loader.cpp:

    int N = atoi(split(split(headerString, "-n=")[1], "-")[0]);
    int numPlanes = atoi(split(split(headerString, "-numplanes=")[1], "-")[0]);
    int imageSize = atoi(split(split(headerString, "-imagewidth=")[1], "-")[0]);
    int imageSizeRepeated = atoi(split(split(headerString, "-imageheight=")[1], "-")[0]);
   ...
    if(numRecords == 0) {
        numRecords = N - startRecord;
    }
   ...
    for(int n = 0; n < numRecords; n++) {
     ...
   }

As far as the output, the relevant code is in src/main/predict.cpp:

        if(!config.writeLabels) {
            if(config.outputFormat == "text") {
                float const*output = net->getLayer(config.outputLayer)->getOutput();
                const int numFields = net->getLayer(config.outputLayer)->getOutputCubeSize();
                for(int i = 0; i < config.batchSize; i++) {
                    for(int f = 0; f < numFields; f++) {
                        if(f > 0) {
                            *outFile << " ";
                        }
                        *outFile << output[ i * numFields + f ];
                    }
                    *outFile << "\n";
                }
            } else {
                outFile->write(reinterpret_cast<const char *>(net->getOutput()), net->getOutputNumElements() * 4 * config.batchSize);
            }
        } else {
            SoftMaxLayer *softMaxLayer = dynamic_cast< SoftMaxLayer *>(net->getLayer(config.outputLayer) );
            if(softMaxLayer == 0) {
                cout << "must choose softmaxlayer, if want to output labels" << endl;
                return;
            }
            softMaxLayer->getLabels(labels);
            if(config.outputFormat == "text") {
                for(int i = 0; i < config.batchSize; i++) {
                    *outFile << labels[i] << "\n";
                }
            } else {
                outFile->write(reinterpret_cast< char * >(labels), config.batchSize * 4l);
            }
            outFile->flush();
        }