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
| # Build Neural Network - Classifier | |
| classifier = tf.estimator.DNNClassifier( | |
| feature_columns=columns_feat, | |
| # Two hidden layers of 10 nodes each. | |
| hidden_units=[10, 10], | |
| # The model is classifying 3 classes | |
| n_classes=3) |
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
| # Define train function | |
| def train_function(inputs, outputs, batch_size): | |
| dataset = tf.data.Dataset.from_tensor_slices((dict(inputs), outputs)) | |
| dataset = dataset.shuffle(1000).repeat().batch(batch_size) | |
| return dataset.make_one_shot_iterator().get_next() | |
| # Train the Model. | |
| classifier.train( | |
| input_fn=lambda:train_function(train_x, train_y, 100), | |
| steps=1000) |
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
| # Define evaluation function | |
| def evaluation_function(attributes, classes, batch_size): | |
| attributes=dict(attributes) | |
| if classes is None: | |
| inputs = attributes | |
| else: | |
| inputs = (attributes, classes) | |
| dataset = tf.data.Dataset.from_tensor_slices(inputs) | |
| assert batch_size is not None, "batch_size must not be None" | |
| dataset = dataset.batch(batch_size) |
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
| from keras.models import Sequential | |
| from keras.layers import Dense | |
| model = Sequential() | |
| model.add(Dense(3, input_dim=2, activation='relu')) | |
| model.add(Dense(1, activation='softmax')) |
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
| # Importing libraries | |
| from keras.models import Sequential | |
| from keras.layers import Dense | |
| from keras.utils import np_utils | |
| import numpy | |
| import pandas as pd |
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
| # Import training dataset | |
| training_dataset = pd.read_csv('iris_training.csv', names=COLUMN_NAMES, header=0) | |
| train_x = training_dataset.iloc[:, 0:4].values | |
| train_y = training_dataset.iloc[:, 4].values | |
| # Import testing dataset | |
| test_dataset = pd.read_csv('iris_test.csv', names=COLUMN_NAMES, header=0) | |
| test_x = test_dataset.iloc[:, 0:4].values | |
| test_y = test_dataset.iloc[:, 4].values |
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
| # Encoding training dataset | |
| encoding_train_y = np_utils.to_categorical(train_y) | |
| # Encoding training dataset | |
| encoding_test_y = np_utils.to_categorical(test_y) |
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
| # Creating a model | |
| model = Sequential() | |
| model.add(Dense(10, input_dim=4, activation='relu')) | |
| model.add(Dense(10, activation='relu')) | |
| model.add(Dense(3, activation='softmax')) | |
| # Compiling model | |
| model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) |
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
| # Training a model | |
| model.fit(train_x, encoding_train_y, epochs=300, batch_size=10) |
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
| # Evaluate the model | |
| scores = model.evaluate(test_x, encoding_test_y) | |
| print("\nAccuracy: %.2f%%" % (scores[1]*100)) |