[英]tf.GradientTape() returns None value for my neural network function
So I created my own neural network and I want to do an automatic differentiation for it with respect to the input variable.所以我创建了自己的神经网络,我想根据输入变量对它进行自动微分。 My code for the neural network goes like this我的神经网络代码是这样的
n_input = 1
n_hidden_1 = 50
n_hidden_2 = 50
n_output = 1
weights = {
'h1': tf.Variable(tf.random.normal([n_input, n_hidden_1],0,0.5)),
'h2': tf.Variable(tf.random.normal([n_hidden_1, n_hidden_2],0,0.5)),
'out': tf.Variable(tf.random.normal([n_hidden_2, n_output],0,0.5))
}
biases = {
'b1': tf.Variable(tf.random.normal([n_hidden_1],0,0.5)),
'b2': tf.Variable(tf.random.normal([n_hidden_2],0,0.5)),
'out': tf.Variable(tf.random.normal([n_output],0,0.5))
}
def multilayer_perceptron(x):
x = np.array([[[x]]], dtype='float32')
layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])
layer_1 = tf.nn.tanh(layer_1)
layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])
layer_2 = tf.nn.tanh(layer_2)
output = tf.matmul(layer_2, weights['out']) + biases['out']
return output
And with tf.GradientTape()
, I tried to differentiate the neural network with this使用tf.GradientTape()
,我试图用这个来区分神经网络
x = tf.Variable(1.0)
with tf.GradientTape() as tape:
y = multilayer_perceptron(x)
dNN1 = tape.gradient(y,x)
print(dNN1)
Which results None
.哪个结果None
。 What did I do wrong here?我在这里做错了什么?
Because you are converting x
to a numpy array by np.array
which is not differentiable.因为您正在通过np.array
将x
转换为不可微的 numpy 数组。
Modify your code like this:像这样修改你的代码:
def multilayer_perceptron(x):
#x = np.array([[[x]]], dtype='float32') #comment
layer_1 = tf.add(tf.matmul([[[x]]], weights['h1']), biases['b1']) #change x shape by adding []
layer_1 = tf.nn.tanh(layer_1)
layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])
layer_2 = tf.nn.tanh(layer_2)
output = tf.matmul(layer_2, weights['out']) + biases['out']
return output
For a good running of some tensorflow operations, it's preferable that all elements of operations are of type tf.tensor, you have to reshape using为了更好地运行一些 tensorflow 操作,最好是所有操作元素都是 tf.tensor 类型,你必须使用
def multilayer_perceptron(x):
x = tf.reshape(x , (1,1,1))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.