python - I can't figure out how to feed a Placeholder by data from matlab -
i trying implement simple feed forward network. however, can't figure out how feed placeholder data matlab. example:
import tensorflow tf import numpy np import scipy.io scio import math # # create data train_input=scio.loadmat('/users/liutianyuan/desktop/image_restore/data/input_for_tensor.mat') train_output=scio.loadmat('/users/liutianyuan/desktop/image_restore/data/output_for_tensor.mat') x_data=np.float32(train_input['input_for_tensor']) y_data=np.float32(train_output['output_for_tensor']) print x_data.shape print y_data.shape ## create tensorflow structure start ### def add_layer(inputs, in_size, out_size, activation_function=none): weights = tf.variable(tf.random_uniform([in_size,out_size], -4.0*math.sqrt(6.0/(in_size+out_size)), 4.0*math.sqrt(6.0/(in_size+out_size)))) biases = tf.variable(tf.zeros([1, out_size])) wx_plus_b = tf.matmul(inputs, weights) + biases if activation_function none: outputs = wx_plus_b else: outputs = activation_function(wx_plus_b) return outputs xs = tf.placeholder(tf.float32, [none, 256]) ys = tf.placeholder(tf.float32, [none, 1024]) y= add_layer(xs, 256, 1024, activation_function=none) loss = tf.reduce_mean(tf.square(y - ys)) optimizer = tf.train.gradientdescentoptimizer(0.1) train = optimizer.minimize(loss) init = tf.initialize_all_variables() ### create tensorflow structure end ### sess = tf.session() sess.run(init) step in range(201): sess.run(train) if step % 20 == 0: print(step, sess.run(loss,feed_dict={xs: x_data, ys: y_data}))
gives me following error:
/usr/local/cellar/python/2.7.12_2/frameworks/python.framework/versions/2.7/bin/python2.7 /users/liutianyuan/pycharmprojects/untitled1/easycode.py (1, 256) (1, 1024) traceback (most recent call last): file "/users/liutianyuan/pycharmprojects/untitled1/easycode.py", line 46, in <module> sess.run(train) file "/library/python/2.7/site-packages/tensorflow/python/client/session.py", line 340, in run run_metadata_ptr) file "/library/python/2.7/site-packages/tensorflow/python/client/session.py", line 564, in _run feed_dict_string, options, run_metadata) file "/library/python/2.7/site-packages/tensorflow/python/client/session.py", line 637, in _do_run target_list, options, run_metadata) file "/library/python/2.7/site-packages/tensorflow/python/client/session.py", line 659, in _do_call e.code) tensorflow.python.framework.errors.invalidargumenterror: **you must feed value placeholder tensor 'placeholder' dtype float** [[node: placeholder = placeholder[dtype=dt_float, shape=[], _device="/job:localhost/replica:0/task:0/cpu:0"]()]] caused op u'placeholder', defined at: file "/users/liutianyuan/pycharmprojects/untitled1/easycode.py", line 30, in <module> xs = tf.placeholder(tf.float32, [none, 256]) file "/library/python/2.7/site-packages/tensorflow/python/ops/array_ops.py", line 762, in placeholder name=name) file "/library/python/2.7/site-packages/tensorflow/python/ops/gen_array_ops.py", line 976, in _placeholder name=name) file "/library/python/2.7/site-packages/tensorflow/python/ops/op_def_library.py", line 655, in apply_op op_def=op_def) file "/library/python/2.7/site-packages/tensorflow/python/framework/ops.py", line 2154, in create_op original_op=self._default_original_op, op_def=op_def) file "/library/python/2.7/site-packages/tensorflow/python/framework/ops.py", line 1154, in __init__ self._traceback = _extract_stack()
i have checked both type , shape of x_data , y_data, seams corret. have no ideal goes wrong.
your train
operation depends on placeholders xs
, ys
, must feed values placeholders when call sess.run(train)
.
a common way divide input data mini-batches:
batch_size = ... step in range(201): # n.b. you'll need code handle cases start_index and/or end_index # wrap around end of x_data , y_data. start_index = step * batch_size end_index = (step + 1) * batch_size start_index = sess.run(train, {xs: x_data[start_index:end_index,:], ys: y_data[start_index:end_index,:]})
the code in example started. more flexible way of generating data feeding, see mnist dataset example in tf.learn
codebase.
Comments
Post a Comment