Coder Social home page Coder Social logo

tensorflow's Introduction

TensorFlow

This Repository contains all tensorflow tutorials . There might be some replicas from other repositories.

Working with Graphs

import tensorflow as tf  
slim = tf.contrib.slim 

# Define a basic graph:
def ciresan(X):
     with slim.arg_scope([slim.conv2d, slim.fully_connected], activation_fn=tf.nn.tanh, weights_initializer=tf.truncated_normal_initializer(stddev=0.01), weights_regularizer=slim.l2_regularizer(0.0005)):
    net = slim.conv2d(X, 32, [4, 4], scope="conv1", padding = "SAME")
    net = slim.max_pool2d(net,[2, 2], scope="pool1", padding = "SAME")
    net = slim.conv2d(X, 48, [5, 5], scope="conv2", padding = "SAME")
    net = slim.max_pool2d(net,[3, 3], scope="pool2", padding = "SAME")
    net = slim.fully_connected(net, 150, scope="fc3")
    net = slim.fully_connected(net, 5, scope="fc4", activation_fn = tf.nn.softmax)
return net

X = tf.placeholder(tf.float32, [None, 224, 224, 3])
net = ciresan(X)

How to save a graph?

import os
with tf.Session() as sess:
    tf.train.write_graph(sess.graph_def, os.getcwd(), "model.pb", as_text = False)

To see what variable names does the model contain

[v.name for v in slim.get_model_variables()]
out:
['conv1/weights:0',
'conv1/biases:0',
'conv2/weights:0',
'conv2/biases:0',
'fc3/weights:0',
'fc3/biases:0',
'fc4/weights:0',
'fc4/biases:0']

To just see the model variables

[<tensorflow.python.ops.variables.Variable at 0x1150f6f60>,
<tensorflow.python.ops.variables.Variable at 0x1150f6e80>,
<tensorflow.python.ops.variables.Variable at 0x1150f6a20>,
<tensorflow.python.ops.variables.Variable at 0x1150f6e48>,
<tensorflow.python.ops.variables.Variable at 0x1150f68d0>,
<tensorflow.python.ops.variables.Variable at 0x1150f6b38>,
<tensorflow.python.ops.variables.Variable at 0x1150f62e8>,
<tensorflow.python.ops.variables.Variable at 0x1151a2ef0>]

To see the operations in the graph (You will see many, so to cut short I have given here only the first string.

sess = tf.Session()
op = sess.graph.get_operations()
[m.values() for m in op][1]
out:
(<tf.Tensor 'conv1/weights:0' shape=(4, 4, 3, 32) dtype=float32_ref>,)

How to internally access a layer (an op ) . We will dicuss about the graph layer

from tensorflow.python.platform import gfile
with tf.Session() as sess:
with gfile.FastGFile("model.pb",'rb') as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())
    tf.import_graph_def(graph_def, name='')
    po3 = sess.graph.get_tensor_by_name("conv1/weights:0")
    print (po3)
    
out:
Tensor("conv1/weights:0", shape=(4, 4, 3, 32), dtype=float32_ref)

Instead of print(po3) . You can actually print weights by using

print (sess.run(po3, feed_dict={X:images}) # This is how transfer learning works 

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.