In [1]:
# import and check version
import tensorflow as tf
# tf can be really verbose
tf.logging.set_verbosity(tf.logging.ERROR)
print(tf.__version__)
In [2]:
# a small sanity check, does tf seem to work ok?
hello = tf.constant('Hello TF!')
sess = tf.Session()
print(sess.run(hello))
sess.close()
In [3]:
x = tf.placeholder(tf.float32)
y = tf.placeholder(tf.float32)
z = x + y
r = tf.random_normal([10, 2])
dataset = tf.data.Dataset.from_tensor_slices(r)
iterator = dataset.make_initializable_iterator()
next_row = iterator.get_next()
with tf.Session() as sess:
sess.run(iterator.initializer)
while True:
try:
data = sess.run(next_row)
print(data)
print(sess.run(z, feed_dict={x: data[0], y: data[1]}))
except tf.errors.OutOfRangeError:
break
In [4]:
x = tf.placeholder(tf.float32, shape=[None, 3])
y = tf.layers.dense(inputs=x, units=1)
with tf.Session() as sess:
try:
print(sess.run(y, {x: [[1, 2, 3], [4, 5, 6]]}))
except tf.errors.FailedPreconditionError as fpe:
print(fpe.message)
In [5]:
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(y, {x: [[1, 2, 3], [4, 5, 6]]}))
In [6]:
y = tf.layers.dense(inputs=x, units=2, activation=tf.nn.tanh)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
print(sess.run(y, {x: [[1, 2, 3], [4, 5, 6]]}))
In [0]:
features = {
'sales' : [[5], [10], [8], [9]],
'department': ['sports', 'sports', 'gardening', 'gardening']
}
# numeric values are simple
sales_column = tf.feature_column.numeric_column('sales')
columns = {
sales_column
}
inputs = tf.feature_column.input_layer(features, columns)
In [8]:
# categories are harders, as NNs only accept dense numeric values
categorical_department_column = tf.feature_column.categorical_column_with_vocabulary_list(
'department', ['sports', 'gardening'])
columns = {
sales_column,
categorical_department_column
}
# we can decide if we want the category to be encoded as embedding or multi-hot
try:
inputs = tf.feature_column.input_layer(features, columns)
except ValueError as ve:
print(ve)
In [0]:
multi_hot_department_column = tf.feature_column.indicator_column(categorical_department_column)
In [0]:
columns = {
sales_column,
multi_hot_department_column
}
inputs = tf.feature_column.input_layer(features, columns)
In [11]:
# feature columns also need initialization
var_init = tf.global_variables_initializer()
table_init = tf.tables_initializer()
with tf.Session() as sess:
sess.run((var_init, table_init))
# first two are departments last entry is just sales as is
print(sess.run(inputs))
In [12]:
# multi (one in our case) hot encoding of departments
columns = {
multi_hot_department_column
}
inputs = tf.feature_column.input_layer(features, columns)
var_init = tf.global_variables_initializer()
table_init = tf.tables_initializer()
with tf.Session() as sess:
sess.run((var_init, table_init))
print(sess.run(inputs))
In [13]:
# alternative, embedding in three dimensions
embedding_department_column = tf.feature_column.embedding_column(categorical_department_column, dimension=3)
columns = {
embedding_department_column
}
inputs = tf.feature_column.input_layer(features, columns)
var_init = tf.global_variables_initializer()
table_init = tf.tables_initializer()
with tf.Session() as sess:
sess.run((var_init, table_init))
print(sess.run(inputs))
In [0]: