本文整理汇总了Python中tensorflow.contrib.learn.python.learn.infer_real_valued_columns_from_input函数的典型用法代码示例。如果您正苦于以下问题:Python infer_real_valued_columns_from_input函数的具体用法?Python infer_real_valued_columns_from_input怎么用?Python infer_real_valued_columns_from_input使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了infer_real_valued_columns_from_input函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testIrisStreaming
def testIrisStreaming(self):
iris = datasets.load_iris()
def iris_data():
while True:
for x in iris.data:
yield x
def iris_predict_data():
for x in iris.data:
yield x
def iris_target():
while True:
for y in iris.target:
yield y
classifier = learn.TensorFlowLinearClassifier(
feature_columns=learn.infer_real_valued_columns_from_input(iris.data),
n_classes=3, steps=100)
classifier.fit(iris_data(), iris_target())
score1 = accuracy_score(iris.target, classifier.predict(iris.data))
score2 = accuracy_score(iris.target,
classifier.predict(iris_predict_data()))
self.assertGreater(score1, 0.5, "Failed with score = {0}".format(score1))
self.assertEqual(score2, score1, "Scores from {0} iterator doesn't "
"match score {1} from full "
"data.".format(score2, score1))
示例2: testIris
def testIris(self):
iris = datasets.load_iris()
classifier = learn.TensorFlowLinearClassifier(
feature_columns=learn.infer_real_valued_columns_from_input(iris.data),
n_classes=3)
classifier.fit(iris.data, [x for x in iris.target])
score = accuracy_score(iris.target, classifier.predict(iris.data))
self.assertGreater(score, 0.7, "Failed with score = {0}".format(score))
示例3: testBoston
def testBoston(self):
random.seed(42)
boston = datasets.load_boston()
regressor = learn.LinearRegressor(
feature_columns=learn.infer_real_valued_columns_from_input(boston.data))
regressor.fit(boston.data, boston.target, max_steps=500)
score = mean_squared_error(boston.target, regressor.predict(boston.data))
self.assertLess(score, 150, "Failed with score = {0}".format(score))
示例4: get_classification_score
def get_classification_score(train_encodings, train_labels, test_encodings, test_labels, steps):
feature_columns = learn.infer_real_valued_columns_from_input(train_encodings)
classifier = learn.DNNClassifier(hidden_units=[32, 16], n_classes=10, feature_columns=feature_columns)
classifier.fit(train_encodings, train_labels, steps=steps, batch_size=32)
# For measuring accuracy
test_predictions = list(classifier.predict(test_encodings, as_iterable=True))
score = metrics.accuracy_score(test_labels, test_predictions)
return score
示例5: testOneDim
def testOneDim(self):
random.seed(42)
x = np.random.rand(1000)
y = 2 * x + 3
feature_columns = learn.infer_real_valued_columns_from_input(x)
regressor = learn.TensorFlowLinearRegressor(feature_columns=feature_columns)
regressor.fit(x, y)
score = mean_squared_error(y, regressor.predict(x))
self.assertLess(score, 1.0, "Failed with score = {0}".format(score))
示例6: testIrisSummaries
def testIrisSummaries(self):
iris = datasets.load_iris()
output_dir = tempfile.mkdtemp() + "learn_tests/"
classifier = learn.TensorFlowLinearClassifier(
feature_columns=learn.infer_real_valued_columns_from_input(iris.data),
n_classes=3, model_dir=output_dir)
classifier.fit(iris.data, iris.target)
score = accuracy_score(iris.target, classifier.predict(iris.data))
self.assertGreater(score, 0.5, "Failed with score = {0}".format(score))
示例7: testIrisClassWeight
def testIrisClassWeight(self):
iris = datasets.load_iris()
# Note, class_weight are not supported anymore :( Use weight_column.
with self.assertRaises(ValueError):
classifier = learn.TensorFlowLinearClassifier(
feature_columns=learn.infer_real_valued_columns_from_input(iris.data),
n_classes=3, class_weight=[0.1, 0.8, 0.1])
classifier.fit(iris.data, iris.target)
score = accuracy_score(iris.target, classifier.predict(iris.data))
self.assertLess(score, 0.7, "Failed with score = {0}".format(score))
示例8: testIris_proba
def testIris_proba(self):
# If sklearn available.
if log_loss:
random.seed(42)
iris = datasets.load_iris()
classifier = learn.TensorFlowClassifier(
feature_columns=learn.infer_real_valued_columns_from_input(iris.data),
n_classes=3, steps=250)
classifier.fit(iris.data, iris.target)
score = log_loss(iris.target, classifier.predict_proba(iris.data))
self.assertLess(score, 0.8, "Failed with score = {0}".format(score))
示例9: testBoston
def testBoston(self):
random.seed(42)
boston = datasets.load_boston()
regressor = learn.TensorFlowLinearRegressor(
feature_columns=learn.infer_real_valued_columns_from_input(boston.data),
batch_size=boston.data.shape[0],
steps=500,
learning_rate=0.001)
regressor.fit(boston.data, boston.target)
score = mean_squared_error(boston.target, regressor.predict(boston.data))
self.assertLess(score, 150, "Failed with score = {0}".format(score))
示例10: testMultiRegression
def testMultiRegression(self):
random.seed(42)
rng = np.random.RandomState(1)
x = np.sort(200 * rng.rand(100, 1) - 100, axis=0)
y = np.array([np.pi * np.sin(x).ravel(), np.pi * np.cos(x).ravel()]).T
regressor = learn.LinearRegressor(
feature_columns=learn.infer_real_valued_columns_from_input(x),
target_dimension=2)
regressor.fit(x, y, steps=100)
score = mean_squared_error(regressor.predict(x), y)
self.assertLess(score, 10, "Failed with score = {0}".format(score))
示例11: testIrisContinueTraining
def testIrisContinueTraining(self):
iris = datasets.load_iris()
classifier = learn.LinearClassifier(
feature_columns=learn.infer_real_valued_columns_from_input(iris.data),
n_classes=3)
classifier.fit(iris.data, iris.target, steps=100)
score1 = accuracy_score(iris.target, classifier.predict(iris.data))
classifier.fit(iris.data, iris.target, steps=500)
score2 = accuracy_score(iris.target, classifier.predict(iris.data))
self.assertGreater(
score2, score1,
"Failed with score2 {0} <= score1 {1}".format(score2, score1))
示例12: testIrisES
def testIrisES(self):
random.seed(42)
iris = datasets.load_iris()
x_train, x_test, y_train, y_test = train_test_split(
iris.data, iris.target, test_size=0.2, random_state=42)
x_train, x_val, y_train, y_val = train_test_split(
x_train, y_train, test_size=0.2, random_state=42)
val_monitor = learn.monitors.ValidationMonitor(
x_val, y_val, every_n_steps=50, early_stopping_rounds=100,
early_stopping_metric='loss', early_stopping_metric_minimize=False)
feature_columns = learn.infer_real_valued_columns_from_input(iris.data)
# classifier without early stopping - overfitting
classifier1 = learn.DNNClassifier(
feature_columns=feature_columns, hidden_units=[10, 20, 10], n_classes=3)
classifier1.fit(x_train, y_train, steps=1000)
_ = accuracy_score(y_test, classifier1.predict(x_test))
# Full 1000 steps, 19 summaries and no evaluation summary:
# 1 summary of net at step 1
# 9 x (1 summary of net and 1 summary of global step) for steps 101, 201,...
self.assertEqual(19, len(_get_summary_events(classifier1.model_dir)))
with self.assertRaises(ValueError):
_get_summary_events(classifier1.model_dir + '/eval')
# classifier with early stopping - improved accuracy on testing set
classifier2 = learn.DNNClassifier(
hidden_units=[10, 20, 10], feature_columns=feature_columns, n_classes=3,
config=tf.contrib.learn.RunConfig(save_checkpoints_secs=1))
classifier2.fit(x_train, y_train, monitors=[val_monitor], steps=2000)
_ = accuracy_score(y_val, classifier2.predict(x_val))
_ = accuracy_score(y_test, classifier2.predict(x_test))
# Note, this test is unstable, so not checking for equality.
# See stability_test for examples of stability issues.
if val_monitor.early_stopped:
self.assertLess(val_monitor.best_step, 2000)
# Note, due to validation monitor stopping after the best score occur,
# the accuracy at current checkpoint is less.
# TODO(ipolosukhin): Time machine for restoring old checkpoints?
# flaky, still not always best_value better then score2 value.
# self.assertGreater(val_monitor.best_value, score2_val)
# Early stopped, unstable so checking only < then max.
self.assertLess(len(_get_summary_events(classifier2.model_dir)), 21)
# Eval typically has ~6 events, but it varies based on the run.
self.assertLess(len(_get_summary_events(
classifier2.model_dir + '/eval')), 8)
示例13: test_pandas_series
def test_pandas_series(self):
if HAS_PANDAS:
import pandas as pd # pylint: disable=g-import-not-at-top
random.seed(42)
iris = datasets.load_iris()
data = pd.DataFrame(iris.data)
labels = pd.Series(iris.target)
classifier = learn.LinearClassifier(
feature_columns=learn.infer_real_valued_columns_from_input(data),
n_classes=3)
classifier.fit(data, labels, steps=100)
score = accuracy_score(labels, list(classifier.predict(data)))
self.assertGreater(score, 0.5, "Failed with score = {0}".format(score))
示例14: testIrisDNN
def testIrisDNN(self):
if HAS_SKLEARN:
random.seed(42)
iris = datasets.load_iris()
feature_columns = learn.infer_real_valued_columns_from_input(iris.data)
classifier = learn.DNNClassifier(
feature_columns=feature_columns, hidden_units=[10, 20, 10],
n_classes=3)
grid_search = GridSearchCV(classifier,
{'hidden_units': [[5, 5], [10, 10]]},
scoring='accuracy',
fit_params={'steps': [50]})
grid_search.fit(iris.data, iris.target)
score = accuracy_score(iris.target, grid_search.predict(iris.data))
self.assertGreater(score, 0.5, 'Failed with score = {0}'.format(score))
示例15: test_pandas_dataframe
def test_pandas_dataframe(self):
if HAS_PANDAS:
import pandas as pd # pylint: disable=g-import-not-at-top
random.seed(42)
iris = datasets.load_iris()
data = pd.DataFrame(iris.data)
labels = pd.DataFrame(iris.target)
classifier = learn.TensorFlowLinearClassifier(
feature_columns=learn.infer_real_valued_columns_from_input(data),
n_classes=3)
classifier.fit(data, labels)
score = accuracy_score(labels[0], classifier.predict(data))
self.assertGreater(score, 0.5, "Failed with score = {0}".format(score))
else:
print("No pandas installed. pandas-related tests are skipped.")