title: Keras switch function error
body: <p>Im working on creating a custom loss function for keras using tensorflow as the backend. To do this I wanted to try something similiar to what was shown here: <a href="https://stackoverflow.com/questions/38684768/customize-keras-loss-function-in-a-way-that-the-y-true-will-depend-on-y-pred?rq=1">Customize Keras&#39; loss function in a way that the y_true will depend on y_pred</a></p>
  
  <p>However, upon running the script (taken from Van):</p>
  
  <pre><code>import theano
  from keras import backend as K
  from keras.layers import Dense
  from keras.models import Sequential
  
  def customized_loss(y_true, y_pred):
      loss = K.switch(K.equal(y_true, -1), 0, K.square(y_true-y_pred))
      return K.sum(loss)
  
  if __name__ == '__main__':
      model = Sequential([ Dense(3, input_shape=(4,)) ])
      model.compile(loss=customized_loss, optimizer='sgd')
  
      import numpy as np
      x = np.random.random((1, 4))
      y = np.array([[1,-1,0]])
  
      output = model.predict(x)
      print output
      # [[ 0.47242549 -0.45106074  0.13912249]]
      print model.evaluate(x, y)  # keras's loss
      # 0.297689884901
      print (output[0, 0]-1)**2 + 0 +(output[0, 2]-0)**2 # double-check
      # 0.297689929093
  </code></pre>
  
  <p>I get the following error:
  AttributeError: 'int' object has no attribute 'get_shape'</p>
  
  <p>I tried then to use tf.where instead (as seen in the comment by Van), however I got this error: 
  ValueError: Shapes must be equal rank, but are 0 and 2 for 'loss/dense_1_loss/Select' (op: 'Select') with input shapes: [?,?], [], [?,3].</p>
  
  <p>Any help would be appreciated</p>
  
  <p>Edit:
  The full stack traces are:
  For the first case(K.switch):</p>
  
  <pre><code>Using TensorFlow backend.
  2018-02-21 14:47:22.907033: I C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\36\tensorflow\core\platform\cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
  Traceback (most recent call last):
    File "temp.py", line 19, in &lt;module&gt;
      model.compile(loss=customized_loss, optimizer='sgd')
    File "C:\Users\shai\Anaconda3\lib\site-packages\keras\models.py", line 806, in compile
      **kwargs)
    File "C:\Users\shai\Anaconda3\lib\site-packages\keras\engine\training.py", line 860, in compile
      sample_weight, mask)
    File "C:\Users\shai\Anaconda3\lib\site-packages\keras\engine\training.py", line 460, in weighted
      score_array = fn(y_true, y_pred)
    File "temp.py", line 10, in customized_loss
      loss = K.switch(K.equal(y_true, -1), 0, K.square(y_true-y_pred))
    File "C:\Users\shai\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py", line 2674, in switch
      expr_ndim = ndim(then_expression)
    File "C:\Users\shai\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py", line 590, in ndim
      dims = x.get_shape()._dims
  AttributeError: 'int' object has no attribute 'get_shape'
  </code></pre>
  
  <p>The second error (using tf.where instead of K.switch):</p>
  
  <pre><code>Using TensorFlow backend.
  2018-02-21 14:49:31.651045: I C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\36\tensorflow\core\platform\cpu_feature_guard.cc:137] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
  Traceback (most recent call last):
    File "C:\Users\shai\Anaconda3\lib\site-packages\tensorflow\python\framework\common_shapes.py", line 686, in _call_cpp_shape_fn_impl
      input_tensors_as_shapes, status)
    File "C:\Users\shai\Anaconda3\lib\site-packages\tensorflow\python\framework\errors_impl.py", line 473, in __exit__
      c_api.TF_GetCode(self.status.status))
  tensorflow.python.framework.errors_impl.InvalidArgumentError: Shapes must be equal rank, but are 0 and 2 for 'loss/dense_1_loss/Select' (op: 'Select') with input shapes: [?,?], [], [?,3].
  
  During handling of the above exception, another exception occurred:
  
  Traceback (most recent call last):
    File "temp.py", line 19, in &lt;module&gt;
      model.compile(loss=customized_loss, optimizer='sgd')
    File "C:\Users\shai\Anaconda3\lib\site-packages\keras\models.py", line 806, in compile
      **kwargs)
    File "C:\Users\shai\Anaconda3\lib\site-packages\keras\engine\training.py", line 860, in compile
      sample_weight, mask)
    File "C:\Users\shai\Anaconda3\lib\site-packages\keras\engine\training.py", line 460, in weighted
      score_array = fn(y_true, y_pred)
    File "temp.py", line 12, in customized_loss
      loss = tf.where(K.equal(y_true, -1), 0.0, K.square(y_true - y_pred))
    File "C:\Users\shai\Anaconda3\lib\site-packages\tensorflow\python\ops\array_ops.py", line 2441, in where
      return gen_math_ops._select(condition=condition, t=x, e=y, name=name)
    File "C:\Users\shai\Anaconda3\lib\site-packages\tensorflow\python\ops\gen_math_ops.py", line 3987, in _select
      "Select", condition=condition, t=t, e=e, name=name)
    File "C:\Users\shai\Anaconda3\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 787, in _apply_op_helper
      op_def=op_def)
    File "C:\Users\shai\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 2958, in create_op
      set_shapes_for_outputs(ret)
    File "C:\Users\shai\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 2209, in set_shapes_for_outputs
      shapes = shape_func(op)
    File "C:\Users\shai\Anaconda3\lib\site-packages\tensorflow\python\framework\ops.py", line 2159, in call_with_requiring
      return call_cpp_shape_fn(op, require_shape_fn=True)
    File "C:\Users\shai\Anaconda3\lib\site-packages\tensorflow\python\framework\common_shapes.py", line 627, in call_cpp_shape_fn
      require_shape_fn)
    File "C:\Users\shai\Anaconda3\lib\site-packages\tensorflow\python\framework\common_shapes.py", line 691, in _call_cpp_shape_fn_impl
      raise ValueError(err.message)
  ValueError: Shapes must be equal rank, but are 0 and 2 for 'loss/dense_1_loss/Select' (op: 'Select') with input shapes: [?,?], [], [?,3].
  </code></pre>