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 | |
from tensorflow import keras | |
from keras import Sequential | |
from keral.layers import Dense | |
#build our model | |
model = Sequential() | |
model.add(Dense(50, activation='relu')) |
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
X = scaled_train_df.drop(target, axis=1).values | |
Y = scaled_train_df[[target]].values | |
# Train the model | |
model.fit( | |
X[10:], | |
Y[10:], | |
epochs=50, | |
shuffle=True, | |
verbose=2 |
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
#inference | |
prediction = model.predict(X[:1]) | |
y_0 = prediction[0][0] | |
print('Prediction with scaling - {}',format(y_0)) | |
y_0 -= added | |
y_0 /= multiplied_by | |
print("Housing Price Prediction - ${}".format(y_0)) |
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
def my_input_fn(file_path, perform_shuffle=False, repeat_count=1): | |
def decode_csv(line): | |
parsed_line = tf.decode_csv(line, [[0.], [0.], [0.], [0.], [0]]) | |
label = parsed_line[-1:] # Last element is the label | |
del parsed_line[-1] # Delete last element | |
features = parsed_line # Everything (but last element) are the features | |
d = dict(zip(feature_names, features)), label | |
return d | |
dataset = (tf.data.TextLineDataset(file_path) # Read text file |
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
# Let create a memory dataset for prediction. | |
# We've taken the first 3 examples in FILE_TEST. | |
prediction_input = [[5.9, 3.0, 4.2, 1.5], # -> 1, Iris Versicolor | |
[6.9, 3.1, 5.4, 2.1], # -> 2, Iris Virginica | |
[5.1, 3.3, 1.7, 0.5]] # -> 0, Iris Sentosa | |
def new_input_fn(): | |
def decode(x): | |
x = tf.split(x, 4) # Need to split into our 4 features | |
# When predicting, we don't need (or have) any labels | |
return dict(zip(feature_names, x)) # Then build a dict from them |
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
feature_names = [ | |
'SepalLength', | |
'SepalWidth', | |
'PetalLength', | |
'PetalWidth'] | |
# Create the feature_columns, which specifies the input to our model. | |
# All our input features are numeric, so use numeric_column for each one. | |
feature_columns = [tf.feature_column.numeric_column(k) for k in feature_names] |
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
# Train our model, use the previously function my_input_fn | |
# Input to training is a file with training example | |
# Stop training after 8 iterations of train data (epochs) | |
classifier.train( | |
input_fn=lambda: my_input_fn(FILE_TRAIN, True, 8)) |
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 our model using the examples contained in FILE_TEST | |
# Return value will contain evaluation_metrics such as: loss & average_loss | |
evaluate_result = estimator.evaluate( | |
input_fn=lambda: my_input_fn(FILE_TEST, False, 4) | |
print("Evaluation results") | |
for key in evaluate_result: | |
print(" {}, was: {}".format(key, evaluate_result[key])) |
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
# Predict the type of some Iris flowers. | |
# Let's predict the examples in FILE_TEST, repeat only once. | |
predict_results = classifier.predict( | |
input_fn=lambda: my_input_fn(FILE_TEST, False, 1)) | |
print("Predictions on test file") | |
for prediction in predict_results: | |
# Will print the predicted class, i.e: 0, 1, or 2 if the prediction | |
# is Iris Sentosa, Vericolor, Virginica, respectively. | |
print prediction["class_ids"][0] |
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
# A numeric column for the raw input. | |
numeric_feature_column = tf.feature_column.numeric_column("Year") | |
# Bucketize the numeric column on the years 1960, 1980, and 2000 | |
bucketized_feature_column = tf.feature_column.bucketized_column( | |
source_column = numeric_feature_column, | |
boundaries = [1960, 1980, 2000]) |