title: What's the equivalent of this Keras code in TensorFlow?
body: <p>The code is as below and runs perfectly:</p>
  
   
  
  <pre class="lang-python prettyprint-override"><code>import numpy as np
  from keras.models import Sequential
  from keras.layers import Dense, Dropout
  
  xData = np.array([[5, 3, 7], [1, 2, 6], [8, 7, 6]], dtype=np.float32)
  yTrainData = np.array([[1], [0], [1]], dtype=np.float32)
  
  model = Sequential()
  
  model.add(Dense(64, input_dim=3, activation='relu'))
  
  model.add(Dropout(0.2))
  
  model.add(Dense(1, activation='sigmoid'))
  
  model.compile(loss='binary_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
  
  model.fit(xData, yTrainData, epochs=10, batch_size=128, verbose=2)
  
  xTestData = np.array([[2, 8, 1], [3, 1, 9]], dtype=np.float32)
  
  resultAry = model.predict(xTestData)
  print("Cal result: %s" % resultAry)
  </code></pre>
  
  <p>I can't work out the code in TensowFlow, something I've written is like this:</p>
  
  <pre class="lang-python prettyprint-override"><code>import tensorflow as tf
  import numpy as np
  
  xData = np.array([[5, 3, 7], [1, 2, 6], [8, 7, 6]], dtype=np.float32)
  yTrainData = np.array([[1], [0], [1]], dtype=np.float32)
  
  x = tf.placeholder(tf.float32)
  yTrain = tf.placeholder(tf.float32)
  
  w = tf.Variable(tf.ones([64]), dtype=tf.float32)
  b = tf.Variable(tf.zeros([1]), dtype=tf.float32)
  
  y = tf.nn.relu(w * x + b)
  
  w1 = tf.Variable(tf.ones([3]), dtype=tf.float32)
  b1 = tf.Variable(0, dtype=tf.float32)
  
  y1 = tf.reduce_mean(tf.nn.sigmoid(w1 * y + b1))
  
  loss = tf.abs(y1 - tf.reduce_mean(yTrain))
  
  optimizer = tf.train.AdadeltaOptimizer(0.1)
  
  train = optimizer.minimize(loss)
  
  init = tf.global_variables_initializer()
  sess = tf.Session()
  
  sess.run(init)
  
  for i in range(10):
  
      for j in range(3):
          result = sess.run([loss, y1, yTrain, x, w, b, train], feed_dict={x: xData[j], yTrain: yTrainData[j]})
  
          if i % 10 == 0:
              print("i: %d, j: %d, loss: %10.10f, y1: %f, yTrain: %s, x: %s" % (i, j, float(result[0]), float(result[1]), yTrainData[j], xData[j]))
  
  result = sess.run([y1, loss], feed_dict={x: [1, 6, 0], yTrain: 0})
  print(result)
  </code></pre>
  
  <p>But I will got the following error while running,</p>
  
  <pre class="lang-python prettyprint-override"><code>Traceback (most recent call last):
    File "C:\Python36\lib\site-packages\tensorflow\python\client\session.py", line 1327, in _do_call
      return fn(*args)
    File "C:\Python36\lib\site-packages\tensorflow\python\client\session.py", line 1306, in _run_fn
      status, run_metadata)
    File "C:\Python36\lib\contextlib.py", line 88, in __exit__
      next(self.gen)
    File "C:\Python36\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 466, in raise_exception_on_not_ok_status
      pywrap_tensorflow.TF_GetCode(status))
  tensorflow.python.framework.errors_impl.InvalidArgumentError: Incompatible shapes: [64] vs. [3]
           [[Node: mul = Mul[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](Variable/read, _arg_Placeholder_0_0)]]
  
  During handling of the above exception, another exception occurred:
  
  Traceback (most recent call last):
    File "testidc.py", line 36, in &lt;module&gt;
      result = sess.run([loss, y1, yTrain, x, w, b, train], feed_dict={x: xData[j], yTrain: yTrainData[j]})
    File "C:\Python36\lib\site-packages\tensorflow\python\client\session.py", line 895, in run
      run_metadata_ptr)
    File "C:\Python36\lib\site-packages\tensorflow\python\client\session.py", line 1124, in _run
      feed_dict_tensor, options, run_metadata)
    File "C:\Python36\lib\site-packages\tensorflow\python\client\session.py", line 1321, in _do_run
      options, run_metadata)
    File "C:\Python36\lib\site-packages\tensorflow\python\client\session.py", line 1340, in _do_call
      raise type(e)(node_def, op, message)
  tensorflow.python.framework.errors_impl.InvalidArgumentError: Incompatible shapes: [64] vs. [3]
           [[Node: mul = Mul[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](Variable/read, _arg_Placeholder_0_0)]]
  
  Caused by op 'mul', defined at:
    File "testidc.py", line 15, in &lt;module&gt;
      y = tf.nn.relu(w * x + b)
    File "C:\Python36\lib\site-packages\tensorflow\python\ops\variables.py", line 705, in _run_op
      return getattr(ops.Tensor, operator)(a._AsTensor(), *args)
    File "C:\Python36\lib\site-packages\tensorflow\python\ops\math_ops.py", line 865, in binary_op_wrapper
      return func(x, y, name=name)
    File "C:\Python36\lib\site-packages\tensorflow\python\ops\math_ops.py", line 1088, in _mul_dispatch
      return gen_math_ops._mul(x, y, name=name)
    File "C:\Python36\lib\site-packages\tensorflow\python\ops\gen_math_ops.py", line 1449, in _mul
      result = _op_def_lib.apply_op("Mul", x=x, y=y, name=name)
    File "C:\Python36\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 767, in apply_op
      op_def=op_def)
    File "C:\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 2630, in create_op
      original_op=self._default_original_op, op_def=op_def)
    File "C:\Python36\lib\site-packages\tensorflow\python\framework\ops.py", line 1204, in __init__
      self._traceback = self._graph._extract_stack()  # pylint: disable=protected-access
  
  InvalidArgumentError (see above for traceback): Incompatible shapes: [64] vs. [3]
           [[Node: mul = Mul[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"](Variable/read, _arg_Placeholder_0_0)]]
  </code></pre>
  
  <p>The main reason is the shape of W, must be the same as x in TensowFlow, but in Keras, the hidden Dense layer could have more nodes than the input(such as 64 in the example). </p>
  
  <p>I need help for the equivalent TensorFlow code instead of the Keras one. Thanks.</p>