Все материалы здесь:

https://habrahabr.ru/post/305578/


In [1]:
import tensorflow as tf

In [2]:
graph = tf.get_default_graph()
graph.get_operations()


Out[2]:
[]

In [3]:
input_value = tf.constant(1.0)  # run only once !!!
input_value


Out[3]:
<tf.Tensor 'Const:0' shape=() dtype=float32>

In [4]:
operations = graph.get_operations()
operations


Out[4]:
[<tf.Operation 'Const' type=Const>]

In [5]:
operations[0].node_def


Out[5]:
name: "Const"
op: "Const"
attr {
  key: "dtype"
  value {
    type: DT_FLOAT
  }
}
attr {
  key: "value"
  value {
    tensor {
      dtype: DT_FLOAT
      tensor_shape {
      }
      float_val: 1.0
    }
  }
}

In [6]:
sess = tf.Session()
sess.run(input_value)


Out[6]:
1.0

Simplest neuron


In [7]:
weight = tf.Variable(0.8)
weight


Out[7]:
<tf.Variable 'Variable:0' shape=() dtype=float32_ref>

In [8]:
for operation in graph.get_operations():
    print(operation.name)


Const
Variable/initial_value
Variable
Variable/Assign
Variable/read

In [9]:
output_value = weight * input_value

In [10]:
for operation in graph.get_operations():
    print(operation.name)


Const
Variable/initial_value
Variable
Variable/Assign
Variable/read
mul

In [11]:
init = tf.initialize_all_variables() 
sess.run(init)


WARNING:tensorflow:From /usr/local/lib/python2.7/dist-packages/tensorflow/python/util/tf_should_use.py:118: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02.
Instructions for updating:
Use `tf.global_variables_initializer` instead.

In [12]:
sess.run(output_value)


Out[12]:
0.8

TensorBoard


In [13]:
x = tf.constant(1.0, name="input")
w = tf.Variable(0.8, name="weight")
y = tf.multiply(w, x, name="output")

In [14]:
summary_writer = tf.summary.FileWriter ("log_simple_graph", sess.graph)

In [15]:
!tensorboard --logdir=log_simple_graph


W0213 01:00:22.710533 Reloader plugin_event_accumulator.py:300] Found more than one graph event per run, or there was a metagraph containing a graph_def, as well as one or more graph events.  Overwriting the graph with the newest event.
W0213 01:00:22.711550 Reloader plugin_event_accumulator.py:308] Found more than one metagraph event per run. Overwriting the metagraph with the newest event.
W0213 01:00:22.713455 Reloader plugin_event_accumulator.py:300] Found more than one graph event per run, or there was a metagraph containing a graph_def, as well as one or more graph events.  Overwriting the graph with the newest event.
TensorBoard 1.5.1 at http://shevkunov:6006 (Press CTRL+C to quit)
^C

Fitting neuron


In [16]:
y_ = tf.constant(0.0)
loss = (y - y_) ** 2

In [17]:
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.025)

In [18]:
gradients_and_vars = optimizer.compute_gradients(loss)

In [19]:
sess.run(tf.initialize_all_variables())
sess.run(gradients_and_vars[1][0])


Out[19]:
1.6

In [21]:
sess.run(optimizer.apply_gradients(gradients_and_vars))
sess.run(w)


Out[21]:
0.76

In [24]:
train_step = tf.train.GradientDescentOptimizer(learning_rate=0.025).minimize(loss)
for i in xrange(100):
    sess.run(train_step)
sess.run(y)


Out[24]:
1.5772292e-07

Summary


In [30]:
sess.run(tf.initialize_all_variables())
summary_y = tf.summary.scalar("output", y)
summary_writer = tf.summary.FileWriter("log_simple_stats")
for i in xrange(100):
    summary_str = sess.run(summary_y)
    summary_writer.add_summary(summary_str, i)
    sess.run(train_step)

In [ ]:
!tensorboard --logdir=log_simple_stats


TensorBoard 1.5.1 at http://shevkunov:6006 (Press CTRL+C to quit)
W0213 01:20:19.910441 Thread-1 application.py:273] path /[[_dataImageSrc]] not found, sending 404

In [ ]: