CNTK icon indicating copy to clipboard operation
CNTK copied to clipboard

How to get simple predicted values (regression)?

Open DrTomHill opened this issue 10 years ago • 9 comments

Sorry, I am new to CNTK, so seem to be stuck on a simple problem:

I can replicate the simple and more complex classification tasks, but have problems generating predictions for regression tasks. I looked through the details on this forum, and in the "Write" (Output) section, when i specify the Features and Labels to output, ALL I EVER GET ARE OBSERVED VALUES, not predicted values.

I am showing below the CNTK file. For data, just generate any random data with 6 continuous features (X-variables) and 1 continuous label (Y-variable). The network I specified is VERY simple and should should not give "accurate" results. Still, my Labels file only shows observed values.

Really appreciate the help!

Parameters can be overwritten on the command line

for example: cntk configFile=myConfigFile RootDir=../..

For running from Visual Studio add

currentDirectory=$(SolutionDir)/

RootDir = ".."

RootDir = "."

ConfigDir = "$RootDir$/Config" DataDir = "$RootDir$/Data" OutputDir = "$RootDir$/Output" ModelDir = "$OutputDir$/Models"

deviceId=-1 for CPU, >=0 for GPU devices, "auto" chooses the best GPU, or CPU if no usable GPU is available

deviceId = -1

command = RegressionProblem_Train:RegressionProblem_Test:RegressionProblem_Output

precision = "float" traceLevel = 1 modelPath = "$ModelDir$/RegressionProblem.dnn" outputNodeNames = labels:features

RegressionProblem_Train = [ action = "train"

# Notation xxx:yyy*n:zzz is equivalent to xxx, then yyy repeated n times, then zzz
# Example: 10:20*3:5 is equivalent to 10:20:20:20:5
SimpleNetworkBuilder = [
 run = ndlLR
 ndlLR = [
  # sample and label dimensions
  SDim = 6
  LDim = 1

  features = Input(SDim, 1)
  labels = Input(LDim, 1)

  # parameters to learn
  B = Parameter(LDim, 1)
  W = Parameter(LDim, SDim)

  # operations
  t = Times(W, features)
  z = Plus(t, B)
  s = Sigmoid(z)

  LR = SquareError(labels, s)
  EP = SquareError(labels, s)

  # root nodes
   FeatureNodes = (features)
   LabelNodes = (labels)
   CriteriaNodes = (LR)
   EvalNodes = (EP)
   OutputNodes = (s)
 ]
    layerSizes = 6:1*1:1
    trainingCriterion = "SquareError"
    evalCriterion = "ErrorPrediction"
    layerTypes = "Sigmoid"
    initValueScale = 1.0
    applyMeanVarNorm = true
    uniformInit = true
    needPrior = true
]

SGD = [
    # epochSize = 0 means epochSize is the size of the training set 
    epochSize = 0 
    minibatchSize = 25  
    learningRatesPerMB = 0.5:0.2*20:0.1
    momentumPerMB = 0.9
    dropoutRate = 0.0
    maxEpochs = 10
]

# Parameter values for the reader
reader = [
    readerType = "UCIFastReader"
    file = "$DataDir$/RegressionProblemTrain2.txt"
    miniBatchMode = "partial"
    randomize = "none"
    verbosity = 1  

    features = [
        dim = 6        # two-dimensional input data
        start = 0      # Start with first element on line
    ]

    labels = [
        start = 6      # Skip two elements
        dim = 1        # One label dimensione
        labelType = "regression"
    ]
]
]

]

RegressionProblem_Test = [ action = "test"

# Parameter values for the reader
reader = [
    readerType = "UCIFastReader"
    file = "$DataDir$/RegressionProblemTest2.txt"
    randomize = "none"

    features = [
        dim = 6
        start = 0
    ]


    labels = [
        start = 6      # Skip two elements
        dim = 1        # One label dimension
        #labelDim = 2   # Two labels possible
        labelType = "regression"
        labelMappingFile = "$DataDir$/RegressionProblemMapping.txt"
    ]
]

]

RegressionProblem_Output=[ action = "write" printValues=true

# Parameter values for the reader
reader = [
    readerType = "UCIFastReader"
    file = "$DataDir$/RegressionProblemTest2.txt"
    randomize = "none"

    features = [
        dim = 6
        start = 0
    ]


    labels = [
        start = 6      # Skip two elements
        dim = 1        # One label dimension
        #labelDim = 1   # One labels possible
        labelType = "regression"
        labelMappingFile = "$DataDir$/RegressionProblemMapping.txt"
    ]
]

]
RegressionProblem_Dumpnodes=[ action = "dumpnode" printValues=true

# Parameter values for the reader
reader = [
    readerType = "UCIFastReader"
    file = "$DataDir$/RegressionProblemTest2.txt"
    randomize = "none"

    features = [
        dim = 6
        start = 0
    ]


    labels = [
        start = 6      # Skip two elements
        dim = 1        # One label dimension
        #labelDim = 1   # One labels possible
        labelType = "regression"
        labelMappingFile = "$DataDir$/RegressionProblemMapping.txt"
    ]
]

] outputPath = "$OutputDir$/RegressionProblemOutput" # Dump output as text ]

DrTomHill avatar May 09 '16 17:05 DrTomHill

Hi, If this is regression then you do not need any labelMappingFile, and also your evalCriterion should be equal SquareError.

Regards, Slawek

slaweks17 avatar May 10 '16 16:05 slaweks17

Thanks for the feedback SLAWEK!

The label file was empty, but removing those lines worked also.

I changed to SquareError, and best I can tell I get a trained model with parameter values as expected given that I model random data (6 X variables, 1 Y variable).

BUT: The problem remains that the section "RegressionProblem_Output=[action = "write" printValues=true..." generates the OBSERVED values for Y, NOT the predicted values. I need the predicted values.

Any ideas?

DrTomHill avatar May 10 '16 20:05 DrTomHill

This outputNodeNames = labels:features is what tells the "write" command which nodes to write out. But somehow it didn't. One thing I would try is to move this variable into the "write" command's config block. I wonder if it otherwise might conflict with the network definition.

Another thing that needs to be changed is that the NDL cannot be declared inside the SimpleNetworkBuilder. My colleague @dongyu888 will help you get the config right.

frankseide avatar May 10 '16 22:05 frankseide

Thank you frankseide!

I moved the outputNodeNames around and into the Output block after the write command. That Config file runs, but again generates the same observed output values in the .Labels file.

Thank you for referring me/this-case to your colleague @dongyu888; actually, if you have more documentation -- in addition to "the book" (Introduction to Computational Networks; Agrawal et al.) and what is available here -- would be great if you could point me to it. I am learning most from my mistakes (who hasn't...) and so if I can read about he details and order of things -- that would help. Note that I have not yet looked into the code to track what is finally written into the .labels file.

Also: I am currently running the Windows binary:

Built time: Apr 24 2016 21:32:05 Last modified date: Thu Apr 14 14:07:26 2016 Build type: Release Build target: CPU-only With 1bit-SGD: no Build Branch: HEAD Build SHA1: 29c065ae7c1c370d8852c283d4e9f1a068ba2233 Built by svcphil on DPHAIM-22

DrTomHill avatar May 10 '16 23:05 DrTomHill

I think the problem is in your training block. You are using SimpleNetworkBuilder and so it actually does not use the NDL scripts your have in your config at all. You should change SimpleNetworkBuilder to NDLNetworkBuilder as shown in https://github.com/Microsoft/CNTK/wiki/Tutorial and remove following lines that are not NDL scripts:

layerSizes = 6:1*1:1
trainingCriterion = "SquareError"
evalCriterion = "ErrorPrediction"
layerTypes = "Sigmoid"
initValueScale = 1.0
applyMeanVarNorm = true
uniformInit = true
needPrior = true

you need to delete the existing models and rerun the whole setup.

dongyu888 avatar May 10 '16 23:05 dongyu888

Got it; thanks dongyu888

I now get a much simpler model as expected. BUT: I still see the observed values in the .Labels file.

Is there a working example you can point me to that builds a simple regression network (one or more continuous output) and generates the predicted values?

Thanks!

DrTomHill avatar May 10 '16 23:05 DrTomHill

please remove outputNodeNames = labels:features if you have it in your config file. instead, using the default, which is specified by your model definition

OutputNodes = (s)

dongyu888 avatar May 10 '16 23:05 dongyu888

Aha! That worked; i now get an output file with extension .s, and it has the expected results in it. Great!

Is there a way to use the SimpleNetworkBuilder and also get the predicted values? Or is the more granular NDLNetworkBuilder syntax the way to go for regression problems, to obtain predicted values? Just wondering; i suppose i could live with either but the SimpleNetworkBuilder syntax is "simpler".

Thanks again for your help!

DrTomHill avatar May 10 '16 23:05 DrTomHill

NDL is the way to go.

dongyu888 avatar May 10 '16 23:05 dongyu888