tensorflow - Invalid argument : You must feed a value for placeholder tensor with dtype float and shape [64,1] -
i got unexpected error when running below code :
ith tf.session(graph=graph) session: tf.initialize_all_variables().run() print('initialized') mean_loss = 0 step in xrange(num_steps) : print ("train data ",len(train_data)) batch_inputs, batch_labels = generate_batches(train_dataset, batch_size=64, unrollings=5) feed_dict = dict() in range(unrollings): batch_labels[i] = np.reshape(batch_labels[i], (batch_size, 1)) batch_inputs[i] = np.array(batch_inputs[i]).astype('int32') batch_labels[i] = batch_labels[i].astype('float32') print (train_inputs[i], train_labels[i]) feed_dict = {train_inputs[i] : batch_inputs[i], train_labels[i] : batch_labels[i]} _, l, predictions, lr = session.run([optimizer, loss, train_prediction, learning_rate], feed_dict=feed_dict) mean_loss += l
here generating batches, lstm cell , calculating loss code :
def generate_batches(raw_data, batch_size, unrollings): global data_index data_len = len(raw_data) num_batches = data_len // batch_size inputs = [] labels = [] label = np.zeros(shape=(batch_size, 1), dtype=np.float) print (num_batches, data_len, batch_size) j in xrange(unrollings) : inputs.append([]) labels.append([]) in xrange(batch_size) : inputs[j].append(raw_data[i + data_index]) label[i, 0] = raw_data[i + data_index + 1] data_index = (data_index + 1) % len(raw_data) print (len(inputs), len(inputs[0]), len(labels), label.shape) labels[j].append(label.tolist()) return inputs, labels embedding_size = 128 num_nodes = 32 graph = tf.graph() graph.as_default(): # parameters: # input,forget,candidate,output gate: input, previous output, , bias. ifcox = tf.variable(tf.truncated_normal([embedding_size, num_nodes*4], -0.1, 0.1)) ifcom = tf.variable(tf.truncated_normal([num_nodes, num_nodes*4], -0.1, 0.1)) ifcob = tf.variable(tf.zeros([1, num_nodes*4])) # variables saving state across unrollings. saved_output = tf.variable(tf.zeros([batch_size, num_nodes]), trainable=false) saved_state = tf.variable(tf.zeros([batch_size, num_nodes]), trainable=false) # classifier weights , biases. w = tf.variable(tf.truncated_normal([num_nodes, 1], -0.1, 0.1)) b = tf.variable(tf.zeros([1])) # definition of cell computation. def lstm_cell(i, o, state): embeddings = tf.variable( tf.random_uniform([vocabulary_size, embedding_size], -1.0, 1.0)) embed = tf.nn.embedding_lookup(embeddings, i) = tf.to_float(embed) print (i.get_shape()) combined = tf.matmul(i, ifcox) + tf.matmul(o, ifcom) + ifcob input_gate = tf.sigmoid(combined[:, 0:num_nodes]) forget_gate = tf.sigmoid(combined[:, num_nodes:2*num_nodes]) update = tf.sigmoid(combined[:, 2*num_nodes:3*num_nodes]) state = forget_gate * state + input_gate * tf.tanh(update) output_gate = tf.sigmoid(combined[:, 3*num_nodes:4*num_nodes]) return output_gate * tf.tanh(state), state train_data = list() train_label = list() _ in range(unrollings) : train_data.append(tf.placeholder(shape=[batch_size], dtype=tf.int32)) train_label.append(tf.placeholder(shape=[batch_size, 1], dtype=tf.float32)) train_inputs = train_data[:unrollings] train_labels = train_label[:unrollings] print (train_inputs, train_labels) outputs = list() output = saved_output state = saved_state in train_inputs : output, state = lstm_cell(i, output, state) outputs.append(output) # state saving across unrollings. tf.control_dependencies([saved_output.assign(output),saved_state.assign(state)]): # classifier. logits = tf.nn.xw_plus_b(tf.concat(0, outputs), w, b) loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits, tf.to_float(tf.concat(0, train_labels)))) # optimizer. global_step = tf.variable(0) learning_rate = tf.train.exponential_decay(10.0, global_step, 5000, 0.1, staircase=true) optimizer = tf.train.adamoptimizer(learning_rate) gradients, v = zip(*optimizer.compute_gradients(loss)) gradients, _ = tf.clip_by_global_norm(gradients, 1.25) optimizer = optimizer.apply_gradients(zip(gradients, v), global_step=global_step
the error :
tensorflow/core/framework/op_kernel.cc:940] invalid argument: must feed value placeholder tensor 'placeholder_3' dtype float , shape [64,1] [[node: placeholder_3 = placeholder[dtype=dt_float, shape=[64,1], _device="/job:localhost/replica:0/task:0/cpu:0"]()]] traceback (most recent call last): file "ptb_rnn.py", line 232, in <module> _, l, predictions, lr = session.run([optimizer, loss, train_prediction, learning_rate], feed_dict=feed_dict) file "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 710, in run run_metadata_ptr) file "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 908, in _run feed_dict_string, options, run_metadata) file "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 958, in _do_run target_list, options, run_metadata) file "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 978, in _do_call raise type(e)(node_def, op, message) tensorflow.python.framework.errors.invalidargumenterror: must feed value placeholder tensor 'placeholder_3' dtype float , shape [64,1] [[node: placeholder_3 = placeholder[dtype=dt_float, shape=[64,1], _device="/job:localhost/replica:0/task:0/cpu:0"]()]] caused op u'placeholder_3', defined at: file "ptb_rnn.py", line 163, in <module> train_label.append(tf.placeholder(shape=[batch_size, 1], dtype=tf.float32)) file "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/array_ops.py", line 1212, in placeholder name=name) file "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 1530, in _placeholder name=name) file "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 703, in apply_op op_def=op_def) file "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 2317, in create_op original_op=self._default_original_op, op_def=op_def) file "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 1239, in __init__ self._traceback = _extract_stack()
it looks not feeding using feed_dict value tensor train_labels[i] size (64, 1). when print using batch_labels[i].shape size (64,1) , dtype of both float32.
train_input has size (64,) , batch_inputs has size (64, ) , both of same dtype.
so, error
in code?
p.s. think error in line reshaping batch_labels batch_labels[i] = np.reshape(batch_labels[i], (batch_size, 1))
. dimension or rank gets changed ? 1 reason train_labels[i] not accept size of batch_labels (64, 1) dimension , rank may not same.as 3-d batch_labels[i] gets converted 2-d (batch_size, 1) rank increase? below generating batch output of 1 unrolling : ([9976, 9980, 9981, 9982, 9983, 9984, 9986, 9987, 9988, 9989, 9991, 9992, 9993, 9994, 9995, 9996, 9997, 9998, 9999, 2, 9256, 1, 3, 72, 393, 33, 2133, 0, 146, 19, 6, 9207, 276, 407, 3, 2, 23, 1, 13, 141, 4, 1, 5465, 0, 3081, 1596, 96, 2, 7682, 1, 3, 72, 393, 8, 337, 141, 4, 2477, 657, 2170, 955, 24, 521, 6], [[[9980.0], [9981.0], [9982.0], [9983.0], [9984.0], [9986.0], [9987.0], [9988.0], [9989.0], [9991.0], [9992.0], [9993.0], [9994.0], [9995.0], [9996.0], [9997.0], [9998.0], [9999.0], [2.0], [9256.0], [1.0], [3.0], [72.0], [393.0], [33.0], [2133.0], [0.0], [146.0], [19.0], [6.0], [9207.0], [276.0], [407.0], [3.0], [2.0], [23.0], [1.0], [13.0], [141.0], [4.0], [1.0], [5465.0], [0.0], [3081.0], [1596.0], [96.0], [2.0], [7682.0], [1.0], [3.0], [72.0], [393.0], [8.0], [337.0], [141.0], [4.0], [2477.0], [657.0], [2170.0], [955.0], [24.0], [521.0], [6.0], [9207.0]]])
Comments
Post a Comment