更改Tensorflow中定义的DCGAN丢失功能

时间:2017-09-20 15:33:20

标签: python tensorflow dcgan

我想在DCGAN-tensorflow model.py(代码行127-133)中为生成器丢失函数添加另一个术语。像这样:

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

问题在于此代码中的所有损失函数都被定义为张量流张量(占位符),这使得难以对矩阵行/列执行操作(如numpy)。将这些转换为numpy也不是一种选择,因为还没有任何数据输入到tensorflow占位符。

以下是我想对张量流占位符张量做的例子:

    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

这个函数的工作张量流模拟是什么?

另外,任何其他允许我将这样的术语添加到DCGAN张量流发生器损失函数的解决方案将不胜感激。

P.S。上面的更改导致的确切错误看起来像这样(在一个单词占位符中没有要评估的数据):

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"]()]]

1 个答案:

答案 0 :(得分:0)

看来Tensorflow有numpy中可用的大部分必要操作,所以这里是我上面的numpy代码的tf版本:

    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