Python 3.x 使用Tensorflow的多变量线性回归

Python 3.x 使用Tensorflow的多变量线性回归,python-3.x,tensorflow,linear-regression,Python 3.x,Tensorflow,Linear Regression,我正在尝试使用tensorflow实现多变量线性回归。我有一个csv文件,有200行3列(功能),最后一列作为输出。大概是这样的: 我编写了以下代码: from __future__ import print_function import tensorflow as tf import numpy as np import matplotlib.pyplot as plt import csv import pandas rng = np.random # Parameters lear

我正在尝试使用tensorflow实现多变量线性回归。我有一个csv文件,有200行3列(功能),最后一列作为输出。大概是这样的:

我编写了以下代码:

from __future__ import print_function

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import csv
import pandas
rng = np.random


# Parameters
learning_rate = 0.01
training_epochs = 1000
display_step = 50
我使用pandas从文件中获取数据并将其存储:

# Training Data
dataframe = pandas.read_csv("Advertising.csv", delim_whitespace=True, header=None)
dataset = dataframe.values

X1,X2,X3,y1 = [],[],[],[]
for i in range(1,len(dataset)):
    X = dataset[i][0]
    X1.append(np.float32(X.split(",")[1]))
    X2.append(np.float32(X.split(",")[2]))
    X3.append(np.float32(X.split(",")[3]))
    y1.append(np.float32(X.split(",")[4]))
X = np.column_stack((X1,X2))
X = np.column_stack((X,X3))
我分配占位符、变量和线性回归模型:

n_samples = len(X1)
#print(n_samples) = 17
# tf Graph Input
X_1 = tf.placeholder(tf.float32, [3, None])
Y = tf.placeholder(tf.float32, [None])

# Set model weights
W1 = tf.Variable(rng.randn(), [n_samples,3])
b = tf.Variable(rng.randn(), [n_samples])



# Construct a linear model
pred = tf.add(tf.matmul(W1, X_1), b)

# Mean squared error
cost = tf.reduce_sum(tf.pow(pred-Y, 2))/(2*n_samples)
# Gradient descent
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

# Initializing the variables
init = tf.global_variables_initializer()

# Launch the graph
with tf.Session() as sess:
    sess.run(init)

    # Fit all training data
    for epoch in range(training_epochs):
        for (x1, y) in zip(X, y1):
            sess.run(optimizer, feed_dict={X_1: x1, Y: y})
        # Display logs per epoch step
        if (epoch+1) % display_step == 0:
            c = sess.run(cost, feed_dict={X_1: x1, Y: y})
            print("Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(c), \
                "Weights=", sess.run(W1),"b=", sess.run(b))
我遇到以下无法调试的错误:

ValueError:形状必须为秩2,但对于“MatMul”(op: “MatMul”)与输入形状:[],[3],]

你能帮我解决这个问题吗


提前感谢。

您需要将矩阵馈送到
tf.matmul(W1,X_1)
。检查您的
W1
X_1
代码的类型


如需了解更多详细信息,tf.variable不会像您所想的那样接受输入,第二个参数不是shape。要设置变量的形状,可以使用初始值设定项(第一个参数)进行设置。看

你的代码

# Set model weights
W1 = tf.Variable(rng.randn(), [n_samples,3])
b = tf.Variable(rng.randn(), [n_samples])
我建议的改变

initial1 = tf.constant(rng.randn(), dtype=tf.float32, shape=[n_samples,3])
initial2 = tf.constant(rng.randn(), dtype=tf.float32, shape=[n_samples,3])
W1 = tf.Variable(initial_value=initial1) 
b = tf.Variable(initial_value=initial2)
为了回答在修复初始问题后出现的其他问题,将运行以下代码-但仍可能存在一些需要考虑的逻辑错误-例如“每一步显示日志”

from __future__ import print_function

import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import csv
import pandas
rng = np.random


# Parameters
learning_rate = 0.01
training_epochs = 1000
display_step = 50
# Training Data
#Created some fake data
dataframe = [[230.1,37.8,69.2,22.1],[2230.1,32.8,61.2,21.1]] #pandas.read_csv("Advertising.csv", delim_whitespace=True, header=None)
dataset = dataframe

X1,X2,X3,y1 = [],[],[],[]
for i in range(0,len(dataset)):
    X = dataset[i][0]
    X1.append(np.float32(dataset[i][0]))
    X2.append(np.float32(dataset[i][1]))
    X3.append(np.float32(dataset[i][2]))
    y1.append(np.float32(dataset[i][3]))
#X=np.array([X1,X2,X3])
X = np.column_stack((X1,X2,X3)) ##MYEDIT: This combines all three values. If you find you need to stack in a different way then you will need to ensure the shapes below match this shape.
#X = np.column_stack((X,X3))

n_samples = len(X1)
#print(n_samples) = 17
# tf Graph Input
X_1 = tf.placeholder(tf.float32, [ None,3])##MYEDIT: Changed order
Y = tf.placeholder(tf.float32, [None])
# Set model weights
initial1 = tf.constant(rng.randn(), dtype=tf.float32, shape=[3,1]) ###MYEDIT: change order and you are only giving 1 sample at a time with your method of calling
initial2 = tf.constant(rng.randn(), dtype=tf.float32, shape=[3,1])
W1 = tf.Variable(initial_value=initial1)
b = tf.Variable(initial_value=initial2)


mul=tf.matmul(W1, X_1)   ##MYEDIT: remove matmul from pred for clarity and shape checking
# Construct a linear model
pred = tf.add(mul, b)

# Mean squared error
cost = tf.reduce_sum(tf.pow(pred-Y, 2))/(2*n_samples)
# Gradient descent
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost)

# Initializing the variables
init = tf.global_variables_initializer()

# Launch the graph
with tf.Session() as sess:
    sess.run(init)

    # Fit all training data
    for epoch in range(training_epochs):
        for (x1, y) in zip(X, y1):
            Xformatted=np.array([x1])  #has shape (1,3)  #MYEDIT: separated this to demonstrate shapes
            yformatted=np.array([y])  #shape (1,)  #MYEDIT: separated this to demonstrate shapes
                                                    #NB. X_1 shape is (?,3)   and Y shape is (?,)
            sess.run(optimizer, feed_dict={X_1: Xformatted, Y: yformatted})
        # Display logs per epoch step
        if (epoch+1) % display_step == 0:
            c = sess.run(cost, feed_dict={X_1: Xformatted, Y: yformatted})   #NB. x1 an y are out of scope here - you will only get the last values. Double check if this is what you meant.
            print("Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(c), \
                "Weights=", sess.run(W1),"b=", sess.run(b))

嘿,我尝试了你的解决方案,我得到了以下错误:
ValueError:Cannot feed value of shape(3),for Tensor'Placeholder:0',它有shape'(3,?),这是一个不同的问题,我需要重新发布你的所有代码来回答,而不仅仅是与这个问题相关的代码片段。现在的问题是输入x1和y与占位符形状不匹配。检查这些形状,您将看到形状x1为(3,),y为浮点数。设x1=[x1]和y=[y1],你会发现现在的形状是x as(1,3)和y(1,)。但是你也会发现你的行X=np.column_stack应该是X=np.column_stack((X1,X2,X3)),它改变了你的X_1占位符的形状,并且你的初始值设定项对于你的输入来说也是不正确的形状。我将修改我的答案以包括额外的更改,但是,为了完整起见,请您也修改一下您的问题,添加额外的问题。我非常感谢您的帮助。我是tensorflow的新手,很抱歉弄错了。谢谢!!!我的荣幸。不断提问,不断选择答案,有很多人乐于帮助。