5-3tensorboard 网络运行
文件大小: 10k
源码售价: 10 个金币 积分规则     积分充值
资源说明:载入数据集 mnist=input_data.read_data_sets("MNIST_data",one_hot=True) #设定训练批次的大小 batch_size=50 #计算多少个批次 n_batch=mnist.train.num_examples//batch_size def variable_summaries(var): with tf.name_scope('summaries'): mean=tf.reduce_mean(var) tf.summary.scalar('mean',mean)#平均值 with tf.name_scope('stddev'): stddev=tf.sqrt(tf.reduce_mean(tf.square(var-mean))) tf.summary.scalar('stddev',stddev)#标准差 tf.summary.scalar('max',tf.reduce_max(var))#最大值 tf.summary.scalar('min',tf.reduce_max(var))#最小值 tf.summary.histogram('histogram',var)#直方图 #命名空间 with tf.name_scope('input'): #定义两个placeholder x=tf.placeholder(tf.float32,[None,784],name='x-input') y=tf.placeholder(tf.float32,[None,10],name='y-input') with tf.name_scope('layer'): #建立神经网络 with tf.name_scope('wights'): W=tf.Variable(tf.zeros([784,10]),name='W') variable_summaries(W) with tf.name_scope('biases'): b=tf.Variable(tf.zeros([10]),name='b') variable_summaries(b) with tf.name_scope('wx_plus_b'): wx_plus_b=tf.matmul(x,W)+b with tf.name_scope('softmax'): predicton=tf.nn.softmax(wx_plus_b) #定义二次代价函数 # loss=tf.reduce_mean(tf.square(y-predicton)) with tf.name_scope('loss'): loss=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=predicton)) tf.summary.scalar('loss',loss) with tf.name_scope('train'): #使用梯度下降法 train_step=tf.train.GradientDescentOptimizer(0.2).minimize(loss) #初始化变量 init=tf.global_variables_initializer() with tf.name_scope('accuracy'): with tf.name_scope('predicton_correct'): #预测结果用布尔型列表存放 predicton_correct=tf.equal(tf.argmax(y,1),tf.argmax(predicton,1))#argmax返回一维张量中最大值所在位置 with tf.name_scope('accuracy'): #计算准确率 accuracy=tf.reduce_mean(tf.cast(predicton_correct,tf.float32)) tf.summary.scalar('accuracy',accuracy) #h合并所有summary merged=tf.summary.merge_all() #建立会话 with tf.Session() as sess: sess.run(init) writer=tf.summary.FileWriter('logs/',sess.graph) #设置循环次数 for epoch in range(51): for batch in range(n_batch): batch_x,batch_y=mnist.train.next_batch(batch_size) summary,_=sess.run([merged,train_step],feed_dict={x:batch_x,y:batch_y}) writer.add_summary(summary,epoch) #导入测试集计算准确率 acc=sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels}) #打印正确率 print("Iter "+str(epoch)+",Testing Accuray "+str(acc))
本源码包内暂不包含可直接显示的源代码文件,请下载源码包。