【问题标题】:InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype doubleInvalidArgumentError:您必须使用 dtype double 为占位符张量“Placeholder”提供一个值
【发布时间】:2020-05-16 16:39:45
【问题描述】:

我正在尝试使用 1 个隐藏层 (LINEAR -> RELU -> LINEAR -> SIGMOID) 训练二进制分类模型。我的 x 数据集的形状(示例数,输入特征数)和形状的 y 集(示例数,1)

当我尝试输入数据时出现以下错误。我尝试更改成本函数,但问题似乎仍然存在。

---------------------------------------------------------------------------
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) ]]

我的代码:

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

不知道我做错了什么。有人可以帮忙吗?

【问题讨论】:

    标签: python tensorflow machine-learning deep-learning


    【解决方案1】:

    您已经定义了您在网络中使用的keep_prob = tf.placeholder(tf.float64) ,并且您的cost 依赖于它。你的输出字典是[optimizer, cost]。您将提供输出字典所依赖的所有占位符的值。对于 0.5 的 dropout,您的代码将像这样修改(硬编码。我建议您将其设为参数,以便您可以尝试不同的 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}))
    

    【讨论】:

      猜你喜欢
      • 2019-01-23
      • 1970-01-01
      • 1970-01-01
      • 2017-05-27
      • 1970-01-01
      • 1970-01-01
      • 2018-12-29
      • 2018-12-27
      相关资源
      最近更新 更多