python - TF: Fetch argument x has invalid type <type 'numpy.float32'>, must be a string or Tensor. (Can not convert a float32 into a Tensor or Operation.) -
i have time series model in tf. it's simple auto-regressive model.
the original y
vector of length 100 (n
).
i float not tensor error (as per subject). @ second instance though.
lr = .01 steps = 100 def net(x, w, b): # x has 2 previous values x = [x[-1], x[-2], x[-1] - x[-2]] x = tf.reshape(x, [1, 3]) x = tf.add(tf.matmul(x, w[0]), b[0]) pred = tf.add(tf.matmul(x, w[1]), b[1]) return pred y_data = y - np.mean(y) x = tf.placeholder(tf.float32, [2], name='x') y = tf.placeholder(tf.float32, [1], name='y') w = [tf.variable(tf.random_normal([3, 3])), tf.variable(tf.random_normal([3, 1]))] b = [tf.variable(tf.random_normal([1])), tf.variable(tf.random_normal([1]))] pred = net(x, w, b) cost = tf.sqrt(tf.reduce_mean(tf.square(tf.subtract(pred, y)))) optimizer = tf.train.adamoptimizer(learning_rate=lr).minimize(cost) init = tf.global_variables_initializer() tf.session() sess: sess.run(init) step in range(steps): # random samples of data ts = np.random.choice(np.arange(2, n), int(n * .5), replace=false) t in ts: x_data = [y_data[t - 2], y_data[t - 1]] y_data_cur = [y_data[t]] print(x_data, y_data_cur, x, y, pred) _, cost, p = sess.run([optimizer, cost, pred], feed_dict={x: x_data, y: y_data_cur}) print(cost, p) if step % 10 == 0: print(step, cost)
when run model:
_, cost, p = sess.run([optimizer, cost, pred], feed_dict={x: x_data, y: y_data_cur})
you overwriting cost
variable, used hold tensorflow tensor cost, evaluated value, next iteration fails. change name of variable:
_, cost_val, p = sess.run([optimizer, cost, pred], feed_dict={x: x_data, y: y_data_cur})
and of course replace cost
cost_val
in print
statements.
Comments
Post a Comment