我正在尝试对已有的数据集(X_train
进行推断,并获取logits层的值,而未将softmax应用于输出。我从检查点文件(model_X.ckpt
)加载的模型具有名为“ logits”的logits层。所以基本上,我想运行:
sess.run("model_X/logits:0", feed_dict: {"Placeholder:0": X_train, keep_prob:1.0})
但是该模型将输入数据集的大小限制为32,这不允许我一次传递10,000个输入。这就是为什么我也使用批处理这样的原因:
features_placeholder = tf.placeholder(X_train.dtype, X_train.shape)
dataset = tf.data.Dataset.from_tensor_slices((features_placeholder))
dataset = dataset.batch(32)
我创建了一个迭代器(纯粹基于我的推理,使用单发迭代器会爆炸图的大小):
def initialize_iterator(sess, iterator, features):
sess.run(iterator.initializer, feed_dict={features_placeholder: features})
iterator = dataset.make_initializable_iterator()
initialize_iterator(sess, iterator, X_train)
next_x = iterator.get_next()
# Assign the first batch:
val = sess.run(next_x)
layer = "model_X/logits:0"
units = sess.run(layer,feed_dict={"Placeholder:0": val, keep_prob:1.0})
如何遍历所有批次以推断所有输入?
答案 0 :(得分:0)
因此要遍历所有批次,对于100k的数据集和32(3125次迭代)的批次,我只需要这样做:
def body(sess, batch, arr):
val = sess.run(batch)
def getActivations(sess,layer,stimuli,labels):
units = sess.run(layer,feed_dict={"Placeholder:0": np.reshape(stimuli,[32,64,64,3]), keep_prob:1.0})
return units
try:
res = getActivations(sess,"model_X/logits:0",val)
arr = np.append(arr, np.reshape(res,[32,1024]), axis=0)
except tf.errors.OutOfRangeError:
print("End of dataset")
return arr, pred
with tf.Session() as sess:
load_graph(sess)
initialize_iterator(sess, iterator, X_train)
next_x = iterator.get_next()
arr = np.empty((0,2048), int)
for i in range(0,3125):
arr = body(sess, next_x, arr)