简体   繁体   中英

Feeding data - ValueError: Dimensions must be equal

I am using tensorflow to train a linear regression model. You can find the data at here . This is my load_data() function

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

You can find a similar sample code at here . The differences in my code are about the way of feeding tf.placeholder .

Specifically, I do not want to feed data line by line similar to the sample code . I want to feed everything at once. So, my code will look like this

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)

with fill_feed_dict() like this

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

However, when executing, the following error appears

ValueError: Dimensions must be equal, but are 0 and 42 for 'mul' (op: 'Mul') with input shapes: [0], [42].

  1. Is it possible to feed all of data at once?
  2. If so, could you guys suggest me a solution to this problem?
  1. Is it possible to feed all of data at once?

Yes, we can feed a batch (the batch can be the entire data, if there are no memory constraints).

  1. If so, could you guys suggest me a solution to this problem?

Define the placeholders that accept a batch of input, instead of single inputs :

X = tf.placeholder(tf.float32, shape=[None,1], name='X')
Y = tf.placeholder(tf.float32, shape=[None,1],name='Y')

Your code should be:

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) }) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM