当我运行以下脚本时,我注意到以下几个错误:
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)
[nan] [nan] [nan] [nan] [nan] [nan] [nan]]回溯(大多数 最近的电话):
...
文件 " /Users/aidanrockea/anaconda/lib/python3.6/site-packages/seaborn/matrix.py" ;, 热线图中的485行 yticklabels,mask)
文件 " /Users/aidanrockea/anaconda/lib/python3.6/site-packages/seaborn/matrix.py" ;, 第167行,在 init 中 cmap,center,robust)
文件 " /Users/aidanrockea/anaconda/lib/python3.6/site-packages/seaborn/matrix.py" ;, 第206行,在_determine_cmap_params中 vmin = np.percentile(calc_data,2)if robust else calc_data.min()
文件 " /Users/aidanrockea/anaconda/lib/python3.6/site-packages/numpy/core/_methods.py" ;, 第29行,在_amin return umr_minimum(a,axis,None,out,keepdims)
ValueError:零大小数组到减少操作的最小值 没有身份
ValueError:变量model / w_h / Adagrad /已经存在,不允许。 你的意思是在VarScope中设置reuse = True或reuse = tf.AUTO_REUSE吗?
我不清楚为什么会出现这些错误。此外,当我使用:
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的常量函数的网络。现在我的预感是这可能只是一个数字问题,但我可能错了。
如果是这样,这是一个严重的问题,因为我在这里使用的模型非常简单。
答案 0 :(得分:1)
现在我的预感是这可能只是一个数字问题
的确,在运行potential(100)
时,我有时会获得与1E21
一样大的值。最大的点将主导您的损失功能,并将驱动网络参数。
即使规范化目标值,例如对于单位方差,主导损失的最大值的问题仍然存在(例如在plt.hist(np.log(potential(100)[1]), bins = 100)
)。
如果可以,请尝试学习val
而不是val
本身的日志。但请注意,那么您正在改变损失函数的假设,以及预测遵循围绕目标值的正态分布'日志预测遵循目标值对数的正态分布'。