Skip to content

Commit 6be082e

Browse files
author
xuming06
committed
add tensorflow cnn rnn. xuming 20171124
1 parent 4d8503c commit 6be082e

24 files changed

Lines changed: 387994 additions & 68268 deletions

17tensorflow/2_network/cnn.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# -*- coding: utf-8 -*-
2+
# Author: XuMing <[email protected]>
3+
# Data: 17/11/23
4+
# Brief: convolutional_network
5+
6+
import tensorflow as tf
7+
from tensorflow.examples.tutorials.mnist import input_data
8+
9+
mnist = input_data.read_data_sets("../data/", one_hot=False)
10+
11+
# training params
12+
learning_rate = 0.001
13+
num_steps = 20 # 00
14+
batch_size = 128
15+
16+
# network params
17+
num_input = 784 # (28*28)
18+
num_classes = 10
19+
dropout = 0.5 # Dropout,probability to keep units
20+
21+
22+
# network
23+
def conv_net(x_dict, n_classes, dropout, reuse, is_training):
24+
# variables
25+
with tf.variable_scope("convnet", reuse=reuse):
26+
x = x_dict["images"]
27+
# tensor input is 4D: [batch size, height, width, channel]
28+
x = tf.reshape(x, shape=[-1, 28, 28, 1])
29+
30+
# convolution layer with 32 filters and a kernel size of 5
31+
conv1 = tf.layers.conv2d(x, 32, 5, activation=tf.nn.relu)
32+
# max pooling with strides of 2 and kernel size of 2
33+
conv1 = tf.layers.max_pooling2d(conv1, 2, 2)
34+
35+
# convolution layer with 64 filters and a kernel size of 3
36+
conv2 = tf.layers.conv2d(conv1, 64, 3, activation=tf.nn.relu)
37+
# max pooling
38+
conv2 = tf.layers.max_pooling2d(conv2, 2, 2)
39+
40+
# flatten the data to a 1-D vector for the fully connected layer
41+
fc1 = tf.contrib.layers.flatten(conv2)
42+
43+
# fully connected layer
44+
fc1 = tf.layers.dense(fc1, 1024)
45+
# apply dropout (only in test)
46+
fc1 = tf.layers.dropout(fc1, rate=dropout, training=is_training)
47+
48+
# output layer, class prediction
49+
out = tf.layers.dense(fc1, n_classes)
50+
return out
51+
52+
53+
# define the model function ( following TF estimator template)
54+
def model_fn(features, labels, mode):
55+
logits_train = conv_net(features, num_classes, dropout, reuse=False, is_training=True)
56+
logits_test = conv_net(features, num_classes, dropout, reuse=True, is_training=False)
57+
58+
# prediction
59+
pred_classes = tf.argmax(logits_test, axis=1)
60+
pred_probas = tf.nn.softmax(logits_test)
61+
62+
if mode == tf.estimator.ModeKeys.PREDICT:
63+
return tf.estimator.EstimatorSpec(mode, predictions=pred_classes)
64+
65+
# define loss and optimizer
66+
loss_op = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(
67+
logits=logits_train, labels=tf.cast(labels, dtype=tf.int32)
68+
))
69+
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
70+
train_op = optimizer.minimize(loss_op, global_step=tf.train.get_global_step())
71+
72+
# Evaluate the accuracy of the model
73+
acc_op = tf.metrics.accuracy(labels, predictions=pred_classes)
74+
75+
# TF estimators requires to return a estimatorSpec
76+
estimator_specs = tf.estimator.EstimatorSpec(
77+
mode=mode,
78+
predictions=pred_classes,
79+
loss=loss_op,
80+
train_op=train_op,
81+
eval_metric_ops={'accuracy': acc_op}
82+
)
83+
return estimator_specs
84+
85+
86+
def train():
87+
# build the estimator
88+
model = tf.estimator.Estimator(model_fn=model_fn)
89+
# define the input function for training
90+
input_fn = tf.estimator.inputs.numpy_input_fn(
91+
x={'images': mnist.train.images}, y=mnist.train.labels,
92+
batch_size=batch_size, num_epochs=None, shuffle=True
93+
)
94+
# train the model
95+
model.train(input_fn, steps=num_steps)
96+
97+
# evaluate the model
98+
# define the input function for evaluating
99+
input_fn = tf.estimator.inputs.numpy_input_fn(
100+
x={'images': mnist.train.images}, y=mnist.train.labels,
101+
batch_size=batch_size, shuffle=False
102+
)
103+
# use the estimator 'evaluate' method
104+
e = model.evaluate(input_fn)
105+
106+
print("testing accuracy:", e['accuracy'])
107+
108+
109+
train()

17tensorflow/2_network/rnn.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# -*- coding: utf-8 -*-
2+
# Author: XuMing <[email protected]>
3+
# Data: 17/11/23
4+
# Brief: recurrent neural network
5+
6+
import tensorflow as tf
7+
from tensorflow.contrib import rnn
8+
from tensorflow.examples.tutorials.mnist import input_data
9+
10+
# mnist data
11+
mnist = input_data.read_data_sets("../data/", one_hot=True)
12+
13+
"""
14+
classify images using rnn, we consider every image row as a sequence of pixels.
15+
"""
16+
17+
# training parameters
18+
learning_rate = 0.001
19+
training_steps = 10000
20+
batch_size = 128
21+
display_step = 200
22+
23+
# network parameters
24+
num_input = 28
25+
timesteps = 28
26+
num_hidden = 128
27+
num_classes = 10
28+
29+
# tf graph input
30+
X = tf.placeholder("float", [None, timesteps, num_input])
31+
Y = tf.placeholder("float", [None, num_classes])
32+
33+
# define weights
34+
weights = {
35+
"out": tf.Variable(tf.random_normal([num_hidden, num_classes]))
36+
}
37+
biases = {
38+
"out": tf.Variable(tf.random_normal([num_classes]))
39+
}
40+
41+
42+
def RNN(x, weights, biases):
43+
# unstack to get a list of 'timesteps' tensors of shape (batch_size, n_input)
44+
x = tf.unstack(x, timesteps, 1)
45+
# define a lstm cell with tensorflow
46+
lstm_cell = rnn.BasicLSTMCell(num_hidden, forget_bias=1.0)
47+
# get lstm cell output
48+
outputs, states = rnn.static_rnn(lstm_cell, x, dtype=tf.float32)
49+
50+
# linear activation, using rnn inner loop last output
51+
return tf.matmul(outputs[-1], weights['out']) + biases['out']
52+
53+
54+
def train():
55+
logits = RNN(X, weights, biases)
56+
prediction = tf.nn.softmax(logits)
57+
58+
# define loss and optimizer
59+
loss_op = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
60+
logits=logits, labels=Y
61+
))
62+
optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate)
63+
train_op = optimizer.minimize(loss_op)
64+
65+
# evaluate model with test logits, dropout disabled
66+
correct_pred = tf.equal(tf.argmax(prediction, 1), tf.argmax(Y, 1))
67+
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
68+
69+
# init
70+
init = tf.global_variables_initializer()
71+
72+
# training
73+
with tf.Session() as sess:
74+
# run the init
75+
sess.run(init)
76+
77+
for step in range(1, training_steps + 1):
78+
batch_x, batch_y = mnist.train.next_batch(batch_size)
79+
# reshape data to get 28 seq of 28 elements
80+
batch_x = batch_x.reshape((batch_size, timesteps, num_input))
81+
# run optimization op
82+
sess.run(train_op, feed_dict={X: batch_x, Y: batch_y})
83+
if step % display_step == 0 or step == 1:
84+
# calculate batch loss and accuracy
85+
loss, acc = sess.run([loss_op, accuracy], feed_dict={X: batch_x,
86+
Y: batch_y})
87+
print("step " + str(step) + ", minibatch loss= " + \
88+
"{:.4f}".format(loss) + ", training accuracy= " + \
89+
"{:.3f}".format(acc))
90+
print("optimization finished.")
91+
92+
# calculate accuracy for 128 mnist test images
93+
test_len = 128
94+
test_data = mnist.test.images[:test_len].reshpae((-1, timesteps, num_input))
95+
test_label = mnist.test.labels[:test_len]
96+
print("testing accuracy:", \
97+
sess.run(accuracy, feed_dict={X: test_data, Y: test_label}))
98+
train()
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
驱动还有系统要自装,还有显卡太鸡巴低了.还有装系统太麻烦了
2-
价格不是最便宜的,招商还是浦发银行是238*12=2856.00人家还可以分期的。
2+
价格不是最便宜的,招商还是浦发银行是238*12=2856.00人家还可以分期的。
3+
我喜欢这油画
4+
我看了这油画半天,没看出什么有意思的内容
5+
我看了这油画半天,真是讨厌的背景
6+
我看了这油画半天,真是大爱的名作
7+
啊啊啊,要难吃死了。这土豆丝非常烂!
8+
这土豆丝非常美味!

17tensorflow/4_cnn_text_classification/data/zh_polarity/neg.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18534,7 +18534,7 @@ cup amd 经常移动稳定性不如intel;不过我这很少移动;
1853418534
还是有点热啊
1853518535
纸箱被拆开过了,小小的郁闷
1853618536
但是HD的硬盘通电3小时,还好啦
18537-
物流太糟糕了,申通服务恶劣啊,自己去取的
18537+
物流太糟糕了,申通{服务恶劣啊,自己去取的
1853818538
系统折腾了我一个上午才搞定,貌似还有几个驱动还没搞定
1853918539
多出镜面处理,也就成了指纹收集器,HP预装花哨无用的软件太多,浪费资源,拖累速度,这个价位的本子不送包不送鼠标,就一个最简配说不过去
1854018540
1出风口热量大,比其他机型散热大;装机非常费劲,关闭sata还是不行,格来格去,终于搞定,3驱动不好装,但找好顺序基本没问题。

17tensorflow/4_cnn_text_classification/eval.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,5 +84,6 @@
8484
predictions_human_readable = np.column_stack((np.array(x_raw), all_predictions))
8585
out_path = os.path.join(checkpoint_dir, "..", "prediction.csv")
8686
print("Saving evaluation to {0}".format(out_path))
87+
print(predictions_human_readable)
8788
with open(out_path, "w")as f:
8889
csv.writer(f).writerows(predictions_human_readable)

0 commit comments

Comments
 (0)