0
votes

I am begginer in using tensorflow and am using for a school project. Here I am attempting to make a house identifier, where I made some data on an excel sheet, turned it into a csv file and I was testing if the data would be read. The data was read but it produces multiple errors when I do the matrix multiplication and says.... "ValueError: Shape must be rank 2 but is rank 0 for 'MatMul' (op: 'MatMul') with input shapes: [], [1,1]." Thank you so much!

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
1
It looks like you are overwriting the x1 variable. - Aaron
The input from the csv file is what I wanted the x1 vatiable to be. Thanks so much for the help! - anonymous
I used the x1 as a testing example when debugging it - anonymous
I think you may need to find an intro to tensorflow tutorial. It seems like you are trying to treat tensorflow commands like regular python commands and it's not like that. - Aaron
Honestly, I am quite familiar with Java and I am new to this, but I need to do this for a school project. Can you please explain what is wrong? Thank you so much! - anonymous

1 Answers

0
votes

@Aaron is right, you are overwriting the variables when you loading the data from the csv file.

You need to save the loaded value into a separate variable, say _x1 instead of x1, and then use feed_dict to feed the value to the placeholder. And because the shape of your x1 is [None,1], you need to convert your string scalar _x1 into a float with the same shape, which is [1,1] in this case.

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)]]})