Tensorflow基础语法总结

一、常规语法

1. GPU设置

	gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.67)
	sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
	sess.run(tf.global_variables_initializer())
	…….
	sess.close()

2. 定义常量 tf.constant

	tf.constant(val, shape= )
	tf.constant([3, 3])

3. 定义变量 tf.Variable

	tf.Variable(var, dtype= )

4. 初始化变量 tf.random_normal / tf.truncated_normal

	tf.set_random_seed(1) # optional, to fix the random matrix
	tf.truncated_normal(shape, stddev= )  # if mean > 2*stddev, then drop
	tf.random_normal(shape, mean= , stddev= , dtype=tf.float32)

5. 卷积层  tf.nn.conv2d

	tf.nn.conv2d(x, W, strides=[1,1,1,1], padding=’SAME’)\
	# stride[0], stride[3]  default is 1,  stride[1], stride[2]: x, y move steps

6. 池化层 tf.nn.max_pool

	tf.nn.max_pool(x, ksize=[1,2,2,1], strides=[1,2,2,1], padding=’SAME’)

7. Dropout: tf.nn.dropout

	keep_prob = tf.placeholder(tf.float32)
	hidden_fc1_drop = tf.nn.dropout(hidden_fc1,  keep_prob)
	…….
	feed_dict={x: batch[0], y: batch[1], keep_prob=1.0} # train
	feed_ditc={x: batch[0], y: batch[1], keep_prob=0.8} # test

8. 激活函数 tf.nn.relu  /  tf.nn.softmax

	tf.nn.relu()
	tf.nn.softmax()

9. reshape: tf.reshape

	tf.reshape(x, [-1, 28, 28, 1])
	# -1: neglect input num, (h, w) <==> (28x28), channel 1

10. 判断正误 tf.equal  /  tf.cast

	correct_prediction = tf.equal(A, B) # AB the same shape, return a boolean array
	tf.cast(correct_prediction, tf.float32) # True: 1, False: 0

11. 矩阵运算  tf.matmul  /  tf.add

	tf.matmul(X, W)
	tf.add(a, b)

12. 均值 / 求和  tf.reduce_mean  /  tf.reduce_sum

	# Computes the mean / sum of the elements across dimensions of a tensor.
	accuracy = tf.reduce_mean(arr, axis=1)
	print(accuracy.eval(session= , feed_dict={x: batch[0], y: batch[1], keep_prob: 1.0}))
	tf.reduce_sum(arr, axis=0)   <==>  tf.sum(arr)

13. Adam优化器 tf.train.AdamOptimizer

	train_step = tf.train.AdamOptimizer(lr).minimize(cross_entropy)
	train_step.run(session=see, feed_dict{x: batch[0], y_: batch[1], keep_prob: 0.8})

14. 损失函数

	# Cross Entropy:    Hy‘(y):=−∑i y′ilog(yi)
	y_ = tf.placeholder(tf.float32, [None, 10]) # acutal output
	cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_*tf.log(prediction), reduction_indices=[1]))
	
	# label for no sparse: one-hot, label for sparse: int
	cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(prediction, y_))
	cost_int = tf.sparse_softmax_cross_entropy_with_logits( )

15. 变量初始化和Session申明 tf.global_variables_initializer  /  tf.Session

	init = tf.global_variables_initializer()
	with tf.Session() as sess: # no need to close session manually
		result = sess.run(init)

16. 赋值 tf.assign

	state = tf.Variabel(0, name=’counter’)
	one = tf.Variable(1)
	new_state = tf.add(state, one)
	new_val = tf.assign(state, new_state)

17. 保存模型 tf.train.saver   /  saver.save

	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.global_variables_initializer()
	saver = tf.train.Saver()
	with tf.Session() as sess:
		sess.run(init)
		save_path = saver.save(sess, ‘./save_net.ckpt’)

18. 加载模型 tf.train.saver  /  saver.restore

	W = tf.Variable(np.arange(6).reshape((2, 3)), dtype=tf.float32, name="weights")
	b = tf.Variable(np.arange(3).reshape((1, 3)), dtype=tf.float32, name="biases")
	
	saver = tf.train.Saver()
	with tf.Session() as sess:
		saver.restore(sess, ‘./save_net.ckpt’)
		W = sess.run(W)
		b = sess.run(b)

19. 矩阵操作

	a = tf.constant([1,2,3])
	b = tf.constant([4,5,6])
	c = tf.stack([a,b], axis=0) # [[1,2,3], [4,5,6]]
	d = tf.unstack(c, axis=0)  # [1,2,3],  [4,5,6]
	e = tf.unstack(c, axis=1)  # [1,2], [3,4], [5,6]

	outputs = tf.transpose(outputs, [1,0,2])

20. 数组操作

	test = tf.constant([[1, 2, 3], [2, 3, 4], [5, 4, 3], [8, 7, 2]])
	tf.argmax(test, 0)   # array([3, 3, 1] <==> 8, 7, 4
	tf.argmax(test, 1)   # array([2, 2, 0, 0] <==>  3, 4, 5, 8

 

二、CNN -- MNIST

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

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


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

def bias_variable(shape):
    return tf.Variable(tf.constant(0.1, shape=shape))

def conv2d(x, W):
    return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')

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


x_image = tf.reshape(x, [-1, 28, 28, 1]) # num, h, w , c
W_conv1 = weight_variable([5, 5, 1, 32]) # kernel: h, w, c, num
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool(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(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)  # dropout

W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
y_predict = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)

cross_entropy = -tf.reduce_sum(y_actual * tf.log(y_predict))  
train_step = tf.train.GradientDescentOptimizer(1e-3).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_predict, 1), tf.argmax(y_actual, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
sess = tf.InteractiveSession()
sess.run(tf.initialize_all_variables())
for i in range(20000):
    batch = mnist.train.next_batch(50)
    if i % 100 == 0:  
        train_acc = accuracy.eval(feed_dict={x: batch[0], y_actual: batch[1], keep_prob: 1.0})
        print('step', i, 'training accuracy', train_acc)
        train_step.run(feed_dict={x: batch[0], y_actual: batch[1], keep_prob: 0.5})

test_acc = accuracy.eval(feed_dict={x: mnist.test.images, y_actual: mnist.test.labels, keep_prob: 1.0})
print("test accuracy", test_acc)

三、RNN -- MNIST

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
tf.set_random_seed(1)   # set random seed
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)

# hyperparameters
lr = 0.001
training_iters = 100000     # train step upper limit
batch_size = 128
n_inputs = 28               # MNIST data input (img shape: 28*28)
n_steps = 28                # time steps
n_hidden_units = 128
n_classes = 10

x = tf.placeholder(tf.float32, [None, n_steps, n_inputs])
y = tf.placeholder(tf.float32, [None, n_classes])

weights = {
    'in': tf.Variable(tf.random_normal([n_inputs, n_hidden_units])),
    'out': tf.Variable(tf.random_normal([n_hidden_units, n_classes]))
}
biases = {
    'in': tf.Variable(tf.constant(0.1, shape=[n_hidden_units, ])),
    'out': tf.Variable(tf.constant(0.1, shape=[n_classes, ]))
}

def rnn(X, weights, biases):
    X = tf.reshape(X, [-1, n_inputs])
    X_in = tf.matmul(X, weights['in']) + biases['in']
    X_in = tf.reshape(X_in, [-1, n_steps, n_hidden_units])
    cell = tf.contrib.rnn.BasicLSTMCell(n_hidden_units)
    init_state = cell.zero_state(batch_size, dtype=tf.float32)
    outputs, final_state = tf.nn.dynamic_rnn(cell, X_in, initial_state=init_state, time_major=False)
    outputs = tf.unstack(tf.transpose(outputs, [1,0,2]))
    results = tf.matmul(outputs[-1], weights['out']) + biases['out']
    return results


pred = rnn(x, weights, biases)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y))
train_op = tf.train.AdamOptimizer(lr).minimize(cost)

correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1)) # tf.argmax(arr, axis= )
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

with tf.Session() as sess:
    init = tf.global_variables_initializer()
    sess.run(init)
    step = 0
    while step * batch_size < training_iters:
        batch_xs, batch_ys = mnist.train.next_batch(batch_size)
        batch_xs = batch_xs.reshape([batch_size, n_steps, n_inputs])
        sess.run([train_op], feed_dict={x: batch_xs, y: batch_ys})
        if step % 20 == 0:
            print(sess.run(accuracy, feed_dict={x: batch_xs, y: batch_ys}))
        step += 1

 

你可能感兴趣的:(Tensorflow基础语法总结)