我想创建一个名为NeuralNetworkClassifier的类,它设置并适合神经网络分类器,并返回其对某些测试数据的预测。
为了设置和拟合神经网络,我使用的是tensorflow的DNNClassifier类。
我的问题是我的fit
方法因此返回类型为tensorflow.python.estimator.canned.dnn.DNNClassifier
的对象,当我随后尝试在此DNNClassifier对象上运行predict
方法时,没有任何反应。我想这是因为我试图在不属于这个类的对象上运行NeuralNetworkClassifier方法?
我该如何解决这个问题?有没有办法在我自己的类中继续使用DNNClassifier类?
以下是代码:
class NeuralNetworkClassifier(object):
def __init__(self, batch_size, n_steps, n_hidden_units1, n_hidden_units2,
n_classes):
self.batch_size = batch_size
self.n_steps = n_steps
self.n_hidden_units1 = n_hidden_units1
self.n_hidden_units2 = n_hidden_units2
self.n_classes = n_classes
def set_up_architecture(self, train_data_features, train_sentiments):
# Convert the scarce scipy feature matrices to pandas dataframes
train_df = pd.DataFrame(train_data_features.toarray())
# Convert column names from numbers to strings
train_df.columns = train_df.columns.astype(str)
# Create feature columns which describe how to use the input
feat_cols = []
for key in train_df.keys():
feat_cols.append(tf.feature_column.numeric_column(key=key))
# Set up classifier with two hidden unit layers
classifier = tf.estimator.DNNClassifier(
feature_columns=feat_cols,
hidden_units=[self.n_hidden_units1,
self.n_hidden_units2],
n_classes=self.n_classes)
return train_df, classifier
def shape_train_input(self, features, labels, batch_size):
"""An input function for training"""
# Convert the input to a dataset
dataset = tf.data.Dataset.from_tensor_slices((dict(features), labels))
# Shuffle, repeat, and batch the examples
dataset = dataset.shuffle(1000).repeat().batch(self.batch_size)
return dataset
def fit(self, train_data_features, train_sentiments):
train_df, classifier = self.set_up_architecture(train_data_features,
train_sentiments)
print(type(classifier))
fitted_classifier = classifier.train(input_fn=
lambda:self.shape_train_input(train_df,
train_sentiments,
self.batch_size),
steps=1000)
return fitted_classifier
def shape_pred_input(self, features, labels, batch_size):
"""An input function for evaluation or prediction"""
features=dict(features)
if labels is None:
# No labels, use only features.
inputs = features
else:
inputs = (features, labels)
# Convert the inputs to a dataset
dataset = tf.data.Dataset.from_tensor_slices(inputs)
# Batch the examples
assert batch_size is not None, "batch_size must not be None"
dataset = dataset.batch(self.batch_size)
return dataset
def predict(self, test_data_features):
# Convert the scarce scipy feature matrices to pandas dataframes
test_df = pd.DataFrame(test_data_features.toarray())
# Convert column names from numbers to strings
test_df.columns = test_df.columns.astype(str)
predictions = self.evaluate(input_fn=
lambda:shape_pred_input(test_df,
test_data['sentiment'],
self.batch_size))
return predictions
运行代码
neuralnet = _make_classifier(conf)
# This creates an instance of the NeuralNetworkClassifier class through
# another function, which works fine. type(neuralnet) is
# __main__.NeuralNetworkClassifier. So far so good.
neuralnet_fitted = neuralnet.fit(train_data_features, train_sentiments)
# This works, but neuralnet_fitted is a DNNClassifier, not a NeuralNetworkClassifier.
predictions = neuralnet_fitted.predict(test_data_features)
# I don't get an error message, but an empty generator is returned.
# I presume this is the case because an object of class DNNClassifier,
# and not NeuralNetworkClassifier, was passed to predict.