1
votes

I am trying to train a model for binary classification using 1 hidden layer (LINEAR -> RELU -> LINEAR -> SIGMOID). My x dataset is of the shape (number of examples, number of input features) and y set of the shape (number of examples, 1)

I am getting the following error, when I try to feed the data. I tried changing the cost function but the issue still seems to be there.

---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
~/.local/lib/python3.7/site-packages/tensorflow_core/python/client/session.py in _do_call(self, fn, *args)
   1364     try:
-> 1365       return fn(*args)
   1366     except errors.OpError as e:

~/.local/lib/python3.7/site-packages/tensorflow_core/python/client/session.py in _run_fn(feed_dict, fetch_list, target_list, options, run_metadata)
   1349       return self._call_tf_sessionrun(options, feed_dict, fetch_list,
-> 1350                                       target_list, run_metadata)
   1351 

~/.local/lib/python3.7/site-packages/tensorflow_core/python/client/session.py in _call_tf_sessionrun(self, options, feed_dict, fetch_list, target_list, run_metadata)
   1442                                             fetch_list, target_list,
-> 1443                                             run_metadata)
   1444 

InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype double
     [[{{node Placeholder}}]]

During handling of the above exception, another exception occurred:

InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-25-d2dca3403a73> in <module>
     12 
     13                 _, c = sess.run([optimizer, cost], feed_dict={x: batch_x,
---> 14                                         y: batch_y})
     15                 epoch_loss += c
     16                 i+=batch_size

~/.local/lib/python3.7/site-packages/tensorflow_core/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
    954     try:
    955       result = self._run(None, fetches, feed_dict, options_ptr,
--> 956                          run_metadata_ptr)
    957       if run_metadata:
    958         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

~/.local/lib/python3.7/site-packages/tensorflow_core/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
   1178     if final_fetches or final_targets or (handle and feed_dict_tensor):
   1179       results = self._do_run(handle, final_targets, final_fetches,
-> 1180                              feed_dict_tensor, options, run_metadata)
   1181     else:
   1182       results = []

~/.local/lib/python3.7/site-packages/tensorflow_core/python/client/session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
   1357     if handle is None:
   1358       return self._do_call(_run_fn, feeds, fetches, targets, options,
-> 1359                            run_metadata)
   1360     else:
   1361       return self._do_call(_prun_fn, handle, feeds, fetches)

~/.local/lib/python3.7/site-packages/tensorflow_core/python/client/session.py in _do_call(self, fn, *args)
   1382                     '\nsession_config.graph_options.rewrite_options.'
   1383                     'disable_meta_optimizer = True')
-> 1384       raise type(e)(node_def, op, message)
   1385 
   1386   def _extend_graph(self):

InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype double
     [[node Placeholder (defined at /Users/xx.xx/.local/lib/python3.7/site-packages/tensorflow_core/python/framework/ops.py:1748) ]]

My code:

#Initialise
n_hidden_1 = 14

W1 = tf.get_variable("W1", [n_input,n_hidden_1], dtype=tf.float64, initializer = tf.contrib.layers.xavier_initializer())
b1 = tf.get_variable("b1", [n_hidden_1], dtype=tf.float64, initializer = tf.zeros_initializer())
W2 = tf.get_variable("W2", [n_hidden_1,n_output], dtype=tf.float64, initializer = tf.contrib.layers.xavier_initializer())
b2 = tf.get_variable("b2", [n_output], dtype=tf.float64, initializer = tf.zeros_initializer())

keep_prob = tf.placeholder(tf.float64)

#creating placeholders
x = tf.placeholder(tf.float64, [None,n_input])
y = tf.placeholder(tf.float64)

#Model
def model(x, W1, b1, W2, b2, keep_prob):
    layer_1 = tf.add(tf.matmul(x, W1), b1)
    layer_1 = tf.nn.relu(layer_1)
    layer_1 = tf.nn.dropout(layer_1, keep_prob)
    out_layer = tf.add(tf.matmul(layer_1, W2),b2)
    return out_layer

predictions = model(x, W1,b1,W2,b2, keep_prob)
cost = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels = y,logits = predictions))
optimizer = tf.train.AdamOptimizer().minimize(cost)

with tf.Session() as sess:
        sess.run(tf.initialize_all_variables())

        for epoch in range(training_epochs):
            epoch_loss = 0
            i = 0
            while i < len(x_train):
                start = i
                end = i + batch_size
                batch_x = np.array(x_train[start:end])
                batch_y = np.array(y_train[start:end])

                _, c = sess.run([optimizer, cost], feed_dict={x: batch_x,
                                        y: batch_y})
                epoch_loss += c
                i+=batch_size

            print('Epoch', epoch, 'completed out of', training_epochs, 'loss:', epoch_loss)


        # correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
        # accuracy = tf.reduce_mean(tf.cast(correct, 'float'))

        print (test_x.shape)
        accuracy = tf.nn.l2_loss(prediction-y,name="squared_error_test_cost")/test_x.shape[0]
        print('Accuracy:', accuracy.eval({x: test_x, y: test_y}))

Not sure what I am doing wrong. Can someone help?

1

1 Answers

1
votes

You have defined keep_prob = tf.placeholder(tf.float64) that you are using in your network and your cost is dependent upon it. Your output dictionary is [optimizer, cost]. You are to provide the values of all the placeholders that your output dictionary depends upon. For a dropout of 0.5 your code would be modified like this (hardcoded. i suggest you make it a parameter so that you can experiment around with different values of dropout)

#Initialise
n_hidden_1 = 14

W1 = tf.get_variable("W1", [n_input,n_hidden_1], dtype=tf.float64, initializer = tf.contrib.layers.xavier_initializer())
b1 = tf.get_variable("b1", [n_hidden_1], dtype=tf.float64, initializer = tf.zeros_initializer())
W2 = tf.get_variable("W2", [n_hidden_1,n_output], dtype=tf.float64, initializer = tf.contrib.layers.xavier_initializer())
b2 = tf.get_variable("b2", [n_output], dtype=tf.float64, initializer = tf.zeros_initializer())

keep_prob = tf.placeholder(tf.float64)

#creating placeholders
x = tf.placeholder(tf.float64, [None,n_input])
y = tf.placeholder(tf.float64)

#Model
def model(x, W1, b1, W2, b2, keep_prob):
    layer_1 = tf.add(tf.matmul(x, W1), b1)
    layer_1 = tf.nn.relu(layer_1)
    layer_1 = tf.nn.dropout(layer_1, keep_prob)
    out_layer = tf.add(tf.matmul(layer_1, W2),b2)
    return out_layer

predictions = model(x, W1,b1,W2,b2, keep_prob)
cost = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(labels = y,logits = predictions))
optimizer = tf.train.AdamOptimizer().minimize(cost)

with tf.Session() as sess:
        sess.run(tf.initialize_all_variables())

        for epoch in range(training_epochs):
            epoch_loss = 0
            i = 0
            while i < len(x_train):
                start = i
                end = i + batch_size
                batch_x = np.array(x_train[start:end])
                batch_y = np.array(y_train[start:end])

                _, c = sess.run([optimizer, cost], feed_dict={x: batch_x,
                                        y: batch_y,keep_prob:0.5})
                epoch_loss += c
                i+=batch_size

            print('Epoch', epoch, 'completed out of', training_epochs, 'loss:', epoch_loss)


        # correct = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
        # accuracy = tf.reduce_mean(tf.cast(correct, 'float'))

        print (test_x.shape)
        accuracy = tf.nn.l2_loss(prediction-y,name="squared_error_test_cost")/test_x.shape[0]
        print('Accuracy:', accuracy.eval({x: test_x, y: test_y}))