Tensorflow 学习笔记
Created
October 11, 2017 11:55
-
-
Save qxj/570e92505421c4fbeeb6c5c558f4d77c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
需要拟合的平面:y = W1 * x1_data + W2*x2_data + b,其中,已知x1_data、x2_data和y,但是都包含一点噪声。 | |
''' | |
import tensorflow as tf | |
import numpy as np | |
# Create 100 phony x, y data points in NumPy, y = x * 0.1 + 0.3 | |
x1_data = np.random.rand(100).astype(np.float32) | |
x2_data = np.random.rand(100).astype(np.float32) | |
y_data = x1_data * 10 + x2_data * 5 + 3 + tf.random_uniform([100], -0.1, 0.1) | |
# Try to find values for W and b that compute y_data = W * x_data + b | |
# (We know that W should be 0.1 and b 0.3, but TensorFlow will | |
# figure that out for us.) | |
# note: W b and y just statement/container before initialization | |
W1 = tf.Variable(tf.random_uniform([1], -1.0, 1.0)) | |
W2 = tf.Variable(tf.random_uniform([1], -1.0, 1.0)) | |
b = tf.Variable(tf.zeros([1])) | |
y = W1 * x1_data + W2*x2_data + b | |
# Minimize the mean squared errors. | |
loss = tf.reduce_mean(tf.square(y - y_data)) | |
optimizer = tf.train.AdagradOptimizer(0.6) | |
train = optimizer.minimize(loss) | |
# Before starting, initialize the variables. We will 'run' this first. | |
init = tf.initialize_all_variables() | |
# Launch the graph. | |
sess = tf.Session() | |
sess.run(init) | |
# Fit the line. | |
for step in range(20001): | |
sess.run(train) | |
#if step % 20 == 0: | |
#print(step, sess.run(W), sess.run(b),sess.run(loss)) | |
print(step, sess.run(W1), sess.run(W2), sess.run(b),sess.run(loss)) | |
# Learns best fit is W: [0.1], b: [0.3] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
两个比较大的矩阵相乘,分别使用GPU和CPU,比较运行时间 | |
''' | |
import tensorflow as tf | |
import numpy as np | |
#when put here the "cpu" is same as "gpu" , because it has been deploied on gpu or cpu | |
#select the fastest device automatically | |
#matrix1 = np.random.rand(20000,1500).astype(np.float32) | |
#matrix2 = np.random.rand(1500,20000).astype(np.float32) | |
#product = tf.matmul(matrix1, matrix2) | |
with tf.Session() as sess3: | |
with tf.device("/gpu:0"):#gpu 11.6s and cpu 20.2s | |
matrix1 = np.random.rand(20000,1500).astype(np.float32) | |
matrix2 = np.random.rand(1500,20000).astype(np.float32) | |
product = tf.matmul(matrix1, matrix2) | |
result = sess3.run(product) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
以上程序需要显示地使用sess.run(...)运行节点,想法自然,但TF也提供了另一种形式 | |
''' | |
import tensorflow as tf | |
import numpy as np | |
#deploy a session | |
sess = tf.InteractiveSession() | |
#design the grape | |
matrix1 = np.random.rand(2000,1500).astype(np.float32) | |
matrix2 = np.random.rand(1500,2000).astype(np.float32) | |
product = tf.matmul(matrix1, matrix2) | |
#run the operation | |
print product.eval() | |
sess.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
TF把很多操作都规定成内部的函数,先显式地规定网络,然后才是运行 | |
''' | |
import tensorflow as tf | |
#design the graph | |
state = tf.Variable(0, name="counter") | |
one = tf.constant(1) | |
new_value = tf.add(state, one) | |
update = tf.assign(state, new_value) | |
#initialization | |
init_op = tf.initialize_all_variables() | |
#run | |
with tf.Session() as sess: | |
sess.run(init_op) | |
print sess.run(state) | |
for _ in range(3): | |
sess.run(update) | |
print sess.run(state) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
程序运行时需要获取一些数据,每个节点获取的数据可以理解为:对每个点单独有通路,从底部运行过来(实际不是这样,但数据同步行类似) | |
''' | |
import tensorflow as tf | |
input1 = tf.constant(3.0) | |
input2 = tf.constant(2.0) | |
input3 = tf.constant(5.0) | |
intermed = tf.add(input2, input3) | |
mul = tf.mul(input1, intermed) | |
with tf.Session() as sess: | |
result = sess.run([mul, intermed]) | |
print result | |
result = sess.run([intermed]) | |
print result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
随时给程序填充一些数据 | |
''' | |
import tensorflow as tf | |
import numpy as np | |
input1 = tf.placeholder(tf.float32,shape=(5, 5)) | |
input2 = tf.placeholder(tf.float32,shape=(5, 5)) | |
output = tf.matmul(input1, input2)#matmul is different mul | |
with tf.Session() as sess: | |
rand_array = np.ones([5, 5]) | |
print sess.run([output], feed_dict={input1: rand_array,input2: rand_array}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
http://blog.csdn.net/phdat101/article/details/52397284 | |
''' | |
#download teh MNIST data in folder "MNIST_data" that in the same path as this *.py | |
from tensorflow.examples.tutorials.mnist import input_data | |
mnist = input_data.read_data_sets('MNIST_data', one_hot=True) | |
import tensorflow as tf | |
#图片的占位 | |
x = tf.placeholder(tf.float32, [None, 784]) | |
#系数 | |
W = tf.Variable(tf.zeros([784, 10])) | |
b = tf.Variable(tf.zeros([10])) | |
#softmax层 | |
y = tf.nn.softmax(tf.matmul(x, W) + b) | |
#用于训练的真实值占位 | |
y_ = tf.placeholder(tf.float32, [None, 10]) | |
#交叉熵:-tf.reduce_sum(y_ * tf.log(y)是一个样本的,外面的tf.reduce_mean是batch的 | |
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1])) | |
#规定训练的方法:注意:使用GradientDescentOptimizer适合上述的误差项 | |
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) | |
#初始化 | |
init = tf.initialize_all_variables() | |
sess = tf.Session() | |
sess.run(init) | |
#训练 | |
for i in range(5000): | |
batch_xs, batch_ys = mnist.train.next_batch(100) | |
#print batch_xs.shape | |
sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys}) | |
#验证,argmax(y,1)是获得y的第一个维度(即每一行)的最大值的位置 | |
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1)) | |
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) | |
print(sess.run([accuracy,tf.shape(y)], feed_dict={x: mnist.test.images, y_: mnist.test.labels})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#download teh MNIST data in folder "MNIST_data" that in the same path as this *.py | |
from tensorflow.examples.tutorials.mnist import input_data | |
mnist = input_data.read_data_sets('MNIST_data', one_hot=True) | |
import tensorflow as tf | |
#这里不同:这样,之后的运行不再显式地sess.run(...) | |
sess = tf.InteractiveSession() | |
x = tf.placeholder(tf.float32, [None, 784]) | |
W = tf.Variable(tf.zeros([784, 10])) | |
b = tf.Variable(tf.zeros([10])) | |
y_ = tf.placeholder(tf.float32, [None, 10]) | |
sess.run(tf.initialize_all_variables()) | |
y = tf.nn.softmax(tf.matmul(x, W) + b) | |
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1])) | |
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) | |
for i in range(1000): | |
batch = mnist.train.next_batch(100) | |
train_step.run(feed_dict={x: batch[0], y_: batch[1]}) | |
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1)) | |
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) | |
print accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from tensorflow.examples.tutorials.mnist import input_data | |
mnist = input_data.read_data_sets('MNIST_data', one_hot=True) | |
import tensorflow as tf | |
sess = tf.InteractiveSession() | |
#定义一些函数:分配系数函数、分配偏置函数、卷积函数、pooling函数 | |
def weight_variable(shape): | |
initial = tf.truncated_normal(shape, stddev=0.1)#均值0标准方差0.1,剔除2倍标准方差之外的随机数据 | |
return tf.Variable(initial) | |
def bias_variable(shape): | |
initial = tf.constant(0.1, shape=shape)#统一值0.1 | |
return tf.Variable(initial) | |
def conv2d(x, W): | |
#待操作的数据x,模板W,tensor不同维度上的步长,强制与原tensor等大 | |
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') | |
def max_pool_2x2(x): | |
#平面数据的pool模板2*2,平面数据滑动步长2*2(非重叠的pool) | |
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1], padding='SAME') | |
#x是输入的图像,y_是对应的标签 | |
x = tf.placeholder(tf.float32, [None, 784]) | |
y_ = tf.placeholder(tf.float32, [None, 10]) | |
#第1层卷积层,Receptive Field 5*5,单个batch生成32通道数据 | |
W_conv1 = weight_variable([5, 5, 1, 32]) | |
b_conv1 = bias_variable([32]) | |
#把图像向量还原成28*28的图像 | |
x_image = tf.reshape(x, [-1,28,28,1]) | |
#第1个卷积层,使用了ReLU激活函数 | |
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) | |
h_pool1 = max_pool_2x2(h_conv1) | |
#第2层卷积层,Receptive Field 5*5,单个batch 32通道生成64通道数据 | |
W_conv2 = weight_variable([5, 5, 32, 64]) | |
b_conv2 = bias_variable([64]) | |
#第2个卷积层 | |
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]) | |
#全链接层:把64通道数据展开方便全链接 | |
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) | |
#全链层神经元使用dropout防止过拟合 | |
keep_prob = tf.placeholder("float") | |
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) | |
#softmax层系数 | |
W_fc2 = weight_variable([1024, 10]) | |
b_fc2 = bias_variable([10]) | |
#softmax层 | |
y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) | |
#交叉熵和训练构型:AdamOptimizer适合这种求和的误差项 | |
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.run(tf.initialize_all_variables()) | |
#开始训练 | |
for i in range(20000): | |
batch = mnist.train.next_batch(50) | |
if i%100 == 0: | |
#验证的时候dropout=1.0,训练时=0.5 | |
train_accuracy = accuracy.eval(feed_dict={x:batch[0], y_: batch[1], keep_prob: 1.0}) | |
print "step %d, training accuracy %g"%(i, train_accuracy) | |
train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5}) | |
#验证最终的准确率 | |
print "test accuracy %g"%accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import absolute_import | |
from __future__ import division | |
from __future__ import print_function | |
import math | |
import tensorflow as tf | |
# The MNIST dataset has 10 classes, representing the digits 0 through 9. | |
NUM_CLASSES = 10 | |
# The MNIST images are always 28x28 pixels. | |
IMAGE_SIZE = 28 | |
IMAGE_PIXELS = IMAGE_SIZE * IMAGE_SIZE | |
#从图像输入层一直到准备链接softmax之前的10个输出(可以作为全链接的模板) | |
def inference(images, hidden1_units, hidden2_units): | |
#images : [000000...0000000] | |
#weights: [000000] | |
# [000000] | |
# .... | |
# [000000] | |
#biases: [000000] | |
with tf.name_scope('hidden1'): | |
weights = tf.Variable( | |
tf.truncated_normal([IMAGE_PIXELS, hidden1_units], | |
stddev=1.0 / math.sqrt(float(IMAGE_PIXELS))), | |
name='weights') | |
biases = tf.Variable(tf.zeros([hidden1_units]), | |
name='biases') | |
hidden1 = tf.nn.relu(tf.matmul(images, weights) + biases) | |
# Hidden 2 | |
with tf.name_scope('hidden2'): | |
weights = tf.Variable( | |
tf.truncated_normal([hidden1_units, hidden2_units], | |
stddev=1.0 / math.sqrt(float(hidden1_units))), | |
name='weights') | |
biases = tf.Variable(tf.zeros([hidden2_units]), | |
name='biases') | |
hidden2 = tf.nn.relu(tf.matmul(hidden1, weights) + biases) | |
# Linear | |
with tf.name_scope('softmax_linear'): | |
weights = tf.Variable( | |
tf.truncated_normal([hidden2_units, NUM_CLASSES], | |
stddev=1.0 / math.sqrt(float(hidden2_units))), | |
name='weights') | |
biases = tf.Variable(tf.zeros([NUM_CLASSES]), | |
name='biases') | |
logits = tf.matmul(hidden2, weights) + biases | |
return logits | |
#损失函数:使用了inference最后10个输出,通过softmax,与标签计算cross_entropy的平均值作为损失 | |
def loss(logits, labels): | |
#labels is not noe-hot style | |
labels = tf.to_int64(labels) | |
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits, labels, name='xentropy') | |
loss = tf.reduce_mean(cross_entropy, name='xentropy_mean') | |
return loss | |
#给可视化tensorboard传递损失值,定义训练方法 | |
def training(loss, learning_rate): | |
#for visualize of loss | |
tf.scalar_summary(loss.op.name, loss) | |
optimizer = tf.train.GradientDescentOptimizer(learning_rate) | |
#counter the step num | |
global_step = tf.Variable(0, name='global_step', trainable=False) | |
train_op = optimizer.minimize(loss, global_step=global_step) | |
return train_op | |
#计算准确的个数 | |
def evaluation(logits, labels): | |
correct = tf.nn.in_top_k(logits, labels, 1) | |
#bool转变成int32,并求和 | |
return tf.reduce_sum(tf.cast(correct, tf.int32)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
http://blog.csdn.net/phdat101/article/details/52410569 | |
Trains and Evaluates the MNIST network using a feed dictionary. | |
''' | |
# pylint: disable=missing-docstring | |
#system | |
from __future__ import absolute_import | |
from __future__ import division | |
from __future__ import print_function | |
import os.path | |
import time | |
from six.moves import xrange # pylint: disable=redefined-builtin | |
#加载数据 | |
import tensorflow as tf | |
from tensorflow.examples.tutorials.mnist import input_data | |
from tensorflow.examples.tutorials.mnist import mnist | |
# 模型的基本参数,类似C语言开头的defined | |
flags = tf.app.flags | |
FLAGS = flags.FLAGS | |
flags.DEFINE_float('learning_rate', 0.01, 'Initial learning rate.') | |
flags.DEFINE_integer('max_steps', 2000, 'Number of steps to run trainer.') | |
flags.DEFINE_integer('hidden1', 128, 'Number of units in hidden layer 1.') | |
flags.DEFINE_integer('hidden2', 32, 'Number of units in hidden layer 2.') | |
flags.DEFINE_integer('batch_size', 100, 'Batch size. ' | |
'Must divide evenly into the dataset sizes.') | |
flags.DEFINE_string('train_dir', 'data', 'Directory to put the training data.') | |
flags.DEFINE_boolean('fake_data', False, 'If true, uses fake data ' | |
'for unit testing.') | |
#占位符(就如同神经网络的接口):一组图像的以及对应的标签(不是one-hot的) | |
def placeholder_inputs(batch_size): | |
images_placeholder = tf.placeholder(tf.float32, shape=(batch_size,mnist.IMAGE_PIXELS)) | |
labels_placeholder = tf.placeholder(tf.int32, shape=(batch_size)) | |
return images_placeholder, labels_placeholder | |
#给占位符对应真实的数据字典false表示非one-hot数据 | |
def fill_feed_dict(data_set, images_pl, labels_pl): | |
# Create the feed_dict for the placeholders filled with the next `batch size` examples. | |
# data_set : the original data class of images and labels from input_data.read_data_sets(), like mnist in CNN_MNIST | |
images_feed, labels_feed = data_set.next_batch(FLAGS.batch_size,FLAGS.fake_data) | |
# python dictionary | |
feed_dict = { | |
images_pl: images_feed, | |
labels_pl: labels_feed, | |
} | |
return feed_dict | |
#验证准确率就比较综合了: | |
#sess提供了下面这一堆运行空间的接口 | |
#*_placeholder提供了训练好的网络的接口 | |
#data_set是不同的数据集(训练集、测试集、验证集) | |
#eval_correct是集合数据验证正确的个数总和 | |
def do_eval(sess, | |
eval_correct, | |
images_placeholder, | |
labels_placeholder, | |
data_set): | |
#获得一些相关常数 | |
true_count = 0 | |
steps_per_epoch = data_set.num_examples // FLAGS.batch_size | |
num_examples = steps_per_epoch * FLAGS.batch_size | |
#每一代的每一步都分开计算:因为fill_feed_dict中已经定义死了每次获取FLAGS.batch_size数据 | |
for step in xrange(steps_per_epoch): | |
feed_dict = fill_feed_dict(data_set,images_placeholder,labels_placeholder) | |
true_count += sess.run(eval_correct, feed_dict=feed_dict) | |
precision = true_count / num_examples | |
print(' Num examples: %d Num correct: %d Precision @ 1: %0.04f' % | |
(num_examples, true_count, precision)) | |
#训练的主体部分 | |
def run_training(): | |
#"train_dir" is "data", the folder contains all mnist data | |
#指明数据源 | |
data_sets = input_data.read_data_sets(FLAGS.train_dir, FLAGS.fake_data) | |
#指定使用的操作所在的图(graph):其实大多数情况只要一个graph就可以了,这里为了完整性 | |
with tf.Graph().as_default(): | |
# 首先是构建网络图片数据和标签数据的占位符,指定他们的结构,好往上搭积木 | |
images_placeholder, labels_placeholder = placeholder_inputs(FLAGS.batch_size) | |
# 构建网络主体,也就是推理部分(inference部分) | |
logits = mnist.inference(images_placeholder,FLAGS.hidden1,FLAGS.hidden2) | |
# 根据推理部分的结果和对应的真实标签构建损失函数 | |
loss = mnist.loss(logits, labels_placeholder) | |
# 根据损失函数和学习速率构建训练过程 | |
train_op = mnist.training(loss, FLAGS.learning_rate) | |
# 根据推理部分的结果和对应的真实标签计算给定数据的准确率(是一个总和) | |
eval_correct = mnist.evaluation(logits, labels_placeholder) | |
# 收集图表的信息,用于给Tensoroard提供信息 | |
summary_op = tf.merge_all_summaries() | |
# 初始化网络的参数 | |
init = tf.initialize_all_variables() | |
# 主要是记录训练的参数 | |
saver = tf.train.Saver() | |
# 指定Session | |
sess = tf.Session() | |
# 与summary_op是配套的,用于具体地操作收集的信息,比如写到缓冲区等 | |
summary_writer = tf.train.SummaryWriter(FLAGS.train_dir, sess.graph) | |
#运行参数初始化 | |
sess.run(init) | |
# 进入训练环节 | |
for step in xrange(FLAGS.max_steps): | |
start_time = time.time()#先记录这一步的时间 | |
#获取这一步用于训练的batch数据 | |
feed_dict = fill_feed_dict(data_sets.train, | |
images_placeholder, | |
labels_placeholder) | |
#按照给定的数据训练(feed_dict)、给定的训练方法(train_op),训练一次网络 | |
#顺便获得训练之前损失函数的值(注:train_op没有输出,所以是“_”) | |
_, loss_value = sess.run([train_op, loss],feed_dict=feed_dict) | |
#训练完一次,赶紧计算一下消耗了多少时间 | |
duration = time.time() - start_time | |
#下面都是一些为了方便调试而输出的各种验证信息和关心的主要参数 | |
# 每隔训练100步就把:步数、损失函数的值、使用的时间直接输出 | |
if step % 100 == 0: | |
print('Step %d: loss = %.2f (%.3f sec)' % (step, loss_value, duration)) | |
# 然后把图表运行的信息写到缓冲区,更新磁盘文件 | |
summary_str = sess.run(summary_op, feed_dict=feed_dict) | |
summary_writer.add_summary(summary_str, step) | |
summary_writer.flush() | |
# 每隔1000步就保存训练数据一次,并验证一下训练集、验证集、测试集的准确率 | |
if (step + 1) % 1000 == 0 or (step + 1) == FLAGS.max_steps: | |
checkpoint_file = os.path.join(FLAGS.train_dir, 'checkpoint') | |
saver.save(sess, checkpoint_file, global_step=step) | |
# Evaluate against the training set. | |
print('Training Data Eval:') | |
do_eval(sess, | |
eval_correct, | |
images_placeholder, | |
labels_placeholder, | |
data_sets.train) | |
# Evaluate against the validation set. | |
print('Validation Data Eval:') | |
do_eval(sess, | |
eval_correct, | |
images_placeholder, | |
labels_placeholder, | |
data_sets.validation) | |
# Evaluate against the test set. | |
print('Test Data Eval:') | |
do_eval(sess, | |
eval_correct, | |
images_placeholder, | |
labels_placeholder, | |
data_sets.test) | |
def main(_): | |
run_training() | |
if __name__ == '__main__': | |
tf.app.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 原始的变量 | |
weights = tf.Variable(tf.random_normal([784, 200], stddev=0.35),name="weights") | |
# 创造相同内容的变量 | |
w2 = tf.Variable(weights.initialized_value(), name="w2") | |
# 也可以直接乘以比例 | |
w_twice = tf.Variable(weights.initialized_value() * 0.2, name="w_twice") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
import numpy as np | |
# 生成0和1矩阵 | |
v1 = tf.Variable(tf.zeros([3,3,3]), name="v1") | |
v2 = tf.Variable(tf.ones([10,5]), name="v2") | |
#填充单值矩阵 | |
v3 = tf.Variable(tf.fill([2,3], 9)) | |
#常量矩阵 | |
v4_1 = tf.constant([1, 2, 3, 4, 5, 6, 7]) | |
v4_2 = tf.constant(-1.0, shape=[2, 3]) | |
#生成等差数列 | |
v6_1 = tf.linspace(10.0, 12.0, 30, name="linspace")#float32 or float64 | |
v7_1 = tf.range(10, 20, 3)#just int32 | |
#生成各种随机数据矩阵 | |
v8_1 = tf.Variable(tf.random_uniform([2,4], minval=0.0, maxval=2.0, dtype=tf.float32, seed=1234, name="v8_1")) | |
v8_2 = tf.Variable(tf.random_normal([2,3], mean=0.0, stddev=1.0, dtype=tf.float32, seed=1234, name="v8_2")) | |
v8_3 = tf.Variable(tf.truncated_normal([2,3], mean=0.0, stddev=1.0, dtype=tf.float32, seed=1234, name="v8_3")) | |
v8_4 = tf.Variable(tf.random_uniform([2,3], minval=0.0, maxval=1.0, dtype=tf.float32, seed=1234, name="v8_4")) | |
v8_5 = tf.random_shuffle([[1,2,3],[4,5,6],[6,6,6]], seed=134, name="v8_5") | |
# 初始化 | |
init_op = tf.initialize_all_variables() | |
# 保存变量,也可以指定保存的内容 | |
saver = tf.train.Saver() | |
#saver = tf.train.Saver({"my_v2": v2}) | |
#运行 | |
with tf.Session() as sess: | |
sess.run(init_op) | |
# 输出形状和值 | |
print tf.Variable.get_shape(v1)#shape | |
print sess.run(v1)#vaule | |
# numpy保存文件 | |
np.save("v1.npy",sess.run(v1))#numpy save v1 as file | |
test_a = np.load("v1.npy") | |
print test_a[1,2] | |
#一些输出 | |
print sess.run(v3) | |
v5 = tf.zeros_like(sess.run(v1)) | |
print sess.run(v6_1) | |
print sess.run(v7_1) | |
print sess.run(v8_5) | |
#保存图的变量 | |
save_path = saver.save(sess, "/tmp/model.ckpt") | |
#加载图的变量 | |
#saver.restore(sess, "/tmp/model.ckpt") | |
print "Model saved in file: ", save_path |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
import numpy as np | |
# Create 100 phony x, y data points in NumPy, y = x * 0.1 + 0.3 | |
x_data = np.random.rand(1000,1).astype(np.float32) | |
y_data = tf.sin(x_data)*tf.cos(x_data)+tf.random_uniform([1000,1], -0.1, 0.1) | |
#graph | |
X = tf.placeholder(tf.float32,[None,1],name = 'X-input') | |
Y = tf.placeholder(tf.float32,[None,1],name = 'Y-input') | |
W1 = tf.Variable(tf.random_uniform([1,5], -1.0, 1.0),name = 'weight1') | |
W2 = tf.Variable(tf.random_uniform([5,2], -1.0, 1.0),name = 'weight2') | |
W3 = tf.Variable(tf.random_uniform([2,1], -1.0, 1.0),name = 'weight3') | |
b1 = tf.Variable(tf.zeros([5]), name = 'bias1') | |
b2 = tf.Variable(tf.zeros([2]), name = 'bias2') | |
b3 = tf.Variable(tf.zeros([1]), name = 'bias3') | |
with tf.name_scope('layer2') as scope: | |
L2 = tf.sigmoid(tf.matmul(X,W1)+b1) | |
with tf.name_scope('layer3') as scope: | |
L3 = tf.sigmoid(tf.matmul(L2,W2)+b2) | |
with tf.name_scope('layer4') as scope: | |
hypothesis = tf.sigmoid(tf.matmul(L3,W3)+b3) | |
with tf.name_scope('cost') as scope: | |
cost = -tf.reduce_mean(Y*tf.log(hypothesis)) | |
cost_summery = tf.scalar_summary("cost",cost) | |
with tf.name_scope('train') as scope: | |
optimizer = tf.train.GradientDescentOptimizer(0.01) | |
train = optimizer.minimize(cost) | |
#the summery | |
w1_hist = tf.histogram_summary("weight1",W1) | |
w2_hist = tf.histogram_summary("weight2",W2) | |
b1_hist = tf.histogram_summary("bisa1",b1) | |
b2_hist = tf.histogram_summary("bisa2",b2) | |
y_hist = tf.histogram_summary("y",Y) | |
init = tf.initialize_all_variables() | |
#run | |
with tf.Session() as sess: | |
sess.run(init) | |
#the workers who translate data to TensorBoard | |
merged = tf.merge_all_summaries() #collect the tf.xxxxx_summary | |
writer = tf.train.SummaryWriter('keep',sess.graph) | |
# maybe many writers to show different curvs in the same figure | |
for step in range(20000): | |
summary, _ = sess.run([merged, train], feed_dict={X:x_data,Y:y_data.eval()}) | |
writer.add_summary(summary, step) | |
if step%10 ==0: | |
print('step %s' % (step)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import absolute_import | |
from __future__ import division | |
from __future__ import print_function | |
import tensorflow as tf | |
from tensorflow.examples.tutorials.mnist import input_data | |
flags = tf.app.flags | |
FLAGS = flags.FLAGS | |
flags.DEFINE_boolean('fake_data', False, 'If true, uses fake data ' | |
'for unit testing.') | |
flags.DEFINE_integer('max_steps', 1000, 'Number of steps to run trainer.') | |
flags.DEFINE_float('learning_rate', 0.001, 'Initial learning rate.') | |
flags.DEFINE_float('dropout', 0.5, 'Keep probability for training dropout.') | |
flags.DEFINE_string('data_dir', '/tmp/data', 'Directory for storing data') | |
flags.DEFINE_string('summaries_dir', '/tmp/mnist_logs', 'Summaries directory') | |
# Import data | |
mnist = input_data.read_data_sets(FLAGS.data_dir,one_hot=True,fake_data=FLAGS.fake_data) | |
sess = tf.InteractiveSession() | |
# We can't initialize these variables to 0 - the network will get stuck. | |
def weight_variable(shape): | |
"""Create a weight variable with appropriate initialization.""" | |
initial = tf.truncated_normal(shape, stddev=0.1) | |
return tf.Variable(initial) | |
def bias_variable(shape): | |
"""Create a bias variable with appropriate initialization.""" | |
initial = tf.constant(0.1, shape=shape) | |
return tf.Variable(initial) | |
def variable_summaries(var, name): | |
"""Attach a lot of summaries to a Tensor.""" | |
with tf.name_scope('summaries'): | |
mean = tf.reduce_mean(var) | |
tf.scalar_summary('mean/' + name, mean) | |
with tf.name_scope('stddev'): | |
stddev = tf.sqrt(tf.reduce_sum(tf.square(var - mean))) | |
tf.scalar_summary('sttdev/' + name, stddev) | |
tf.scalar_summary('max/' + name, tf.reduce_max(var)) | |
tf.scalar_summary('min/' + name, tf.reduce_min(var)) | |
tf.histogram_summary(name, var) | |
def nn_layer(input_tensor, input_dim, output_dim, layer_name, act=tf.nn.relu): | |
with tf.name_scope(layer_name): | |
# This Variable will hold the state of the weights for the layer | |
with tf.name_scope('weights'): | |
weights = weight_variable([input_dim, output_dim]) | |
variable_summaries(weights, layer_name + '/weights') | |
with tf.name_scope('biases'): | |
biases = bias_variable([output_dim]) | |
variable_summaries(biases, layer_name + '/biases') | |
with tf.name_scope('Wx_plus_b'): | |
preactivate = tf.matmul(input_tensor, weights) + biases | |
tf.histogram_summary(layer_name + '/pre_activations', preactivate) | |
activations = act(preactivate, 'activation') | |
tf.histogram_summary(layer_name + '/activations', activations) | |
return activations | |
# Input placehoolders | |
with tf.name_scope('input'): | |
x = tf.placeholder(tf.float32, [None, 784], name='x-input') | |
y_ = tf.placeholder(tf.float32, [None, 10], name='y-input') | |
keep_prob = tf.placeholder(tf.float32) | |
def feed_dict(train): | |
"""Make a TensorFlow feed_dict: maps data onto Tensor placeholders.""" | |
if train or FLAGS.fake_data: | |
xs, ys = mnist.train.next_batch(100, fake_data=FLAGS.fake_data) | |
k = FLAGS.dropout | |
else: | |
xs, ys = mnist.test.images, mnist.test.labels | |
k = 1.0 | |
return {x: xs, y_: ys, keep_prob: k} | |
def train(): | |
with tf.name_scope('input_reshape'): | |
image_shaped_input = tf.reshape(x, [-1, 28, 28, 1]) | |
tf.image_summary('input', image_shaped_input, 10) | |
hidden1 = nn_layer(x, 784, 500, 'layer1') | |
with tf.name_scope('dropout1'): | |
tf.scalar_summary('dropout_keep_probability1', keep_prob) | |
dropped1 = tf.nn.dropout(hidden1, keep_prob) | |
hidden2 = nn_layer(dropped1, 500, 300, 'layer2') | |
with tf.name_scope('dropout2'): | |
tf.scalar_summary('dropout_keep_probability2', keep_prob) | |
dropped2 = tf.nn.dropout(hidden2, keep_prob) | |
y = nn_layer(dropped2, 300, 10, 'layer3', act=tf.nn.softmax) | |
with tf.name_scope('cross_entropy'): | |
diff = y_ * tf.log(y) | |
with tf.name_scope('total'): | |
cross_entropy = -tf.reduce_mean(diff) | |
tf.scalar_summary('cross entropy', cross_entropy) | |
with tf.name_scope('train'): | |
train_step = tf.train.AdamOptimizer(FLAGS.learning_rate).minimize( | |
cross_entropy) | |
with tf.name_scope('accuracy'): | |
with tf.name_scope('correct_prediction'): | |
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) | |
with tf.name_scope('accuracy'): | |
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) | |
tf.scalar_summary('accuracy', accuracy) | |
# Merge all the summaries and write them out to /tmp/mnist_logs (by default) | |
merged = tf.merge_all_summaries() | |
train_writer = tf.train.SummaryWriter(FLAGS.summaries_dir + '/train',sess.graph) | |
test_writer = tf.train.SummaryWriter(FLAGS.summaries_dir + '/test') | |
tf.initialize_all_variables().run() | |
# | |
for i in range(FLAGS.max_steps): | |
if i % 10 == 0: # Record summaries and test-set accuracy | |
summary, acc = sess.run([merged, accuracy], feed_dict=feed_dict(False)) | |
test_writer.add_summary(summary, i) | |
print('Accuracy at step %s: %s' % (i, acc)) | |
else: # Record train set summaries, and train | |
summary, _ = sess.run([merged, train_step], feed_dict=feed_dict(True)) | |
train_writer.add_summary(summary, i) | |
def main(_): | |
if tf.gfile.Exists(FLAGS.summaries_dir): | |
tf.gfile.DeleteRecursively(FLAGS.summaries_dir) | |
tf.gfile.MakeDirs(FLAGS.summaries_dir) | |
train() | |
if __name__ == '__main__': | |
tf.app.run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment