Search code examples
pythontensorflowdcgan

Change DCGAN loss function, that is defined in Tensorflow


I would like to add another term to the generator loss function in DCGAN-tensorflow model.py (code lines 127-133). Like this:

self.g_loss = self.g_loss + TV(self.G)

The problem is that all the loss functions in this code are defined as tensorflow tensors (placeholders), which makes it hard to perform operations on matrix row/columns (like in numpy). Converting these into numpy isn't an option either as there aren't any data fed to the tensorflow placeholders yet.

Here is the numpy example of what I'd like to do to a tensorflow placeholder tensor:

    def TV(tensor):
       # tensor dimensions are [batch_size, dimension, length, depth]
       # so [64, 25, 176, 1] in our case
       tensor = tensor.eval()
       output = np.zeros((64, 175))
       for i in range(np.shape(tensor)[2]-1):
        output[:, i] = np.sum(np.abs(tensor[:, :, i, 0] - tensor[:, :, i+1, 0]), axis=1)
       tv = np.mean(np.sum(output, axis=1))
       tv = tf.convert_to_tensor(tv)
       return tv

What would be a working tensorflow analogue of this function?

Alternatively any other solution that would allow me to add such a term to DCGAN tensorflow generator loss function would be appreciated.


P.S. the exact error the above changes cause looks like this (in a word placeholder has no data to be evaluated):

2017-09-20 16:08:31.216474: W tensorflow/core/framework/op_kernel.cc:1152] Invalid argument: You must feed a value for placeholder tensor 'z' with dtype float
     [[Node: z = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/gpu:0"]()]]
Traceback (most recent call last):
  File "/home/marija/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1039, in _do_call
    return fn(*args)
  File "/home/marija/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1021, in _run_fn
    status, run_metadata)
  File "/home/marija/anaconda3/envs/tensorflow-gpu-3.5/lib/python3.5/contextlib.py", line 66, in __exit__
    next(self.gen)
  File "/home/marija/.local/lib/python3.5/site-packages/tensorflow/python/framework/errors_impl.py", line 466, in raise_exception_on_not_ok_status
    pywrap_tensorflow.TF_GetCode(status))
tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'z' with dtype float
     [[Node: z = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/gpu:0"]()]]
     [[Node: generator/Tanh/_5 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/gpu:0", send_device_incarnation=1, tensor_name="edge_367_generator/Tanh", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "main.py", line 115, in <module>
    tf.app.run()
  File "/home/marija/.local/lib/python3.5/site-packages/tensorflow/python/platform/app.py", line 48, in run
    _sys.exit(main(_sys.argv[:1] + flags_passthrough))
  File "main.py", line 92, in main
    sample_dir=FLAGS.sample_dir)
  File "/home/marija/DCGAN-tensorflow/model_25x176.py", line 107, in __init__
    self.build_model()
  File "/home/marija/DCGAN-tensorflow/model_25x176.py", line 184, in build_model
    self.g_loss = self.g_loss + TV(self.G)
  File "/home/marija/DCGAN-tensorflow/model_25x176.py", line 164, in TV
    tensor = tensor.eval()
  File "/home/marija/.local/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 569, in eval
    return _eval_using_default_session(self, feed_dict, self.graph, session)
  File "/home/marija/.local/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 3741, in _eval_using_default_session
    return session.run(tensors, feed_dict)
  File "/home/marija/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 778, in run
    run_metadata_ptr)
  File "/home/marija/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 982, in _run
    feed_dict_string, options, run_metadata)
  File "/home/marija/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1032, in _do_run
    target_list, options, run_metadata)
  File "/home/marija/.local/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1052, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'z' with dtype float
     [[Node: z = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/gpu:0"]()]]
     [[Node: generator/Tanh/_5 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/gpu:0", send_device_incarnation=1, tensor_name="edge_367_generator/Tanh", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

Caused by op 'z', defined at:
  File "main.py", line 115, in <module>
    tf.app.run()
  File "/home/marija/.local/lib/python3.5/site-packages/tensorflow/python/platform/app.py", line 48, in run
    _sys.exit(main(_sys.argv[:1] + flags_passthrough))
  File "main.py", line 92, in main
    sample_dir=FLAGS.sample_dir)
  File "/home/marija/DCGAN-tensorflow/model_25x176.py", line 107, in __init__
    self.build_model()
  File "/home/marija/DCGAN-tensorflow/model_25x176.py", line 131, in build_model
    tf.float32, [None, self.z_dim], name='z')
  File "/home/marija/.local/lib/python3.5/site-packages/tensorflow/python/ops/array_ops.py", line 1507, in placeholder
    name=name)
  File "/home/marija/.local/lib/python3.5/site-packages/tensorflow/python/ops/gen_array_ops.py", line 1997, in _placeholder
    name=name)
  File "/home/marija/.local/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py", line 768, in apply_op
    op_def=op_def)
  File "/home/marija/.local/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 2336, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "/home/marija/.local/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1228, in __init__
    self._traceback = _extract_stack()

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'z' with dtype float
     [[Node: z = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/gpu:0"]()]]
     [[Node: generator/Tanh/_5 = _Recv[client_terminated=false, recv_device="/job:localhost/replica:0/task:0/cpu:0", send_device="/job:localhost/replica:0/task:0/gpu:0", send_device_incarnation=1, tensor_name="edge_367_generator/Tanh", tensor_type=DT_FLOAT, _device="/job:localhost/replica:0/task:0/cpu:0"]()]]

Solution

  • It appears Tensorflow has most of the necessary operations that are available in numpy, so here is the tf version of my numpy code above:

        def TV(tensor):
            List =[]
            for i in range(np.shape(tensor)[2]-1):
                a = tf.abs(tensor[:, :, i, 0] - tensor[:, :, i+1, 0]); 
                if a != None:
                    List.append(a); 
            output = tf.stack(List)        
            tv = tf.reduce_mean(tf.reduce_sum(tf.reduce_sum(output, axis=0), axis=1))
            return tv