Python 3.x Tensorflow Adagrad优化器是';行不通
运行以下脚本时,我注意到以下两个错误:Python 3.x Tensorflow Adagrad优化器是';行不通,python-3.x,tensorflow,neural-network,Python 3.x,Tensorflow,Neural Network,运行以下脚本时,我注意到以下两个错误: import tensorflow as tf import numpy as np import seaborn as sns import random #set random seed: random.seed(42) def potential(N): points = np.random.rand(N,2)*10 values = np.array([np.exp((points[i][0]-5.0)**2 + (poi
import tensorflow as tf
import numpy as np
import seaborn as sns
import random
#set random seed:
random.seed(42)
def potential(N):
points = np.random.rand(N,2)*10
values = np.array([np.exp((points[i][0]-5.0)**2 + (points[i][1]-5.0)**2) for i in range(N)])
return points, values
def init_weights(shape,var_name):
"""
Xavier initialisation of neural networks
"""
init = tf.contrib.layers.xavier_initializer()
return tf.get_variable(initializer=init,name = var_name,shape=shape)
def neural_net(X):
with tf.variable_scope("model",reuse=tf.AUTO_REUSE):
w_h = init_weights([2,10],"w_h")
w_h2 = init_weights([10,10],"w_h2")
w_o = init_weights([10,1],"w_o")
### bias terms:
bias_1 = init_weights([10],"bias_1")
bias_2 = init_weights([10],"bias_2")
bias_3 = init_weights([1],"bias_3")
h = tf.nn.relu(tf.add(tf.matmul(X, w_h),bias_1))
h2 = tf.nn.relu(tf.add(tf.matmul(h, w_h2),bias_2))
return tf.nn.relu(tf.add(tf.matmul(h2, w_o),bias_3))
X = tf.placeholder(tf.float32, [None, 2])
with tf.Session() as sess:
model = neural_net(X)
## define optimizer:
opt = tf.train.AdagradOptimizer(0.0001)
values =tf.placeholder(tf.float32, [None, 1])
squared_loss = tf.reduce_mean(tf.square(model-values))
## define model variables:
model_vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,"model")
train_model = opt.minimize(squared_loss,var_list=model_vars)
sess.run(tf.global_variables_initializer())
for i in range(10):
points, val = potential(100)
train_feed = {X : points,values: val.reshape((100,1))}
sess.run(train_model,feed_dict = train_feed)
print(sess.run(model,feed_dict = {X:points}))
### plot the approximating model:
res = 0.1
xy = np.mgrid[0:10:res, 0:10:res].reshape(2,-1).T
values = sess.run(model, feed_dict={X: xy})
sns.heatmap(values.reshape((int(10/res),int(10/res))),xticklabels=False,yticklabels=False)
for i in range(10):
points, val = potential(10)
train_feed = {X : points,values: val.reshape((10,1))}
sess.run(train_model,feed_dict = train_feed)
print(sess.run(model,feed_dict = {X:points}))
我发现在第一次运行时,我有时会得到一个网络,该网络已塌陷为常量函数,输出为0。现在我的直觉是,这可能只是一个数字问题,但我可能错了
如果是这样,这是一个严重的问题,因为我在这里使用的模型非常简单
现在我的直觉是这可能只是一个数字问题
实际上,当运行potential(100)
时,我有时会得到与1E21
一样大的值。最大点将支配损失函数,并驱动网络参数
即使将目标值标准化,例如单位方差,最大值主导损失的问题仍然存在(例如查看plt.hist(np.log(潜在(100)[1]),bin=100)
)
如果可以,请尝试学习
val
的日志,而不是val
本身。但是请注意,然后您将损失函数的假设从“预测遵循目标值周围的正态分布”更改为“日志预测遵循目标值周围的正态分布”。添加完整的控制台消息而不仅仅是错误行可能会有所帮助。