N.B。 Tensorflow版本低于2.0
在下面的可复制代码中,wd_d_op=sess.run([wd_d_op], feed_dict={X: x})
运行成功,但是grads_and_vars=sess.run([grad_and_vars], feed_dict={X: x})
引发了所提到的noneType
错误。如果grad_and_vars
,那么下一个操作如何成功运行?
import tensorflow as tf
import numpy as np
from sklearn.datasets import make_blobs
##function for creating layer with fixed weight, don't worry about this
def fc_layer(input_tensor, input_dim, output_dim, component_name,act=tf.nn.relu, input_type='dense'):
# weight = tf.Variable(tf.truncated_normal([input_dim, output_dim], stddev=1. / tf.sqrt(input_dim / 2.)), name='weight')
if component_name=="weight1":
weight=tf.Variable([[-0.46401197, -0.02868146, -0.02945778, -0.19310321],[-0.06130088, -0.3782992 , -1.4025078 , -0.8482222 ]])
bias=tf.Variable([0.1,0.1,0.1,0.1])
else:
weight=tf.Variable([[ 0.27422005],[-1.2150304 ],[-0.43404067],[-0.3352416 ]])
bias=tf.Variable([0.1])
# weight=tf.Print(weight,[weight],component_name,summarize=-1)
bias = tf.Variable(tf.constant(0.1, shape=[output_dim]), name='bias')
# bias=tf.Print(bias,[type(bias)],component_name+"bias",summarize=-1)
weight=tf.cast(weight, tf.float32)
bias=tf.cast(bias, tf.float32)
input_tensor=tf.cast(input_tensor, tf.float32)
if input_type == 'sparse':
activations = act(tf.sparse_tensor_dense_matmul(input_tensor, weight) + bias)
else:
activations = act(tf.matmul(input_tensor, weight) + bias,name="features")
return activations
"""fixed input"""
x=np.array([[-0.9233333412304945, -0.5148649076298134],[-0.9366679176350374, -2.086600005395918],[50.366624846708156, -9.02965996391532],[51.09416621163187, -12.101430685982692]])
lr_wd_D = 1e-3
with tf.name_scope('input'):
X = tf.placeholder(dtype=tf.float32,name="exmaple")
with tf.name_scope('generator'):
h1 = fc_layer(X, 2, 4,component_name="weight1",input_type='dense')
output = fc_layer(h1, 4, 1,component_name="weight2",act=tf.identity,input_type='dense')
# output=tf.Print(output,[output],"output",summarize=-1)
output=tf.convert_to_tensor(output, dtype=tf.float32)
critic_s = tf.slice(output, [0, 0], [2, -1])
critic_t = tf.slice(output, [2, 0], [2, -1])
wd_loss = (tf.reduce_mean(critic_s) - tf.reduce_mean(critic_t))
# wd_loss=tf.convert_to_tensor(wd_loss, dtype=tf.float32)
theta_C = [v for v in tf.global_variables() if 'generator' in v.name]
wd_op=tf.train.AdamOptimizer(lr_wd_D)
"""only calling this operation does not work, raised the mentioned error"""
grad_and_vars = wd_op.compute_gradients(wd_loss,var_list=theta_C)
"""But the following operation works even that use the previous variable"""
wd_d_op=wd_op.apply_gradients(grad_and_vars)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
#this works
wd_loss,theta_C=sess.run([wd_loss,theta_C], feed_dict={X: x})
print("wd_loss")
print(wd_loss)
print("theta_C")
print(theta_C)
#this does works
wd_d_op=sess.run([wd_d_op], feed_dict={X: x})
#this does not work , even though grads_and_vars used by wd_d_op
grads_and_vars=sess.run([grad_and_vars], feed_dict={X: x})
解决方案:
如果注释掉以下两行代码,它将正常运行。
# bias=tf.Variable([0.1,0.1,0.1,0.1])
# bias=tf.Variable([0.1])
解释:
如果图形中的None
和wd_loss
之间没有显式连接,则返回theta_C
的渐变。如果打印theta_C
,则会找到两个bias
变量。这两个bias
变量实际上并不参与wd_loss
的计算。
[下面,当w3
不参与y
的计算但对其进行区分时,将给出一个错误示例。
import tensorflow as tf
w1 = tf.Variable([[1.,2.]])
w2 = tf.Variable([[9.],[10.]])
w3 = tf.Variable([[5.,6.]])
y = tf.matmul(w1, w2)
# this work
grads = tf.gradients(y,[w1,w2])
# this does not work, TypeError: Fetch argument None has invalid type <class 'NoneType'>
# grads = tf.gradients(y,[w1,w2,w3])
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
gradval = sess.run(grads)
print(gradval)