TensorFlow Save and Restore

时间:2018-03-22 00:53:07

标签: python tensorflow

I am trying to save/load a model using Tensorflow. The model appears to be saving correctly but I can not get the predict function to work correctly. I keep getting the following error:

AttributeError: 'list' object has no attribute 'keys'

Any suggestions would be greatly appreciated!

import os
import urllib
import numpy as np
import tensorflow as tf
import csv
import pandas as pd
import sys

##########################
# User Command line inputs
##########################
SYM = sys.argv[1]
TYPE =  sys.argv[2]
AHEAD = sys.argv[3]
DATE = sys.argv[4]


# Set the training file
TRAINING = './training/' + str(SYM) + "_" + str(TYPE) + "_" + str(AHEAD) + 
'.csv'

READER = csv.reader(open(TRAINING, 'rb'), delimiter=",")
NUM_COLS = len(next(READER))
TARGETCOLUMN_CLASS = NUM_COLS - 1
PREDICTORSTART = 1
PREDICTOREND = NUM_COLS - 2

TRAININGDATA = pd.read_csv(TRAINING, delim_whitespace=False, delimiter=',')

# Remove the Test Data from the Sample
TESTDATA = TRAININGDATA.tail(450)
TRAININGDATA.drop(TRAININGDATA.tail(470).index,inplace=True)

TRAININGDATA=TRAININGDATA.replace([np.inf, -np.inf], np.nan)
TRAININGDATA = TRAININGDATA.dropna()
ROW_NUM = len(TRAININGDATA.index)

predictors = TRAININGDATA.iloc[:, PREDICTORSTART:PREDICTOREND]
targets_class = TRAININGDATA.iloc[:, TARGETCOLUMN_CLASS]

# Separate Test Data ( Independant Sample )
predictors_ind = TESTDATA.iloc[:, PREDICTORSTART:PREDICTOREND]
targets_ind_class = TESTDATA.iloc[:, TARGETCOLUMN_CLASS]

feature_columns = [tf.contrib.layers.real_valued_column(k) for k in 
list(predictors)]

# Build 3 layer DNN with 10, 20, 10 units respectively.
classifier = tf.estimator.DNNClassifier(feature_columns=feature_columns,
                                      hidden_units=[10, 20, 10],dropout=.7,
                                      n_classes=2,
                                      )
# Define the training inputs
train_input_fn = tf.estimator.inputs.pandas_input_fn(
  x=predictors,
  y=targets_class,
  num_epochs=None,
  shuffle=True)

# Train model.
classifier.train(input_fn=train_input_fn, steps=10)

# Define the test inputs
test_input_fn = tf.estimator.inputs.pandas_input_fn(
    x=predictors_ind,
    y=targets_ind_class,
    num_epochs=1,
    shuffle=False)

# Evaluate accuracy.
accuracy_score = classifier.evaluate(input_fn=test_input_fn)["accuracy"]
print("\nTest Accuracy: {0:f}\n".format(accuracy_score))

export_dir="./tf/"
feature_spec = 
tf.feature_column.make_parse_example_spec(feature_columns)
input_receiver_fn = 
tf.estimator.export.build_parsing_serving_input_receiver_fn(feature_spec)
classifier.export_savedmodel(export_dir,input_receiver_fn, as_text=False)

X = predictors.tail(1)
from tensorflow.contrib import predictor
predict_fn = predictor.from_saved_model(export_dir + "/1521675931/",    
    signature_def_key=None,
    signature_def=None,
    tags=None,
    graph=None)

X = X.to_dict("records")
print X
predictions = predict_fn(X)
print(predictions['scores'])

Thanks in Advance! - Tim

0 个答案:

没有答案