tensorflow v1梯度带:属性错误:';非类型';对象没有属性';评估';

tensorflow v1梯度带:属性错误:';非类型';对象没有属性';评估';,tensorflow,signal-processing,gradient,tensorflow1.15,Tensorflow,Signal Processing,Gradient,Tensorflow1.15,我想计算两个正弦波的NSynth波网编码之间的距离梯度。 这是tensorflow v1 我正在使用基于 我的错误的一个最简单的例子是在这个colab笔记本中: 代码如下: # Commented out IPython magic to ensure Python compatibility. # %tensorflow_version 1.x !pip3 install -q magenta !wget -c http://download.magenta.tensorflow.org/m

我想计算两个正弦波的NSynth波网编码之间的距离梯度。 这是tensorflow v1

我正在使用基于

我的错误的一个最简单的例子是在这个colab笔记本中:

代码如下:

# 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'
---------------------------------------------------------------------------
AttributeError回溯(最近一次呼叫上次)
在()
4 g.手表(fp)
5 dd_dfp=梯度(距离编码,fp)
---->6打印(dd_dfp.eval(会话=sess))
AttributeError:“非类型”对象没有属性“eval”
我认为我需要定义在这个块中要执行的操作。然而,我使用的是一个预训练的模型,我只是在计算距离,所以我不确定如何定义该块中的执行

倒数第二个块将帮助我修复最后一个块,它给出以下错误:

---------------------------------------------------------------------------

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'
---------------------------------------------------------------------------
AttributeError回溯(最近一次呼叫上次)
在()
3将tf.GradientTape()作为g:
4 g.手表(fp)
---->5 dd_dfp=g.梯度(distencode,g)
6打印(dd_dfp.eval(会话=sess))
/渐变中的tensorflow-1.15.2/python3.6/tensorflow_core/python/eager/backprop.py(自身、目标、源、输出渐变、未连接的渐变)
997平面源=[[平面源中x的手柄或自身(x)]
998对于平面源中的t\u原始:
-->999如果t.dtype.不是浮动的:
1000.vlog(
1001 logging.WARN,“源张量的数据类型必须为”
AttributeError:“GradientTape”对象没有属性“dtype”

---------------------------------------------------------------------------
InvalidArgumentError回溯(最后一次最近调用)
/tensorflow-1.15.2/python3.6/tensorflow_core/python/client/session.py in_do_call(self,fn,*args)
1364尝试:
->1365返回fn(*args)
1366错误除外。操作错误为e:
8帧
InvalidArgumentError:发现2个根错误。
(0)无效参数:必须为带有dtype float和shape[264000]的占位符张量“占位符”输入一个值
[{{节点占位符}}]]
[[平均值/_759]]
(1) 无效参数:必须为带有dtype float和shape[264000]的占位符张量“占位符”输入一个值
[{{节点占位符}}]]
0成功的操作。
忽略0个派生错误。
在处理上述异常期间,发生了另一个异常:
InvalidArgumentError回溯(最后一次最近调用)
/tensorflow-1.15.2/python3.6/tensorflow_core/python/client/session.py in_do_call(self,fn,*args)
1382'\n会话配置图选项。重写选项。'
1383“禁用元优化器=真”)
->1384提升类型(e)(节点定义、操作、消息)
1385
1386定义扩展图(自):
InvalidArgumentError:发现2个根错误。
(0)无效参数:必须为带有dtype float和shape[264000]的占位符张量“占位符”输入一个值
[[节点占位符(定义于/tensorflow-1.15.2/python3.6/tensorflow_core/python/framework/ops.py:1748)]]
[[平均值/_759]]
(1) 无效参数:必须为带有dtype float和shape[264000]的占位符张量“占位符”输入一个值
[[节点占位符(定义于/tensorflow-1.15.2/python3.6/tensorflow_core/python/framework/ops.py:1748)]]
0成功的操作。
忽略0个派生错误。
“占位符”的原始堆栈跟踪:
文件“/usr/lib/python3.6/runpy.py”,第193行,在“运行”模块中作为“主”
“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
文件“/usr/lib/python3.6/runpy.py”,第85行,在运行代码中
exec(代码、运行\全局)
文件“/usr/local/lib/python3.6/dist packages/ipykernel_launcher.py”,第16行,在
app.launch_new_instance()
文件“/usr/local/lib/python3.6/dist-packages/traitlets/config/application.py”,第664行,在launch_实例中
app.start()
文件“/usr/local/lib/python3.6/dist-packages/ipykernel/kernelapp.py”,第499行,开始
self.io_loop.start()
文件“/usr/local/lib/python3.6/dist-packages/tornado/platform/asyncio.py”,第132行,开头
self.asyncio\u loop.run\u forever()
文件“/usr/lib/python3.6/asyncio/base\u events.py”,第438行,永远运行
self._run_once()
文件“/usr/lib/python3.6/asyncio/base\u events.py”,第1451行,运行一次
句柄。_run()
文件“/usr/lib/python3.6/asyncio/events.py”,第145行,在运行中
self.\u回调(*self.\u参数)
文件“/usr/local/lib/python3.6/dist-packages/tornado/ioloop.py”,第758行,在运行回调中
ret=callback()
文件“/usr/local/lib/python3.6/dist-packages/tornado/stack\u-context.py”,第300行,空包装
返回fn(*args,**kwargs)
文件“/usr/local/lib/python3.6/dist packages/zmq/eventloop/zmqstream.py”,第548行,在
self.io\u循环.add\u回调(lambda:self.\u处理\u事件(self.socket,0))
文件“/usr/local/lib/python3.6/dist packages/zmq/eventloop/zmqstream.py”,第462行,在事件句柄中
self.\u handle\u recv()
文件“/usr/local/lib/python3.6/dist packages/zmq/eventloop/zmqstream.py”,第492行,位于
self.\u运行\u回调(回调,消息)
文件“/usr/local/lib/python3.6/dist packages/zmq/eventloop/zmqstream.py”,第444行,在运行回调中
回调(*args,**kwargs)
文件“/usr/local/lib/python3.6/dist-packages/tornado/stack\u-context.py”,第300行,空包装
返回fn(*args,**kwargs)
dispatcher中的文件“/usr/local/lib/python3.6/dist packages/ipykernel/kernelbase.py”,第283行
返回self.dispatch\u shell(流,消息)
文件“/usr/local/lib/python3.6/dist packages/ipykernel/kernelbase.py”,第233行,在dispatch_shell中
处理程序(流、标识、消息)
文件“/usr/local/lib/python3.6/dist-packages/ipykernel/kernelb