Python 送料数据-值错误:尺寸必须相等

Python 送料数据-值错误:尺寸必须相等,python,tensorflow,Python,Tensorflow,我正在使用tensorflow来训练一个线性回归模型。您可以在以下位置找到数据:。 这是我的load\u data()函数 def load_data(): book = xlrd.open_workbook(DATA_DIR, encoding_override="utf-8") sheet = book.sheet_by_index(0) data = np.asarray([sheet.row_values(i) for i in range(1, sheet.nr

我正在使用tensorflow来训练一个线性回归模型。您可以在以下位置找到数据:。 这是我的
load\u data()
函数

def load_data():
    book = xlrd.open_workbook(DATA_DIR, encoding_override="utf-8")
    sheet = book.sheet_by_index(0)
    data = np.asarray([sheet.row_values(i) for i in range(1, sheet.nrows)])
    n_samples = len(data)

    return data, n_samples
您可以在中找到类似的示例代码。我的代码中的差异是关于馈送
tf.placeholder
的方式

具体地说,我不想逐行输入数据,类似于。我想马上喂饱所有的东西。因此,我的代码将如下所示

print('Load data')
train_data, n_samples = load_data()

print('Define placeholders')
features = [tf.placeholder(tf.float32, shape=(), name='sample_' + str(i))
            for i in range(n_samples)]
labels = [tf.placeholder(tf.float32, shape=(), name='label_' + str(i))
          for i in range(n_samples)]

print('Define variables')
w = tf.Variable(tf.zeros(0.0, tf.float32))
b = tf.Variable(tf.zeros(0.0, tf.float32))

print('Define hypothesis function')
pred_labels = w * features + b

print('Define loss function')
loss = tf.square(labels - pred_label, name='loss')

print('Define optimizer function')
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.0001).minimize(loss)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    saver = tf.train.Saver(tf.trainable_variables())
    feed_dict = fill_feed_dict(train_data, features, labels)

    for i in range(100):
        __, loss_value = sess.run([optimizer, loss], feed_dict)
        print('Epoch {} has loss value {}'.format(i, loss_value))
        if i == 99:
            saver.save(sess, CKPT_DIR)
像这样使用
fill\u feed\u dict()

def fill_feed_dict(data, features, labels):
    feed_dict = {}

    for i in range(len(features)):
        feed_dict[features[i]] = data[i, 0]
        feed_dict[labels[i]] = data[i, 1]

    return feed_dict
但是,在执行时,会出现以下错误

ValueError:尺寸必须相等,但对于输入形状为[0]、[42]的“mul”(op:'mul'),尺寸分别为0和42

  • 是否可以一次提供所有数据
  • 如果是的话,你们能给我一个解决这个问题的方法吗
  • 是否可以一次提供所有数据
  • 是的,我们可以提供一个批(如果没有内存限制,批可以是整个数据)

  • 如果是的话,你们能给我一个解决这个问题的方法吗
  • 定义接受一批输入而不是单个输入的占位符:

    X = tf.placeholder(tf.float32, shape=[None,1], name='X')
    Y = tf.placeholder(tf.float32, shape=[None,1],name='Y')
    
    您的代码应该是:

    w = tf.Variable(0.0, name='weights')
    b = tf.Variable(0.0, name='bias')
    
    Y_predicted = X * w + b 
    loss = tf.reduce_mean(tf.square(Y - Y_predicted, name='loss'))
    optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001).minimize(loss)
    
    with tf.Session() as sess:
    
       sess.run(tf.global_variables_initializer()) 
    
       #train the model
       for i in range(50): # train the model 100 epochs
          #Session runs train_op and fetch values of loss
          _, l = sess.run([optimizer, loss], feed_dict={X: feed input of size (batch,1), Y: Output of size (batch,1) }) 
    
  • 是否可以一次提供所有数据
  • 是的,我们可以提供一个批(如果没有内存限制,批可以是整个数据)

  • 如果是的话,你们能给我一个解决这个问题的方法吗
  • 定义接受一批输入而不是单个输入的占位符:

    X = tf.placeholder(tf.float32, shape=[None,1], name='X')
    Y = tf.placeholder(tf.float32, shape=[None,1],name='Y')
    
    您的代码应该是:

    w = tf.Variable(0.0, name='weights')
    b = tf.Variable(0.0, name='bias')
    
    Y_predicted = X * w + b 
    loss = tf.reduce_mean(tf.square(Y - Y_predicted, name='loss'))
    optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.001).minimize(loss)
    
    with tf.Session() as sess:
    
       sess.run(tf.global_variables_initializer()) 
    
       #train the model
       for i in range(50): # train the model 100 epochs
          #Session runs train_op and fetch values of loss
          _, l = sess.run([optimizer, loss], feed_dict={X: feed input of size (batch,1), Y: Output of size (batch,1) }) 
    

    shape=(无,1)
    shape=[None,1]
    之间有什么区别吗?我刚刚尝试了这两种方法,但仍然得到了相同的预期结果。是的,它可以表示为列表或元组。在
    shape=(无,1)
    shape=[None,1]
    之间有什么区别吗?我刚刚尝试了这两种方法,但仍然得到了相同的预期结果。是的,它可以表示为列表或元组。