我正在尝试安装Tensorflow DNNRegressor并通过向model_dir arg提供目录来保存模型。 另外,我正在定义功能列。 我的问题是如何保存要素列定义并稍后加载模型以进行预测?
我尝试用泡菜,但提示
AttributeError: Can't pickle local object 'embedding_column.<locals>._creator'
estimator = tf.estimator.DNNRegressor(
hidden_units=[20, 15],
feature_columns=self.feature_columns,
model_dir=SAVE_DIR,
optimizer='Adagrad',
activation_fn=tf.nn.relu,
dropout=0.5)
def save_model(self, directory_path):
path = os.path.join(directory_path, 'model.pkl')
with open(path, 'wb') as file:
pickle.dump(file=file, obj=self)
@classmethod
def load_model(cls, directory_path):
path = os.path.join(directory_path, 'model.pkl')
with open(path, 'rb') as file:
model = pickle.load(file=file)
return model
def predict(self, data, SAVE_DIR):
estimator = tf.estimator.DNNRegressor(
hidden_units=[20, 15],
feature_columns=self.feature_columns,
model_dir=SAVE_DIR,
optimizer='Adagrad',
activation_fn=tf.nn.relu,
dropout=0.5 )
predict_input_fn = tf.estimator.inputs.pandas_input_fn(x=data)
predictions = extimator.predict(predict_input_fn)
return predictions
我想找到一种方法来定义带有特征列的预制估算器,并保存模型以备将来使用。之后,我想加载模型并使用padas数据框进行预测。