TensorFlow AttributeError:Tensor.op在启用急切执行时没有意义
我没有一个非常复杂的模型来识别MNIST,玩弄它的架构,并试图将内部发生的事情形象化。在我添加了用于关闭网络某些部分的变量(towerWeights…)后,我在尝试获取层的渐变时开始出错TensorFlow AttributeError:Tensor.op在启用急切执行时没有意义,tensorflow,keras,runtime-error,tensorflow2.0,Tensorflow,Keras,Runtime Error,Tensorflow2.0,我没有一个非常复杂的模型来识别MNIST,玩弄它的架构,并试图将内部发生的事情形象化。在我添加了用于关闭网络某些部分的变量(towerWeights…)后,我在尝试获取层的渐变时开始出错 Traceback (most recent call last): File "VisQtMain.py", line 1021, in onGradientsByImagesPressed dataList.append(self.netWrapper.getGradients(layerNam
Traceback (most recent call last):
File "VisQtMain.py", line 1021, in onGradientsByImagesPressed
dataList.append(self.netWrapper.getGradients(layerName, imageNum, 1, epochNum, True))
File "E:\Projects\Python\Visualiz_Zeiler\MnistNetVisWrapper.py", line 241, in getGradients
outputs=self.gradientTensors)
File "C:\Program Files\Python35\lib\site-packages\keras\backend\tensorflow_backend.py", line 3009, in function
**kwargs)
File "C:\Program Files\Python35\lib\site-packages\tensorflow_core\python\keras\backend.py", line 3758, in function
return EagerExecutionFunction(inputs, outputs, updates=updates, name=name)
File "C:\Program Files\Python35\lib\site-packages\tensorflow_core\python\keras\backend.py", line 3655, in __init__
base_graph=source_graph)
File "C:\Program Files\Python35\lib\site-packages\tensorflow_core\python\eager\lift_to_graph.py", line 249, in lift_to_graph
visited_ops = set([x.op for x in sources])
File "C:\Program Files\Python35\lib\site-packages\tensorflow_core\python\eager\lift_to_graph.py", line 249, in <listcomp>
visited_ops = set([x.op for x in sources])
File "C:\Program Files\Python35\lib\site-packages\tensorflow_core\python\ops\resource_variable_ops.py", line 559, in op
return self._handle.op
File "C:\Program Files\Python35\lib\site-packages\tensorflow_core\python\framework\ops.py", line 1098, in op
"Tensor.op is meaningless when eager execution is enabled.")
AttributeError: Tensor.op is meaningless when eager execution is enabled.
它可以在这个切割变体上复制。TensorFlow 2.0 RC、2.0.0和今天的夜间tf——结果是一样的这是TF中的错误还是我做错了什么?
TowerWeights用于将网络的某些部分乘以其组件。但在cut变体中,它只是存在,什么也不做
我不太了解正在发生的事情,我们渴望的模式正在发挥作用,但它并没有发挥作用。我尝试使用tf.compat.v1.disable_eager_execution()禁用急切执行,
但是我现在越来越
File "VisQtMain.py", line 1021, in onGradientsByImagesPressed
dataList.append(self.netWrapper.getGradients(layerName, imageNum, 1, epochNum, True))
File "E:\Projects\Python\Visualiz_Zeiler\MnistNetVisWrapper.py", line 246, in getGradients
gradients = self.gradientKerasFunc(inp)
File "C:\Program Files\Python35\lib\site-packages\tensorflow_core\python\keras\backend.py", line 3565, in __call__
run_metadata=self.run_metadata)
File "C:\Program Files\Python35\lib\site-packages\tensorflow_core\python\client\session.py", line 1470, in __call__
run_metadata_ptr)
tensorflow.python.framework.errors_impl.InvalidArgumentError: You must feed a value for placeholder tensor 'softmax_target' with dtype float and shape [?,?]
[[{{node softmax_target}}]]
所以在这种情况下也有问题
Python 3.5、Windows 7 x64我还没有找到解决方案,但找到了解决办法,就是这样
def get_tensor_array_element(x):
def f(X): # Here we have e.g. np.ones([4])
return X[x : x + 1]
def g(input_shape):
output_shape = list(input_shape) # Here - e.g. (None, 4)
output_shape[-1] = 1
return tuple(output_shape)
return Lambda(f, output_shape=lambda input_shape: g(input_shape))
towerWeightsKerasVar = tf.compat.v1.Variable(np.ones([towerCount]),
dtype=tf.float32, name='tower_weights')
towerWeightsKerasVar._trainable = False # "Doesn't help against Tensor.op is meaningless when eager execution is enabled."
towerWeights = Input(shape=(towerCount, ), tensor=towerWeightsKerasVar)
# Looks like input, should be declared in model's input, but (if the tensor parameter is present)
# should not be supplied to the net (in model.fit and so on)
...
conv = Multiply()([conv, get_tensor_array_element(i)(towerWeights)])
我用了TF2.0。我也有类似的错误。我建立模型的方式似乎有问题。不过我把它修好了。如果你想让别人看一看,你可以发布你的整个模型代码。不止是臭虫,我猜这可能是你们做错了什么。你们找到答案了吗?@SantoshGupta7,没有,但我发现了一个因某种原因有效的组合,我现在就添加答案
def get_tensor_array_element(x):
def f(X): # Here we have e.g. np.ones([4])
return X[x : x + 1]
def g(input_shape):
output_shape = list(input_shape) # Here - e.g. (None, 4)
output_shape[-1] = 1
return tuple(output_shape)
return Lambda(f, output_shape=lambda input_shape: g(input_shape))
towerWeightsKerasVar = tf.compat.v1.Variable(np.ones([towerCount]),
dtype=tf.float32, name='tower_weights')
towerWeightsKerasVar._trainable = False # "Doesn't help against Tensor.op is meaningless when eager execution is enabled."
towerWeights = Input(shape=(towerCount, ), tensor=towerWeightsKerasVar)
# Looks like input, should be declared in model's input, but (if the tensor parameter is present)
# should not be supplied to the net (in model.fit and so on)
...
conv = Multiply()([conv, get_tensor_array_element(i)(towerWeights)])