tensorflow训练中出现nan问题的解决
作者:你不来我不老 时间:2023-02-10 09:34:09
深度学习中对于网络的训练是参数更新的过程,需要注意一种情况就是输入数据未做归一化时,如果前向传播结果已经是[0,0,0,1,0,0,0,0]这种形式,而真实结果是[1,0,0,0,0,0,0,0,0],此时由于得出的结论不惧有概率性,而是错误的估计值,此时反向传播会使得权重和偏置值变的无穷大,导致数据溢出,也就出现了nan的问题。
解决办法:
1、对输入数据进行归一化处理,如将输入的图片数据除以255将其转化成0-1之间的数据;
2、对于层数较多的情况,各层都做batch_nomorlization;
3、对设置Weights权重使用tf.truncated_normal(0, 0.01, [3,3,1,64])生成,同时值的均值为0,方差要小一些;
4、激活函数可以使用tanh;
5、减小学习率lr。
实例:
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('data',one_hot = True)
def add_layer(input_data,in_size, out_size,activation_function=None):
Weights = tf.Variable(tf.random_normal([in_size,out_size]))
Biases = tf.Variable(tf.zeros([1, out_size])+0.1)
Wx_plus_b = tf.add(tf.matmul(input_data, Weights), Biases)
if activation_function==None:
outputs = Wx_plus_b
else:
outputs = activation_function(Wx_plus_b)
#return outputs#, Weights
return {'outdata':outputs, 'w':Weights}
def get_accuracy(t_y):
# global l1
# accu = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(l1['outdata'],1),tf.argmax(t_y,1)), dtype = tf.float32))
global prediction
accu = tf.reduce_mean(tf.cast(tf.equal(tf.argmax(prediction['outdata'],1),tf.argmax(t_y,1)), dtype = tf.float32))
return accu
X = tf.placeholder(tf.float32, [None, 784])
Y = tf.placeholder(tf.float32, [None, 10])
#l1 = add_layer(X, 784, 10, tf.nn.softmax)
#cross_entropy = tf.reduce_mean(-tf.reduce_sum(Y*tf.log(l1['outdata']), reduction_indices= [1]))
#l1 = add_layer(X, 784, 1024, tf.nn.relu)
l1 = add_layer(X, 784, 1024, None)
prediction = add_layer(l1['outdata'], 1024, 10, tf.nn.softmax)
cross_entropy = tf.reduce_mean(-tf.reduce_sum(Y*tf.log(prediction['outdata']), reduction_indices= [1]))
optimizer = tf.train.GradientDescentOptimizer(0.000001)
train = optimizer.minimize(cross_entropy)
newW = tf.Variable(tf.random_normal([1024,10]))
newOut = tf.matmul(l1['outdata'],newW)
newSoftMax = tf.nn.softmax(newOut)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
#print(sess.run(l1_Weights))
for i in range(2):
X_train, y_train = mnist.train.next_batch(1)
X_train = X_train/255 #需要进行归一化处理
#print(sess.run(l1['w'],feed_dict={X:X_train}))
#print(sess.run(prediction['w'],feed_dict={X:X_train, Y:y_train}))
#print(sess.run(l1['outdata'],feed_dict={X:X_train, Y:y_train}).shape)
print(sess.run(prediction['outdata'],feed_dict={X:X_train, Y:y_train}))
print(sess.run(newOut, feed_dict={X:X_train}))
print(sess.run(newSoftMax, feed_dict={X:X_train}))
print(y_train)
#print(sess.run(l1['outdata'], feed_dict={X:X_train}))
sess.run(train, feed_dict={X:X_train, Y:y_train})
if i%100 == 0:
#print(sess.run(cross_entropy, feed_dict={X:X_train, Y:y_train}))
accuracy = get_accuracy(mnist.test.labels)
print(sess.run(accuracy,feed_dict={X:mnist.test.images}))
#if i%100==0:
#print(sess.run(prediction, feed_dict={X:X_train}))
#print(sess.run(cross_entropy, feed_dict={X:X_train,Y:y_train}))
来源:http://blog.csdn.net/fireflychh/article/details/73691373
标签:tensorflow,nan
0
投稿
猜你喜欢
python使用代理ip访问网站的实例
2022-02-08 08:16:36
python列表倒序的几种方法(切片、reverse()、reversed())
2022-01-28 02:46:52
python 实现一个图形界面的汇率计算器
2021-05-07 07:12:24
python如何运行js语句
2022-04-10 02:34:27
vue 自定义全局方法,在组件里面的使用介绍
2024-05-29 22:46:17
pyqt5 使用cv2 显示图片,摄像头的实例
2023-09-28 10:33:24
C#使用checkedListBox1控件链接数据库的方法示例
2024-01-24 19:15:09
详解pygame捕获键盘事件的两种方式
2021-02-27 08:51:54
ASP下批量删除数据的两种方法
2011-02-05 11:01:00
PHP把空格、换行符、中文逗号等替换成英文逗号的正则表达式
2024-04-10 10:56:49
一道sql面试题附答案
2024-01-18 04:50:33
Django框架下在URLconf中指定视图缓存的方法
2023-10-03 01:54:28
基于SQL Server OS的任务调度机制详解
2024-01-14 22:17:59
浅谈Python大神都是这样处理XML文件的
2021-09-20 22:40:42
微信小程序列表渲染功能之列表下拉刷新及上拉加载的实现方法分析
2024-05-11 09:34:44
一些关于SQL2005+ASP.NET2.0的问题
2007-09-23 13:01:00
Python telnet登陆功能实现代码
2022-08-13 01:52:56
python机器学习Sklearn实战adaboost算法示例详解
2023-08-15 06:35:59
Golang中HTTP服务的分析与设计详解
2024-05-25 15:12:57
Jsp+Servlet实现文件上传下载 删除上传文件(三)
2023-06-27 16:29:29