码迷,mamicode.com
首页 > 其他好文 > 详细

Tensorflow细节-P80-深度神经网络

时间:2019-10-02 13:08:23      阅读:80      评论:0      收藏:0      [点我收藏+]

标签:art   i+1   init   path   db2   png   puts   write   细节   

1、本节多为复习内容,从以下图片可见一般:
技术图片
技术图片
2、学会使用
from numpy.random import RandomState
然后

rdm = RandomState(1)
dataset_size = 128
X = rdm.rand(dataset_size, 2)
Y = [[(x1 + x2) + rdm.rand() / 10.0-0.05] for(x1, x2) in X]

进行赋值的时候就可以不变了

import tensorflow as tf
from numpy.random import RandomState


batch_size=8
with tf.name_scope("inputs"):
    xs = tf.placeholder(tf.float32, [None, 2], name="xs")
    ys = tf.placeholder(tf.float32, [None, 1], name="ys")


with tf.variable_scope("get_variable"):
    w1 = tf.get_variable("w1", [2, 1], tf.float32, tf.truncated_normal_initializer(seed=1))
    b1 = tf.get_variable("b1", [1], tf.float32, tf.zeros_initializer())

with tf.name_scope("op"):
    y = tf.matmul(xs, w1) + b1
with tf.name_scope("loss_op"):
    loss = tf.reduce_mean(tf.where(tf.greater(ys, y), (ys-y)*1, (y-ys)*10))
    tf.summary.scalar("loss", loss)
train_step = tf.train.GradientDescentOptimizer(0.001).minimize(loss)

rdm = RandomState(1)
dataset_size = 128
X = rdm.rand(dataset_size, 2)
Y = [[(x1 + x2) + rdm.rand() / 10.0-0.05] for(x1, x2) in X]

merged = tf.summary.merge_all()
with tf.Session() as sess:
    writer = tf.summary.FileWriter("path/", graph=tf.get_default_graph())
    tf.global_variables_initializer().run()
    for i in range (5000):
        start = i*batch_size % dataset_size
        end = min((i+1)*batch_size% dataset_size, dataset_size)
        train_op = sess.run(train_step, feed_dict={xs: X, ys: Y})
        if i % 100 == 0:
            result, losses = sess.run([merged, loss], feed_dict={xs: X, ys: Y})
            print("After %d , loss is %g" % (i, losses))
            writer.add_summary(result, i)
writer.close()

Tensorflow细节-P80-深度神经网络

标签:art   i+1   init   path   db2   png   puts   write   细节   

原文地址:https://www.cnblogs.com/liuboblog/p/11616998.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!