In [15]:
import tensorflow as tf

In [16]:
sess = tf.InteractiveSession()


Exception AssertionError: AssertionError("Nesting violated for default stack of <type 'weakref'> objects",) in <bound method InteractiveSession.__del__ of <tensorflow.python.client.session.InteractiveSession object at 0x7fa05e709b10>> ignored

In [23]:
x = tf.constant(2, dtype= tf.float32)
y = tf.placeholder(dtype= tf.float32, shape= [1])
inp = tf.placeholder(dtype= tf.bool, shape= [1])
#r = tf.cond(tf.less(x, y), f1, f2)
r = tf.cond(inp[0], lambda: x, lambda: y)
res = sess.run([r], feed_dict= {inp: [True]})
print (res)

# r is set to f1().
# Operations in f2 (e.g., tf.add) are not executed.


---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-23-b33add7d3f31> in <module>()
      4 #r = tf.cond(tf.less(x, y), f1, f2)
      5 r = tf.cond(inp[0], lambda: x, lambda: y)
----> 6 res = sess.run([r], feed_dict= {inp: [True]})
      7 print (res)
      8 

/home/frederik/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in run(self, fetches, feed_dict, options, run_metadata)
    370     try:
    371       result = self._run(None, fetches, feed_dict, options_ptr,
--> 372                          run_metadata_ptr)
    373       if run_metadata:
    374         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

/home/frederik/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in _run(self, handle, fetches, feed_dict, options, run_metadata)
    634     try:
    635       results = self._do_run(handle, target_list, unique_fetches,
--> 636                              feed_dict_string, options, run_metadata)
    637     finally:
    638       # The movers are no longer used. Delete them.

/home/frederik/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
    706     if handle is None:
    707       return self._do_call(_run_fn, self._session, feed_dict, fetch_list,
--> 708                            target_list, options, run_metadata)
    709     else:
    710       return self._do_call(_prun_fn, self._session, handle, feed_dict,

/home/frederik/anaconda2/lib/python2.7/site-packages/tensorflow/python/client/session.pyc in _do_call(self, fn, *args)
    726         except KeyError:
    727           pass
--> 728       raise type(e)(node_def, op, message)
    729 
    730   def _extend_graph(self):

InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder_23' with dtype float and shape [1]
	 [[Node: Placeholder_23 = Placeholder[dtype=DT_FLOAT, shape=[1], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
Caused by op u'Placeholder_23', defined at:
  File "/home/frederik/anaconda2/lib/python2.7/runpy.py", line 162, in _run_module_as_main
    "__main__", fname, loader, pkg_name)
  File "/home/frederik/anaconda2/lib/python2.7/runpy.py", line 72, in _run_code
    exec code in run_globals
  File "/home/frederik/anaconda2/lib/python2.7/site-packages/ipykernel/__main__.py", line 3, in <module>
    app.launch_new_instance()
  File "/home/frederik/anaconda2/lib/python2.7/site-packages/traitlets/config/application.py", line 596, in launch_instance
    app.start()
  File "/home/frederik/anaconda2/lib/python2.7/site-packages/ipykernel/kernelapp.py", line 442, in start
    ioloop.IOLoop.instance().start()
  File "/home/frederik/anaconda2/lib/python2.7/site-packages/zmq/eventloop/ioloop.py", line 162, in start
    super(ZMQIOLoop, self).start()
  File "/home/frederik/anaconda2/lib/python2.7/site-packages/tornado/ioloop.py", line 883, in start
    handler_func(fd_obj, events)
  File "/home/frederik/anaconda2/lib/python2.7/site-packages/tornado/stack_context.py", line 275, in null_wrapper
    return fn(*args, **kwargs)
  File "/home/frederik/anaconda2/lib/python2.7/site-packages/zmq/eventloop/zmqstream.py", line 440, in _handle_events
    self._handle_recv()
  File "/home/frederik/anaconda2/lib/python2.7/site-packages/zmq/eventloop/zmqstream.py", line 472, in _handle_recv
    self._run_callback(callback, msg)
  File "/home/frederik/anaconda2/lib/python2.7/site-packages/zmq/eventloop/zmqstream.py", line 414, in _run_callback
    callback(*args, **kwargs)
  File "/home/frederik/anaconda2/lib/python2.7/site-packages/tornado/stack_context.py", line 275, in null_wrapper
    return fn(*args, **kwargs)
  File "/home/frederik/anaconda2/lib/python2.7/site-packages/ipykernel/kernelbase.py", line 276, in dispatcher
    return self.dispatch_shell(stream, msg)
  File "/home/frederik/anaconda2/lib/python2.7/site-packages/ipykernel/kernelbase.py", line 228, in dispatch_shell
    handler(stream, idents, msg)
  File "/home/frederik/anaconda2/lib/python2.7/site-packages/ipykernel/kernelbase.py", line 391, in execute_request
    user_expressions, allow_stdin)
  File "/home/frederik/anaconda2/lib/python2.7/site-packages/ipykernel/ipkernel.py", line 199, in do_execute
    shell.run_cell(code, store_history=store_history, silent=silent)
  File "/home/frederik/anaconda2/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2723, in run_cell
    interactivity=interactivity, compiler=compiler, result=result)
  File "/home/frederik/anaconda2/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2825, in run_ast_nodes
    if self.run_code(code, result):
  File "/home/frederik/anaconda2/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2885, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-23-b33add7d3f31>", line 2, in <module>
    y = tf.placeholder(dtype= tf.float32, shape= [1])
  File "/home/frederik/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/array_ops.py", line 895, in placeholder
    name=name)
  File "/home/frederik/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/gen_array_ops.py", line 1238, in _placeholder
    name=name)
  File "/home/frederik/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/op_def_library.py", line 704, in apply_op
    op_def=op_def)
  File "/home/frederik/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2260, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/home/frederik/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1230, in __init__
    self._traceback = _extract_stack()

In [22]:
x = tf.constant(1, shape=(2,1), dtype= tf.float32 )
x_ = x*2
print(tf.shape(x_).eval())
print(tf.greater(x,x_).eval())
print
print(x.eval())
print(x_.eval())
a =tf.cond(tf.greater(x,x_),lambda: x, lambda: x_)
a.eval()


[2 1]
[[False]
 [False]]

[[ 1.]
 [ 1.]]
[[ 2.]
 [ 2.]]
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-22-e49e819a1adc> in <module>()
      6 print(x.eval())
      7 print(x_.eval())
----> 8 a =tf.cond(tf.greater(x,x_),lambda: x, lambda: x_)
      9 a.eval()

/home/frederik/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/control_flow_ops.pyc in cond(pred, fn1, fn2, name)
   1314     if isinstance(pred, bool):
   1315       raise TypeError("pred must not be a Python bool")
-> 1316     p_2, p_1 = switch(pred, pred)
   1317     pivot_1 = array_ops.identity(p_1, name="switch_t")
   1318     pivot_2 = array_ops.identity(p_2, name="switch_f")

/home/frederik/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/control_flow_ops.pyc in switch(data, pred, dtype, name)
    258     pred = ops.convert_to_tensor(pred, name="pred")
    259     if isinstance(data, ops.Tensor):
--> 260       return gen_control_flow_ops._switch(data, pred, name=name)
    261     else:
    262       if not isinstance(data, (ops.IndexedSlices, ops.SparseTensor)):

/home/frederik/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/gen_control_flow_ops.pyc in _switch(data, pred, name)
    368     output_true: A `Tensor`. Has the same type as `data`. If `pred` is true, data will be forwarded to this output.
    369   """
--> 370   result = _op_def_lib.apply_op("Switch", data=data, pred=pred, name=name)
    371   return _SwitchOutput._make(result)
    372 

/home/frederik/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/op_def_library.pyc in apply_op(self, op_type_name, name, **keywords)
    702           op = g.create_op(op_type_name, inputs, output_types, name=scope,
    703                            input_types=input_types, attrs=attr_protos,
--> 704                            op_def=op_def)
    705           outputs = op.outputs
    706           return _Restructure(ops.convert_n_to_tensor(outputs),

/home/frederik/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc in create_op(self, op_type, inputs, dtypes, input_types, name, attrs, op_def, compute_shapes, compute_device)
   2260                     original_op=self._default_original_op, op_def=op_def)
   2261     if compute_shapes:
-> 2262       set_shapes_for_outputs(ret)
   2263     self._add_op(ret)
   2264     self._record_op_seen_by_control_dependencies(ret)

/home/frederik/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc in set_shapes_for_outputs(op)
   1700       raise RuntimeError("No shape function registered for standard op: %s"
   1701                          % op.type)
-> 1702   shapes = shape_func(op)
   1703   if shapes is None:
   1704     raise RuntimeError(

/home/frederik/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/control_flow_ops.pyc in _SwitchShape(op)
   2345 def _SwitchShape(op):
   2346   input_shape = op.inputs[0].get_shape()
-> 2347   unused_pred_shape = op.inputs[1].get_shape().merge_with(tensor_shape.scalar())
   2348   return [input_shape] * 2

/home/frederik/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/tensor_shape.pyc in merge_with(self, other)
    568       except ValueError:
    569         raise ValueError("Shapes %s and %s are not compatible" %
--> 570                          (self, other))
    571 
    572   def concatenate(self, other):

ValueError: Shapes (2, 1) and () are not compatible

In [3]:
x = tf.placeholder(tf.float32, [None, 2], name='x-input')

In [ ]:


In [4]:
import numpy as np
x_feed = np.ones((10,1))
x_feed = np.concatenate((x_feed, x_feed*2),axis = 1)
x_feed


Out[4]:
array([[ 1.,  2.],
       [ 1.,  2.],
       [ 1.,  2.],
       [ 1.,  2.],
       [ 1.,  2.],
       [ 1.,  2.],
       [ 1.,  2.],
       [ 1.,  2.],
       [ 1.,  2.],
       [ 1.,  2.]])

In [5]:
x_ = x*2
x_.eval(feed_dict= {x: x_feed})


Out[5]:
array([[ 2.,  4.],
       [ 2.,  4.],
       [ 2.,  4.],
       [ 2.,  4.],
       [ 2.,  4.],
       [ 2.,  4.],
       [ 2.,  4.],
       [ 2.,  4.],
       [ 2.,  4.],
       [ 2.,  4.]], dtype=float32)

In [6]:
x__ = tf.slice(input_= x_, begin= [0,0], size= [-1,1])
print(x__.eval(feed_dict= {x: x_feed}))


[[ 2.]
 [ 2.]
 [ 2.]
 [ 2.]
 [ 2.]
 [ 2.]
 [ 2.]
 [ 2.]
 [ 2.]
 [ 2.]]

In [7]:
x__shape = tf.shape_n(input=[x__])
x__shape[0].eval(feed_dict={x: x_feed})


Out[7]:
array([10,  1], dtype=int32)

In [8]:
a = tf.constant([1 ,2])

In [9]:
c = tf.constant([1,10], shape=[1, 2], dtype=tf.float32)

In [10]:
c.eval()


Out[10]:
array([[  1.,  10.]], dtype=float32)

In [11]:
c_ = tf.tile(c,[tf.shape(x_)[0],1])

In [12]:
tf.mul(x_,c_).eval(feed_dict={x: x_feed})


Out[12]:
array([[  2.,  40.],
       [  2.,  40.],
       [  2.,  40.],
       [  2.,  40.],
       [  2.,  40.],
       [  2.,  40.],
       [  2.,  40.],
       [  2.,  40.],
       [  2.,  40.],
       [  2.,  40.]], dtype=float32)

In [27]:
x = tf.truncated_normal([10,2], mean=0, stddev=1.5, dtype=tf.float32)
print(x.eval())


[[-0.158813    1.77133346]
 [ 0.87237686 -1.09295547]
 [-0.10041773  1.09056079]
 [-2.79314137  2.22229767]
 [ 0.29225031 -1.41230857]
 [ 0.66840565  1.30554199]
 [-0.09482305 -0.25442618]
 [-1.83611119  1.96604156]
 [-1.39865112  0.42568493]
 [ 0.20962244 -0.18830238]]

In [28]:
mean, var = tf.nn.moments(x, axes= [0])
print(mean.eval())
print(var.eval())


[ 0.58605134 -0.03287976]
[ 0.8871752   1.40605807]

In [40]:
offset = tf.constant([0], shape= [10,2], dtype= tf.float32 )
scale = tf.constant([1], shape= [10,2], dtype= tf.float32 )

mean = tf.constant([0,0], shape= [1,2], dtype= tf.float32 )
print(mean.eval())
var = tf.constant([1,1], shape= [1,2], dtype= tf.float32 )
print(var.eval())
variance_epsilon = tf.constant([1e-6], shape= [1], dtype= tf.float32 )

bnx = tf.nn.batch_normalization(x, mean, var, None, None, variance_epsilon, name=None)


[[ 0.  0.]]
[[ 1.  1.]]

In [41]:
bnx_res, x_res = sess.run([bnx, x])
print(bnx_res)
print(x_res)


[[-0.62233335 -1.60913754]
 [ 1.08371496 -1.54664779]
 [ 0.71530092 -0.90577787]
 [-1.11423528  0.28348175]
 [ 1.192662    2.76610923]
 [-0.79604554 -0.81836849]
 [ 1.44703376 -0.6092152 ]
 [-1.30276906  1.6902281 ]
 [ 1.76405537  1.76658356]
 [ 1.52547836 -0.65387738]]
[[-0.62233365 -1.60913825]
 [ 1.08371544 -1.5466485 ]
 [ 0.71530128 -0.90577829]
 [-1.11423576  0.2834819 ]
 [ 1.1926626   2.76611066]
 [-0.7960459  -0.81836891]
 [ 1.44703448 -0.6092155 ]
 [-1.30276966  1.69022894]
 [ 1.76405621  1.7665844 ]
 [ 1.52547908 -0.65387768]]

In [4]:
x = tf.constant([1], shape= [1], dtype= tf.float32 )
x = x*2
y = tf.Variable(x)
y = x*2
x.eval()


Out[4]:
array([ 2.], dtype=float32)

In [25]:
x = tf.constant([1], shape= [1], dtype= tf.float32 )
x_ = x*2
print(tf.greater(x,x_).eval())
print(x.eval())
print(x_.eval())
a =tf.cond(tf.greater(x,x_),lambda : x, lambda : x_)
a.eval()


[False]
[ 1.]
[ 2.]
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-25-af18265bccd8> in <module>()
      4 print(x.eval())
      5 print(x_.eval())
----> 6 a =tf.cond(tf.greater(x,x_),lambda : x, lambda : x_)
      7 a.eval()
      8 

/home/frederik/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/control_flow_ops.pyc in cond(pred, fn1, fn2, name)
   1314     if isinstance(pred, bool):
   1315       raise TypeError("pred must not be a Python bool")
-> 1316     p_2, p_1 = switch(pred, pred)
   1317     pivot_1 = array_ops.identity(p_1, name="switch_t")
   1318     pivot_2 = array_ops.identity(p_2, name="switch_f")

/home/frederik/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/control_flow_ops.pyc in switch(data, pred, dtype, name)
    258     pred = ops.convert_to_tensor(pred, name="pred")
    259     if isinstance(data, ops.Tensor):
--> 260       return gen_control_flow_ops._switch(data, pred, name=name)
    261     else:
    262       if not isinstance(data, (ops.IndexedSlices, ops.SparseTensor)):

/home/frederik/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/gen_control_flow_ops.pyc in _switch(data, pred, name)
    368     output_true: A `Tensor`. Has the same type as `data`. If `pred` is true, data will be forwarded to this output.
    369   """
--> 370   result = _op_def_lib.apply_op("Switch", data=data, pred=pred, name=name)
    371   return _SwitchOutput._make(result)
    372 

/home/frederik/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/op_def_library.pyc in apply_op(self, op_type_name, name, **keywords)
    702           op = g.create_op(op_type_name, inputs, output_types, name=scope,
    703                            input_types=input_types, attrs=attr_protos,
--> 704                            op_def=op_def)
    705           outputs = op.outputs
    706           return _Restructure(ops.convert_n_to_tensor(outputs),

/home/frederik/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc in create_op(self, op_type, inputs, dtypes, input_types, name, attrs, op_def, compute_shapes, compute_device)
   2260                     original_op=self._default_original_op, op_def=op_def)
   2261     if compute_shapes:
-> 2262       set_shapes_for_outputs(ret)
   2263     self._add_op(ret)
   2264     self._record_op_seen_by_control_dependencies(ret)

/home/frederik/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc in set_shapes_for_outputs(op)
   1700       raise RuntimeError("No shape function registered for standard op: %s"
   1701                          % op.type)
-> 1702   shapes = shape_func(op)
   1703   if shapes is None:
   1704     raise RuntimeError(

/home/frederik/anaconda2/lib/python2.7/site-packages/tensorflow/python/ops/control_flow_ops.pyc in _SwitchShape(op)
   2345 def _SwitchShape(op):
   2346   input_shape = op.inputs[0].get_shape()
-> 2347   unused_pred_shape = op.inputs[1].get_shape().merge_with(tensor_shape.scalar())
   2348   return [input_shape] * 2

/home/frederik/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/tensor_shape.pyc in merge_with(self, other)
    568       except ValueError:
    569         raise ValueError("Shapes %s and %s are not compatible" %
--> 570                          (self, other))
    571 
    572   def concatenate(self, other):

ValueError: Shapes (1,) and () are not compatible

In [49]:
a = tf.constant(1)

In [64]:
b = tf.Variable(tf.constant(1))
tf.initialize_all_variables().run()
print(b.eval())

c= tf.Variable.assign_add(b, use_locking=False)


1
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-64-e350ad29ddc4> in <module>()
      3 print(b.eval())
      4 
----> 5 c= tf.Variable.assign_add(b, use_locking=False)
      6 

TypeError: assign_add() takes at least 2 arguments (2 given)