如何打印一些调试?

时间:2016-04-03 17:54:51

标签: tensorflow

我用tensorflow做我的第一个脚本。 我想尝试一个简单的Logistic回归开始,我正在研究kaggle titanic数据集。

我的问题是,我无法打印一些张量来调试我做错的事情。

我读过这篇文章(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}))
我猜,我所做的一切都是基础知识。如果有人可以帮助我,我会非常棒!我有点卡住了,无法改进我的模特。如果您愿意,请不要犹豫,告诉我良好的做法:)

感谢您阅读本文!

1 个答案:

答案 0 :(得分:1)

@Ghoso在这里。

好你开始使用TensorFlow。我喜欢你的想法,从一个简单的分类器开始熟悉。

Tensorflow有这个概念,首先定义计算图。接下来你训练它。在培训之后和培训期间,您可以提供和获取您感兴趣的变量。

在此代码中,您似乎定义了Tensor"准确度"在你的会议期间。在我看来,"准确性"是图表的一部分。也就是说,"准确性"取决于图中的其他节点,可以将其视为节点。

接下来,您可以使用提取列表展开sess.run(),而不是一次提取。像这样: output_list = sess.run([train_step, accuracy], feed_dict = feed_dict)

我在网上有一个小例子,我也定义了"准确度"作为节点并在会话中获取它。您可能需要查看this示例。您感兴趣的变量名为'准​​确度'和' correct_prediction'。

我希望这有帮助!

相关问题