def DNN_Classifier(self, X_train, X_test, y_train, y_test): scaler = MinMaxScaler() scaled_x_train = scaler.fit_transform(X_train) scaled_x_test = scaler.transform(X_test) feat_cols = [ tf.feature_column.numeric_column("x", shape=[self.NUMBER_FEATURES]) ] deep_model = estimator.DNNClassifier( hidden_units=[ self.NUMBER_FEATURES, self.NUMBER_FEATURES, self.NUMBER_FEATURES ], feature_columns=feat_cols, n_classes=self.N_CLASSES, optimizer=tf.train.GradientDescentOptimizer( learning_rate=self.LEARNING_RATE)) input_fn = estimator.inputs.numpy_input_fn(x={'x': scaled_x_train}, y=y_train, shuffle=True, batch_size=self.BATCH_SIZE, num_epochs=self.NUM_EPOCHS) deep_model.train(input_fn=input_fn, steps=self.STEPS) input_fn_eval = estimator.inputs.numpy_input_fn(x={'x': scaled_x_test}, shuffle=False) #return list(deep_model.predict(input_fn=input_fn_eval)) return deep_model.predict(input_fn=input_fn_eval, predict_keys='probabilities')
def get_simple_dnn_classifier_and_metadata(n_classes=2, label_vocabulary=None): """Returns metadata for creating simple DNN classifier.""" if label_vocabulary: feature_spec = tf.feature_column.make_parse_example_spec( feature_columns=util.dnn_columns(False, n_classes=n_classes)) feature_spec['label'] = tf.io.FixedLenFeature(shape=[1], dtype=tf.string) else: feature_spec = tf.feature_column.make_parse_example_spec( feature_columns=util.dnn_columns(True, n_classes=n_classes)) classifier = tf_estimator.DNNClassifier( hidden_units=[4], feature_columns=util.dnn_columns(False), n_classes=n_classes, label_vocabulary=label_vocabulary, loss_reduction=tf.losses.Reduction.SUM) classifier = tf_estimator.add_metrics(classifier, util.classifier_extra_metrics) return { 'estimator': classifier, 'serving_input_receiver_fn': (tf_estimator.export.build_parsing_serving_input_receiver_fn( tf.feature_column.make_parse_example_spec( util.dnn_columns(False)))), 'eval_input_receiver_fn': build_parsing_eval_input_receiver_fn(feature_spec, label_key='label'), 'train_input_fn': util.make_classifier_input_fn(feature_spec, n_classes, label_vocabulary=label_vocabulary), }
def testWorksWithTF2DnnClassifier(self): self.skipTest("b/154115879 - needs more investigation for timeout.") comment_embedding_column = hub.text_embedding_column_v2( "comment", self.model, trainable=False) upvotes = tf.feature_column.numeric_column("upvotes") feature_columns = [comment_embedding_column, upvotes] estimator = tf_estimator.DNNClassifier(hidden_units=[10], feature_columns=feature_columns, model_dir=self.get_temp_dir()) # This only tests that estimator apis are working with the feature # column without throwing exceptions. def input_fn(): features = { "comment": np.array([ ["the quick brown fox"], ["spam spam spam"], ]), "upvotes": np.array([ [20], [1], ]), } labels = np.array([[1], [0]]) return features, labels estimator.train(input_fn, max_steps=1) estimator.evaluate(input_fn, steps=1) estimator.predict(input_fn)
def estimator(): from tensorflow import estimator feat_cols = [tf.feature_column.numeric_column('x', shape=[13])] deep_model = estimator.DNNClassifier( hidden_units=[13, 13, 13], feature_columns=feat_cols, n_classes=3, optimizer=tf.train.AdamOptimizer(learning_rate=0.01) ) train_input_fn = estimator.inputs.numpy_input_fn( x={"x": scaled_x_train}, y=y_train, shuffle=True, batch_size=10, num_epochs=5 ) deep_model.train(input_fn=train_input_fn, steps=5000) test_input_fn = estimator.inputs.numpy_input_fn( x={"x": scaled_x_test}, shuffle=False ) preds = list(deep_model.predict(input_fn=test_input_fn)) preds = [p["class_ids"][0] for p in preds] print(classification_report(y_test, preds))
def build_estimator(model_dir, model_type): """build an estimator""" if model_type == 'wide': m = estimator.LinearClassifier(model_dir=model_dir, feature_columns=base_columns + crossed_columns) elif model_type == 'deep': m = estimator.DNNClassifier(model_dir=model_dir, feature_columns=deep_columns, hidden_units=[100, 50]) else: m = estimator.DNNLinearCombinedClassifier( model_dir=model_dir, linear_feature_columns=crossed_columns, dnn_feature_columns=deep_columns, dnn_hidden_units=[100, 50]) return m
def main(argv): with mlflow.start_run(): args = parser.parse_args(argv[1:]) # Fetch the data (train_x, train_y), (test_x, test_y) = load_data() # Feature columns describe how to use the input. my_feature_columns = [] for key in train_x.keys(): my_feature_columns.append( tf.feature_column.numeric_column(key=key)) # Two hidden layers of 10 nodes each. hidden_units = [10, 10] # Build 2 hidden layer DNN with 10, 10 units respectively. classifier = tf_estimator.DNNClassifier( feature_columns=my_feature_columns, hidden_units=hidden_units, # The model must choose between 3 classes. n_classes=3, ) # Train the Model. classifier.train( input_fn=lambda: train_input_fn(train_x, train_y, args.batch_size), steps=args.train_steps, ) # Evaluate the model. eval_result = classifier.evaluate( input_fn=lambda: eval_input_fn(test_x, test_y, args.batch_size)) print("\nTest set accuracy: {accuracy:0.3f}\n".format(**eval_result)) # Generate predictions from the model expected = ["Setosa", "Versicolor", "Virginica"] predict_x = { "SepalLength": [5.1, 5.9, 6.9], "SepalWidth": [3.3, 3.0, 3.1], "PetalLength": [1.7, 4.2, 5.4], "PetalWidth": [0.5, 1.5, 2.1], } predictions = classifier.predict(input_fn=lambda: eval_input_fn( predict_x, labels=None, batch_size=args.batch_size)) old_predictions = [] template = '\nPrediction is "{}" ({:.1f}%), expected "{}"' for pred_dict, expec in zip(predictions, expected): class_id = pred_dict["class_ids"][0] probability = pred_dict["probabilities"][class_id] print(template.format(SPECIES[class_id], 100 * probability, expec)) old_predictions.append(SPECIES[class_id]) # Creating output tf.Variables to specify the output of the saved model. feat_specifications = { "SepalLength": tf.Variable([], dtype=tf.float64, name="SepalLength"), "SepalWidth": tf.Variable([], dtype=tf.float64, name="SepalWidth"), "PetalLength": tf.Variable([], dtype=tf.float64, name="PetalLength"), "PetalWidth": tf.Variable([], dtype=tf.float64, name="PetalWidth"), } receiver_fn = tf_estimator.export.build_raw_serving_input_receiver_fn( feat_specifications) temp = tempfile.mkdtemp() try: # The model is automatically logged when export_saved_model() is called. saved_estimator_path = classifier.export_saved_model( temp, receiver_fn).decode("utf-8") # Since the model was automatically logged as an artifact (more specifically # a MLflow Model), we don't need to use saved_estimator_path to load back the model. # MLflow takes care of it! pyfunc_model = pyfunc.load_model(mlflow.get_artifact_uri("model")) predict_data = [[5.1, 3.3, 1.7, 0.5], [5.9, 3.0, 4.2, 1.5], [6.9, 3.1, 5.4, 2.1]] df = pd.DataFrame( data=predict_data, columns=[ "SepalLength", "SepalWidth", "PetalLength", "PetalWidth" ], ) # Predicting on the loaded Python Function and a DataFrame containing the # original data we predicted on. predict_df = pyfunc_model.predict(df) # Checking the PyFunc's predictions are the same as the original model's predictions. template = '\nOriginal prediction is "{}", reloaded prediction is "{}"' for expec, pred in zip(old_predictions, predict_df["classes"]): class_id = predict_df["class_ids"][predict_df.loc[ predict_df["classes"] == pred].index[0]] reloaded_label = SPECIES[class_id] print(template.format(expec, reloaded_label)) finally: shutil.rmtree(temp)
def saved_tf_iris_model(tmpdir): # Following code from # https://github.com/tensorflow/models/blob/master/samples/core/get_started/premade_estimator.py train_x, train_y = iris_data_utils.load_data()[0] # Feature columns describe how to use the input. my_feature_columns = [] for key in train_x.keys(): my_feature_columns.append(tf.feature_column.numeric_column(key=key)) # Build 2 hidden layer DNN with 10, 10 units respectively. estimator = tf_estimator.DNNClassifier( feature_columns=my_feature_columns, # Two hidden layers of 10 nodes each. hidden_units=[10, 10], # The model must choose between 3 classes. n_classes=3, ) # Train the Model. batch_size = 100 train_steps = 1000 estimator.train( input_fn=lambda: iris_data_utils.train_input_fn( train_x, train_y, batch_size), steps=train_steps, ) # Generate predictions from the model predict_x = { "SepalLength": [5.1, 5.9, 6.9], "SepalWidth": [3.3, 3.0, 3.1], "PetalLength": [1.7, 4.2, 5.4], "PetalWidth": [0.5, 1.5, 2.1], } estimator_preds = estimator.predict( lambda: iris_data_utils.eval_input_fn(predict_x, None, batch_size)) # Building a dictionary of the predictions by the estimator. if sys.version_info < (3, 0): estimator_preds_dict = estimator_preds.next() else: estimator_preds_dict = next(estimator_preds) for row in estimator_preds: for key in row.keys(): estimator_preds_dict[key] = np.vstack( (estimator_preds_dict[key], row[key])) # Building a pandas DataFrame out of the prediction dictionary. estimator_preds_df = copy.deepcopy(estimator_preds_dict) for col in estimator_preds_df.keys(): if all(len(element) == 1 for element in estimator_preds_df[col]): estimator_preds_df[col] = estimator_preds_df[col].ravel() else: estimator_preds_df[col] = estimator_preds_df[col].tolist() # Building a DataFrame that contains the names of the flowers predicted. estimator_preds_df = pandas.DataFrame.from_dict(data=estimator_preds_df) estimator_preds_results = [ iris_data_utils.SPECIES[id[0]] for id in estimator_preds_dict["class_ids"] ] estimator_preds_results_df = pd.DataFrame( {"predictions": estimator_preds_results}) # Define a function for estimator inference feature_spec = {} for name in my_feature_columns: feature_spec[name.key] = tf.Variable([], dtype=tf.float64, name=name.key) receiver_fn = tf_estimator.export.build_raw_serving_input_receiver_fn( feature_spec) # Save the estimator and its inference function saved_estimator_path = str(tmpdir.mkdir("saved_model")) saved_estimator_path = estimator.export_saved_model( saved_estimator_path, receiver_fn).decode("utf-8") return SavedModelInfo( path=saved_estimator_path, meta_graph_tags=["serve"], signature_def_key="predict", inference_df=pd.DataFrame( data=predict_x, columns=[name.key for name in my_feature_columns]), expected_results_df=estimator_preds_results_df, raw_results=estimator_preds_dict, raw_df=estimator_preds_df, )
def create_tf_estimator_model(directory, export, training_steps=100, use_v1_estimator=False): CSV_COLUMN_NAMES = [ "SepalLength", "SepalWidth", "PetalLength", "PetalWidth", "Species" ] train = pd.read_csv( os.path.join(os.path.dirname(__file__), "iris_training.csv"), names=CSV_COLUMN_NAMES, header=0, ) train_y = train.pop("Species") def input_fn(features, labels, training=True, batch_size=256): """An input function for training or evaluating""" # Convert the inputs to a Dataset. dataset = tf.data.Dataset.from_tensor_slices((dict(features), labels)) # Shuffle and repeat if you are in training mode. if training: dataset = dataset.shuffle(1000).repeat() return dataset.batch(batch_size) my_feature_columns = [] for key in train.keys(): my_feature_columns.append(tf.feature_column.numeric_column(key=key)) feature_spec = {} for feature in CSV_COLUMN_NAMES: feature_spec[feature] = tf.Variable([], dtype=tf.float64, name=feature) receiver_fn = tf_estimator.export.build_raw_serving_input_receiver_fn( feature_spec) run_config = tf_estimator.RunConfig( # Emit loss metrics to TensorBoard every step save_summary_steps=1, ) # If flag set to true, then use the v1 classifier that extends Estimator # If flag set to false, then use the v2 classifier that extends EstimatorV2 if use_v1_estimator: classifier = tf.compat.v1.estimator.DNNClassifier( feature_columns=my_feature_columns, # Two hidden layers of 10 nodes each. hidden_units=[30, 10], # The model must choose between 3 classes. n_classes=3, model_dir=directory, config=run_config, ) else: classifier = tf_estimator.DNNClassifier( feature_columns=my_feature_columns, # Two hidden layers of 10 nodes each. hidden_units=[30, 10], # The model must choose between 3 classes. n_classes=3, model_dir=directory, config=run_config, ) classifier.train(input_fn=lambda: input_fn(train, train_y, training=True), steps=training_steps) if export: classifier.export_saved_model(directory, receiver_fn)
from sklearn.preprocessing import MinMaxScaler scaler = MinMaxScaler() X_train = scaler.fit_transform(X_train) X_test = scaler.transform(X_test) #estimator import tensorflow as tf print(tf.VERSION) feature_columns = [tf.feature_column.numeric_column("x",shape=[13])] from tensorflow import estimator deep_model = estimator.DNNClassifier(hidden_units=[13,13,13], feature_columns=feature_columns, n_classes=3 , optimizer=tf.train.GradientDescentOptimizer(learning_rate=.01)) input_fn = estimator.inputs.numpy_input_fn(x={"x":X_train}, y=y_train,shuffle=True,batch_size=2,num_epochs=10) deep_model.train(input_fn=input_fn, steps=500) input_fn_evaluation=estimator.inputs.numpy_input_fn(x={"x":X_test}, shuffle=False) y_pred = list(deep_model.predict(input_fn=input_fn_evaluation)) y_pred = [p["class_ids"][0] for p in y_pred] import numpy as np y_pred = np.asarray(y_pred) from sklearn.metrics import confusion_matrix, classification_report cr = classification_report(y_test, y_pred)
normalizador = MinMaxScaler() # normalizamos los datos x_train_normalizado = normalizador.fit_transform(x_train) x_test_normalizado = normalizador.transform(x_test) # imprime los valores normalizados print('\n' + str(x_train_normalizado) + '\n') print('\n' + str(x_test_normalizado) + '\n') # imprime filas y comulmas de entrenamiento y pruebas print('\n' + str(x_train_normalizado.shape) + '\n') # print('\n' + str(x_test_normalizado.shape) + '\n') # calcula las caracteristicas de las columnas columnas_caracteristicas = [tf.feature_column.numeric_column('x', shape=[13])] # creamos un modelo, mediante la api de estimator usando el DNNClassifier modelo = estimator.DNNClassifier( hidden_units=[20, 20, 20], feature_columns=columnas_caracteristicas, n_classes=3, optimizer=tf.keras.optimizers.Adam(learning_rate=0.01)) # tf.train.GradientDescentOptimizer(learning_rate=0.01)) # ahora entrenaremos nuestro modelo # pero primero creamos nuestra funciĆ³n de entrada funcion_entrada = tf.compat.v1.estimator.inputs.numpy_input_fn( x={'x': x_train_normalizado}, y=y_train, shuffle=True, batch_size=10, num_epochs=10) # funcion original, pero no funciona con TF v2 # estimator.inputs.numpy_input_fn(x={'x': x_train_normalizado}, y=y_train, shuffle=True, batch_size=10, num_epochs=10) # ejecuta el entrenamiento 600 veces modelo.train(input_fn=funcion_entrada, steps=600)
objetivo = vino['target'] from sklearn.model_selection import train_test_split x_train, x_test, y_train, y_test = train_test_split(caracteristicas, objetivo, test_size=0.3) from sklearn.preprocessing import MinMaxScaler normalizador = MinMaxScaler() x_train_normalizado = normalizador.fit_transform(x_train) x_test_normalizado = normalizador.transform(x_test) columnas_caracteristicas = [tf.feature_column.numeric_column('x', shape=[13])] modelo = estimator.DNNClassifier( hidden_units=[20, 20, 20], feature_columns=columnas_caracteristicas, n_classes=3, optimizer=tf.train.GradientDescentOptimizer(learning_rate=0.01)) funcion_entrada = estimator.inputs.numpy_input_fn(x={'x': x_train_normalizado}, y=y_train, shuffle=True, batch_size=10, num_epochs=10) modelo.train(input_fn=funcion_entrada, steps=600) funcion_evaluacion = estimator.inputs.numpy_input_fn( x={'x': x_test_normalizado}, shuffle=False) predicciones = list(modelo.predict(input_fn=funcion_evaluacion)) predicciones_finales = [p['class_ids'][0] for p in predicciones]