哪个是我模型中线性回归的正确形状?

问题描述 投票:0回答:1

我正在设计回归网络,以预测一个人的体重从10到100公斤。我的数据集有50个训练数据

Vector 1: 1024x1 corresponding to 40kg
Vector 2: 1024x1 corresponding to 20kg
Vector 3: 1024x1 corresponding to 40kg
...
Vector 50: 1024x1 corresponding to 30kg

因此,我的数据集大小是1024x50,标签大小是1x50。如果我设计一个简单的线性回归,如y=xW+b,那么Wb的大小将是

W is 1024x1
b is 1x50  

我对吗?

这是我的张量流代码,但它提供了错误的预测

# Training Data
train_X = ...# shape of 1024 x 50
train_Y = ...# shape of 1x50
n_samples = 50
learning_rate = 0.0001
training_epochs = 1000
display_step = 50
# tf Graph Input
X = tf.placeholder("float")
Y = tf.placeholder("float")
# Set model weights
W = tf.Variable(tf.truncated_normal([1024, 1], mean=0.0, stddev=1.0, dtype=tf.float32))
b = tf.Variable(tf.zeros(1, dtype = tf.float32))
# Construct a linear model
pred = tf.add(tf.multiply(X, W), b)
# Mean squared error
cost = tf.reduce_sum(tf.pow(pred-Y, 2))/(2*n_samples)
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

init = tf.global_variables_initializer()
# Start training
with tf.Session() as sess:
    # Run the initializer
    sess.run(init)
    # Fit all training data
    for epoch in range(training_epochs):
        for (x, y) in zip(train_X, train_Y):
            sess.run(optimizer, feed_dict={X: x, Y: y})
        # Display logs per epoch step
        if (epoch + 1) % display_step == 0:
            c = sess.run(cost, feed_dict={X: train_X, Y: train_Y})
            print("Epoch:", '%04d' % (epoch + 1), "cost=", "{:.9f}".format(c), \
                  "W=", sess.run(W), "b=", sess.run(b))
    print("Optimization Finished!")
tensorflow deep-learning
1个回答
1
投票

W是1024x1 b是1x50 我对吗?

不,W的形状是正确的,但b应该是标量(1x1矩阵)。在您的方法中,每个数据点都有一个可训练的偏差,这是没有意义的。但是,在您的代码中,它已正确设置为大小1。

处理矩阵乘法有什么问题,你的模型应该是:

pred = tf.matmul(X, W) + b # you will have to transpose your train_X

tf.multiply是逐点乘法,而不是矩阵乘法。

© www.soinside.com 2019 - 2024. All rights reserved.