Last active
August 8, 2018 19:07
-
-
Save nbortolotti/64e00bf4bc8d4eaef7a6830e07239f18 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # imports | |
| import tensorflow as tf | |
| import pandas as pd | |
| import numpy as np | |
| # Data download and dataset creation witout tf.data | |
| train_ds_url = "http://download.tensorflow.org/data/iris_training.csv" | |
| test_ds_url = "http://download.tensorflow.org/data/iris_test.csv" | |
| ds_columns = ['SepalLength', 'SepalWidth','PetalLength', 'PetalWidth', 'Plants'] | |
| species = np.array(['Setosa', 'Versicolor', 'Virginica'], dtype=np.object) | |
| # Load data | |
| categories='Plants' | |
| train_path = tf.keras.utils.get_file(train_ds_url.split('/')[-1], train_ds_url) | |
| test_path = tf.keras.utils.get_file(test_ds_url.split('/')[-1], test_ds_url) | |
| train = pd.read_csv(train_path, names=ds_columns, header=0) | |
| train_plantfeatures, train_categories = train, train.pop(categories) | |
| test = pd.read_csv(test_path, names=ds_columns, header=0) | |
| test_plantfeatures, test_categories = test, test.pop(categories) | |
| #to_categorical | |
| y_categorical = tf.contrib.keras.utils.to_categorical(train_categories, num_classes=3) | |
| y_categorical_test = tf.contrib.keras.utils.to_categorical(test_categories, num_classes=3) | |
| # Build the dataset | |
| # train | |
| dataset = tf.data.Dataset.from_tensor_slices((train_plantfeatures, y_categorical)) | |
| dataset = dataset.batch(32) | |
| dataset = dataset.shuffle(1000) | |
| dataset = dataset.repeat() | |
| #test | |
| dataset_test = tf.data.Dataset.from_tensor_slices((test_plantfeatures, y_categorical_test)) | |
| dataset_test = dataset_test.batch(32) | |
| dataset_test = dataset_test.shuffle(1000) | |
| dataset_test = dataset_test.repeat() | |
| #Build the model | |
| model = tf.keras.Sequential([ | |
| tf.keras.layers.Dense(16, input_dim=4), | |
| tf.keras.layers.Dense(3, activation=tf.nn.softmax), | |
| ]) | |
| model.summary() | |
| #Compile the model | |
| model.compile(loss='categorical_crossentropy', | |
| optimizer='sgd', | |
| metrics=['accuracy']) | |
| # Train the model | |
| model.fit(dataset, steps_per_epoch=32, epochs=100, verbose=1) | |
| #Eval the model | |
| loss, accuracy = model.evaluate(dataset_test, steps=32) | |
| print("loss:%f"% (loss)) | |
| print("accuracy: %f"% (accuracy)) | |
| # Use the model | |
| new_specie = np.array([7.9,3.8,6.4,2.0]) | |
| predition = np.around(model.predict(np.expand_dims(new_specie, axis=0))).astype(np.int)[0] | |
| print("This species should be %s" % species[predition.astype(np.bool)][0]) | |
| #Save the model | |
| !mkdir model | |
| tf.keras.models.save_model( | |
| model, | |
| "./model/iris_model.h5", | |
| overwrite=True, | |
| include_optimizer=True | |
| ) | |
| #Check the model | |
| new_model = tf.keras.models.load_model("./model/iris_model.h5") | |
| xarray2 = np.array([7.9,3.8,6.4,2.0]) | |
| pred = np.around(new_model.predict(np.expand_dims(xarray2, axis=0))).astype(np.int)[0] | |
| print(pred) | |
| print("That means it's a %s" % species[pred.astype(np.bool)][0]) | |
| #[optional] Visualize the graph | |
| graph = tf.get_default_graph() | |
| # Let's visualize our graph! | |
| # Tip: to make your graph more readable you can add a | |
| # name="..." parameter to the individual Ops. | |
| # src: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/tutorials/deepdream/deepdream.ipynb | |
| # requeried if is not importated before | |
| # import tensorflow as tf | |
| # import numpy as np | |
| from IPython.display import clear_output, Image, display, HTML | |
| def strip_consts(graph_def, max_const_size=32): | |
| """Strip large constant values from graph_def.""" | |
| strip_def = tf.GraphDef() | |
| for n0 in graph_def.node: | |
| n = strip_def.node.add() | |
| n.MergeFrom(n0) | |
| if n.op == 'Const': | |
| tensor = n.attr['value'].tensor | |
| size = len(tensor.tensor_content) | |
| if size > max_const_size: | |
| tensor.tensor_content = "<stripped %d bytes>"%size | |
| return strip_def | |
| def show_graph(graph_def, max_const_size=32): | |
| """Visualize TensorFlow graph.""" | |
| if hasattr(graph_def, 'as_graph_def'): | |
| graph_def = graph_def.as_graph_def() | |
| strip_def = strip_consts(graph_def, max_const_size=max_const_size) | |
| code = """ | |
| <script> | |
| function load() {{ | |
| document.getElementById("{id}").pbtxt = {data}; | |
| }} | |
| </script> | |
| <link rel="import" href="https://tensorboard.appspot.com/tf-graph-basic.build.html" onload=load()> | |
| <div style="height:600px"> | |
| <tf-graph-basic id="{id}"></tf-graph-basic> | |
| </div> | |
| """.format(data=repr(str(strip_def)), id='graph'+str(np.random.rand())) | |
| iframe = """ | |
| <iframe seamless style="width:1200px;height:620px;border:0" srcdoc="{}"></iframe> | |
| """.format(code.replace('"', '"')) | |
| display(HTML(iframe)) | |
| show_graph(graph) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment