tensorflow

tensorflow的Summary用法

https://www.cnblogs.com/lyc-seu/p/8647792.html

tf.summary.merge_all():将所有summary全部保存到磁盘,以便tensorboard显示。如果没有特殊要求,一般用这一句就可一显示训练时的各种信息了。

sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init) 

import tensorflow as tf
#损失
with tf.name_scope('loss'): #损失
    loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys-output2),reduction_indices=[1]))
    tf.summary.scalar('loss',loss)

tf.summary.histogram('hidden_layer/weight', W1)
tf.summary.scalar('accuracy',acc)                   #生成准确率标量图  
merge_summary = tf.summary.merge_all()  # #将图形、训练过程等数据合并在一起
train_writer = tf.summary.FileWriter('logs/',sess.graph)# #将训练日志写入到logs文件夹
......(交叉熵、优化器等定义)  
for step in xrange(training_step):                  #训练循环  
    train_summary = sess.run(merge_summary,feed_dict =  {...})#调用sess.run运行图,生成一步的训练过程数据  
    train_writer.add_summary(train_summary,step)#调用train_writer的add_summary方法将训练过程以及训练步数保存
train_writer.close()

执行上述代码,会在“当前路径/logs”目录下生成一个events.out.tfevents.{time}.{machine-name}的文件。在当前目录新建“查看训练过程.bat”,里面输入" tensorboard --logdir=logs "。执行上述bat文件,打开浏览器,输入地址:http://localhost:6006,就可以查看训练过程中的各种图形

X = tf.placeholder(tf.float64, [None, num_input])
tf.Variable(tf.random_normal([num_input, num_hidden_1], dtype=tf.float64))#初始化
#nn
layer_1 = tf.nn.relu(tf.add(tf.matmul(x, weights['encoder_h1']), biases['encoder_b1']))
# 定义损失函数和优化器,最小化square error
loss = tf.losses.mean_squared_error(y_true, y_pred)
optimizer = tf.train.RMSPropOptimizer(0.03).minimize(loss)
# 变量初始化
init = tf.global_variables_initializer()
# 在session中run
with tf.Session() as session:
    session.run(init)
    for i in range(epochs):
        for batch in matrix:
            _, l = session.run([optimizer, loss], feed_dict={X: batch})
    preds = session.run(decoder_op, feed_dict={X: matrix})

tensorflow版本和设备查看

# Creates a graph.
with tf.device('/device:GPU:0'):
    a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
    b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
    c = tf.matmul(a, b)
# Creates a session with log_device_placement set to True.
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True,allow_soft_placement=True))

# Runs the op.
options = tf.RunOptions(output_partition_graphs=True)
metadata = tf.RunMetadata()
c_val = sess.run(c, options=options, run_metadata=metadata)

print (metadata.partition_graphs)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值