1

Here, I read some tensorflow implementation of style transfer. Specifically, it defines the loss which is then to be optimized. In one loss function, it says: `

def sum_style_losses(sess, net, style_imgs):
  total_style_loss = 0.
  weights = args.style_imgs_weights
  for img, img_weight in zip(style_imgs, weights):
    sess.run(net['input'].assign(img))
    style_loss = 0.
    for layer, weight in zip(args.style_layers, args.style_layer_weights):
      a = sess.run(net[layer])
      x = net[layer]
      a = tf.convert_to_tensor(a)
      style_loss += style_layer_loss(a, x) * weight
    style_loss /= float(len(args.style_layers))
    total_style_loss += (style_loss * img_weight)

`

The optimizer is called with the current session:

optimizer.minimize(sess)

So the session is up and running, but during the run, it calls further runs in the for loop. Can anyone exlain the tensorflow logic, especially why x contains the feature vector of input image (and not of the style image). For me, there seem to be two runs in parallel.

1 Answers1

0

The code from the github repo is as follows:

init_op = tf.global_variables_initializer()
sess.run(init_op)
sess.run(net['input'].assign(init_img))
optimizer.minimize(sess)

A session schedules operations to be run on devices and holds some variables. It can be used to schedule many operations across different (sub)graphs. So in the code above the session

  1. Initializes variables.
  2. Assigns an image to the symbolic input tensor. Note you can also use feeds for this.
  3. Minimize using scipy's optimizer (which has already been passed a loss in its constructor... More details found here).

At each of these stages the session is responsible for scheduling execution of the subgraphs that allow for the computations that occur at that stage.

Alex
  • 18,484
  • 8
  • 60
  • 80
  • That's clear to me. So when `minimize` actually runs, it calls the loss function. Here, another run is started, but still there has to be the state of the minimizer run to get `x=net[layer]`. The statement before, i.e. `a=...`, also evaluates net at the same layer, but does not interfere with `x=...`. So my question was how tensorflow handles this situation. Is there a new subgraph constructed? – user1225905 May 17 '17 at 18:02
  • The graph is constructed only once... The session can execute different parts of the graph (subgraphs) depending on the output desired. You can examine the subgraph using `tensorboard`. See an example [here](http://stackoverflow.com/questions/43830022/compute-gradient-norm-of-each-part-of-composite-loss-function/43944183#43944183). – Alex May 18 '17 at 18:56