<commit_before>---
layout: article
title:  "TensorFlow"
excerpt: "Bring your Tensorflow graph model to Algorithmia."
categories: model-guides
tags: [algo-model-guide]
show_related: true
author: steph_kim
permalink: /model-deployment/tensorflow/
redirect_from:
  - /algorithm-development/model-guides/tensorflow/
image:
    teaser: /language_logos/tensorflow.svg
---


Welcome to deploying your pre-trained <a href="https://www.tensorflow.org/">Tensorflow</a> model on Algorithmia!

**Currently we support tensorflow-gpu up to version 1.3.0, future versions such as the latest 1.7.0 will not function properly due to gpu limitations.**

For tensorflow-gpu==1.3.0 support please add one of the following wheels to your dependencies file in replacement of `tensorflow-gpu==1.3.0`: [python2](https://s3.amazonaws.com/algorithmia-wheels/tensorflow-1.3.0-cp27-cp27mu-linux_x86_64.whl) / [python3](https://s3.amazonaws.com/algorithmia-wheels/tensorflow-1.3.0-cp35-cp35m-linux_x86_64.whl). We apologize for the inconvenience. If you run into any issues please <a onclick="Intercom('show')">let us know</a>.

## Table of Contents
* [Prerequisites](#prerequisites)
  * [Save your Pre-Trained Model](#save-your-pre-trained-model)
  * [Create a Data Collection](#create-a-data-collection)
  * [Host Your Model File](#host-your-model-file)
* [Create your Algorithm](#create-your-algorithm)
* [Set your Dependencies](#set-your-dependencies)
* [Load your Model](#load-your-model)
  * [Using the SavedModel Method](#using-the-savedmodel-method)
  * [GPU memory tricks](#gpu-memory-tricks)
* [Publish your Algorithm](#publish-your-algorithm)


## Prerequisites
Before you get started deploying your pre-trained model on Algorithmia, there are a few things you'll want to do first:

### Save your Pre-Trained Model
You'll want to do the training and saving of your model on your local machine, or the platform you're using for training, before you deploy it to production on the Algorithmia platform.

After training your Tensorflow model, you'll need to save it, along with its assets and variables.

There are a few ways to save models in different versions of Tensorflow, but below, we'll use the <a href="https://www.tensorflow.org/api_docs/python/tf/saved_model">SavedModel</a> method that works with multiple versions - from Tensorflow 1.2 to the current version.

```python
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
import shutil

# Pulls data from a folder in your local directory data/MNIST
mnist = input_data.read_data_sets("data/MNIST/", one_hot=True)


def preprocessing(save_directory):
    """
    Read data from mnist dataset.
    Every data point has two parts: image (xs) and label (ys).
    """
    x = tf.placeholder(tf.float32, [None, 784])

    # Weights and biases for model
    W = tf.Variable(tf.zeros([784, 10]))
    b = tf.Variable(tf.zeros([10]))

    # Implement model.
    y = tf.nn.softmax(tf.matmul(x, W) + b)

    # Add new placeholder for correct answers<commit_msg>added warning about using .saver method<commit_after>---
layout: article
title:  "TensorFlow"
excerpt: "Bring your Tensorflow graph model to Algorithmia."
categories: model-guides
tags: [algo-model-guide]
show_related: true
author: steph_kim
permalink: /model-deployment/tensorflow/
redirect_from:
  - /algorithm-development/model-guides/tensorflow/
image:
    teaser: /language_logos/tensorflow.svg
---


Welcome to deploying your pre-trained <a href="https://www.tensorflow.org/">Tensorflow</a> model on Algorithmia!

**Currently we support tensorflow-gpu up to version 1.3.0, future versions such as the latest 1.7.0 will not function properly due to gpu limitations.**

For tensorflow-gpu==1.3.0 support please add one of the following wheels to your dependencies file in replacement of `tensorflow-gpu==1.3.0`: [python2](https://s3.amazonaws.com/algorithmia-wheels/tensorflow-1.3.0-cp27-cp27mu-linux_x86_64.whl) / [python3](https://s3.amazonaws.com/algorithmia-wheels/tensorflow-1.3.0-cp35-cp35m-linux_x86_64.whl). We apologize for the inconvenience. If you run into any issues please <a onclick="Intercom('show')">let us know</a>.

## Table of Contents
* [Prerequisites](#prerequisites)
  * [Save your Pre-Trained Model](#save-your-pre-trained-model)
  * [Create a Data Collection](#create-a-data-collection)
  * [Host Your Model File](#host-your-model-file)
* [Create your Algorithm](#create-your-algorithm)
* [Set your Dependencies](#set-your-dependencies)
* [Load your Model](#load-your-model)
  * [Using the SavedModel Method](#using-the-savedmodel-method)
  * [GPU memory tricks](#gpu-memory-tricks)
* [Publish your Algorithm](#publish-your-algorithm)


## Prerequisites
Before you get started deploying your pre-trained model on Algorithmia, there are a few things you'll want to do first:

### Save your Pre-Trained Model
You'll want to do the training and saving of your model on your local machine, or the platform you're using for training, before you deploy it to production on the Algorithmia platform.

After training your Tensorflow model, you'll need to save it, along with its assets and variables.

There are a few ways to save models in different versions of Tensorflow, but below, we'll use the <a href="https://www.tensorflow.org/api_docs/python/tf/saved_model">SavedModel</a> method that works with multiple versions - from Tensorflow 1.2 to the current version.

Because of how Tensorflow doesn't save entire graph architecture when using saver.save & saver.restore (which require the same Tensorflow global context to be used), you'll need to use <a href="https://www.tensorflow.org/api_docs/python/tf/saved_model/Builder">tf.saved_model.Builder</a> to save your TF model.
{: .notice-warning}

```python
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
import shutil

# Pulls data from a folder in your local directory data/MNIST
mnist = input_data.read_data_sets("data/MNIST/", one_hot=True)


def preprocessing(save_directory):
    """
    Read data from mnist dataset.
    Every data point has two parts: image (xs) and label (ys).
    """
    x = tf.placeholder(tf.float32, [None, 784])

    # Weights and biases for model
    W = tf.Variable(tf.zeros([784, 10]))
    b = tf.Variable(tf.zeros([10]))

    # Implement model.
    y = tf.nn.softmax(tf.matmul(x, W) + b)

    # Add new placeholder for correct answers
{'fraction_non_alphanumeric': 0.11230865354576695, 'fraction_numerical': 0.016244923461418308, 'mean_word_length': 4.538927335640138, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 9, 'lorem ipsum': 0, 'www.': 5, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1993394', 'n_tokens_mistral': 2065, 'n_tokens_neox': 1940, 'n_words': 651}