Reputation: 35
I'm doing my first script with tensorflow. I wanted to try a simple Logistic regression to start, i'm working on kaggle titanic dataset.
My problem is that i'm not able to print some tensor to debug things that i'm doing wrong.
i read this post (How to print the value of a Tensor object in TensorFlow?) but i did not understand how i can print tensors. :(
I'm pretty sure to be close but can't figure it out
Let me show you what i am doing;
train = pd.read_csv("./titanic_data/train.csv", dtype={"Age": np.float64}, )
# My parameters for start
train_input = train[['Pclass','Age','SibSp','Parch','Fare']];
train_label = train['Survived']
train_label = train_label.reshape(891, 1)
#split my dataset
test_input = train_input[800:891]
test_label = train_label[800:891]
train_input = train_input[0:800]
train_label = train_label[0:800]
x = tf.placeholder(tf.float32, [None, 5]) #placeholder for input data
W = tf.Variable(tf.zeros([5, 1])) #weight for softmax
b = tf.Variable(tf.zeros([1])) # bias for softmax
y = tf.nn.softmax(tf.matmul(x, W) + b) #our model -> pred from model
y_ = tf.placeholder(tf.float32, [None, 1])#placeholder for input
cross_entropy = -tf.reduce_sum(y_*tf.log(y)) # crossentropy cost function
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
init = tf.initialize_all_variables() # create variable
sess = tf.InteractiveSession()
sess.run(init)
testacc = []
trainacc = []
for i in range(15):
batch_xs = train_input[i*50:(i + 1) * 50]
batch_ys = train_label[i*50:(i + 1) * 50]
result = sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
correct_prediction = tf.equal(y,y_)
want to print here
**#correct_prediction.eval()
#trying to print correct_prediction or y so i can see what is my model actualy doing**
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
trainacc.append(sess.run(accuracy, feed_dict={x: train_input, y_: train_label}))
testacc.append(sess.run(accuracy, feed_dict={x: test_input, y_: test_label}))
everything i'm doing is basics i guess. If somebody can help me i would be so great ! i'm kinda stuck and can't imporve my model. Don't hesitate to tell me good practice if you feel like :)
Thanks for reading this !
Upvotes: 2
Views: 1178
Reputation: 863
@Ghoso here.
Good you are starting on TensorFlow. I like your idea to start with a simple classifier to get familiar.
Tensorflow has this concept where first you define a computational graph. Next you train it. After and during training, you can feed and fetch your variables of interest.
In this code, you seem to define a Tensor "accuracy" during your session. In my opinion, "accuracy" is a part of your graph. That is, "accuracy" depends on other nodes in the graph and can be considered itself a node.
Next, you can expand sess.run()
with a list of fetches, rather than one fetch. Like this:
output_list = sess.run([train_step, accuracy], feed_dict = feed_dict)
I have a small example online where I also define "accuracy" as a node and fetch it in a session. You might want to look at this example. The variables of your interest are named 'accuracy' and 'correct_prediction'.
I hope this helps!
Upvotes: 1