莫烦TENSORFLOW(5)

import tensorflow as tf
import numpy as np
import 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)
Wx_plus_b = tf.matmul(inputs,Weights) + biases

if activation_function is None:
outputs = Wx_plus_b
else:
outputs = activation_function(Wx_plus_b)
return outputs

#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

xs = tf.placeholder(tf.float32,[None,1])
ys = tf.placeholder(tf.float32,[None,1])

l1 = add_layer(xs,1,10,activation_function=tf.nn.relu)
prediction = add_layer(l1,10,1,activation_function=None)

loss = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction),reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.1).minimize(loss)

init = tf.initialize_all_variables()

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

fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.scatter(x_data,y_data)
plt.ion()
plt.show()
for i in range(1000):
sess.run(train_step,feed_dict={xs:x_data,ys:y_data})
if i%50 == 0:
# print(sess.run(loss,feed_dict={xs:x_data,ys:y_data}))
try:
ax.lines.remove(lines[0])
except Exception:
pass
prediction_value = sess.run(prediction,feed_dict={xs:x_data,ys:y_data})
lines = ax.plot(x_data,prediction_value,‘-r‘,lw=5)
plt.pause(0.1)

时间: 2024-10-09 03:50:36

莫烦TENSORFLOW(5)的相关文章

莫烦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(6)-tensorboard

import tensorflow as tfimport 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_sco

莫烦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