2017-04-20 106 views
1

我想用tensorflow實現多變量線性迴歸。我有一個包含200行和3列(功能)的csv文件,最後一列作爲輸出。事情是這樣的:enter image description here使用Tensorflow的多變量線性迴歸

我寫了下面的下面的代碼:

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 
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: Shape must be rank 2 but is rank 0 for 'MatMul' (op: 'MatMul') with input shapes: [], [3,?].

你能幫我解決這個問題嗎?

在此先感謝。

回答

1

tf.variable不採取投入如你所想,第二個參數是沒有成型。要設置變量的形狀,請使用初始值設定項(第一個參數)。看到https://www.tensorflow.org/api_docs/python/tf/Variable

您的代碼

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

在回答固定下面的代碼運行最初的問題後出現的其他問題 - 但可能仍然存在一些邏輯你需要考慮的錯誤 - 比如你的#display記錄每個時期的步驟。

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)) 
+0

嘿,我想你的解決方案,我得到了以下錯誤: 'ValueError異常:張量 '佔位符:0' 不能喂形狀(3)的值(?3),它具有形狀」 ' –

+0

這是一個不同的問題,我需要重新發布所有代碼來回答 - 而不僅僅是與此問題相關的代碼段。您現在的問題是您的輸入x1和y與您的佔位符形狀不匹配。檢查這些形狀,你會看到形狀x1是(3,),y是一個浮動。使x1 = [x1]和y = [y1],你會發現形狀現在爲x(1,3)和y(1,)。但是你也會發現你的X = np.column_stack行應該是X = np.column_stack((X1,X2,X3)),它改變了你的X_1佔位符的形狀,並且你的初始化器也是你輸入的不正確形狀。 –

+0

我會修改我的答案以包含其他更改,但是您是否也可以修改您的問題以添加額外問題以確保完整性。 –