Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1b70523

Browse files
Merge pull request #25 from whyscience/mater_python3
add another folder for Python 3.5 and Tensorflow 1.1
2 parents ffc72d3 + 1a86f6a commit 1b70523

34 files changed

+2739
-0
lines changed
19.4 MB
Binary file not shown.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# 1 - 3 小节
2+
主要讲解 TensorFlow 的最最最基本使用。
3+
4+
接触过TensorFlow,或者阅读英文文档没有问题的同学可以直接开始 4 - 6 小节
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
# encoding: utf-8
2+
# 为了 Python3 的兼容,如果你用的 Python2.7
3+
from __future__ import print_function, division
4+
import tensorflow as tf
5+
6+
print('Loaded TF version', tf.__version__, '\n\n')
7+
8+
# Tensor 在数学中是"张量"
9+
# 标量,矢量/向量,张量
10+
11+
# 简单地理解
12+
# 标量表示值
13+
# 矢量表示位置(空间中的一个点)
14+
# 张量表示整个空间
15+
16+
# 一维数组是矢量
17+
# 多维数组是张量, 矩阵也是张量
18+
19+
20+
# 4个重要的类型
21+
# @Variable 计算图谱中的变量
22+
# @Tensor 一个多维矩阵,带有很多方法
23+
# @Graph 一个计算图谱
24+
# @Session 用来运行一个计算图谱
25+
26+
27+
# 三个重要的函数
28+
29+
# Variable 变量
30+
"""
31+
tf.Variable.__init__(
32+
initial_value=None, @Tensor
33+
trainable=True,
34+
collections=None,
35+
validate_shape=True,
36+
caching_device=None,
37+
name=None,
38+
variable_def=None,
39+
dtype=None)
40+
"""
41+
42+
43+
# 注意:Variable是一个Class,Tensor也是一个Class
44+
45+
# Constant 常数
46+
# tf.constant(value, dtype=None, shape=None, name='Const')
47+
# return: a constant @Tensor
48+
49+
# Placeholder 暂时变量?
50+
# tf.placeholder(dtype, shape=None, name=None)
51+
# return: 一个还尚未存在的 @Tensor
52+
53+
# 让我们用计算图谱来实现一些简单的函数
54+
# + - * / 四则运算
55+
def basic_operation():
56+
v1 = tf.Variable(10)
57+
v2 = tf.Variable(5)
58+
addv = v1 + v2
59+
print(addv)
60+
print(type(addv))
61+
print(type(v1))
62+
63+
c1 = tf.constant(10)
64+
c2 = tf.constant(5)
65+
addc = c1 + c2
66+
print(addc)
67+
print(type(addc))
68+
print(type(c1))
69+
70+
# 用来运行计算图谱的对象/实例?
71+
# session is a runtime
72+
sess = tf.Session()
73+
74+
# Variable -> 初始化 -> 有值的Tensor
75+
tf.initialize_all_variables().run(session=sess)
76+
77+
print('变量是需要初始化的')
78+
print('加法(v1, v2) = ', addv.eval(session=sess))
79+
print('加法(v1, v2) = ', sess.run(addv))
80+
print('加法(c1, c2) = ', addc.eval(session=sess))
81+
print('\n\n')
82+
# 这种定义操作,再执行操作的模式被称之为"符号式编程" Symbolic Programming
83+
84+
# tf.Graph.__init__()
85+
# Creates a new, empty Graph.
86+
graph = tf.Graph()
87+
with graph.as_default():
88+
value1 = tf.constant([1, 2])
89+
value2 = tf.Variable([3, 4])
90+
mul = value1 / value2
91+
92+
with tf.Session(graph=graph) as mySess:
93+
tf.initialize_all_variables().run()
94+
print('一一对应的除法(value1, value2) = ', mySess.run(mul))
95+
print('一一对应的除法(value1, value2) = ', mul.eval())
96+
97+
# tensor.eval(session=sess)
98+
# sess.run(tensor)
99+
100+
# 省内存?placeholder才是王道
101+
# def use_placeholder():
102+
graph = tf.Graph()
103+
with graph.as_default():
104+
value1 = tf.placeholder(dtype=tf.float64)
105+
value2 = tf.Variable([3, 4], dtype=tf.float64)
106+
mul = value1 * value2
107+
108+
with tf.Session(graph=graph) as mySess:
109+
tf.initialize_all_variables().run()
110+
# 我们想象一下这个数据是从远程加载进来的
111+
# 文件,网络
112+
# 假装是 10 GB
113+
value = load_from_remote()
114+
for partialValue in load_partial(value, 2):
115+
runResult = mySess.run(mul, feed_dict={value1: partialValue})
116+
# evalResult = mul.eval(feed_dict={value1: partialValue})
117+
print('乘法(value1, value2) = ', runResult)
118+
# cross validation
119+
120+
121+
def load_from_remote():
122+
return [-x for x in range(1000)]
123+
124+
125+
# 自定义的 Iterator
126+
# yield, generator function
127+
def load_partial(value, step):
128+
index = 0
129+
while index < len(value):
130+
yield value[index:index + step]
131+
index += step
132+
return
133+
134+
135+
if __name__ == '__main__':
136+
basic_operation()
137+
# use_placeholder()
3.58 MB
Binary file not shown.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/board/
2+
/fc1_weights/
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# TensorBoard 计算图谱可视化
2+
TensorBoard 是TF自带的强大可视化工具
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
# 为了 Python2 玩家们
2+
from __future__ import print_function, division
3+
4+
# 第三方
5+
import tensorflow as tf
6+
from sklearn.metrics import confusion_matrix
7+
import numpy as np
8+
9+
# 我们自己
10+
import load
11+
12+
train_samples, train_labels = load._train_samples, load._train_labels
13+
test_samples, test_labels = load._test_samples, load._test_labels
14+
15+
print('Training set', train_samples.shape, train_labels.shape)
16+
print(' Test set', test_samples.shape, test_labels.shape)
17+
18+
image_size = load.image_size
19+
num_labels = load.num_labels
20+
num_channels = load.num_channels
21+
22+
23+
def get_chunk(samples, labels, chunkSize):
24+
"""
25+
Iterator/Generator: get a batch of data
26+
这个函数是一个迭代器/生成器,用于每一次只得到 chunkSize 这么多的数据
27+
用于 for loop, just like range() function
28+
"""
29+
if len(samples) != len(labels):
30+
raise Exception('Length of samples and labels must equal')
31+
stepStart = 0 # initial step
32+
i = 0
33+
while stepStart < len(samples):
34+
stepEnd = stepStart + chunkSize
35+
if stepEnd < len(samples):
36+
yield i, samples[stepStart:stepEnd], labels[stepStart:stepEnd]
37+
i += 1
38+
stepStart = stepEnd
39+
40+
41+
class Network():
42+
def __init__(self, num_hidden, batch_size):
43+
"""
44+
@num_hidden: 隐藏层的节点数量
45+
@batch_size:因为我们要节省内存,所以分批处理数据。每一批的数据量。
46+
"""
47+
self.batch_size = batch_size
48+
self.test_batch_size = 500
49+
50+
# Hyper Parameters
51+
self.num_hidden = num_hidden
52+
53+
# Graph Related
54+
self.graph = tf.Graph()
55+
self.tf_train_samples = None
56+
self.tf_train_labels = None
57+
self.tf_test_samples = None
58+
self.tf_test_labels = None
59+
self.tf_test_prediction = None
60+
61+
# 统计
62+
self.merged = None
63+
64+
# 初始化
65+
self.define_graph()
66+
self.session = tf.Session(graph=self.graph)
67+
self.writer = tf.summary.FileWriter('./board', self.graph)
68+
69+
def define_graph(self):
70+
"""
71+
定义我的的计算图谱
72+
"""
73+
with self.graph.as_default():
74+
# 这里只是定义图谱中的各种变量
75+
with tf.name_scope('inputs'):
76+
self.tf_train_samples = tf.placeholder(
77+
tf.float32, shape=(self.batch_size, image_size, image_size, num_channels), name='tf_train_samples'
78+
)
79+
self.tf_train_labels = tf.placeholder(
80+
tf.float32, shape=(self.batch_size, num_labels), name='tf_train_labels'
81+
)
82+
self.tf_test_samples = tf.placeholder(
83+
tf.float32, shape=(self.test_batch_size, image_size, image_size, num_channels),
84+
name='tf_test_samples'
85+
)
86+
87+
# fully connected layer 1, fully connected
88+
with tf.name_scope('fc1'):
89+
fc1_weights = tf.Variable(
90+
tf.truncated_normal([image_size * image_size, self.num_hidden], stddev=0.1), name='fc1_weights'
91+
)
92+
fc1_biases = tf.Variable(tf.constant(0.1, shape=[self.num_hidden]), name='fc1_biases')
93+
tf.summary.histogram('fc1_weights', fc1_weights)
94+
tf.summary.histogram('fc1_biases', fc1_biases)
95+
96+
# fully connected layer 2 --> output layer
97+
with tf.name_scope('fc2'):
98+
fc2_weights = tf.Variable(
99+
tf.truncated_normal([self.num_hidden, num_labels], stddev=0.1), name='fc2_weights'
100+
)
101+
fc2_biases = tf.Variable(tf.constant(0.1, shape=[num_labels]), name='fc2_biases')
102+
tf.summary.histogram('fc2_weights', fc2_weights)
103+
tf.summary.histogram('fc2_biases', fc2_biases)
104+
105+
# 想在来定义图谱的运算
106+
def model(data):
107+
# fully connected layer 1
108+
shape = data.get_shape().as_list()
109+
reshape = tf.reshape(data, [shape[0], shape[1] * shape[2] * shape[3]])
110+
111+
with tf.name_scope('fc1_model'):
112+
fc1_model = tf.matmul(reshape, fc1_weights) + fc1_biases
113+
hidden = tf.nn.relu(fc1_model)
114+
115+
# fully connected layer 2
116+
with tf.name_scope('fc2_model'):
117+
return tf.matmul(hidden, fc2_weights) + fc2_biases
118+
119+
# Training computation.
120+
logits = model(self.tf_train_samples)
121+
with tf.name_scope('loss'):
122+
self.loss = tf.reduce_mean(
123+
tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=self.tf_train_labels)
124+
)
125+
tf.summary.scalar('Loss', self.loss)
126+
127+
# Optimizer.
128+
with tf.name_scope('optimizer'):
129+
self.optimizer = tf.train.GradientDescentOptimizer(0.0001).minimize(self.loss)
130+
131+
# Predictions for the training, validation, and test data.
132+
with tf.name_scope('predictions'):
133+
self.train_prediction = tf.nn.softmax(logits, name='train_prediction')
134+
self.test_prediction = tf.nn.softmax(model(self.tf_test_samples), name='test_prediction')
135+
136+
self.merged = tf.summary.merge_all()
137+
138+
def run(self):
139+
"""
140+
用到Session
141+
"""
142+
143+
# private function
144+
def print_confusion_matrix(confusionMatrix):
145+
print('Confusion Matrix:')
146+
for i, line in enumerate(confusionMatrix):
147+
print(line, line[i] / np.sum(line))
148+
a = 0
149+
for i, column in enumerate(np.transpose(confusionMatrix, (1, 0))):
150+
a += (column[i] / np.sum(column)) * (np.sum(column) / 26000)
151+
print(column[i] / np.sum(column), )
152+
print('\n', np.sum(confusionMatrix), a)
153+
154+
with self.session as session:
155+
tf.initialize_all_variables().run()
156+
157+
# 训练
158+
print('Start Training')
159+
# batch 1000
160+
for i, samples, labels in get_chunk(train_samples, train_labels, chunkSize=self.batch_size):
161+
_, l, predictions, summary = session.run(
162+
[self.optimizer, self.loss, self.train_prediction, self.merged],
163+
feed_dict={self.tf_train_samples: samples, self.tf_train_labels: labels}
164+
)
165+
self.writer.add_summary(summary, i)
166+
# labels is True Labels
167+
accuracy, _ = self.accuracy(predictions, labels)
168+
if i % 50 == 0:
169+
print('Minibatch loss at step %d: %f' % (i, l))
170+
print('Minibatch accuracy: %.1f%%' % accuracy)
171+
#
172+
173+
# 测试
174+
accuracies = []
175+
confusionMatrices = []
176+
for i, samples, labels in get_chunk(test_samples, test_labels, chunkSize=self.test_batch_size):
177+
result = self.test_prediction.eval(feed_dict={self.tf_test_samples: samples})
178+
accuracy, cm = self.accuracy(result, labels, need_confusion_matrix=True)
179+
accuracies.append(accuracy)
180+
confusionMatrices.append(cm)
181+
print('Test Accuracy: %.1f%%' % accuracy)
182+
print(' Average Accuracy:', np.average(accuracies))
183+
print('Standard Deviation:', np.std(accuracies))
184+
print_confusion_matrix(np.add.reduce(confusionMatrices))
185+
#
186+
187+
def accuracy(self, predictions, labels, need_confusion_matrix=False):
188+
"""
189+
计算预测的正确率与召回率
190+
@return: accuracy and confusionMatrix as a tuple
191+
"""
192+
_predictions = np.argmax(predictions, 1)
193+
_labels = np.argmax(labels, 1)
194+
cm = confusion_matrix(_labels, _predictions) if need_confusion_matrix else None
195+
# == is overloaded for numpy array
196+
accuracy = (100.0 * np.sum(_predictions == _labels) / predictions.shape[0])
197+
return accuracy, cm
198+
199+
200+
if __name__ == '__main__':
201+
net = Network(num_hidden=128, batch_size=100)
202+
net.run()

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /