Basic Execution
TensorFlow uses a computational graph approach. Operations are defined first and executed within a session. To manage logging verbosity, you can adjust environment variables.
import tensorflow as tf
import os
# Suppress informational logs
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
val1 = tf.constant(15)
val2 = tf.constant(25)
computation = tf.add(val1, val2)
with tf.Session() as session:
result = session.run(computation)
print(result)
Computational Graphs
The graph serves as the primary structure for TensorFlow, housing tf.Operation units and tf.Tensor data flows. By default, TensorFlow maintains a global graph, but developers can instantiate and utilize custom graphs using tf.Graph().
custom_graph = tf.Graph()
with custom_graph.as_default():
node = tf.constant(5.0)
print(node.graph is custom_graph) # True
Sessions and Data Feeding
A tf.Session is required to allocate resources and execute nodes within a graph. For dynamic inputs, placeholders act as entry points where data is injected at runtime via the feed_dict argument.
input_data = tf.placeholder(tf.float32, shape=(None, 2))
multiplier = tf.constant(2.0)
output = input_data * multiplier
with tf.Session() as session:
feed = [[1.0, 2.0], [3.0, 4.0]]
res = session.run(output, feed_dict={input_data: feed})
print(res)
Tensor Management
Tensors are multidimensional arrays characterized by a name, shape, and data type. While static shapes are defined at initialization, tf.reshape creates new tensors with adjusted dimensions. Static shapes can be queried via get_shape() and adjusted using set_shape() provided there are no conflicts.
Variables and Persistence
Variables represent stateful parameters, such as weights in a neural network, that persist across sessions. They must be explicit initialized using tf.global_variables_initializer().
weights = tf.Variable(tf.random_normal([1, 1]), name='weight')
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
print(weights.eval())
Visualization with TensorBoard
TensorBoard facilitates model monitoring by serializing graph data into event files. By using tf.summary operations (like scalar or histogram), developers can track loss metrics and parameter distributions.
- Collect summaries: Use
tf.summary.scalarortf.summary.merge_all(). - Write logs:
FileWritersaves the event logs to a local directory. - Launch: Execute
tensorboard --logdir=path/to/logsin your terminal.
Linear Regression Workflow
Optimizing parameters requires a loss function (e.g., Mean Squared Error) and an optimizer (e.g., Gradient Descent). By iteratively updating variables against the loss, the model converges toward target values.
# Basic gradient optimization setup
loss_val = tf.reduce_mean(tf.square(y_target - y_predicted))
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)
train_step = optimizer.minimize(loss_val)
# Execution loop
for i in range(100):
sess.run(train_step)
CLI Configuration
TensorFlow provides a helper module to handle command-line arguments, facilitating experiment configuration without modifying source code.
flags = tf.app.flags
flags.DEFINE_integer('epochs', 50, 'Number of training iterations')
params = flags.FLAGS
def main(_):
print(f'Training for {params.epochs} epochs')
if __name__ == '__main__':
tf.app.run()