title: Type error: must be real number not Tensor
body: <p>my model to code a convolutional neural network is as follow:
  Below you will find the code when I use the input examples as a whole.
  Thereunder code for minibatches that is working. I do not see the difference!</p>
  
  <pre><code>    # model
  
      def model(train_x, train_y, test_x, test_y, learning_rate = 0.009,
                num_iterations = 100, print_cost = True):
          """
          Implements a three-layer ConvNet in Tensorflow:
          CONV2D -&gt; RELU -&gt; MAXPOOL -&gt; CONV2D -&gt; RELU -&gt; MAXPOOL -&gt; FLATTEN -&gt; FULLYCONNECTED
  
          Arguments:
          X_train -- training set, of shape (None, 64, 64, 3)
          Y_train -- test set, of shape (None, n_y = 6)
          X_test -- training set, of shape (None, 64, 64, 3)
          Y_test -- test set, of shape (None, n_y = 6)
          learning_rate -- learning rate of the optimization
          num_epochs -- number of epochs of the optimization loop
          minibatch_size -- size of a minibatch
          print_cost -- True to print the cost every 100 epochs
  
          Returns:
          train_accuracy -- real number, accuracy on the train set (X_train)
          test_accuracy -- real number, testing accuracy on the test set (X_test)
          parameters -- parameters learnt by the model. They can then be used to predict.
          """
  
          ops.reset_default_graph()                         # to be able to rerun the model without overwriting tf variables
          tf.set_random_seed(1)                             # to keep results consistent (tensorflow seed)
          seed = 3                                          # to keep results consistent (numpy seed)
          (m, n_H0, n_W0, n_C0) = train_x.shape             
          n_y = train_y.shape[1]                            
          costs = []                                        # To keep track of the cost
  
          # Create Placeholders of the correct shape
          ### START CODE HERE ### (1 line)
          X, Y = create_placeholders(n_H0, n_W0, n_C0, n_y)
          ### END CODE HERE ###
  
          # Initialize parameters
          ### START CODE HERE ### (1 line)
          parameters = initialize_parameters()
          ### END CODE HERE ###
          for i in range(0, num_iterations):
              # Forward propagation: Build the forward propagation in the tensorflow graph
              ### START CODE HERE ### (1 line)
              Z3 = forward_propagation(X, parameters)
              ### END CODE HERE ###
  
              # Cost function: Add cost function to tensorflow graph
              ### START CODE HERE ### (1 line)
              cost = compute_cost(Z3, Y)
              ### END CODE HERE ###
  
              # Backpropagation: Define the tensorflow optimizer. Use an AdamOptimizer that minimizes the cost.
              ### START CODE HERE ### (1 line)
              optimizer = tf.train.AdamOptimizer(learning_rate = learning_rate).minimize(cost)
              ### END CODE HERE ###
  
              # Initialize all the variables globally
              init = tf.global_variables_initializer()
  
              # Start the session to compute the tensorflow graph
              with tf.Session() as sess:
  
                  # Run the initialization
                  sess.run(init)
  
                  # Do the training loop
  
                  _ , temp_cost = sess.run([optimizer, cost], feed_dict = {X: train_x, Y: train_y})
  
                  ### END CODE HERE ###
  
              # Print the cost every 5 itterrations
              print("cost =" +str(cost))
              if print_cost == True and i % 5 == 0:
                  print ("Cost after num_iterations %i: %f" % (i, cost))
              if print_cost == True and i % 1 == 0:
                  costs.append(cost)
  
  
              # plot the cost
              plt.plot(np.squeeze(costs))
              plt.ylabel('cost')
              plt.xlabel('iterations (per tens)')
              plt.title("Learning rate =" + str(learning_rate))
              plt.show()
  
              # Calculate the correct predictions
              predict_op = tf.argmax(Z3, 1)
              correct_prediction = tf.equal(predict_op, tf.argmax(Y, 1))
  
              # Calculate accuracy on the test set
              accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
              print(accuracy)
              train_accuracy = accuracy.eval({X: train_x, Y: train_y})
              test_accuracy = accuracy.eval({X: test_x, Y: test_y})
              print("Train Accuracy:", train_accuracy)
              print("Test Accuracy:", test_accuracy)
  
              return train_accuracy, test_accuracy, parameters
  
  However when I call the function model via
  
      _, _, parameters = model(train_x, train_y, test_x, test_y)
  
  I run into following error:
  
      cost =Tensor("Mean:0", shape=(), dtype=float32)
  
      ---------------------------------------------------------------------------
      TypeError                                 Traceback (most recent call last)
      &lt;ipython-input-78-8bde5fad21ba&gt; in &lt;module&gt;()
      ----&gt; 1 _, _, parameters = model(train_x, train_y, test_x, test_y)
  
      &lt;ipython-input-77-68bdfb51274c&gt; in model(train_x, train_y, test_x, test_y, learning_rate, num_iterations, print_cost)
           73         print("cost =" +str(cost))
           74         if print_cost == True and i % 5 == 0:
      ---&gt; 75             print ("Cost after num_iterations %i: %f" % (i, cost))
           76         if print_cost == True and i % 1 == 0:
           77             costs.append(cost)
  
      TypeError: must be real number, not Tensor
  
  Can somebody explain me how to resolve this error? 
  </code></pre>
  
  <p><strong>My code with minibatches that is working:</strong>
  <strong>Added this code so you can see this is working. I don't see the difference with code above and why code above is not working. Maybe you can help me out. Tnx</strong></p>
  
  <pre><code># GRADED FUNCTION: model
  
  def model(train_x, train_y, test_x, test_y, learning_rate = 0.009,
            num_epochs = 50, minibatch_size = 64, print_cost = True):
      """
      Implements a three-layer ConvNet in Tensorflow:
      CONV2D -&gt; RELU -&gt; MAXPOOL -&gt; CONV2D -&gt; RELU -&gt; MAXPOOL -&gt; FLATTEN -&gt; FULLYCONNECTED
  
      Arguments:
      X_train -- training set, of shape (None, 64, 64, 3)
      Y_train -- test set, of shape (None, n_y = 6)
      X_test -- training set, of shape (None, 64, 64, 3)
      Y_test -- test set, of shape (None, n_y = 6)
      learning_rate -- learning rate of the optimization
      num_epochs -- number of epochs of the optimization loop
      minibatch_size -- size of a minibatch
      print_cost -- True to print the cost every 100 epochs
  
      Returns:
      train_accuracy -- real number, accuracy on the train set (X_train)
      test_accuracy -- real number, testing accuracy on the test set (X_test)
      parameters -- parameters learnt by the model. They can then be used to predict.
      """
  
      ops.reset_default_graph()                         # to be able to rerun the model without overwriting tf variables
      tf.set_random_seed(1)                             # to keep results consistent (tensorflow seed)
      seed = 3                                          # to keep results consistent (numpy seed)
      (m, n_H0, n_W0, n_C0) = train_x.shape             
      n_y = train_y.shape[1]                            
      costs = []                                        # To keep track of the cost
  
      # Create Placeholders of the correct shape
      ### START CODE HERE ### (1 line)
      X, Y = create_placeholders(n_H0, n_W0, n_C0, n_y)
      ### END CODE HERE ###
  
      # Initialize parameters
      ### START CODE HERE ### (1 line)
      parameters = initialize_parameters()
      ### END CODE HERE ###
  
      # Forward propagation: Build the forward propagation in the tensorflow graph
      ### START CODE HERE ### (1 line)
      Z3 = forward_propagation(X, parameters)
      ### END CODE HERE ###
  
      # Cost function: Add cost function to tensorflow graph
      ### START CODE HERE ### (1 line)
      cost = compute_cost(Z3, Y)
      ### END CODE HERE ###
  
      # Backpropagation: Define the tensorflow optimizer. Use an AdamOptimizer that minimizes the cost.
      ### START CODE HERE ### (1 line)
      optimizer = tf.train.AdamOptimizer(learning_rate = learning_rate).minimize(cost)
      ### END CODE HERE ###
  
      # Initialize all the variables globally
      init = tf.global_variables_initializer()
  
      # Start the session to compute the tensorflow graph
      with tf.Session() as sess:
  
          # Run the initialization
          sess.run(init)
  
          # Do the training loop
          for epoch in range(num_epochs):
              #print("m = " + str(m))
              minibatch_cost = 0.
              num_minibatches = int(m / minibatch_size) # number of minibatches of size minibatch_size in the train set
              #print(" minibatch_size =" + str(minibatch_size))
              seed = seed + 1
              minibatches = random_mini_batches(train_x, train_y, minibatch_size, seed)
              #print("minibatch =" + str(minibatch))
              #print("minibatches = " + str(minibatches))
  
              for minibatch in minibatches:
  
                  # Select a minibatch
                  (minibatch_X, minibatch_Y) = minibatch
                  # IMPORTANT: The line that runs the graph on a minibatch.
                  # Run the session to execute the optimizer and the cost, the feedict should contain a minibatch for (X,Y).
                  ### START CODE HERE ### (1 line)
                  _ , temp_cost = sess.run([optimizer, cost], feed_dict = {X: minibatch_X, Y: minibatch_Y})
                  ### END CODE HERE ###
  
                  minibatch_cost += temp_cost / num_minibatches
  
              # Print the cost every 5 epoch
              if print_cost == True and epoch % 5 == 0:
                  print ("Cost after epoch %i: %f" % (epoch, minibatch_cost))
              if print_cost == True and epoch % 1 == 0:
                  costs.append(minibatch_cost)
  
  
          # plot the cost
          plt.plot(np.squeeze(costs))
          plt.ylabel('cost')
          plt.xlabel('iterations (per tens)')
          plt.title("Learning rate =" + str(learning_rate))
          plt.show()
  
          # Calculate the correct predictions
          predict_op = tf.argmax(Z3, 1)
          correct_prediction = tf.equal(predict_op, tf.argmax(Y, 1))
  
          # Calculate accuracy on the test set
          accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
          print(accuracy)
          train_accuracy = accuracy.eval({X: train_x, Y: train_y})
          test_accuracy = accuracy.eval({X: test_x, Y: test_y})
          print("Train Accuracy:", train_accuracy)
          print("Test Accuracy:", test_accuracy)
  
          return train_accuracy, test_accuracy, parameters
  </code></pre>
  
  <p><strong>And below the output:</strong></p>
  
  <pre><code>_, _, parameters = model(X_train, Y_train, X_test, Y_test)
  
  Cost after epoch 0: 1.917929
  Cost after epoch 5: 1.506757
  Cost after epoch 10: 0.955359
  Cost after epoch 15: 0.845802
  Cost after epoch 20: 0.701174
  Cost after epoch 25: 0.571977
  Cost after epoch 30: 0.518435
  Cost after epoch 35: 0.495806
  Cost after epoch 40: 0.429827
  Cost after epoch 45: 0.407291
  </code></pre>