对于MNIST识别,我没有一个非常复杂的模型,需要研究其体系结构并试图可视化内部发生的事情。添加了用于关闭网络某些部分的变量(towerWeights…)后,尝试获取图层的渐变时我开始出现错误
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.
网络就像
inputs = Input(shape=(28, 28, 1))
conv_1 = Conv2D(16, 5, strides=(2, 2), activation='relu', name='conv_1_common')(inputs)
towerWeightsKerasVar = tf.compat.v1.Variable(np.ones([4]), dtype=tf.float32, name='tower_weights')
# towerWeightsKerasVar._trainable = False
**towerWeights = Input(shape=(4, ), tensor=towerWeightsKerasVar)**
…
dense_2 = Dropout(0.3)(conv _1)
dense_2 = Dense(10, name='dense_2')(dense_2)
prediction = Activation("softmax", name="softmax")(dense_2)
model = Model(inputs=[inputs, towerWeights], outputs=prediction)
,并且可以在此cut变体上复制。 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
答案 0 :(得分:0)
我还没有找到解决方案,但是找到了解决方法,就像这样
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)])