Tensorflow

Frequently used code for tensorflow related code snippets

General steps

  • Create Tensors (variables) that are not yet executed/evaluated.

  • Write operations between those Tensors.

  • Initialize your Tensors.

  • Create a Session.

  • Run the Session. This will run the operations you'd written above.

One-liners

  • tf.sigmoid

  • tf.nn.sigmoid_cross_entropy_with_logits

  • tf.ones

  • tf.zeros_initializer

  • tf.contrib.layers.xavier_initializer(seed = 1)

  • tf.nn.relu

  • tf.add

  • tf.matmul

  • tf.transpose

Recipes

Installing

import tensorflow as tf

Set different types of values

Initialize variables

Clean memory from device

Computing cost for sigmoid

One-hot encoding

Given a cost function

Convolutions

Input

  • M: 2d-Tensor with real values (where we will apply the convolution)

  • K: Smaller 2d-tensor with real values (the convolution kernel)

Process

  1. Multiply each entry of the 2d-tensor K with each one on the 2d-tensor M

I think they could be affected by the resolution of the images

conv2d

Use: Computing convolutions of W kernel over X with a stride over 1st and 4th dimension(batch dimension ie: one example, and 1 channel)

max_pool

Use: Given an input A it performs a pooling layer with a windown of size (f,f), ie usually it takes one example and one channel at a time.

flatten

Use: Given an input tensor P it takes each example from batch and generate an 1D array as output For example, receiving a tensor of shape [batch_size, width, height, channels] it would return a tensor of shape = [batch_size, width x height x channels]

fully_connected

Use: Given an input tensor F (flattened) it generates an initialized layer of weights in the graph, so they don't need to be initialized. This layers needs to have an additional argument activation_fn=None to not apply softmax

Cost computation

"Logits" are the result of multiplying the weights and adding the biases. Logits are passed through an activation function (such as a relu), and the result is called the "activation."

Example of functional code for a tf project is at docs/career/convnet_course

Images

Read functions

ResNet50 preprocessing

Utils

Display on notebook

Transfer learning example

Feeding data into models with ImageGenerator

TPU usage

Works for colab

When training:

TFX

TFDV

References

  • https://www.tensorflow.org/tfx/data_validation/get_started

  • https://blog.tensorflow.org/2018/09/introducing-tensorflow-data-validation.html

  • https://colab.research.google.com/github/tensorflow/tfx/blob/master/docs/tutorials/data_validation/tfdv_basic.ipynb#scrollTo=mPt5BHTwy_0F

  • https://www.tensorflow.org/tfx/data_validation/api_docs/python/tfdv

Import

Generate statistics

Visualize statistics

Infer data schema

Comparing stats from training/test

Calculate and display anomalies

Fix anomalies in the schema

More flexible extension of schema

Manual set of range for int values

Data environments

Data drift and skew

Freeze schema

Data slicing

Generate tensorflow data directory

The snippet above is meant to be loaded with keras as follows:

Feature preprocessing

Metadata definition

Sample preprocessing function

Generate a constant graph with the required transformations

Run tf pipeline

read csv file

_data_root can be csv, tf.Record and BigQuery.

Inspect generated artifact

It will keep each run associated with an ID for that execution for debugging

Read and print tf.Record files

Sample usage

Generate statistics for a given dataset

Show the statistics

Infer schema for a given dataset

Show schema

Detect anomalies for a given dataset

Show anomalies (if any)

Apply transformations to a given dataset

Transformations need to be passed as modules to tfx a common pattern is to have a constant file as follows

Then, having the following sample processing function in a file

_census_transform_module_file = 'census_transform.py'

we will pass it to the transform function as follows:

This execution will produce (in .component.outputs):

  • transform_graph is the graph that can perform the preprocessing operations. This graph will be included during training and serving to ensure consistent transformations of incoming data.

  • transformed_examples points to the preprocessed training and evaluation data.

  • updated_analyzer_cache are stored calculations from previous runs.

transform_graph for example would have (in transform.outputs['transform_graph'].get()[0].uri):

  • The metadata subdirectory contains the schema of the original data.

  • The transformed_metadata subdirectory contains the schema of the preprocessed data.

  • The transform_fn subdirectory contains the actual preprocessing graph.

A sample of transformed data can be retrieved with

References

  • https://www.tensorflow.org/api_docs/python/tf/

  • https://huggingface.co/docs/datasets/v1.11.0/splits.html

Last updated

Was this helpful?