《一个图像复原实例入门深度学习&TensorFlow—第一篇》深度学习和TensorFlow初探

版权声明:转载注明出处,谢谢~ https://blog.csdn.net/qq_43024357/article/details/81940183

深度学习和TensorFlow初探

1.什么是深度学习

深度学习,感觉很难的样子,但其实很Easy的(至少入门很Easy,Easy,Easy…心理暗示很重要),不就是给你一堆数据,算法学习数据内部的联系,经过训练学到东西之后,再处理类似问题,得到较好的处理结果这样一个过程嘛。如图:
这里写图片描述
上图不同于分类问题,labels不是输入数据的类别!这里是在做一个end-to-end learning,也就是让神经网络学习到images和labels之间的映射关系(转置),网络学到东西后,在输入测试图,网络输出近似看做测试图的转置。看似简单的一个映射关系,神经网络也需要大量的数据集来拟合,就像分类一样,你一看这就是一条狗,但是网络可不是那么容易识别,它需要大量训练集(包括images和labels,images是图片和labels是图片中是否是狗)来构建深度神经网络学习识别这到底是不是一条狗(所以就称为在做深度学习啦)。MNIST手写字符识别做的人已经太多了,所以本系列博客不再做分类问题,而是专注于解决这个端到端的深度学习问题,这种方法可以广泛的运用于图像分割、图像复原、图像去噪中,做图像处理的小伙伴Mark一下

2.什么是TensorFlows

TensorFlow comes from Google,一个超级简单的深度学习框架,之所以说它简单,是因为它大大降低了初学者构建深度神经网络的成本,有了它你可以快速构建网络,快速训练,快速测试,同时对训练过程进行实时监控。打个比方,做深度学习就像在挖坑,用C,C++,C#,java这些编程语言做深度学习,就像是在用自己勤劳的双手挖坑,用TensorFlow做深度学习,就像找来了一台挖掘机,挖坑效率强的不行。下面体会一下TensorFlow进行深度学习主要代码,初学者扫一眼就好,千万别细看!

import tensorflow as tf
import numpy as np
import input_data
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)

def weight_variable(shape):
  initial = tf.truncated_normal(shape, stddev=0.1) 
  return tf.Variable(initial)

def bias_variable(shape):
  initial = tf.constant(0.1, shape=shape)
  return tf.Variable(initial)
def conv2d(x, W):
  return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')

def max_pool_2x2(x):
  return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
  strides=[1, 2, 2, 1], padding='SAME')

x = tf.placeholder("float", shape=[None, 784])
y_ = tf.placeholder("float", shape=[None, 10])

W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])
x_image = tf.reshape(x, [-1,28,28,1])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])

h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)
W_fc1 = weight_variable([7 * 7 * 64, 1024])
b_fc1 = bias_variable([1024])

h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
keep_prob = tf.placeholder("float")
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])

y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)

cross_entropy = -tf.reduce_sum(y_*tf.log(y_conv))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
sess = tf.Session()
sess.run(tf.initialize_all_variables())

for i in range(100):
    batch = mnist.train.next_batch(50)
    if i%100 == 0:
        train_accuracy = sess.run(accuracy,feed_dict={ x:batch[0], y_: batch[1], keep_prob:1.0})
        print ("step %d, training accuracy %g"%(i, train_accuracy))
    sess.run(train_step,feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})      
predict = sess.run(y_conv,feed_dict={x: batch[0][0], y_: batch[1][0], keep_prob: 1})
print(predict)
sess.close()

内容暂且不管,代码没有想象中的多吧,几十行代码就实现了深度学习!这都是TensorFlow的功劳哦,所以学了TensorFlow,深度学习问题大不大,问题不大,几十行代码而已嘛,就算你一天搞懂两行,不出一个月你不也会用TensorFlow进行深度学习了嘛,悄悄告诉你,AlphaGo就是用TensorFlow构建出来的一个深度神经网络!

下面推荐通过Andrew Ng吴恩达的公开课补一些基础知识:重点看1、2、5、7、8、9、10章
http://study.163.com/course/introduction.htm?courseId=1004570029#/courseDetail?tab=1
公开课最好赶紧看完,而且要反复看,因为值得!
下一节让我们一起搭建深度学习框架,也就是把TensorFlow用起来。

猜你喜欢

转载自blog.csdn.net/qq_43024357/article/details/81940183