如何解决tensorflow v1 GradientTape:AttributeError:'NoneType'对象没有属性'eval'
我想计算两个正弦波的NSynth WaveNet编码之间的距离的梯度。 这是tensorflow v1。
我正在使用基于https://github.com/magenta/magenta/blob/master/magenta/models/nsynth/wavenet/fastgen.py
的代码我的错误的一个最小示例是在此colab笔记本中:https://colab.research.google.com/drive/1oTEU8QAaOs0K1A0KHrAdt7kA7MkadNDr?usp=sharing
代码如下:
# Commented out IPython magic to ensure Python compatibility.
# %tensorflow_version 1.x
!pip3 install -q magenta
!wget -c http://download.magenta.tensorflow.org/models/nsynth/wavenet-ckpt.tar && tar xvf wavenet-ckpt.tar
checkpoint_path = './wavenet-ckpt/model.ckpt-200000'
import math
from magenta.models.nsynth.wavenet import fastgen
import tensorflow as tf
session_config = tf.ConfigProto(allow_soft_placement=True)
session_config.gpu_options.allow_growth = True
sess = tf.Session(config=session_config)
pi = 3.1415926535897
SR = 16000
sample_length = 64000
DURATION_SECONDS = sample_length / SR
def sine(hz):
time = tf.linspace(0.0,DURATION_SECONDS,sample_length)
return tf.constant(0.5) * tf.cos(2.0 * pi * time * hz)
net = fastgen.load_nsynth(batch_size=2,sample_length=sample_length)
saver = tf.train.Saver()
saver.restore(sess,checkpoint_path)
"""We have two sine waves at 440 and 660 Hz. We use the encoder to generate two (125,16) encodings:"""
twosines = tf.stack([sine(440),sine(660)]).eval(session=sess)
print(sess.run(net["encoding"],Feed_dict={net["X"]: twosines}).shape)
"""Compute the distance between the two sine waves"""
distencode = tf.reduce_mean(tf.abs(net["encoding"][0] - net["encoding"][1]))
print(sess.run(distencode,Feed_dict={net["X"]: twosines}))
"""I don't kNow why the following code doesn't work,but if I did I Could solve the real task....
"""
net["X"] = twosines
distencode.eval(session=sess)
"""Here is the code that I need to work. I want to compute the gradient of the distance between the NSynth encoding of two sine waves:"""
fp = tf.constant(660.0)
newsines = tf.stack([sine(440),sine(fp)])
with tf.GradientTape() as g:
g.watch(fp)
dd_dfp = g.gradient(distencode,fp)
print(dd_dfp.eval(session=sess))
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-12-b5b8cdd00b24> in <module>()
4 g.watch(fp)
5 dd_dfp = g.gradient(distencode,fp)
----> 6 print(dd_dfp.eval(session=sess))
AttributeError: 'nonetype' object has no attribute 'eval'
我相信我需要定义要在此块中执行的操作。但是,我使用的是经过预先训练的模型,我只是在计算距离,因此我不确定如何在该块中定义执行。
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-10-c3411dcbfa2c> in <module>()
3 with tf.GradientTape() as g:
4 g.watch(fp)
----> 5 dd_dfp = g.gradient(distencode,g)
6 print(dd_dfp.eval(session=sess))
/tensorflow-1.15.2/python3.6/tensorflow_core/python/eager/backprop.py in gradient(self,target,sources,output_gradients,unconnected_gradients)
997 flat_sources = [_handle_or_self(x) for x in flat_sources]
998 for t in flat_sources_raw:
--> 999 if not t.dtype.is_floating:
1000 logging.vlog(
1001 logging.WARN,"The dtype of the source tensor must be "
AttributeError: 'GradientTape' object has no attribute 'dtype'
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
/tensorflow-1.15.2/python3.6/tensorflow_core/python/client/session.py in _do_call(self,fn,*args)
1364 try:
-> 1365 return fn(*args)
1366 except errors.OpError as e:
8 frames
InvalidArgumentError: 2 root error(s) found.
(0) Invalid argument: You must Feed a value for placeholder tensor 'Placeholder' with dtype float and shape [2,64000]
[[{{node Placeholder}}]]
[[Mean/_759]]
(1) Invalid argument: You must Feed a value for placeholder tensor 'Placeholder' with dtype float and shape [2,64000]
[[{{node Placeholder}}]]
0 successful operations.
0 derived errors ignored.
During handling of the above exception,another exception occurred:
InvalidArgumentError Traceback (most recent call last)
/tensorflow-1.15.2/python3.6/tensorflow_core/python/client/session.py in _do_call(self,*args)
1382 '\nsession_config.graph_options.rewrite_options.'
1383 'disable_Meta_optimizer = True')
-> 1384 raise type(e)(node_def,op,message)
1385
1386 def _extend_graph(self):
InvalidArgumentError: 2 root error(s) found.
(0) Invalid argument: You must Feed a value for placeholder tensor 'Placeholder' with dtype float and shape [2,64000]
[[node Placeholder (defined at /tensorflow-1.15.2/python3.6/tensorflow_core/python/framework/ops.py:1748) ]]
[[Mean/_759]]
(1) Invalid argument: You must Feed a value for placeholder tensor 'Placeholder' with dtype float and shape [2,64000]
[[node Placeholder (defined at /tensorflow-1.15.2/python3.6/tensorflow_core/python/framework/ops.py:1748) ]]
0 successful operations.
0 derived errors ignored.
Original stack trace for 'Placeholder':
File "/usr/lib/python3.6/runpy.py",line 193,in _run_module_as_main
"__main__",mod_spec)
File "/usr/lib/python3.6/runpy.py",line 85,in _run_code
exec(code,run_globals)
File "/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py",line 16,in <module>
app.launch_new_instance()
File "/usr/local/lib/python3.6/dist-packages/traitlets/config/application.py",line 664,in launch_instance
app.start()
File "/usr/local/lib/python3.6/dist-packages/ipykernel/kernelapp.py",line 499,in start
self.io_loop.start()
File "/usr/local/lib/python3.6/dist-packages/tornado/platform/asyncio.py",line 132,in start
self.asyncio_loop.run_forever()
File "/usr/lib/python3.6/asyncio/base_events.py",line 438,in run_forever
self._run_once()
File "/usr/lib/python3.6/asyncio/base_events.py",line 1451,in _run_once
handle._run()
File "/usr/lib/python3.6/asyncio/events.py",line 145,in _run
self._callback(*self._args)
File "/usr/local/lib/python3.6/dist-packages/tornado/ioloop.py",line 758,in _run_callback
ret = callback()
File "/usr/local/lib/python3.6/dist-packages/tornado/stack_context.py",line 300,in null_wrapper
return fn(*args,**kwargs)
File "/usr/local/lib/python3.6/dist-packages/zmq/eventloop/zmqstream.py",line 548,in <lambda>
self.io_loop.add_callback(lambda : self._handle_events(self.socket,0))
File "/usr/local/lib/python3.6/dist-packages/zmq/eventloop/zmqstream.py",line 462,in _handle_events
self._handle_recv()
File "/usr/local/lib/python3.6/dist-packages/zmq/eventloop/zmqstream.py",line 492,in _handle_recv
self._run_callback(callback,msg)
File "/usr/local/lib/python3.6/dist-packages/zmq/eventloop/zmqstream.py",line 444,in _run_callback
callback(*args,**kwargs)
File "/usr/local/lib/python3.6/dist-packages/tornado/stack_context.py",**kwargs)
File "/usr/local/lib/python3.6/dist-packages/ipykernel/kernelbase.py",line 283,in dispatcher
return self.dispatch_shell(stream,msg)
File "/usr/local/lib/python3.6/dist-packages/ipykernel/kernelbase.py",line 233,in dispatch_shell
handler(stream,idents,line 399,in execute_request
user_expressions,allow_stdin)
File "/usr/local/lib/python3.6/dist-packages/ipykernel/ipkernel.py",line 208,in do_execute
res = shell.run_cell(code,store_history=store_history,silent=silent)
File "/usr/local/lib/python3.6/dist-packages/ipykernel/zmqshell.py",line 537,in run_cell
return super(ZMQInteractiveShell,self).run_cell(*args,**kwargs)
File "/usr/local/lib/python3.6/dist-packages/IPython/core/interactiveshell.py",line 2718,in run_cell
interactivity=interactivity,compiler=compiler,result=result)
File "/usr/local/lib/python3.6/dist-packages/IPython/core/interactiveshell.py",line 2822,in run_ast_nodes
if self.run_code(code,result):
File "/usr/local/lib/python3.6/dist-packages/IPython/core/interactiveshell.py",line 2882,in run_code
exec(code_obj,self.user_global_ns,self.user_ns)
File "<ipython-input-5-5120c8282e75>",line 1,in <module>
net = fastgen.load_nsynth(batch_size=2,sample_length=sample_length)
File "/tensorflow-1.15.2/python3.6/magenta/models/nsynth/wavenet/fastgen.py",line 64,in load_nsynth
x = tf.placeholder(tf.float32,shape=[batch_size,sample_length])
File "/tensorflow-1.15.2/python3.6/tensorflow_core/python/ops/array_ops.py",line 2619,in placeholder
return gen_array_ops.placeholder(dtype=dtype,shape=shape,name=name)
File "/tensorflow-1.15.2/python3.6/tensorflow_core/python/ops/gen_array_ops.py",line 6669,in placeholder
"Placeholder",dtype=dtype,name=name)
File "/tensorflow-1.15.2/python3.6/tensorflow_core/python/framework/op_def_library.py",line 794,in _apply_op_helper
op_def=op_def)
File "/tensorflow-1.15.2/python3.6/tensorflow_core/python/util/deprecation.py",line 507,in new_func
return func(*args,**kwargs)
File "/tensorflow-1.15.2/python3.6/tensorflow_core/python/framework/ops.py",line 3357,in create_op
attrs,op_def,compute_device)
File "/tensorflow-1.15.2/python3.6/tensorflow_core/python/framework/ops.py",line 3426,in _create_op_internal
op_def=op_def)
File "/tensorflow-1.15.2/python3.6/tensorflow_core/python/framework/ops.py",line 1748,in __init__
self._traceback = tf_stack.extract_stack()
谢谢。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。