我使用张量流来训练线性回归模型。您可以在here找到数据。
这是我的load_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
您可以在here找到类似的示例代码。我的代码中的差异是关于喂养tf.placeholder
的方式。
具体来说,我不想要提供类似于sample code的逐行数据。我想一次喂一切。所以,我的代码看起来像这样
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_feed_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:尺寸必须相等,但对于' mul'是0和42。 (op:' Mul')输入形状:[0],[42]。
答案 0 :(得分:2)
- 是否可以一次提供所有数据?
醇>
是的,我们可以提供批处理(如果没有内存限制,批处理可以是整个数据)。
- 如果是这样,你们可以建议我解决这个问题吗?
醇>
定义接受一批输入而不是单个输入的占位符:
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) })