从深度学习入门Tensorflow

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40006058/article/details/82935556

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]))

多层神经网络实现MNIST识别

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
# 加载数据
mnist = input_data.read_data_sets("MNIST_data", one_hot=True)
batch_size = 100
hidden1_nodes = 200
# 输入节点
x = tf.placeholder(tf.float32,shape=(None,784))
y = tf.placeholder(tf.float32,shape=(None,10))
# 权值&隐藏层
w1 = tf.Variable(tf.random_normal([784,hidden1_nodes],stddev=0.1))
w2 = tf.Variable(tf.random_normal([hidden1_nodes,10],stddev=0.1))
b1 = tf.Variable(tf.random_normal([hidden1_nodes],stddev=0.1))
b2 = tf.Variable(tf.random_normal([10],stddev=0.1))
hidden = tf.nn.relu(tf.matmul(x,w1)+b1)
y_predict = tf.nn.relu(tf.matmul(hidden,w2)+b2)
# 损失函数
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=y_predict))
# 设置学习率
global_step = tf.Variable(0, name='global_step', trainable=False)    # 全局的一个计数器
learning_rate = tf.train.exponential_decay(0.1, global_step, 100, 0.96, staircase=True)
# 训练函数
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy,global_step=global_step)
# 测试函数
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_predict, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
# 执行
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for i in range(501):
        print ('Learning_rate:',sess.run(learning_rate),'Global_step:',sess.run(global_step))
        batch_xs, batch_ys = mnist.train.next_batch(batch_size)
        sess.run(train_step, feed_dict={x: batch_xs, y: batch_ys})
        if i%100==0:
            print (sess.run(accuracy, feed_dict={x: mnist.test.images, y:mnist.test.labels}))

基本命令【点击查看所有命令

CNN基本命令

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)

猜你喜欢

转载自blog.csdn.net/qq_40006058/article/details/82935556