InvalidArgumentError:您必须为占位符张量提供一个值' Placeholder_109'与dtype浮动

时间:2017-07-06 06:29:39

标签: python numpy tensorflow

我正在使用Tensorflow来提供数据。 但我得到了这个错误,我提供了代码和错误。 我的数据是400灰度和16x16图像尺寸。

这是my_dataset()函数



def my_dataset():
    
    print ('HELLO')
    
# fully connected variables
resulting_width = image_width // (max_pool_size1 * max_pool_size2)
resulting_height = image_height // (max_pool_size1 * max_pool_size2)
full1_input_size = resulting_width * resulting_height*conv2_features
full1_weight = tf.Variable(tf.truncated_normal([full1_input_size, fully_connected_size1], stddev=0.1, dtype=tf.float32))
full1_bias = tf.Variable(tf.truncated_normal([fully_connected_size1], stddev=0.1, dtype=tf.float32))
full2_weight = tf.Variable(tf.truncated_normal([fully_connected_size1, target_sizeconvert], stddev=0.1, dtype=tf.float32))
full2_bias = tf.Variable(tf.truncated_normal([target_sizeconvert], stddev=0.1, dtype=tf.float32))

# Dropout placeholder
dropout = tf.placeholder(tf.float32, shape=())

print("cheking5")

#%%
#shape of input = [batch, in_height, in_width, in_channels]
#shape of filter = [filter_height, filter_width, in_channels, out_channels]

# Initialize Model Operations 
def my_conv_net(input_data):
    
    ########### First Conv-ReLU-MaxPool Layer ###########
    conv1 = tf.nn.conv2d(input_data, conv1_weight, strides=[1, 1, 1, 1], padding='SAME')
    relu1 = tf.nn.relu(tf.nn.bias_add(conv1, conv1_bias))
    max_pool1 = tf.nn.max_pool(relu1, ksize=[1, max_pool_size1, max_pool_size1, 1], strides=[1, max_pool_size1, max_pool_size1, 1], padding='SAME')
    
    print("input_data>>>: ", input_data)
    print("conv1_weight>>>: ", conv1_weight)
    print("conv1_weight get shape: ", conv1_weight.get_shape())
    print("conv1_bias>>>: ", conv1_bias)
    print("conv1>>>: ", conv1)
    print("relu1>>>: ", relu1)
    print("max_pool1>>>: ", max_pool1)
    print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n")
    
    # Second Conv-ReLU-MaxPool Layer
    conv2 = tf.nn.conv2d(max_pool1, conv2_weight, strides=[1, 1, 1, 1], padding='SAME')
    relu2 = tf.nn.relu(tf.nn.bias_add(conv2, conv2_bias))
    max_pool2 = tf.nn.max_pool(relu2, ksize=[1, max_pool_size2, max_pool_size2, 1], strides=[1, max_pool_size2, max_pool_size2, 1], padding='SAME')
    
    print("conv2_weight>>>: ", conv2_weight)
    print("conv2_bias>>>: ", conv2_bias)
    print("conv2>>>: ", conv2)
    print("relu2>>>: ", relu2)
    print("max_pool2>>>: ", max_pool2)
    print("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n")
    
    # Transform Output into a 1xN layer for next fully connected layer
    final_conv_shape = max_pool2.get_shape().as_list()
    final_shape = final_conv_shape[1] * final_conv_shape[2] * final_conv_shape[3]
    flat_output = tf.reshape(max_pool2, [final_conv_shape[0], final_shape])
    
    # First Fully Connected Layer
    print("flat_output ***: ", type(flat_output))
    print("flat_output ***: ", flat_output)
    print("full1_weight ***: ", type(full1_weight))
    print("full1_weight ***: ", full1_weight)
    print("full1_bias ***: ", type(full1_bias))
    print("full1_bias ***: ", full1_bias)
    print("$$$$$$$$$$$$$$$$ checking2 $$$$$$$$$$$$$$$$$$$\n")
    
    fully_connected1 = tf.nn.relu(tf.add(tf.matmul(flat_output, full1_weight), full1_bias))
    
    # Second Fully Connected Layer
    final_model = tf.add(tf.matmul(fully_connected1, full2_weight), full2_bias)
    
    # Add dropout
    final_model_output = tf.nn.dropout(final_model, dropout)
    
    return(final_model_output)
print("checking6\n")

# We can declare the model on the training and test data
model_output = my_conv_net(x_input)
test_model_output = my_conv_net(eval_input)
print("YYYYYYy_target***: ", y_target)
print("XXXXXx_input***: ", x_input)

# Declare Loss Function (softmax cross entropy)    
loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=model_output, labels=y_target))
print("checking7")

# Create a prediction function
prediction = tf.nn.softmax(model_output)
test_prediction = tf.nn.softmax(test_model_output)
print("checking8")
    
# Create accuracy function
def get_accuracy(logits, targets):
    batch_predictions = np.argmax(logits, axis=1)
    num_correct = np.sum(np.equal(batch_predictions, targets))
    return(100. * num_correct/batch_predictions.shape[0])

# Create an optimizer
my_optimizer = tf.train.MomentumOptimizer(learning_rate, 0.9)
train_step = my_optimizer.minimize(loss)

# Initialize Variables
#init = tf.global_variables_initializer()
init=tf.group(tf.global_variables_initializer(),tf.local_variables_initializer())
sess = tf.Session()
sess.run(init)
print("checking9")

# Start training loop
train_loss = []
train_acc = []
test_acc = []
for i in range(generations):
    rand_index = np.random.choice(len(ttrainData), size=batch_size)
    print("Random Index",rand_index)
    rand_x = ttrainData[rand_index]
    rand_x = np.expand_dims(rand_x, 3)
    rand_y = ttrainLabels[rand_index]
    print("TRAIN LABEL",ttrainLabels.dtype," dtype",ttrainLabels[0].dtype)
    #rand_y = np.array(rand_y,dtype=np.uint8)
    train_dict = {x_input: rand_x, y_target: rand_y}
    print("Dictionary",train_dict)
    
    print("\nKJKJKJKJKJ\n")
#    print("train_dict***: ", train_dict)
    print("train_step***: ", train_step)
    print("train_step***: ", type(train_step))
    
    print("\nrand_index***: ", rand_index.dtype)
    print("Type of rand_index***: ", type(rand_index))
    print("Length of the rand_index: ", len(rand_index))
    
    print("\nrand_x***: ", rand_x.dtype)
    print("rand_x***:", type(rand_x))
    print("rand_x[0]***:", type(rand_x[0]))
    print("Length of rand_x: ", len(rand_x))
#    print("Convert rand_x to float32: ", rand_x.astype(np.float32))
    print("rand_x[0]***:", type(rand_x[0]))
    print("rand_x[0]***: ", rand_x[0].dtype)
    
    print("\nrand_y***: ", rand_y.dtype)
    print("rand_y***:",type(rand_y))
    print("rand_y[0]***:", type(rand_y[0]))
    print("Convert rand_y to Uint8: ", rand_y[0].astype(int))
    print("rand_y[0]***:", type(rand_y[0]))
    print("Length of rand_y: ", len(rand_y))    

    sess.run(train_step, feed_dict = train_dict)
    print(sess1.run(loss), feed_dict={xs:rand_x, ys:rand_y})
    temp_train_loss, temp_train_preds = sess.run([loss, prediction], feed_dict = train_dict)
    temp_train_acc = get_accuracy(temp_train_preds, rand_y)
    

    if (i+1) % eval_every == 0:
        eval_index = np.random.choice(len(ttestData), size=evaluation_size)
        eval_x = ttestData[eval_index]
        eval_x = np.expand_dims(eval_x, 3)
        eval_y = ttestLabels[eval_index]
        test_dict = {eval_input: eval_x, eval_target: eval_y}
        test_preds = sess.run(test_prediction, feed_dict=test_dict)
        temp_test_acc = get_accuracy(test_preds, eval_y)
        
        # Record and print results
        train_loss.append(temp_train_loss)
        train_acc.append(temp_train_acc)
        test_acc.append(temp_test_acc)
        acc_and_loss = [(i+1), temp_train_loss, temp_train_acc, temp_test_acc]
        acc_and_loss = [np.round(x,2) for x in acc_and_loss]
        print('Generation # {}. Train Loss: {:.2f}. Train Acc (Test Acc): {:.2f} ({:.2f})'.format(*acc_and_loss))
        print("checking10")
    
   




它给了我这个错误。



runfile('C:/Users/Tala/Desktop/KSU/GRA-Thesis/Poster-Spring17/CNN-Xray/Xray-TF/CNN-Xray-TF-CookBook.py', wdir='C:/Users/Tala/Desktop/KSU/GRA-Thesis/Poster-Spring17/CNN-Xray/Xray-TF')
Reloaded modules: Mydataset_Xray
HELLO
First path of image:  C:/Users/Tala/Desktop/KSU/GRA-Thesis/Poster-Spring17/CNN-Xray/Xray-TF/Xrays-01-resized/sample_0_111.jpg

cheking5
checking6

input_data>>>:  Tensor("Placeholder:0", shape=(100, 16, 16, 1), dtype=float32)
conv1_weight>>>:  <tf.Variable 'Variable_66:0' shape=(4, 4, 1, 32) dtype=float32_ref>
conv1_weight get shape:  (4, 4, 1, 32)
conv1_bias>>>:  <tf.Variable 'Variable_67:0' shape=(32,) dtype=float32_ref>
conv1>>>:  Tensor("Conv2D_20:0", shape=(100, 16, 16, 32), dtype=float32)
relu1>>>:  Tensor("Relu_32:0", shape=(100, 16, 16, 32), dtype=float32)
max_pool1>>>:  Tensor("MaxPool_20:0", shape=(100, 8, 8, 32), dtype=float32)
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

conv2_weight>>>:  <tf.Variable 'Variable_68:0' shape=(4, 4, 32, 64) dtype=float32_ref>
conv2_bias>>>:  <tf.Variable 'Variable_69:0' shape=(64,) dtype=float32_ref>
conv2>>>:  Tensor("Conv2D_21:0", shape=(100, 8, 8, 64), dtype=float32)
relu2>>>:  Tensor("Relu_33:0", shape=(100, 8, 8, 64), dtype=float32)
max_pool2>>>:  Tensor("MaxPool_21:0", shape=(100, 4, 4, 64), dtype=float32)
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

flat_output ***:  <class 'tensorflow.python.framework.ops.Tensor'>
flat_output ***:  Tensor("Reshape_15:0", shape=(100, 1024), dtype=float32)
full1_weight ***:  <class 'tensorflow.python.ops.variables.Variable'>
full1_weight ***:  <tf.Variable 'Variable_70:0' shape=(1024, 100) dtype=float32_ref>
full1_bias ***:  <class 'tensorflow.python.ops.variables.Variable'>
full1_bias ***:  <tf.Variable 'Variable_71:0' shape=(100,) dtype=float32_ref>
$$$$$$$$$$$$$$$$ checking2 $$$$$$$$$$$$$$$$$$$

input_data>>>:  Tensor("Placeholder_42:0", shape=(500, 16, 16, 1), dtype=float32)
conv1_weight>>>:  <tf.Variable 'Variable_66:0' shape=(4, 4, 1, 32) dtype=float32_ref>
conv1_weight get shape:  (4, 4, 1, 32)
conv1_bias>>>:  <tf.Variable 'Variable_67:0' shape=(32,) dtype=float32_ref>
conv1>>>:  Tensor("Conv2D_22:0", shape=(500, 16, 16, 32), dtype=float32)
relu1>>>:  Tensor("Relu_35:0", shape=(500, 16, 16, 32), dtype=float32)
max_pool1>>>:  Tensor("MaxPool_22:0", shape=(500, 8, 8, 32), dtype=float32)
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

conv2_weight>>>:  <tf.Variable 'Variable_68:0' shape=(4, 4, 32, 64) dtype=float32_ref>
conv2_bias>>>:  <tf.Variable 'Variable_69:0' shape=(64,) dtype=float32_ref>
conv2>>>:  Tensor("Conv2D_23:0", shape=(500, 8, 8, 64), dtype=float32)
relu2>>>:  Tensor("Relu_36:0", shape=(500, 8, 8, 64), dtype=float32)
max_pool2>>>:  Tensor("MaxPool_23:0", shape=(500, 4, 4, 64), dtype=float32)
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

flat_output ***:  <class 'tensorflow.python.framework.ops.Tensor'>
flat_output ***:  Tensor("Reshape_16:0", shape=(500, 1024), dtype=float32)
full1_weight ***:  <class 'tensorflow.python.ops.variables.Variable'>
full1_weight ***:  <tf.Variable 'Variable_70:0' shape=(1024, 100) dtype=float32_ref>
full1_bias ***:  <class 'tensorflow.python.ops.variables.Variable'>
full1_bias ***:  <tf.Variable 'Variable_71:0' shape=(100,) dtype=float32_ref>
$$$$$$$$$$$$$$$$ checking2 $$$$$$$$$$$$$$$$$$$

YYYYYYy_target***:  Tensor("Placeholder_41:0", shape=(100,), dtype=int32)
XXXXXx_input***:  Tensor("Placeholder:0", shape=(100, 16, 16, 1), dtype=float32)
checking7
checking8
checking9
Random Index [118 269 164 ...,  15  10 170]
TRAIN LABEL uint8  dtype uint8

KJKJKJKJKJ

train_step***:  name: "Momentum_4"
op: "NoOp"
input: "^Momentum_4/update_Variable_66/ApplyMomentum"
input: "^Momentum_4/update_Variable_67/ApplyMomentum"
input: "^Momentum_4/update_Variable_68/ApplyMomentum"
input: "^Momentum_4/update_Variable_69/ApplyMomentum"
input: "^Momentum_4/update_Variable_70/ApplyMomentum"
input: "^Momentum_4/update_Variable_71/ApplyMomentum"
input: "^Momentum_4/update_Variable_72/ApplyMomentum"
input: "^Momentum_4/update_Variable_73/ApplyMomentum"

train_step***:  <class 'tensorflow.python.framework.ops.Operation'>

rand_index***:  int32
Type of rand_index***:  <class 'numpy.ndarray'>
Length of the rand_index:  100

y_target***:  Tensor("Placeholder_41:0", shape=(100,), dtype=int32)
y_target***: <class 'tensorflow.python.framework.ops.Tensor'>

x_input***:  Tensor("Placeholder:0", shape=(100, 16, 16, 1), dtype=float32)
x_input***: <class 'tensorflow.python.framework.ops.Tensor'>

rand_x***:  float32
rand_x***: <class 'numpy.ndarray'>
rand_x[0]***: <class 'numpy.ndarray'>
Length of rand_x:  100
rand_x[0]***: <class 'numpy.ndarray'>
rand_x[0]***:  float32

rand_y***:  uint8
rand_y***: <class 'numpy.ndarray'>
rand_y[0]***: <class 'numpy.uint8'>
Convert rand_y to Uint8:  1
rand_y[0]***: <class 'numpy.uint8'>
Length of rand_y:  100
Traceback (most recent call last):

  File "C:\Program Files\Anaconda2\envs\py3.5\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
    execfile(filename, namespace)

  File "C:\Program Files\Anaconda2\envs\py3.5\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/Tala/Desktop/KSU/GRA-Thesis/Poster-Spring17/CNN-Xray/Xray-TF/CNN-Xray-TF-CookBook.py", line 265, in <module>
    sess.run(train_step, feed_dict={x_input: rand_x, y_target: rand_y})

  File "C:\Program Files\Anaconda2\envs\py3.5\lib\site-packages\tensorflow\python\client\session.py", line 789, in run
    run_metadata_ptr)

  File "C:\Program Files\Anaconda2\envs\py3.5\lib\site-packages\tensorflow\python\client\session.py", line 997, in _run
    feed_dict_string, options, run_metadata)

  File "C:\Program Files\Anaconda2\envs\py3.5\lib\site-packages\tensorflow\python\client\session.py", line 1132, in _do_run
    target_list, options, run_metadata)

  File "C:\Program Files\Anaconda2\envs\py3.5\lib\site-packages\tensorflow\python\client\session.py", line 1152, in _do_call
    raise type(e)(node_def, op, message)

InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder_44' with dtype float
	 [[Node: Placeholder_44 = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

Caused by op 'Placeholder_44', defined at:
  File "C:\Program Files\Anaconda2\envs\py3.5\lib\site-packages\spyder\utils\ipython\start_kernel.py", line 227, in <module>
    main()
  File "C:\Program Files\Anaconda2\envs\py3.5\lib\site-packages\spyder\utils\ipython\start_kernel.py", line 223, in main
    kernel.start()
  File "C:\Program Files\Anaconda2\envs\py3.5\lib\site-packages\ipykernel\kernelapp.py", line 474, in start
    ioloop.IOLoop.instance().start()
  File "C:\Program Files\Anaconda2\envs\py3.5\lib\site-packages\zmq\eventloop\ioloop.py", line 177, in start
    super(ZMQIOLoop, self).start()
  File "C:\Program Files\Anaconda2\envs\py3.5\lib\site-packages\tornado\ioloop.py", line 887, in start
    handler_func(fd_obj, events)
  File "C:\Program Files\Anaconda2\envs\py3.5\lib\site-packages\tornado\stack_context.py", line 275, in null_wrapper
    return fn(*args, **kwargs)
  File "C:\Program Files\Anaconda2\envs\py3.5\lib\site-packages\zmq\eventloop\zmqstream.py", line 440, in _handle_events
    self._handle_recv()
  File "C:\Program Files\Anaconda2\envs\py3.5\lib\site-packages\zmq\eventloop\zmqstream.py", line 472, in _handle_recv
    self._run_callback(callback, msg)
  File "C:\Program Files\Anaconda2\envs\py3.5\lib\site-packages\zmq\eventloop\zmqstream.py", line 414, in _run_callback
    callback(*args, **kwargs)
  File "C:\Program Files\Anaconda2\envs\py3.5\lib\site-packages\tornado\stack_context.py", line 275, in null_wrapper
    return fn(*args, **kwargs)
  File "C:\Program Files\Anaconda2\envs\py3.5\lib\site-packages\ipykernel\kernelbase.py", line 276, in dispatcher
    return self.dispatch_shell(stream, msg)
  File "C:\Program Files\Anaconda2\envs\py3.5\lib\site-packages\ipykernel\kernelbase.py", line 228, in dispatch_shell
    handler(stream, idents, msg)
  File "C:\Program Files\Anaconda2\envs\py3.5\lib\site-packages\ipykernel\kernelbase.py", line 390, in execute_request
    user_expressions, allow_stdin)
  File "C:\Program Files\Anaconda2\envs\py3.5\lib\site-packages\ipykernel\ipkernel.py", line 196, in do_execute
    res = shell.run_cell(code, store_history=store_history, silent=silent)
  File "C:\Program Files\Anaconda2\envs\py3.5\lib\site-packages\ipykernel\zmqshell.py", line 501, in run_cell
    return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  File "C:\Program Files\Anaconda2\envs\py3.5\lib\site-packages\IPython\core\interactiveshell.py", line 2717, in run_cell
    interactivity=interactivity, compiler=compiler, result=result)
  File "C:\Program Files\Anaconda2\envs\py3.5\lib\site-packages\IPython\core\interactiveshell.py", line 2827, in run_ast_nodes
    if self.run_code(code, result):
  File "C:\Program Files\Anaconda2\envs\py3.5\lib\site-packages\IPython\core\interactiveshell.py", line 2881, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-28-1db506e814de>", line 1, in <module>
    runfile('C:/Users/Tala/Desktop/KSU/GRA-Thesis/Poster-Spring17/CNN-Xray/Xray-TF/CNN-Xray-TF-CookBook.py', wdir='C:/Users/Tala/Desktop/KSU/GRA-Thesis/Poster-Spring17/CNN-Xray/Xray-TF')
  File "C:\Program Files\Anaconda2\envs\py3.5\lib\site-packages\spyder\utils\site\sitecustomize.py", line 866, in runfile
    execfile(filename, namespace)
  File "C:\Program Files\Anaconda2\envs\py3.5\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)
  File "C:/Users/Tala/Desktop/KSU/GRA-Thesis/Poster-Spring17/CNN-Xray/Xray-TF/CNN-Xray-TF-CookBook.py", line 111, in <module>
    dropout = tf.placeholder(tf.float32, shape=())
  File "C:\Program Files\Anaconda2\envs\py3.5\lib\site-packages\tensorflow\python\ops\array_ops.py", line 1530, in placeholder
    return gen_array_ops._placeholder(dtype=dtype, shape=shape, name=name)
  File "C:\Program Files\Anaconda2\envs\py3.5\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 1954, in _placeholder
    name=name)
  File "C:\Program Files\Anaconda2\envs\py3.5\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 767, in apply_op
    op_def=op_def)
  File "C:\Program Files\Anaconda2\envs\py3.5\lib\site-packages\tensorflow\python\framework\ops.py", line 2506, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "C:\Program Files\Anaconda2\envs\py3.5\lib\site-packages\tensorflow\python\framework\ops.py", line 1269, in __init__
    self._traceback = _extract_stack()

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder_44' with dtype float
	 [[Node: Placeholder_44 = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]]
&#13;
&#13;
&#13;

我真的不知道问题出在哪里。我将每个参数的dtype更改为float32,但它仍然给了我sam eerror。

任何帮助都会受到赞赏

1 个答案:

答案 0 :(得分:0)

我发现了问题所在。我为dropout创建了占位符,但我没有将它添加到train_dic和test_dic。所以它一直要求填写这个占位符。

train_dict = {x_input:rand_x,y_target:rand_y,dropout:dropout_prob}

test_dict = {eval_input:eval_x,eval_target:eval_y,dropout:1.0}