我想在dcgan-tensorflow model.py中为发电机损耗函数添加另一个术语(代码行127-133)。这样:
self.g_loss = self.g_loss + TV(self.G)
问题在于,此代码中的所有损耗函数均定义为张量张量(占位符),这使得很难在矩阵行/列(例如numpy)上执行操作。将它们转换为numpy也不是一个选项,因为还没有任何数据馈送到Tensorflow占位符。
这是我想对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 tensorflow发电机损耗函数中添加这样的术语。
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