從深度學習入門Tensorflow
阿新 • • 發佈:2018-12-13
Softmax實現MNIST識別
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data # 載入資料 mnist = input_data.read_data_sets("MNIST_data", one_hot=True) # None表示輸入任意數量的MNIST影象,每一張圖展平成784維的向量 # placeholder是佔位符,在訓練時指定 x = tf.placeholder(tf.float32, [None, 28*28]) y_ = tf.placeholder(tf.float32, [None, 10]) # 初始化W,b矩陣 w = tf.Variable(tf.zeros([28*28, 10])) b = tf.Variable(tf.zeros([10])) # 計算softmax y = tf.nn.softmax(tf.matmul(x, w) + b) # 計算交叉熵 cross_entropy = -tf.reduce_sum(y_ * tf.log(y)) # 模型的訓練,不斷的降低成本函式 # 要求TensorFlow用梯度下降演算法(gradient descent algorithm)以0.01的學習速率最小化交叉熵 train_step = tf.train.AdamOptimizer(0.01).minimize(cross_entropy) # 在執行計算之前,需要新增一個操作來初始化我們建立的變數 init = tf.global_variables_initializer() # 在Session裡面啟動我模型,並且初始化變數 sess = tf.Session() sess.run(init) # 開始訓練模型,迴圈訓練50次 for i in range(50): batch_xs, batch_ys = mnist.train.next_batch(100) sess.run(train_step, feed_dict={x:batch_xs, y_:batch_ys}) # 檢驗真實標籤與預測標籤是否一致 correct_pre = tf.equal(tf.arg_max(y, 1), tf.argmax(y_, 1)) # 計算精確度,將true和false轉化成相應的浮點數,求和取平均 accuracy = tf.reduce_mean(tf.cast(correct_pre, "float")) # 計算所學習到的模型在測試資料集上面的正確率 print(sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}))
CNN實現MNIST識別
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data # 載入資料 mnist = 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={xs:v_xs, keep_prob:1}) 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, keep_prob:1}) return result def weight_variable(shape): # tf.truncated_normal生成的值服從具有指定平均值和標準偏差的正態分佈,如果生成的值大於平均值2個標準偏差的值則丟棄重新選擇。 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): # stride[1,x_movement,y_movement, 1]前後兩位必須為1 # 在卷積核移動逐漸掃描整體圖時候,因為步長的設定問題,可能導致剩下未掃描的空間不足以提供給卷積核的, # 大小掃描 比如有圖大小為5*5,卷積核為2*2,步長為2,卷積核掃描了兩次後,剩下一個元素,不夠卷積核掃描了, # 這個時候就在後面補零,補完後滿足卷積核的掃描,這種方式就是same。如果說把剛才不足以掃描的元素位置拋棄掉,就是valid方式。 return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') def max_pool_2x2(x): # stride[1,x_movement,y_movement, 1]前後兩位必須為1 # ksize:池化視窗的大小,取一個四維向量,一般是[1, height, width, 1] return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') xs = tf.placeholder(tf.float32,[None, 784])/255 #28x28 ys = tf.placeholder(tf.float32,[None, 10]) keep_prob = tf.placeholder(tf.float32) # 把x變成一個4d向量,其第2、第3維對應圖片的寬、高,最後一維代表圖片的顏色通道數(因為是灰度圖所以這裡的通道數為1,如果是rgb彩色圖,則為3)。 x_image = tf.reshape(xs, [-1,28,28,1]) ## conv1 layer ## W_conv1 = weight_variable([5,5,1,32]) # patch 5x5, in_size 1, outsize 32 b_conv1 = bias_variable([32]) h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) # 28x28x32 h_pool1 = max_pool_2x2(h_conv1) # 14x14x32 ## conv2 layer ## W_conv2 = weight_variable([5,5,32,64]) # patch 5x5, in_size 32, outsize 64 b_conv2 = bias_variable([64]) h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) # 14x14x64 h_pool2 = max_pool_2x2(h_conv2) # 7x7x64 ## 全連線 ## 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) h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) ## softmax ## W_fc2 = weight_variable([1024, 10]) b_fc2 = bias_variable([10]) prediction = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys*tf.log(prediction), reduction_indices=[1])) train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy) sess = tf.Session() sess.run(tf.global_variables_initializer()) if int((tf.__version__).split('.')[1]) < 12 and int((tf.__version__).split('.')[0]) < 1: init = tf.initialize_all_variables() else: init = tf.global_variables_initializer() sess.run(init) for i in range(100): batch_xs, batch_ys = mnist.train.next_batch(100) sess.run(train_step, feed_dict={xs: batch_xs, ys: batch_ys, keep_prob: 0.5}) if i % 50 == 0: print(compute_accuracy( mnist.test.images[:1000], mnist.test.labels[:1000]))
Activation Functions tf.nn.relu(features, name=None) tf.nn.relu6(features, name=None) tf.nn.softplus(features, name=None) tf.nn.dropout(x, keep_prob, noise_shape=None, seed=None, name=None) tf.nn.bias_add(value, bias, name=None) tf.sigmoid(x, name=None) tf.tanh(x, name=None) Convolution tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, name=None) tf.nn.depthwise_conv2d(input, filter, strides, padding, name=None) tf.nn.separable_conv2d(input, depthwise_filter, pointwise_filter, strides, padding, name=None) Pooling tf.nn.avg_pool(value, ksize, strides, padding, name=None) tf.nn.max_pool(value, ksize, strides, padding, name=None) tf.nn.max_pool_with_argmax(input, ksize, strides, padding, Targmax=None, name=None) Normalization tf.nn.l2_normalize(x, dim, epsilon=1e-12, name=None) tf.nn.local_response_normalization(input, depth_radius=None, bias=None, alpha=None, beta=None, name=None) tf.nn.moments(x, axes, name=None) Losses tf.nn.l2_loss(t, name=None) Classification tf.nn.sigmoid_cross_entropy_with_logits(logits, targets, name=None) tf.nn.softmax(logits, name=None) tf.nn.softmax_cross_entropy_with_logits(logits, labels, name=None) Embeddings tf.nn.embedding_lookup(params, ids, name=None) Evaluation tf.nn.top_k(input, k, name=None) tf.nn.in_top_k(predictions, targets, k, name=None) Candidate Sampling Sampled Loss Functions tf.nn.nce_loss(weights, biases, inputs, labels, num_sampled, num_classes, num_true=1, sampled_values=None, remove_accidental_hits=False, name='nce_loss') tf.nn.sampled_softmax_loss(weights, biases, inputs, labels, num_sampled, num_classes, num_true=1, sampled_values=None, remove_accidental_hits=True, name='sampled_softmax_loss') Candidate Samplers tf.nn.uniform_candidate_sampler(true_classes, num_true, num_sampled, unique, range_max, seed=None, name=None) tf.nn.log_uniform_candidate_sampler(true_classes, num_true, num_sampled, unique, range_max, seed=None, name=None) tf.nn.learned_unigram_candidate_sampler(true_classes, num_true, num_sampled, unique, range_max, seed=None, name=None) tf.nn.fixed_unigram_candidate_sampler(true_classes, num_true, num_sampled, unique, range_max, vocab_file='', distortion=0.0, num_reserved_ids=0, num_shards=1, shard=0, unigrams=[], seed=None, name=None) Miscellaneous candidate sampling utilities tf.nn.compute_accidental_hits(true_classes, sampled_candidates, num_true, seed=None, name=None)
Images
Encoding and Decoding
tf.image.decode_jpeg(contents, channels=None, ratio=None, fancy_upscaling=None, try_recover_truncated=None, acceptable_fraction=None, name=None)
tf.image.encode_jpeg(image, format=None, quality=None, progressive=None, optimize_size=None, chroma_downsampling=None, density_unit=None, x_density=None, y_density=None, xmp_metadata=None, name=None)
tf.image.decode_png(contents, channels=None, name=None)
tf.image.encode_png(image, compression=None, name=None)
Resizing
tf.image.resize_images(images, new_height, new_width, method=0)
tf.image.resize_area(images, size, name=None)
tf.image.resize_bicubic(images, size, name=None)
tf.image.resize_bilinear(images, size, name=None)
tf.image.resize_nearest_neighbor(images, size, name=None)
Cropping
tf.image.resize_image_with_crop_or_pad(image, target_height, target_width)
tf.image.pad_to_bounding_box(image, offset_height, offset_width, target_height, target_width)
tf.image.crop_to_bounding_box(image, offset_height, offset_width, target_height, target_width)
tf.image.random_crop(image, size, seed=None, name=None)
tf.image.extract_glimpse(input, size, offsets, centered=None, normalized=None, uniform_noise=None, name=None)
Flipping and Transposing
tf.image.flip_up_down(image)
tf.image.random_flip_up_down(image, seed=None)
tf.image.flip_left_right(image)
tf.image.random_flip_left_right(image, seed=None)
tf.image.transpose_image(image)
Image Adjustments
tf.image.adjust_brightness(image, delta, min_value=None, max_value=None)
tf.image.random_brightness(image, max_delta, seed=None)
tf.image.adjust_contrast(images, contrast_factor, min_value=None, max_value=None)
tf.image.random_contrast(image, lower, upper, seed=None)
tf.image.per_image_whitening(image)