我正在做我的第一個腳本與tensorflow。 我想嘗試一個簡單的邏輯迴歸來開始,我正在研究kaggle泰坦尼克號數據集。如何打印一些調試?
我的問題是,我無法打印一些張量來調試我做錯的事情。
我看了這篇文章(How to print the value of a Tensor object in TensorFlow?),但我不明白我怎麼打印張量。 :(
我敢肯定是接近,但無法弄清楚
讓我告訴你我在做什麼;
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_)
需要在這裏印
**#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}))
我所做的一切都是基礎知識,如果有人能幫助我,我會變得非常棒!我有點卡住了,無法保護我的模型不要猶豫,告訴我好的做法,如果你覺得: )
感謝您閱讀本文!
謝謝!這很容易,我錯過了。我試過了,它imediatly工作! 我按照你的建議使用了print(sess.run([accuracy,y,correct_prediction],feed_dict = {x:test_input,y_:test_label})),現在我可以看到我的所有數據。 我知道我的y向量充滿nan,我可以繼續挖掘,看看有什麼不對。 非常感謝您對張量流動力學的簡單解釋! –