0
votes

I had an error: "InvalidArgumentError: You must feed a value for placeholder tensor 'Placeholder' with dtype float." And I can't tell why it happened.

I want to make a CNN model for sentence classifications. Now I set a random data as data for training. Due to there is text data, it can't become square matrix like MNIST data after "reshape" the data. Instead, it is a 10*100 matrix. Maybe it is a problem of my code.

import tensorflow as tf
import numpy as np
from numpy.random import RandomState

# sess = tf.InteractiveSession()

x = tf.placeholder(tf.float32, shape=[None,1000], name='x_input')
y_ = tf.placeholder(tf.float32, shape=[None,2], name='y_input')


# x = tf.image.convert_image_dtype(x,tf.float32)  



# Define funstions.
def weight_variable(shape):
    initial = tf.truncated_normal(shape,stddev=0.1)
    return tf.Variable(initial)

def bias_variable(shape):
    initial = tf.constant(0.1,shape=shape)
    return tf.Variable(initial)


def conv2d(x,W,m,n):
    return tf.nn.conv2d(x,W,strides=[1,m,n,1],padding = 'SAME')

def max_pool_2x2(x):
    return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')


x_1 = tf.reshape(x, [-1,10,100,1])


# Convolution Layer #1
W_conv1 = weight_variable([2,20,1,25])
b_conv1 = bias_variable([25])
h_conv1 = tf.nn.relu(conv2d(x_1,W_conv1,1,1)+b_conv1)

# Pooling Layer #1
h_pool1 = tf.nn.max_pool(h_conv1,ksize=[1,2,20,1],strides=[1,1,1,1],padding='VALID')

# Convolution Layer #2
W_conv2 = weight_variable([3,9,25,100])
b_conv2 = bias_variable([100])
h_conv2 = tf.nn.relu(conv2d(h_pool1,W_conv2,3,9)+b_conv2)

# Pooling Layer #2
h_pool2 = tf.nn.max_pool(h_conv2,ksize=[1,3,3,1],strides=[1,3,3,1],padding='VALID')


# Dense Layer 
pool_shape = h_pool2.get_shape().as_list()
nodes = pool_shape[1]*pool_shape[2]*pool_shape[3]

W_fc1 = weight_variable([nodes,200])
b_fc1 = bias_variable([200])

h_pool2_flat = tf.reshape(h_pool2,[-1,nodes])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat,W_fc1)+b_fc1)

# Dropout regulation
keep_prob = tf.placeholder("float")
h_fc1_drop = tf.nn.dropout(h_fc1,keep_prob)


# Logits Layer
W_fc2 = weight_variable([200,2])
b_fc2 = bias_variable([2])
y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop,W_fc2)+b_fc2)



# Back propagation
cross_entropy = -tf.reduce_mean(
        y_ * tf.log(tf.clip_by_value(y_conv,1e-10,1.0)))
train_step = tf.train.AdamOptimizer(0.001).minimize(cross_entropy)


# -----------------------------------------------------------------------------
# Initialization
rdm = RandomState(1)
dataset_size = 128
X = rdm.rand(dataset_size,1000)
Y = [[int(x1<0.5),1-int(x1<0.5)]for x1 in X[:,1]]


# Create a Session
with tf.Session() as sess:
    init_op = tf.initialize_all_variables()
    sess.run(init_op)

    steps = 1000
    batch_size = 8
    for i in range(steps):
        start  = (i*batch_size) % dataset_size
        end = min(start + batch_size, dataset_size)

        sess.run(train_step,feed_dict={x:X[start:end], y_:Y[start:end]})

        if i%1000 ==0:
            total_cross_entropy = sess.run(
                    cross_entropy, feed_dict={x:X,y_:Y})
            print("After %d training step(s), cross entropy on all data is %g"%
                  (i,total_cross_entropy))

Thanks for reading.:)

How to solve this problem?

1

1 Answers

0
votes

You need to feed a value for keep_prob, which is a placeholder with dtype float.