简体   繁体   English

InvalidArgumentError:您必须使用 dtype double 为占位符张量“Placeholder”提供一个值

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

I am trying to train a model for binary classification using 1 hidden layer (LINEAR -> RELU -> LINEAR -> SIGMOID).我正在尝试使用 1 个隐藏层(LINEAR -> RELU -> LINEAR -> SIGMOID)训练 model 进行二进制分类。 My x dataset is of the shape (number of examples, number of input features) and y set of the shape (number of examples, 1)我的 x 数据集是形状(示例数,输入特征数)和 y 形状集(示例数,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.我尝试更改成本 function 但问题似乎仍然存在。

---------------------------------------------------------------------------
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?有人可以帮忙吗?

You have defined keep_prob = tf.placeholder(tf.float64) that you are using in your.network and your cost is dependent upon it.您已经定义了您在 your.network 中使用的keep_prob = tf.placeholder(tf.float64)并且您的cost取决于它。 Your output dictionary is [optimizer, cost] .您的 output 字典是[optimizer, cost] You are to provide the values of all the placeholders that your output dictionary depends upon.您要提供 output 字典所依赖的所有占位符的值。 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)对于 0.5 的丢失,您的代码将像这样修改(硬编码。我建议您将其作为参数,以便您可以尝试不同的丢失值)

#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}))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 InvalidArgumentError:您必须使用dtype float来输入占位符张量&#39;Placeholder&#39;的值 - InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype float tensorflow InvalidArgumentError:您必须使用dtype float为占位符张量提供值 - tensorflow InvalidArgumentError: You must feed a value for placeholder tensor with dtype float InvalidArgumentError:您必须使用dtype float输入占位符张量&#39;Placeholder_109&#39;的值 - InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder_109' with dtype float InvalidArgumentError:您必须使用 dtype float 和 shape 为占位符张量“Placeholder”提供一个值 - InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype float and shape InvalidArgumentError(请参阅上面的回溯):您必须使用dtype float输入占位符张量“ Placeholder_2”的值 - InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder_2' with dtype float InvalidArgumentError(请参阅上面的回溯):您必须使用dtype float输入占位符张量“ Placeholder”的值 - InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder' with dtype float InvalidArgumentError(请参阅上面的回溯):您必须使用dtype float输入占位符张量&#39;Placeholder_8&#39;的值 - InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder_8' with dtype float InvalidArgumentError:您必须使用 dtype float 和 shape [?,?,3] 为占位符张量“Placeholder_1”提供一个值 - InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder_1' with dtype float and shape [?,?,3] Tensorflow:InvalidArgumentError:您必须输入占位符张量&#39;Placeholder_1&#39;的值 - Tensorflow: InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder_1' InvalidArgumentError:您必须为占位符张量“Placeholder_1”提供一个值 - InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder_1'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM