莫烦TENSORFLOW(7)-mnist

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
#number 1 to 10 data
mnist = input_data.read_data_sets(‘MNIST_data‘,one_hot=True)

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

def compute_accuracy(v_xs,v_ys):
global prediction
y_pre = sess.run(prediction,feed_dict={xs:v_xs})
correct_prediction = tf.equal(tf.argmax(y_pre,1),tf.argmax(v_ys,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
result = sess.run(accuracy,feed_dict={xs:v_xs,ys:v_ys})
return result
#define placeholder for inputs to network
xs = tf.placeholder(tf.float32,[None,784])
ys = tf.placeholder(tf.float32,[None,10])

#add output layer
prediction = add_layer(xs,784,10,activation_function=tf.nn.softmax)

#the error between prediction and real data
cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys*tf.log(prediction),reduction_indices=[1]))#loss
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)

sess = tf.Session()

#important step
sess.run(tf.initialize_all_variables())

for i in range(1000):
batch_xs,batch_ys = mnist.train.next_batch(100)
sess.run(train_step,feed_dict={xs:batch_xs,ys:batch_ys})
if i%50 == 0:
print(compute_accuracy(mnist.test.images,mnist.test.labels))

时间: 2024-07-31 17:56:35

莫烦TENSORFLOW(7)-mnist的相关文章

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