0

I want to provide the output from one model (f) into another model (c). The following code works

features_ = sess.run(f.features, feed_dict={x:x_, y:y_, dropout:1.0, training:False})

sess.run(c.optimize, feed_dict={x:x_, y:y_, features:features_, dropout:1.0, training:False})

c only needs features_ and y_. It does not need x_. However, if I try to remove x_ as an input, i.e.,

feed_dict={y:y_, features:features_}

I get the following error:

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder' with dtype float and shape [?,28,28,1] [[Node: Placeholder = Placeholderdtype=DT_FLOAT, shape=[?,28,28,1], _device="/job:localhost/replica:0/task:0/device:CPU:0"]]

Is there a reason for this? features_ is a numpy ndarray, so it doesn't seem to be a tensor type or anything like that.

Here is the code for f:

class ConvModelSmall(object):
    def __init__(self, x, y, settings, num_chan, num_features, lr, reg, dropout, training, scope):
        """ init the model with hyper-parameters etc """
        self.x = x
        self.y = y
        self.dropout = dropout
        self.training = training

        initializer = tf.contrib.layers.xavier_initializer(uniform=False)
        self.weights = get_parameters(scope=scope, initializer=initializer, dims)
        self.biases = get_parameters(scope=scope, initializer=initializer, dims)

        self.features = self.feature_model()
        self.acc = settings.acc(self.features, self.y)
        self.loss = settings.loss(self.features, self.y) + reg * reg_loss_fn(self.weights)
        update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
        with tf.control_dependencies(update_ops):
            self.optimize = tf.train.AdagradOptimizer(lr).minimize(self.loss)

    def feature_model(self):
        conv1 = conv2d('conv1', self.x, self.weights['wc1'], self.biases['bc1'], 2, self.training, self.dropout)
        conv2 = conv2d('conv2', conv1, self.weights['wc2'], self.biases['bc2'], 2, self.training, self.dropout)
        conv3 = conv2d('conv3', conv2, self.weights['wc3'], self.biases['bc3'], 2, self.training, self.dropout)

        dense1_reshape = tf.reshape(conv3, [-1, self.weights['wd1'].get_shape().as_list()[0]])
        dense1 = fc_batch_relu(dense1_reshape, self.weights['wd1'], self.biases['bd1'], self.training, self.dropout)
        dense2 = fc_batch_relu(dense1, self.weights['wd2'], self.biases['bd2'], self.training, self.dropout)

        out = tf.matmul(dense2, self.weights['wout']) + self.biases['bout']
        return out

Here is the code for c:

class LinearClassifier(object):
    def __init__(self, features, y, training, num_features, num_classes, lr, reg, scope=""):
        self.features = features
        self.y = y
        self.num_features = num_features
        self.num_classes = num_classes

        initializer = tf.contrib.layers.xavier_initializer(uniform=False)
        self.W = get_scope_variable(scope=scope, var="W", shape=[num_features, num_classes], initializer=initializer)
        self.b = get_scope_variable(scope=scope, var="b", shape=[num_classes], initializer=initializer)

        scores = tf.matmul(tf.layers.batch_normalization(self.features, training=training), self.W) + self.b
        self.loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(labels=self.y, logits=scores)) + reg * tf.nn.l2_loss(self.W)
        update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
        with tf.control_dependencies(update_ops):
            self.optimize = tf.train.GradientDescentOptimizer(lr).minimize(self.loss)

1 Answers1

0

The devil is probably in these lines:

    update_ops = tf.get_collection( tf.GraphKeys.UPDATE_OPS )
    with tf.control_dependencies(update_ops):
        self.optimize = tf.train.GradientDescentOptimizer( lr ).minimize( self.loss )

By the time you define c, f is already defined, so when you say update_ops = tf.get_collection( tf.GraphKeys.UPDATE_OPS ) it will collect all the update ops in the current graph. That will include the ops related to f, x within it.

Then with tf.control_dependencies(update_ops): means "you should do the following only after all update_ops are executed, including having given a value to x. But there is no value for x and the error happens.

To get around this, you can either separate the two networks into two different tf.Graphs, or, probably easier, when you get the update_ops you should filter them by scope in the tf.get_collection() method. For that to work, you should add tf.name_scopes to your network classes ConvModelSmall and LinearClassifier.

Peter Szoldan
  • 4,792
  • 1
  • 14
  • 24