In [1]:
import tensorflow as tf

In [2]:
interactive_session=tf.InteractiveSession()

In [3]:
def get_weight(shape,lamb):
    var=tf.Variable(tf.random_normal(shape),dtype=tf.float32)
    tf.add_to_collection('losses',tf.contrib.layers.l2_regularizer(lamb)(var))
    return var

In [4]:
x=tf.placeholder(tf.float32,shape=(None,2))

In [5]:
y_=tf.placeholder(tf.float32,shape=(None,1))

In [6]:
batch_size=8

In [7]:
layer_dimension=[2,10,10,10,1]

In [8]:
n_layers=len(layer_dimension)

In [9]:
in_dimension=layer_dimension[0]

In [10]:
cur_layer=x

In [11]:
for i in range(1,n_layers):
    print("Current layer:%d"%i)
    out_dimension=layer_dimension[i]
    weight=get_weight([in_dimension,out_dimension],0.001)
    bias=tf.Variable(tf.constant(0.1,shape=[out_dimension]))
    cur_layer=tf.nn.relu(tf.matmul(cur_layer,weight) + bias)
    in_dimension=layer_dimension[i]


Current layer:1
Current layer:2
Current layer:3
Current layer:4

In [12]:
mse_loss=tf.reduce_mean(tf.square(y_-cur_layer))

In [13]:
tf.add_to_collection('losses',mse_loss)

In [14]:
loss=tf.add_n(tf.get_collection('losses'))

In [15]:
global_steps=tf.Variable(0)

In [16]:
learning_rate=tf.train.exponential_decay(0.1,global_steps,100,0.96,staircase=False)

In [17]:
learning_step=tf.train.GradientDescentOptimizer(learning_rate).minimize(loss,global_step=global_steps)

generate random x and y_


In [18]:
from numpy.random import RandomState

In [19]:
rdm=RandomState(1)

In [20]:
dataset_size=128

In [21]:
X=rdm.rand(dataset_size,2)

In [22]:
Y=[[x1 + x2 + rdm.rand()/10.0-0.05] for (x1,x2) in X]

In [23]:
init_op=tf.initialize_all_variables()

In [24]:
interactive_session.run(init_op)

In [25]:
for i in range(1000):
    start=(i*batch_size)%dataset_size
    end=min(start+batch_size,dataset_size)
    interactive_session.run(learning_step,feed_dict={x:X[start:end],y_:Y[start:end]})
    print(interactive_session.run(loss))


---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
/home/jun_gentoo/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
    729     try:
--> 730       return fn(*args)
    731     except errors.OpError as e:

/home/jun_gentoo/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py in _run_fn(session, feed_dict, fetch_list, target_list, options, run_metadata)
    711                                  feed_dict, fetch_list, target_list,
--> 712                                  status, run_metadata)
    713 

/home/jun_gentoo/anaconda3/lib/python3.5/contextlib.py in __exit__(self, type, value, traceback)
     65             try:
---> 66                 next(self.gen)
     67             except StopIteration:

/home/jun_gentoo/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/errors.py in raise_exception_on_not_ok_status()
    449           compat.as_text(pywrap_tensorflow.TF_Message(status)),
--> 450           pywrap_tensorflow.TF_GetCode(status))
    451   finally:

InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype float
	 [[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

During handling of the above exception, another exception occurred:

InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-25-9e3ad9f1744a> in <module>()
      3     end=min(start+batch_size,dataset_size)
      4     interactive_session.run(learning_step,feed_dict={x:X[start:end],y_:Y[start:end]})
----> 5     print(interactive_session.run(loss))

/home/jun_gentoo/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
    380     try:
    381       result = self._run(None, fetches, feed_dict, options_ptr,
--> 382                          run_metadata_ptr)
    383       if run_metadata:
    384         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

/home/jun_gentoo/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
    653     movers = self._update_with_movers(feed_dict_string, feed_map)
    654     results = self._do_run(handle, target_list, unique_fetches,
--> 655                            feed_dict_string, options, run_metadata)
    656 
    657     # User may have fetched the same tensor multiple times, but we

/home/jun_gentoo/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
    721     if handle is None:
    722       return self._do_call(_run_fn, self._session, feed_dict, fetch_list,
--> 723                            target_list, options, run_metadata)
    724     else:
    725       return self._do_call(_prun_fn, self._session, handle, feed_dict,

/home/jun_gentoo/anaconda3/lib/python3.5/site-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
    741         except KeyError:
    742           pass
--> 743       raise type(e)(node_def, op, message)
    744 
    745   def _extend_graph(self):

InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype float
	 [[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
Caused by op 'Placeholder', defined at:
  File "/home/jun_gentoo/anaconda3/lib/python3.5/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/home/jun_gentoo/anaconda3/lib/python3.5/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/jun_gentoo/anaconda3/lib/python3.5/site-packages/ipykernel/__main__.py", line 3, in <module>
    app.launch_new_instance()
  File "/home/jun_gentoo/anaconda3/lib/python3.5/site-packages/traitlets/config/application.py", line 653, in launch_instance
    app.start()
  File "/home/jun_gentoo/anaconda3/lib/python3.5/site-packages/ipykernel/kernelapp.py", line 474, in start
    ioloop.IOLoop.instance().start()
  File "/home/jun_gentoo/anaconda3/lib/python3.5/site-packages/zmq/eventloop/ioloop.py", line 162, in start
    super(ZMQIOLoop, self).start()
  File "/home/jun_gentoo/anaconda3/lib/python3.5/site-packages/tornado/ioloop.py", line 887, in start
    handler_func(fd_obj, events)
  File "/home/jun_gentoo/anaconda3/lib/python3.5/site-packages/tornado/stack_context.py", line 275, in null_wrapper
    return fn(*args, **kwargs)
  File "/home/jun_gentoo/anaconda3/lib/python3.5/site-packages/zmq/eventloop/zmqstream.py", line 440, in _handle_events
    self._handle_recv()
  File "/home/jun_gentoo/anaconda3/lib/python3.5/site-packages/zmq/eventloop/zmqstream.py", line 472, in _handle_recv
    self._run_callback(callback, msg)
  File "/home/jun_gentoo/anaconda3/lib/python3.5/site-packages/zmq/eventloop/zmqstream.py", line 414, in _run_callback
    callback(*args, **kwargs)
  File "/home/jun_gentoo/anaconda3/lib/python3.5/site-packages/tornado/stack_context.py", line 275, in null_wrapper
    return fn(*args, **kwargs)
  File "/home/jun_gentoo/anaconda3/lib/python3.5/site-packages/ipykernel/kernelbase.py", line 276, in dispatcher
    return self.dispatch_shell(stream, msg)
  File "/home/jun_gentoo/anaconda3/lib/python3.5/site-packages/ipykernel/kernelbase.py", line 228, in dispatch_shell
    handler(stream, idents, msg)
  File "/home/jun_gentoo/anaconda3/lib/python3.5/site-packages/ipykernel/kernelbase.py", line 390, in execute_request
    user_expressions, allow_stdin)
  File "/home/jun_gentoo/anaconda3/lib/python3.5/site-packages/ipykernel/ipkernel.py", line 196, in do_execute
    res = shell.run_cell(code, store_history=store_history, silent=silent)
  File "/home/jun_gentoo/anaconda3/lib/python3.5/site-packages/ipykernel/zmqshell.py", line 501, in run_cell
    return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  File "/home/jun_gentoo/anaconda3/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2717, in run_cell
    interactivity=interactivity, compiler=compiler, result=result)
  File "/home/jun_gentoo/anaconda3/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2821, in run_ast_nodes
    if self.run_code(code, result):
  File "/home/jun_gentoo/anaconda3/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2881, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-4-cfd37550b740>", line 1, in <module>
    x=tf.placeholder(tf.float32,shape=(None,2))
  File "/home/jun_gentoo/anaconda3/lib/python3.5/site-packages/tensorflow/python/ops/array_ops.py", line 1274, in placeholder
    name=name)
  File "/home/jun_gentoo/anaconda3/lib/python3.5/site-packages/tensorflow/python/ops/gen_array_ops.py", line 1522, in _placeholder
    name=name)
  File "/home/jun_gentoo/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py", line 703, in apply_op
    op_def=op_def)
  File "/home/jun_gentoo/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 2310, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/home/jun_gentoo/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1232, in __init__
    self._traceback = _extract_stack()

In [ ]: