2017-08-25 7 views
0

私はテンソルフローを使用していますが、学校のプロジェクトに使用しています。ここでは、ハウスIDを作成しようとしています。ここで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が可能であることを望んでいました。助けてくれてありがとう! – 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)]]})