2021-07-27 15:22

针对神经网络模型，二阶偏导始终为0

f(x,y)=x²+y

``````model = keras.Sequential([
keras.layers.Dense(100, activation='relu'),
keras.layers.Dense(100, activation='relu'),
keras.layers.Dense(100, activation='relu'),
keras.layers.Dense(100, activation='relu'),
keras.layers.Dense(100, activation='relu'),
keras.layers.Dense(1),
])
``````

``````with tf.GradientTape(persistent=True) as tape3:
tape3.watch(X)
tape3.watch(Y)
tape4.watch(X)
tape4.watch(Y)
Z = tf.concat([X, Y], 1)
ff = model(Z)
print(tf.concat([dx, dy], 1))
print(dxdx)
``````

dx和dy都基本正确，但dxdx全部为0，按理说dxdx应该为2左右。找不到问题的原因，是我哪里写的不对吗，还是说，神经网络的拟合，是只针对x和y进行一次线性拟合，所以二阶导均为0...应该不是吧……

``````import tensorflow as tf
import numpy as np
from tensorflow import keras
import sys
import random

np.set_printoptions(threshold=np.inf)
np.set_printoptions(suppress=True)

x = np.arange(0, 101, dtype=float)
y = np.arange(0, 101, dtype=float)
for i in range(101):
x[i]=np.round(random.random()*10,2)
y[i]=np.round(random.random()*10,2)
x=x.reshape(101,1)
y=y.reshape(101,1)

z = np.arange(1, 203, dtype=float).reshape(101, 2)
lis = np.arange(2, dtype=float)
for i in range(101):
lis[0]=float(x[i])
lis[1]=float(y[i])
z[i] = lis

ans=(x*x+y).reshape(101,1)

X=tf.convert_to_tensor(x, dtype=float)
Y=tf.convert_to_tensor(y, dtype=float)

model = keras.Sequential([
keras.layers.Dense(100, activation='relu'),
keras.layers.Dense(100, activation='relu'),
keras.layers.Dense(100, activation='relu'),
keras.layers.Dense(100, activation='relu'),
keras.layers.Dense(100, activation='relu'),
keras.layers.Dense(1),
])

for i in range(2000):
tape.watch(X)
tape.watch(Y)
tape2.watch(X)
tape2.watch(Y)
Z = tf.concat([X, Y], 1)
f=model(Z)
loss = tf.reduce_mean(tf.square(ans - f))
#print(dxdx)

if i%10==0:
print(i, loss)

tape3.watch(X)
tape3.watch(Y)
tape4.watch(X)
tape4.watch(Y)
Z = tf.concat([X, Y], 1)
ff = model(Z)
print(tf.concat([dx, dy], 1))
print(dxdx)
``````
• 好问题 提建议
• 收藏

1条回答默认 最新

• 爱晚乏客游 2021-07-28 09:24

你模型的输入输出呢？输入是什么，输出又是什么？

评论
解决 无用
打赏 举报