2017-08-25 82 views
0

我在使用tensorflow並且正在用於學校項目。在這裏,我試圖創建一個房屋標識符,我在一張Excel表格上創建了​​一些數據,將其轉換爲一個csv文件,然後測試數據是否會被讀取。數據被讀取,但是當我進行矩陣乘法並且說...時,它會產生多個錯誤。「ValueError:形狀必須是等級2,但是'MatMul'(op:'MatMul')的等級爲0,輸入形狀爲:[] ,[1,1]。「非常感謝!矩陣乘法不起作用 - Tensorflow

import tensorflow as tf 
import os 
dir_path = os.path.dirname(os.path.realpath(__file__)) 
filename = dir_path+ "\House Price Data .csv" 
w1=tf.Variable(tf.zeros([1,1])) 
w2=tf.Variable(tf.zeros([1,1])) #Feature 1's weight 
w3=tf.Variable(tf.zeros([1,1])) #Feature 1's weight 
b=tf.Variable(tf.zeros([1])) #bias for various features 
x1= tf.placeholder(tf.float32,[None, 1]) 
x2= tf.placeholder(tf.float32,[None, 1]) 
x3= tf.placeholder(tf.float32,[None, 1]) 
Y= tf.placeholder(tf.float32,[None, 1]) 
y_=tf.placeholder(tf.float32,[None,1]) 
with tf.Session() as sess: 
    sess.run(tf.global_variables_initializer()) 
    with open(filename) as inf: 
     # Skip header 
     next(inf) 
     for line in inf: 
      # Read data, using python, into our features 
      housenumber, x1, x2, x3, y_ = line.strip().split(",") 
      x1 = float(x1) 
      product = tf.matmul(x1, w1) 
      y = product + b 
+0

它看起來像你正在覆蓋x1變量。 – Aaron

+0

來自csv文件的輸入是我想要的x1 vatiable。非常感謝你的幫助! – anonymous

+0

我在調試時將x1用作測試示例 – anonymous

回答

0

@Aaron是對的,你從csv文件加載數據時覆蓋變量。

您需要將加載的值保存到一個單獨的變量中,例如_x1而不是x1,然後使用feed_dict將值提供給佔位符。並且因爲x1的形狀爲[None,1],所以需要將字符串標量_x1轉換爲具有相同形狀的浮動,在這種情況下爲[1,1]

import tensorflow as tf 
import os 
dir_path = os.path.dirname(os.path.realpath(__file__)) 
filename = dir_path+ "\House Price Data .csv" 
w1=tf.Variable(tf.zeros([1,1])) 
b=tf.Variable(tf.zeros([1])) #bias for various features 
x1= tf.placeholder(tf.float32,[None, 1]) 

y_pred = tf.matmul(x1, w1) + b 

with tf.Session() as sess: 
    sess.run(tf.global_variables_initializer()) 
    with open(filename) as inf: 
     # Skip header 
     next(inf) 
     for line in inf: 
      # Read data, using python, into our features 
      housenumber, _x1, _x2, _x3, _y_ = line.strip().split(",") 
      sess.run(y_pred, feed_dict={x1:[[float(_x1)]]})