【发布时间】:2017-04-14 15:42:46
【问题描述】:
您好,
我是 Python 的入门级。我已经搜索了有关 python 和 numpy 的每个文档,但没有找到。我想训练我的多变量逻辑回归模型。我有 100x2 numpy 数组用于 train_x 数据和 100x1 numpy 数组 作为 train_y 数据。我无法提供占位符。我想我无法按照占位符的需要迭代多维矩阵。
这是我的原始代码以便更好地理解:
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as 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 my 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
### I just wanted to try simple cross function as i learned in lesson ###
### I didn't get such error at this scope ###
initializer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
for k in range(total_iterator):
for (x,y) in zip(training_X,training_Y):
sess.run(initializer,feed_dict={x_i: x , y_i: y}) ### ?!??!? ###
### AT THIS SCOPE: i get error such as '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))
感谢您的回答。我想我已经将二维矩阵切片从 train_x 传递到 x_i。也许我从头到尾都错了。
【问题讨论】:
-
在调用run之前添加
x=x.reshape((1,2))和y=y.reshape((1,1))是否有效? -
不。还是谢谢你的回复
-
你能发布完整的错误信息吗?
标签: python arrays loops numpy tensorflow