MNIST Tensorflow 笔记

# coding: utf-8


# In[1]:


import tensorflow as tf




# In[2]:


import numpy as np




# In[3]:


import tensorflow.examples.tutorials.mnist.input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)




# In[4]:


from tensorflow.examples.tutorials.mnist import input_data




# In[5]:


mnist = input_data.read_data_sets("MNIST_data/", one_hot=True




# In[6]:


mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)




# In[7]:


x = tf.placeholder(tf.float32,[None,784])




# In[15]:


W = tf.Variable(tf.zeros([784,10]))




# In[16]:


b = tf.Variable(tf.zeros([10]))




# In[18]:


yy = tf.matmul(x,W) + b




# In[19]:


y = tf.nn.softmax(yy)




# In[20]:


print(y)




# In[26]:


#建立损失函数




# In[23]:


y_ = tf.placeholder(tf.float32,[None,10])




# In[24]:


loss_func = y_*tf.log(y)




# In[25]:


cost_func = -tf.reduce_sum(loss_func)




# In[27]:


#建立训练




# In[28]:


learning_rate = 0.0001




# In[29]:


train_step = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost_func)




# In[30]:


#初始化




# In[31]:


init = tf.global_variables_initializer()




# In[32]:


sess = tf.Session()




# In[33]:


sess.run(init)




# In[70]:


#训练
#进行1000次循环训练,每次取样100




# In[71]:


for i in range(1000):
    batch_xs, batch_ys = mnist.train.next_batch(100)
    sess.run(train_step,feed_dict={x: batch_xs, y_: batch_ys})




# In[72]:


#验证
#用test集做验证




# In[73]:


correct_pred = tf.equal(tf.argmax(y,1),tf.argmax(y_,1))




# In[74]:


accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))




# In[75]:


hit_rate = sess.run(accuracy, feed_dict={x: mnist.test.images, y_:mnist.test.labels})




# In[76]:


print(hit_rate)




# In[63]:


#测试
#测试集第7张图片




# In[77]:


import cv2




# In[78]:


try_img = cv2.threshold(mnist.test.images[5].reshape(28,28),0.2,255,cv2.THRESH_BINARY)




# In[79]:


cv2.imwrite('5.bmp',mnist.test.images[5].reshape(28,28)*255)




# In[85]:


number = tf.argmax(y,1)
print(sess.run(number,feed_dict={x: [mnist.test.images[5]]}))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值