使用 TensorFlow 计算多元回归

Calculating Multivariate regression using TensorFlow

我正在尝试在 tensorflow 中实现多元回归,其中我有 192 个示例,其中包含 6 个特征和一个输出变量。从我的模型中我得到一个矩阵 (192, 6) 而它应该是 (192, 1)。有人知道我的代码有什么问题吗?我在下面提供了我的代码。

# Parameters
learning_rate = 0.0001
training_epochs = 50
display_step = 5

train_X = Data_ABX3[0:192, 0:6]
train_Y = Data_ABX3[0:192, [24]]


# placeholders for a tensor that will be always fed.
X = tf.placeholder('float', shape = [None, 6])
Y = tf.placeholder('float', shape = [None, 1])


# Training Data

n_samples = train_Y.shape[0]


# Set model weights
W = tf.cast(tf.Variable(rng.randn(1, 6), name="weight"), tf.float32)
b = tf.Variable(rng.randn(), name="bias")

# 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)
# Gradient descent
#  Note, minimize() knows to modify W and b because Variable objects are       trainable=True by default
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

# Accuracy
# #accuracy = tf.contrib.metrics.streaming_accuracy(Y, pred)

# Initialize the variables (i.e. assign their default value)
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: train_X, Y: train_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!")
    #training_cost = 0
    #for (x, y) in zip(train_X, train_Y):
    #     tr_cost = sess.run(cost, feed_dict={X: x, Y: y})
    #     training_cost += tr_cost
    training_cost = sess.run(cost, feed_dict={X: train_X, Y: train_Y})
    print("Training cost=", training_cost, "W=", sess.run(W), "b=", sess.run(b), '\n')

    # Graphic display
    plt.plot(train_Y, train_X * sess.run(W) + sess.run(b), label='Fitted line')
    plt.legend()
    plt.show()

请在 pred 等式中使用 tf.matmul 而不是 tf.multiplytf.multiply 执行逐元素乘法,因此,它将生成与 train_X 维度相同的矩阵,而 tf.matmul 将执行矩阵乘法,并根据实际矩阵乘法生成结果矩阵规则。

我不确定你的数据是什么。添加随机数据然后更改代码以满足所有维度要求。如果您能帮助我实现您的意图,那将有助于更好地了解问题。

编辑

import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
# Parameters
learning_rate = 0.0001
training_epochs = 50
display_step = 5

Data_ABX3 = np.random.random((193, 8)).astype('f')

train_X = Data_ABX3[0:192, 0:6]
train_Y = Data_ABX3[0:192, [7]]


# placeholders for a tensor that will be always fed.
X = tf.placeholder('float32', shape = [None, 6])
Y = tf.placeholder('float32', shape = [None, 1])

# Training Data
n_samples = train_Y.shape[0]

# Set model weights
W = tf.cast(tf.Variable(np.random.randn(6, 1), name="weight"), tf.float32)
b = tf.Variable(np.random.randn(), name="bias")

mult_node = tf.matmul(X, W)
print(mult_node.shape)
# Construct a linear model
pred = tf.add(tf.matmul(X, W), b)

# Mean squared error
cost = tf.reduce_sum(tf.pow(pred-Y, 2))/(2*n_samples)
# Gradient descent
#  Note, minimize() knows to modify W and b because Variable objects are               trainable=True by default
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

# Accuracy
# #accuracy = tf.contrib.metrics.streaming_accuracy(Y, pred)

# Initialize the variables (i.e. assign their default value)
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: train_X, Y: train_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!")
#training_cost = 0
#for (x, y) in zip(train_X, train_Y):
#     tr_cost = sess.run(cost, feed_dict={X: x, Y: y})
#     training_cost += tr_cost
training_cost = sess.run(cost, feed_dict={X: train_X, Y: train_Y})
print("Training cost=", training_cost, "W=", sess.run(W), "b=", sess.run(b), '\n')

line = sess.run(tf.add(tf.matmul(train_X, W), b))
# Graphic display
plt.plot(train_Y, line, label='Fitted line')
plt.legend()
plt.show()`