【问题标题】:Iterate over Numpy array for Tensorflow迭代 Tensorflow 的 Numpy 数组
【发布时间】: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


【解决方案1】:

问题是循环中的x,y 是一维的,而占位符是二维的。 (注意您将占位符定义为tf.placeholder(tf.float32,[None,2]),它定义了一个二维占位符。这样做是为了进行批量优化和计算)。

最快的解决方案是重塑xy

sess.run(initializer,feed_dict={x_i: np.reshape(x, [1,-1]),
                                y_i: np.reshape(y, [1, -1])})

【讨论】:

  • 它似乎有效,非常感谢。现在我无法打印成本函数:print("Iteration: ",k, "cost:", sess.run(cost,feed_dict={x_i: numpy.reshape(training_X, [1,-1]),y_i:numpy.reshape(training_Y, [1, -1])
  • ValueError: 无法为形状为“(?, 2)”的张量“Placeholder:0”提供形状 (1, 200) 的值
  • 您只需要重塑x,yX_trainingy_training 的形状已经正确(100x2100x1)(placeholder 定义中的 None 使其接受沿第一轴的任何尺寸)
  • 我试过但它返回 [nan]。即使我已经测试了整个成本和模型变量。x_i:training_X,y_i:training_Y 根本没有发送任何值。我错了吗?
  • 这是一个不同的问题。您的数据开始时包含“nan”,或者计算返回nan。通常,(至少对我而言),如果是第二种情况,降低您的 learning_rate 可能会有所帮助。
猜你喜欢
  • 2018-11-05
  • 1970-01-01
  • 2013-01-04
  • 2015-12-12
  • 1970-01-01
  • 2013-05-05
  • 2011-10-03
  • 2016-01-28
  • 1970-01-01
相关资源
最近更新 更多