python - Iterate over Numpy array for Tensorflow -
hello,
i entry level in python.i have searched every doc on python , numpy didn't find.i want train multivariable logistic regression model.i have 100x2 numpy array train_x data , 100x1 numpy array train_y data.i couldn't feed placeholders.i think can't able iterate multidimensional matrix placeholder wants.
here raw code better understand:
import matplotlib.pyplot plt import tensorflow tf import numpy numpy learning_rate = 0.01 total_iterator = 1500 display_per = 100 data = numpy.loadtxt("ex2data1.txt",dtype=numpy.float32,delimiter=","); training_x = numpy.asarray(data[:,[0,1]]) # 100 x 2 training_y = numpy.asarray(data[:,[2]],dtype=numpy.int) # 100 x 1 m = data.shape[0] # thats sample size = 100 x_i = tf.placeholder(tf.float32,[none,2]) # n x 2 y_i = tf.placeholder(tf.float32,[none,1]) # n x 1 w = tf.variable(tf.zeros([2,1])) # 2 x 1 b = tf.variable(tf.zeros([1,1])) # 1 x 1 h = tf.matmul(w,x_i)+b cost = tf.reduce_sum(tf.add(tf.multiply(y_i,tf.log(h)),tf.multiply(1-y_i,tf.log(1-h)))) / -m ### wanted try simple cross function learned in lesson ### ### didn't such error @ scope ### initializer = tf.train.gradientdescentoptimizer(learning_rate).minimize(cost) init = tf.global_variables_initializer() tf.session() sess: sess.run(init) k in range(total_iterator): (x,y) in zip(training_x,training_y): sess.run(initializer,feed_dict={x_i: x , y_i: y}) ### ?!??!? ### ### @ scope: error such 'can't feed, ### placeholder:0'### if k % display_per==0: print("iteration: ",k, "cost: ", sess.run(cost,feed_dict={x_i:training_x,y_i:training_y}),"w: ",sess.run(w),\ "b: ",sess.run(b)) print("optim. finished") print("iteration: ", k, "cost: ", sess.run(cost, feed_dict={x_i: training_x, y_i: training_y}), "w: ", sess.run(w), \ "b: ", sess.run(b))
thanks answers.i think have pass 2 dimensional matrix slice train_x x_i.maybe wrong start end.
the problem x
,y
in loop 1-d, while placeholder 2-d. (note define placeholder tf.placeholder(tf.float32,[none,2])
, defines 2-d placeholder. done in order optimization , calculations in batches).
the quickest solution reshape x
, y
:
sess.run(initializer,feed_dict={x_i: np.reshape(x, [1,-1]), y_i: np.reshape(y, [1, -1])})
Comments
Post a Comment