<p>As <a href="https://stackoverflow.com/users/3001761/jonrsharpe">jonrsharpe</a> mentioned, that's not really stackoverflow's MO, but in practice, many people do choose to write code to help explain answers (because it's often easier).
So I'm going to assume that this was just miscommunication, and you really intended to ask one of the following two questions:</p>

<ol>
<li>How does one grab the values of the last layer of Alexnet in TensorFlow?</li>
<li>How does feature extraction from the last layer of a deep convolutional network like alexnet work?</li>
</ol>

<p>The answer to the first question is actually very easy. I'll use the <code>cifar10</code> example code in TensorFlow (which is loosely based on AlexNet) as an example. The forward pass of the network is built in the <code>inference</code> function, which returns a variable representing the output of the softmax layer. To actually get predicted image labels, you just argmax the logits, like this: (I've left out some of the setup code, but if you're already running alexnet, you already have that working)</p>

<pre><code>logits = cifar10.inference(images)
predictions = tf.argmax(logits,1)
# Actually run the computation
labels = session.run([predictions])
</code></pre>

<p>So grabbing just the last layer features is literally just as easy as asking for them. The only wrinkle is that, in this case, cifar10 doesn't natively expose them, so you need to modify the cifar10.inference function to return both:</p>

<pre><code># old code in cifar10.inference:
# return softmax_linear
# new code in cifar10.inference:
return softmax_linear, local4
</code></pre>

<p>And then modify all the calls to cifar10.inference, like the one we just showed:</p>

<pre><code>logits,local4 = cifar10.inference(images)
predictions = tf.argmax(logits,1)
# Actually run the computation, this time asking for both answers
labels,last_layer = session.run([predictions, local4])
</code></pre>

<p>And that's it. <code>last_layer</code> contains the last layer for all of the inputs you gave the model.</p>

<p>As for the second question, that's a much deeper question, but I'm guessing that's why you want to work on it. I'd suggest starting by reading up on some of the papers published in this area. I'm not an expert here, but I do like Bolei Zhou's work. For instance, try looking at Figure 2 in <a href="http://arxiv.org/abs/1512.04150" rel="nofollow noreferrer">"Learning Deep Features for Discriminative Localization"</a>. It's a localization paper, but it's using very similar techniques (and several of Bolei's papers use it).</p>
Alex-Net for feature extraction
<p>I try to get reliable features for ImageNet to do further classification on them. To achieve that I would like to use tensorflow with Alexnet, for feature extraction. That means I would like to get the values from the last layer in the CNN. Could someone write a piece of Python code that explains how that works?</p>