python tensorflow:如何避免给每个sess.run()提供feed_dict?

时间:2017-12-12 00:22:57

标签: python tensorflow

假设我想使用KMeans方法找到质心。

对于第一次迭代,我想使用initFirstCentroids从数据集X中获取一些样本数据。

在第二次迭代之前,用户可以看到第一次迭代结果并输入第二次迭代的初始质心。

在静态图中,输入定义为tf.placholder(tf.float32,shape =(None,D))。由于点数未知,我无法将其作为tf.Variable

当我需要调用sess.run()来计算时,它总是要求我提供feed_dict,但实际上我的数据集没有改变。我可以只输入一次(比如使用tf.Variable)

# k=3, centroids is a tf.Variable 
dataset=np.array([[1.,2.],[3.,4.],[5.,6.],[7.,8.],[2.5,5.2],[6.6,4.4]])
With tf.Session() as sess:
    # the feed_dict is the same for the entire session. can I just input once?
    sess.run(init)
    sess.run(initFirstCentroids, feed_dict={X:dataset})
    for i in range(10):
        centroids_value=sess.run(iter_centroids,feed_dict={X:dataset})
        print(centroids_value)
        userInput=input()  # say I can input [ [2.,5.],[3.,3.],[6.5,6.] ]
        userArray=np.array(userInput,np.float)
        assign_userInput=tf.assign(centroids,userArray)
        sess.run(assign_userInput,feed_dict={X:dataset})

0 个答案:

没有答案