2017-09-20 5 views
0

Je voudrais ajouter un autre terme à la fonction de perte de générateur dans DCGAN-tensorflow model.py (lignes de code 127-133). Comme ceci:Modifier la fonction de perte DCGAN, qui est définie dans Tensorflow

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

Le problème est que toutes les fonctions de perte de ce code sont définis comme tensorflow tenseurs (espaces réservés), ce qui fait qu'il est difficile d'effectuer des opérations sur les rangées de la matrice/colonnes (comme dans numpy). Convertir ceux-ci en numpy n'est pas une option non plus car il n'y a pas encore de données fournies aux espaces réservés tensorflow.

Voici l'exemple numpy de ce que je voudrais faire à un tenseur d'espace réservé 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 

Quelle serait un analogue tensorflow de travail de cette fonction?

Sinon, toute autre solution qui me permettrait d'ajouter un tel terme à la fonction de perte de générateur de flux tensoriel DCGAN serait appréciée.


P.S. l'erreur exacte des modifications ci-dessus provoquent ressemble à ceci (dans un espace réservé mot n'a pas de données à évaluer):

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

Répondre

0

Il semble tensorflow a la plupart des opérations nécessaires qui sont disponibles dans numpy, est donc ici tf version de mon code numérique ci-dessus:

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