更改在 Tensorflow 中定义的 DCGAN 损失函数
Change DCGAN loss function, that is defined in Tensorflow
我想在 DCGAN-tensorflow model.py 中为生成器损失函数添加另一个项(代码行 127-133)。像这样:
self.g_loss = self.g_loss + TV(self.G)
问题在于此代码中的所有损失函数都定义为 tensorflow 张量(占位符),这使得难以对矩阵 row/columns 执行操作(就像在 numpy 中一样)。将它们转换为 numpy 也不是一种选择,因为还没有任何数据被提供给 tensorflow 占位符。
这是我想对 tensorflow 占位符张量执行的操作的 numpy 示例:
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
这个函数的工作 tensorflow 模拟是什么?
或者,任何其他允许我将这样的项添加到 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"]()]]
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
我想在 DCGAN-tensorflow model.py 中为生成器损失函数添加另一个项(代码行 127-133)。像这样:
self.g_loss = self.g_loss + TV(self.G)
问题在于此代码中的所有损失函数都定义为 tensorflow 张量(占位符),这使得难以对矩阵 row/columns 执行操作(就像在 numpy 中一样)。将它们转换为 numpy 也不是一种选择,因为还没有任何数据被提供给 tensorflow 占位符。
这是我想对 tensorflow 占位符张量执行的操作的 numpy 示例:
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
这个函数的工作 tensorflow 模拟是什么?
或者,任何其他允许我将这样的项添加到 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"]()]]
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