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
# Defaults to a tf.float32 scalar. | |
numeric_feature_column = tf.feature_column.numeric_column(key="SepalLength") | |
# Represent a tf.float64 scalar. | |
numeric_feature_column = tf.feature_column.numeric_column(key="SepalLength", | |
dtype=tf.float64) | |
# Represent a 10-element vector in which each cell contains a tf.float32. | |
vector_feature_column = tf.feature_column.numeric_column(key="Bowling", | |
shape=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
import pandas as pd | |
import numpy as np | |
import tensorflow as tf | |
from sklearn.model_selection import train_test_split | |
#load training data | |
train_df = pd.read_csv('train.csv', index_col='ID') | |
#split into features and target |
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 input_fn(X, y, batch_size=16, epochs=1, shuffle=False): | |
#create a dictionary of features and their values | |
features = {key:np.array(value) for key,value in dict(X).items()} | |
#create a tf.data compliant dataset | |
d = tf.data.Dataset.from_tensor_slices((features, y)) | |
#we need our data in batches | |
d = d.batch(batch_size).repeat(epochs) | |
#optionally shuffle our data |
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.info() | |
feature_columns = [ | |
tf.feature_column.numeric_column('crim', dtype=tf.float64), | |
tf.feature_column.numeric_column('zn', dtype=tf.float64), | |
tf.feature_column.numeric_column('indus', dtype=tf.float64), | |
tf.feature_column.numeric_column('chas', dtype=tf.int64), | |
tf.feature_column.numeric_column('nox', dtype=tf.float64), | |
tf.feature_column.numeric_column('rm', dtype=tf.float64), | |
tf.feature_column.numeric_column('age', dtype=tf.float64), |
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
optimizer = tf.train.FtrlOptimizer(learning_rate=0.01, l1_regularization_strength=0.1) | |
estimator = tf.estimator.LinearRegressor(feature_columns=feature_columns, optimizer=optimizer) | |
estimator.train(input_fn= lambda: input_fn(X_train, y_train), steps=20) |
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_result = estimator.evaluate(input_fn=lambda: input_fn(X_train, y_train)) | |
print("Training results") | |
for key in evaluate_result: | |
print(" {}, was: {}".format(key, evaluate_result[key])) | |
evaluate_result = estimator.evaluate(input_fn=lambda: input_fn(X_test, y_test)) | |
print("Training 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
preds = estimator.predict(input_fn=lambda: input_fn(X_test, y_test)) | |
predictions = np.array([item['predictions'][0] for item in preds]) |
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 tensorflow as tf | |
import tensorflow_hub as hub | |
import pandas as pd | |
import numpy as np | |
import shutil | |
import seaborn as sns | |
import matplotlib.pyplot as plt | |
MODULE_SPEC_50 = 'https://tfhub.dev/google/nnlm-en-dim50/1' |
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 make_input_fn(df, num_epochs): | |
return tf.estimator.inputs.pandas_input_fn( | |
x = df, | |
y = df['sentiment'], | |
batch_size = 128, | |
num_epochs = num_epochs, | |
shuffle = True, | |
queue_capacity = 1000, | |
num_threads = 1 |
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
CSV_COLUMNS = ['medv', 'crim', 'zn', 'indus', 'chas', 'nox', 'rm', 'age', 'dis', 'rad', 'tax', 'ptratio', 'black', 'lstat'] | |
LABEL_COLUMN = 'medv' | |
DEFAULTS = [[0.0], [0.0], [0.0], [0.0], [0], [0.0], [0.0], [0.0], [0.0], [0], [0], [0.0], [0.0], [0.0]] | |
def read_dataset(filename, mode, batch_size = 16): | |
def _input_fn(): | |
def decode_csv(value_column): | |
columns = tf.decode_csv(value_column, record_defaults = DEFAULTS) | |
features = dict(zip(CSV_COLUMNS, columns)) | |
label = features.pop(LABEL_COLUMN) |