本文整理汇总了Python中tensorflow.contrib.learn.DNNClassifier方法的典型用法代码示例。如果您正苦于以下问题:Python learn.DNNClassifier方法的具体用法?Python learn.DNNClassifier怎么用?Python learn.DNNClassifier使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow.contrib.learn
的用法示例。
在下文中一共展示了learn.DNNClassifier方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: dnntfDef
# 需要导入模块: from tensorflow.contrib import learn [as 别名]
# 或者: from tensorflow.contrib.learn import DNNClassifier [as 别名]
def dnntfDef(self):
self.conf['DNNClassifier'] = {
'runDNNTF' : True,
'runSkflowDNNTF' : True,
'alwaysRetrainDNNTF' : False,
'alwaysImproveDNNTF' : True,
'hidden_layersDNNTF' : [400,],
'optimizerDNNTF' : "ProximalAdagrad",
'learning_rateDNNTF' : 0.1,
'l2_reg_strengthDNNTF' : 1e-4,
'activation_functionDNNTF' : "tanh",
'dropout_percDNNTF' : str(None),
'trainingStepsDNNTF' : 1000,
'valMonitorSecsDNNTF' : 200,
'logCheckpointDNNTF' : True,
'timeCheckpointDNNTF' : 20,
'thresholdProbabilityPredDNNTF' : 0.01,
'plotMapDNNTF' : True,
}
示例2: dnntfDef
# 需要导入模块: from tensorflow.contrib import learn [as 别名]
# 或者: from tensorflow.contrib.learn import DNNClassifier [as 别名]
def dnntfDef(self):
self.conf['DNNClassifier'] = {
'runDNNTF' : True,
'runSkflowDNNTF' : True,
'alwaysRetrainDNNTF' : False,
'alwaysImproveDNNTF' : True,
'hidden_layersDNNTF' : [400,],
'optimizerDNNTF' : "ProximalAdagrad",
'learning_rateDNNTF' : 0.1,
'learning_rate_decayDNNTF' : False,
'learning_rate_decay_rateDNNTF' : 0.96,
'learning_rate_decay_stepsDNNTF' : 100,
'l2_reg_strengthDNNTF' : 1e-4,
'activation_functionDNNTF' : "tanh",
'dropout_percDNNTF' : str(None),
'trainingStepsDNNTF' : 1000,
'valMonitorSecsDNNTF' : 200,
'logCheckpointDNNTF' : True,
'timeCheckpointDNNTF' : 20,
'thresholdProbabilityPredDNNTF' : 0.01,
'plotMapDNNTF' : True,
'shuffleTrainDNNTF' : True,
'shuffleTestDNNTF' : False,
}
示例3: printInfo
# 需要导入模块: from tensorflow.contrib import learn [as 别名]
# 或者: from tensorflow.contrib.learn import DNNClassifier [as 别名]
def printInfo():
print('==========================================================================\n')
print('\033[1m Running Deep Neural Networks: tf.DNNClassifier - TensorFlow...\033[0m')
print(' Optimizer:',dnntfDef.optimizer_tag,
'\n Hidden layers:', dnntfDef.hidden_layers,
'\n Activation function:',dnntfDef.activation_function,
'\n L2:',dnntfDef.l2_reg_strength,
'\n Dropout:', dnntfDef.dropout_perc,
'\n Learning rate:', dnntfDef.learning_rate,
'\n Shuffle Train:', dnntfDef.shuffleTrain,
'\n Shuffle Test:', dnntfDef.shuffleTest,)
if dnntfDef.learning_rate_decay == False:
print(' Fixed learning rate :',dnntfDef.learning_rate,)
else:
print(' Exponential decay - initial learning rate:',dnntfDef.learning_rate,
'\n Exponential decay rate:', dnntfDef.learning_rate_decay_rate,
'\n Exponential decay steps:', dnntfDef.learning_rate_decay_steps,)
#********************************************************************************
示例4: main
# 需要导入模块: from tensorflow.contrib import learn [as 别名]
# 或者: from tensorflow.contrib.learn import DNNClassifier [as 别名]
def main(unused_argv):
iris = load_iris()
x_train, x_test, y_train, y_test = cross_validation.train_test_split(
iris.data, iris.target, test_size=0.2, random_state=42)
# It's useful to scale to ensure Stochastic Gradient Descent
# will do the right thing.
scaler = StandardScaler()
# DNN classifier.
classifier = learn.DNNClassifier(
feature_columns=learn.infer_real_valued_columns_from_input(x_train),
hidden_units=[10, 20, 10], n_classes=3)
pipeline = Pipeline([('scaler', scaler),
('DNNclassifier', classifier)])
pipeline.fit(x_train, y_train, DNNclassifier__steps=200)
score = accuracy_score(y_test, list(pipeline.predict(x_test)))
print('Accuracy: {0:f}'.format(score))
示例5: main
# 需要导入模块: from tensorflow.contrib import learn [as 别名]
# 或者: from tensorflow.contrib.learn import DNNClassifier [as 别名]
def main(unused_argv):
# Load dataset.
iris = learn.datasets.load_dataset('iris')
x_train, x_test, y_train, y_test = cross_validation.train_test_split(
iris.data, iris.target, test_size=0.2, random_state=42)
# Build 3 layer DNN with 10, 20, 10 units respectively.
feature_columns = learn.infer_real_valued_columns_from_input(x_train)
classifier = learn.DNNClassifier(
feature_columns=feature_columns, hidden_units=[10, 20, 10], n_classes=3)
# Fit and predict.
classifier.fit(x_train, y_train, steps=200)
predictions = list(classifier.predict(x_test, as_iterable=True))
score = metrics.accuracy_score(y_test, predictions)
print('Accuracy: {0:f}'.format(score))
示例6: predDNNTF
# 需要导入模块: from tensorflow.contrib import learn [as 别名]
# 或者: from tensorflow.contrib.learn import DNNClassifier [as 别名]
def predDNNTF(clf, le, R, Cl):
import tensorflow as tf
import tensorflow.contrib.learn as skflow
from sklearn import preprocessing
#**********************************************
''' Predict '''
#**********************************************
def input_fn_predict():
x = tf.constant(R.astype(np.float32))
return x
pred_class = list(clf.predict_classes(input_fn=input_fn_predict))[0]
predValue = le.inverse_transform(pred_class)
prob = list(clf.predict_proba(input_fn=input_fn_predict))[0]
predProb = round(100*prob[pred_class],2)
rosterPred = np.where(prob>dnntfDef.thresholdProbabilityPred/100)[0]
print('\n ===================================')
print(' \033[1msk-DNN-TF\033[0m - Probability >',str(dnntfDef.thresholdProbabilityPred),'%')
print(' ===================================')
print(' Prediction\tProbability [%]')
for i in range(rosterPred.shape[0]):
print(' ',str(np.unique(Cl)[rosterPred][i]),'\t\t',str('{:.4f}'.format(100*prob[rosterPred][i])))
print(' ===================================')
print('\033[1m' + '\n Predicted value (skflow.DNNClassifier) = ' + predValue +
' (probability = ' + str(predProb) + '%)\033[0m\n')
return predValue, predProb
#**********************************************
示例7: predDNNTF2
# 需要导入模块: from tensorflow.contrib import learn [as 别名]
# 或者: from tensorflow.contrib.learn import DNNClassifier [as 别名]
def predDNNTF2(clf, le, R, Cl):
import tensorflow as tf
import tensorflow.contrib.learn as skflow
from sklearn import preprocessing
predict_input_fn = tf.estimator.inputs.numpy_input_fn(
x={"x": R},
num_epochs=1,
shuffle=False)
predictions = list(clf.predict(input_fn=predict_input_fn))
pred_class = [p["class_ids"] for p in predictions][0][0]
predValue = le.inverse_transform(pred_class)
prob = [p["probabilities"] for p in predictions][0]
predProb = round(100*prob[pred_class],2)
rosterPred = np.where(prob>dnntfDef.thresholdProbabilityPred/100)[0]
print('\n ==================================')
print(' \033[1mtf.DNN-TF\033[0m - Probability >',str(dnntfDef.thresholdProbabilityPred),'%')
print(' ==================================')
print(' Prediction\tProbability [%]')
for i in range(rosterPred.shape[0]):
print(' ',str(np.unique(Cl)[rosterPred][i]),'\t\t',str('{:.4f}'.format(100*prob[rosterPred][i])))
print(' ==================================')
print('\033[1m' + '\n Predicted value (tf.DNNClassifier) = ' + predValue +
' (probability = ' + str(predProb) + '%)\033[0m\n')
return predValue, predProb
#********************************************************************************
示例8: main
# 需要导入模块: from tensorflow.contrib import learn [as 别名]
# 或者: from tensorflow.contrib.learn import DNNClassifier [as 别名]
def main(unused_argv):
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, early_stopping_rounds=200)
model_dir = '/tmp/iris_model'
clean_folder(model_dir)
# classifier with early stopping on training data
classifier1 = learn.DNNClassifier(
feature_columns=learn.infer_real_valued_columns_from_input(x_train),
hidden_units=[10, 20, 10], n_classes=3, model_dir=model_dir)
classifier1.fit(x=x_train, y=y_train, steps=2000)
predictions1 = list(classifier1.predict(x_test, as_iterable=True))
score1 = metrics.accuracy_score(y_test, predictions1)
model_dir = '/tmp/iris_model_val'
clean_folder(model_dir)
# classifier with early stopping on validation data, save frequently for
# monitor to pick up new checkpoints.
classifier2 = learn.DNNClassifier(
feature_columns=learn.infer_real_valued_columns_from_input(x_train),
hidden_units=[10, 20, 10], n_classes=3, model_dir=model_dir,
config=tf.contrib.learn.RunConfig(save_checkpoints_secs=1))
classifier2.fit(x=x_train, y=y_train, steps=2000, monitors=[val_monitor])
predictions2 = list(classifier2.predict(x_test, as_iterable=True))
score2 = metrics.accuracy_score(y_test, predictions2)
# In many applications, the score is improved by using early stopping
print('score1: ', score1)
print('score2: ', score2)
print('score2 > score1: ', score2 > score1)
示例9: main
# 需要导入模块: from tensorflow.contrib import learn [as 别名]
# 或者: from tensorflow.contrib.learn import DNNClassifier [as 别名]
def main(unused_argv):
# Load dataset.
iris = learn.datasets.load_dataset('iris')
x_train, x_test, y_train, y_test = cross_validation.train_test_split(
iris.data, iris.target, test_size=0.2, random_state=42)
# Note that we are saving and load iris data as h5 format as a simple
# demonstration here.
h5f = h5py.File('/tmp/test_hdf5.h5', 'w')
h5f.create_dataset('X_train', data=x_train)
h5f.create_dataset('X_test', data=x_test)
h5f.create_dataset('y_train', data=y_train)
h5f.create_dataset('y_test', data=y_test)
h5f.close()
h5f = h5py.File('/tmp/test_hdf5.h5', 'r')
x_train = np.array(h5f['X_train'])
x_test = np.array(h5f['X_test'])
y_train = np.array(h5f['y_train'])
y_test = np.array(h5f['y_test'])
# Build 3 layer DNN with 10, 20, 10 units respectively.
feature_columns = learn.infer_real_valued_columns_from_input(x_train)
classifier = learn.DNNClassifier(
feature_columns=feature_columns, hidden_units=[10, 20, 10], n_classes=3)
# Fit and predict.
classifier.fit(x_train, y_train, steps=200)
score = metrics.accuracy_score(y_test, classifier.predict(x_test))
print('Accuracy: {0:f}'.format(score))
示例10: get_model
# 需要导入模块: from tensorflow.contrib import learn [as 别名]
# 或者: from tensorflow.contrib.learn import DNNClassifier [as 别名]
def get_model(filename=CLASSIFIER_FILE):
''' Get CNN classifier object from file or create one if none exists on file.'''
if(filename == None):
# Load dataset
print(Helper.unserialize("../Datasets/raw_new_80.data"))
train_data, train_targets, test_data, expected = get_featureset('raw')
raw_train_data = np.zeros((train_data.shape[0], 20, 20))
i = 0
for item in train_data:
raw_train_data[i] = item.reshape((20,20))
#Display.show_image(raw_train_data[i])
i = i+1
raw_test_data = np.zeros((test_data.shape[0], 20, 20))
i = 0
for item in test_data:
raw_test_data[i] = item.reshape((20,20))
#Display.show_image(raw_test_data[i])
i = i+1
# Build Classifier
# classifier = skflow.TensorFlowEstimator(model_fn=multilayer_conv_model, n_classes=2,
# steps=500, learning_rate=0.05, batch_size=128)
classifier = skflow.DNNClassifier(feature_engineering_fn=conv_model, n_classes=2)
classifier.fit(raw_train_data, train_targets)
# Assess built classifier
predictions = classifier.predict(raw_test_data)
accuracy = metrics.accuracy_score(expected, predictions)
confusion_matrix = metrics.confusion_matrix(expected, predictions)
print("Confusion matrix:\n%s" % confusion_matrix)
print('Accuracy: %f' % accuracy)
return classifier
else:
serialized_classifier = Helper.unserialize(filename)
return serialized_classifier
示例11: predDNNTF
# 需要导入模块: from tensorflow.contrib import learn [as 别名]
# 或者: from tensorflow.contrib.learn import DNNClassifier [as 别名]
def predDNNTF(clf, le, R, Cl):
import tensorflow as tf
#import tensorflow.contrib.learn as skflow
from sklearn import preprocessing
predict_input_fn = tf.estimator.inputs.numpy_input_fn(
x={"x": R},
num_epochs=1,
shuffle=False)
predictions = list(clf.predict(input_fn=predict_input_fn))
if dnntfDef.useRegressor == False:
pred_class = [p["class_ids"] for p in predictions][0][0]
if pred_class.size >0:
predValue = le.inverse_transform([pred_class])[0]
else:
predValue = 0
prob = [p["probabilities"] for p in predictions][0]
predProb = round(100*prob[pred_class],2)
rosterPred = np.where(prob>dnntfDef.thresholdProbabilityPred/100)[0]
print('\n =============================================')
print(' \033[1mtf.DNN Classifier-TF\033[0m - Probability >',str(dnntfDef.thresholdProbabilityPred),'%')
print(' =============================================')
print(' Prediction\tProbability [%]')
for i in range(rosterPred.shape[0]):
print(' ',str(np.unique(Cl)[rosterPred][i]),'\t\t',str('{:.4f}'.format(100*prob[rosterPred][i])))
print(' =============================================')
print('\033[1m' + '\n Predicted value (tf.DNNClassifier) = ' + str(predValue) +
' (probability = ' + str(predProb) + '%)\033[0m\n')
else:
pred = [p["predictions"] for p in predictions][0][0]
predProb = 0
if pred.size >0:
predValue = pred
else:
predValue = 0
print('\n ==================================')
print(' \033[1mtf.DNN Regressor - TF\033[0m')
print(' ==================================')
print('\033[1m' + '\n Predicted value (tf.DNNRegressor) = ' + str(predValue) +'\033[0m\n')
return predValue, predProb
#********************************************************************************