莫烦TENSORFLOW(6)-tensorboard

import tensorflow as tf
import numpy as np

def add_layer(inputs,in_size,out_size,n_layer,activation_function=None):
# add one more layer and return the output of this layer
layer_name = ‘layer%s‘ % n_layer
with tf.name_scope(‘layer‘):
with tf.name_scope(‘weights‘):
Weights = tf.Variable(tf.random_normal([in_size,out_size]),name=‘W‘)
tf.summary.histogram(layer_name+‘/weights‘,Weights)
with tf.name_scope(‘biases‘):
biases = tf.Variable(tf.zeros([1,out_size]) + 0.1,name=‘b‘)
with tf.name_scope(‘Wx_plus_b‘):
Wx_plus_b = tf.add(tf.matmul(inputs,Weights),biases)
tf.summary.histogram(layer_name+‘/biases‘,biases)
if activation_function is None:
outputs = Wx_plus_b
else:
outputs = activation_function(Wx_plus_b)
tf.summary.histogram(layer_name+‘/outputs‘,outputs)
return outputs

# make up some real data
x_data =np.linspace(-1,1,300)[:,np.newaxis]
noise = np.random.normal(0,0.05,x_data.shape)
y_data = np.square(x_data)-0.5+noise

with tf.name_scope(‘inputs‘):
xs = tf.placeholder(tf.float32,[None,1],name=‘x_input‘)
ys = tf.placeholder(tf.float32,[None,1],name=‘y_input‘)

# create hidden layer
l1 = add_layer(xs,1,10,1,activation_function=tf.nn.relu)
# create output layer
prediction = add_layer(l1,10,1,2,activation_function=None)
# the error between prediction adn real data
with tf.name_scope(‘loss‘):
loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction),reduction_indices=[1]))
tf.summary.scalar(‘loss‘,loss)
with tf.name_scope(‘train‘):
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)

sess = tf.Session()
merged = tf.summary.merge_all()
writer = tf.summary.FileWriter("logs/",sess.graph)

# import step
sess.run(tf.global_variables_initializer())

for i in range(1000):
sess.run(train_step,feed_dict={xs:x_data,ys:y_data})
if i%50 == 0:
result = sess.run(merged,feed_dict={xs:x_data,ys:y_data})
writer.add_summary(result,i)

时间: 2024-10-10 04:01:24

莫烦TENSORFLOW(6)-tensorboard的相关文章

莫烦TENSORFLOW(4)-placeholder

import tensorflow as tf input1 = tf.placeholder(tf.float32)input2 = tf.placeholder(tf.float32) output = tf.multiply(input1,input2) with tf.Session() as sess: print(sess.run(output,feed_dict={input1:[7.],input2:[2.0]}))

莫烦TENSORFLOW(5)

import tensorflow as tfimport numpy as npimport matplotlib.pyplot as plt def add_layer(inputs,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) W

莫烦TENSORFLOW(7)-mnist

import tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_data#number 1 to 10 datamnist = input_data.read_data_sets('MNIST_data',one_hot=True) def add_layer(inputs,in_size,out_size,activation_function=None): Weights = tf.Variable(t

莫烦TENSORFLOW(3)-Variable

import tensorflow as tf state = tf.Variable(0,name='counter') one = tf.constant(1) new_value = tf.add(state,one)update = tf.assign(state,new_value) init = tf.initialize_all_variables()#must have if define variable with tf.Session() as sess: sess.run(

莫烦tensorflow(9)-Save&Restore

import tensorflow as tfimport numpy as np ##save to file#rember to define the same dtype and shape when restore# W = tf.Variable([[1,2,3],[3,4,5]],dtype=tf.float32,name='Weights')# b = tf.Variable([[1,2,3]],dtype=tf.float32,name='biases') # init = tf

莫烦tensorflow(1)-训练线性函数模型

import tensorflow as tfimport numpy as np #create datax_data = np.random.rand(100).astype(np.float32)y_data = x_data*0.1+0.3 ####create tensorflow structure start###Weights = tf.Variable(tf.random_uniform([1],-1.0,1.0))biases = tf.Variable(tf.zeros([

莫烦tensorflow(8)-CNN

import tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_data#number 1 to 10 datamnist = input_data.read_data_sets('MNIST_data',one_hot=True) def compute_accuracy(v_xs,v_ys): global prediction y_pre = sess.run(prediction,feed_dict

莫烦tensorflow(2)-Session

import os os.environ['TF_CPP_MIN_LOG_LEVEL']='2' import tensorflow as tfmatrix1 = tf.constant([[3,3]])matrix2 = tf.constant([[2],[2]])protuct = tf.matmul(matrix1,matrix2) # sess = tf.Session()# result = sess.run(protuct) # print(result)# sess.close()

tensorflow 莫烦教程

1,感谢莫烦 2,第一个实例:用tf拟合线性函数 import tensorflow as tf import numpy as np # create data x_data = np.random.rand(100).astype(np.float32) y_data = x_data*0.1 + 0.3 #先创建我们的线性函数目标 #搭建模型 Weights = tf.Variable(tf.random_uniform([1], -1.0, 1.0)) biases = tf.Varia