i'm getting value error:valueerror: setting array element sequence, when doing back_prop in tensorflow. i'm using large imdb dataset , glove 50d pre-trained vectors. have tried converting multi-dimensional list np.array, converting individual lists np.array , did reshape operation x = x.reshape((batch,time_steps,embedding))
on x gave me value error valueerror: total size of new array must unchanged. think wrong input don't know what? run code on pc downloading imdb dataset , 50d glove vectors. please help!
traceback (most recent call last): file "nlp.py", line 109, in <module> sess.run(minimize_loss,feed_dict={x : x, y : y}) file "/home/indy/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 372, in run run_metadata_ptr) file "/home/indy/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 619, in _run np_val = np.array(subfeed_val, dtype=subfeed_dtype) valueerror: setting array element sequence. __future__ import absolute_import __future__ import division __future__ import print_function import tensorflow tf import numpy np import math import os nltk.tokenize import tweettokenizer batch = 500 start = 0 end = batch - 1 learning_rate = 0.2 num_classes = 8 path = "/home/indy/downloads/aclimdb/train/pos" time_steps = 250 embedding = 50 def get_embedding(): gfile_path = os.path.join("/home/indy/downloads/glove.6b", "glove.6b.50d.txt") f = open(gfile_path,'r') embeddings = {} line in f: sp_value = line.split() word = sp_value[0] embedding = [float(value) value in sp_value[1:]] embeddings[word] = embedding return embeddings ebd = get_embedding() def get_y(file_name): y_value = file_name.split('_') y_value = y_value[1].split('.') return y_value[0] def get_x(path,file_name): file_path = os.path.join(path,file_name) x_value = open(file_path,'r') line in x_value: x_value = line.replace("<br /><br />","") x_value = x_value.lower() tokeniz = tweettokenizer() x_value = tokeniz.tokenize(x_value) padding = 250 - len(x_value) if padding > 0: p_value = ['pad' in range(padding)] x_value = np.concatenate((x_value,p_value)) x_value = [ebd['value'] value in x_value] return x_value def batch_f(path): directory = os.listdir(path) y = [get_y(directory[i]) in range(len(directory))] x = [get_x(path,directory[i]) in range(len(directory))] return x,y x = tf.placeholder(tf.float32, [batch,time_steps,embedding]) y = tf.placeholder(tf.int32, [batch]) def build_nlp_model(x, _units, lstm_layers,num_classes): x = tf.transpose(x, [1, 0, 2]) x = tf.reshape(x, [-1, embedding]) x = tf.split(0, time_steps, x) lstm = tf.nn.rnn_cell.lstmcell(num_units = _units, state_is_tuple = true) multi_lstm = tf.nn.rnn_cell.multirnncell([lstm] * lstm_layers, state_is_tuple = true) outputs , state = tf.nn.rnn(multi_lstm,x, dtype = tf.float32) weights = tf.variable(tf.random_normal([_units,num_classes])) biases = tf.variable(tf.random_normal([num_classes])) logits = tf.matmul(outputs[-1], weights) + biases return logits logits = build_nlp_model(x,400,4,num_classes) c_loss = tf.nn.sparse_softmax_cross_entropy_with_logits(logits,y) loss = tf.reduce_mean(c_loss) decayed_learning_rate = tf.train.exponential_decay(learning_rate,0,10000,0.9) optimizer= tf.train.adamoptimizer(decayed_learning_rate) minimize_loss = optimizer.minimize(loss) correct_predict = tf.nn.in_top_k(logits, y, 1) accuracy = tf.reduce_mean(tf.cast(correct_predict, tf.float32)) init = tf.initialize_all_variables() tf.session() sess: sess.run(init) in range(25): x, y = batch_f(path) sess.run(minimize_loss,feed_dict={x : x, y : y}) accu = sess.run(accuracy,feed_dict = {x: x, y: y}) cost = sess.run(loss,feed_dict = {x: x,y: y}) start = end end = (start + batch) print ("minibatch loss = " + "{:.6f}".format(cost) + ", training accuracy= " + "{:.5f}".format(accu))
edit: other error i'm getting, when run code.
(500, 250, 50) (500,) traceback (most recent call last): file "nlp.py", line 115, in <module> accu = sess.run(accuracy,feed_dict = {x: x, y: y}) file "/home/indy/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 372, in run run_metadata_ptr) file "/home/indy/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 636, in _run feed_dict_string, options, run_metadata) file "/home/indy/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 708, in _do_run target_list, options, run_metadata) file "/home/indy/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 728, in _do_call raise type(e)(node_def, op, message) tensorflow.python.framework.errors.invalidargumenterror: targets[0] out of range [[node: intopk = intopk[t=dt_int32, k=1, _device="/job:localhost/replica:0/task:0/cpu:0"](add, _recv_placeholder_1_0)]] caused op u'intopk', defined at: file "nlp.py", line 102, in <module> correct_predict = tf.nn.in_top_k(logits, y, 1) file "/home/indy/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/ops/gen_nn_ops.py", line 890, in in_top_k targets=targets, k=k, name=name) file "/home/indy/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/ops/op_def_library.py", line 704, in apply_op op_def=op_def) file "/home/indy/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 2260, in create_op original_op=self._default_original_op, op_def=op_def) file "/home/indy/tensorflow/local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1230, in __init__ self._traceback = _extract_stack()
Comments
Post a Comment