在TensorFlow中加载数据
我正在使用TensorFlow来运行一些Kaggle比赛。 由于我没有太多训练数据,因此我使用TF常量将所有训练和测试数据预加载到图形中以提高效率。 我的代码看起来像这样
... lots of stuff ...
with tf.Graph().as_default():
train_images = tf.constant(train_data[:36000,1:], dtype=tf.float32)
... more stuff ...
train_set = tf.train.slice_input_producer([train_images, train_labels])
images, labels = tf.train.batch(train_set, batch_size=100)
# this is where my model graph is built
model = MLP(hidden=[512, 512])
logits = model._create_model(images)
loss = model._create_loss_op(logits, labels)
train = model._create_train_op(loss)
# I know I am not supposed to call _something() methods
# from outside of the class. I used to call these internally
# but refactoring is still in progress
现在,当我使用Feed词典来提供数据时,我只能构建一次模型,但可以轻松切换输入,例如我的训练数据和验证数据(以及测试数据)之间的输入。 但是在预加载的时候,似乎我必须为每个输入集创建一个单独的图形副本。 目前,我正是这样做的,我使用变量重用来确保图形使用相同的权重和偏差。 但我忍不住,但觉得这是一种奇怪的做事方式。 所以,例如,这里有一些MLP类和我的验证代码
class MLP(object):
... lots of stuff happens here ...
def _create_dense_layer(self, name, inputs, n_in, n_out, reuse=None, activation=True):
with tf.variable_scope(name, reuse=reuse):
weights = self._weights([n_in, n_out])
self.graph.add_to_collection('weights', weights)
layer = tf.matmul(inputs, weights)
if self.biases:
biases = self._biases([n_out])
layer = layer + biases
if activation:
layer = self.activation(layer)
return layer
... and back to the training code ...
valid_images = tf.constant(train_data[36000:,1:], dtype=tf.float32)
valid_logits = model._create_model(valid_images, reuse=True)
valid_accuracy = model._create_accuracy_op(valid_logits, valid_labels)
那么,我是否真的必须为我想要使用的每组数据创建一个完整的模型副本,还是我缺少TF中的某些内容,并且有更简单的方法实现它?
链接地址: http://www.djcxy.com/p/32055.html