Andrew Taylor
Andrew Taylor

Reputation: 3488

Using the prediction from tensorflow model

I used the following code to create a prediction on new data:

def predict(dfeval, importedModel):
    colNames = dfeval.columns
    dtypes = dfeval.dtypes
    predictions = []
    for row in dfeval.iterrows():
        example = tf.train.Example()
        for i in range(len(colNames)):
            dtype = dtypes[i]
            colName = colNames[i]
            value = row[1][colName]
            if dtype == "object":
                value = bytes(value, "utf-8")
                example.features.feature[colName].bytes_list.value.extend(
                    [value])
            elif dtype == "float":
                example.features.feature[colName].float_list.value.extend(
                    [value])
            elif dtype == "int":
                example.features.feature[colName].int64_list.value.extend(
                    [value])
    predictions.append(
      importedModel.signatures["predict"](
        examples=tf.constant([example.SerializeToString()])))
    return predictions

val = predict(dfeval, imported)
val

which provides:

[{'predictions': <tf.Tensor: shape=(1, 1), dtype=float32, numpy=array([[0.24904668]], dtype=float32)>}]

And then I can print the value via:

tf.print(val)

[{'predictions': [[0.249046683]]}]

But I want to use the value in a future calculation such as:

val + 300

Which I would want to have return:

300.249046683

But as of now I cannot find a way to extract out and use the prediction.

Upvotes: 0

Views: 390

Answers (1)

Marsolgen
Marsolgen

Reputation: 187

You can get it like this:

val[0]['predictions'][0][0]

Upvotes: 1

Related Questions