天天看點

Tensorflow 筆記 1 CNN

%%time
2
from __future__ import division
3
from __future__ import print_function  
4
import numpy as np
5
import pandas as pd
6
import matplotlib.pylab as plt
7
%matplotlib inline
8
import seaborn as sns
9
​
10
import tensorflow as tf
11
​
12
fac = np.load('/home/big/Quotes/TensorFlow deal with Uqer/fac16.npy').astype(np.float32)
13
ret = np.load('/home/big/Quotes/TensorFlow deal with Uqer/ret16.npy').astype(np.float32)
14
#fac = np.load('/home/big/Quotes/TensorFlow deal with Uqer/fac16.npy')
15
#ret = np.load('/home/big/Quotes/TensorFlow deal with Uqer/ret16.npy')
16
​
17
# Parameters
18
learning_rate = 0.001 # 學習速率,
19
training_iters = 20 # 訓練次數
20
batch_size = 1024 # 每次計算數量 批次大小
21
display_step = 10 # 顯示步長
22
​
23
# Network Parameters
24
n_input = 40*17 # 40天×17多因子
25
n_classes = 7 # 根據漲跌幅度分成7類别
26
# 這裡注意要使用 one-hot格式,也就是如果分類如3類 -1,0,1 則需要3列來表達這個分類結果,3類是-1 0 1 然後是哪類,哪類那一行為1 否則為0
27
dropout = 0.8 # Dropout, probability to keep units
28
​
29
# tensorflow 圖 Graph 輸入 input,這裡的占位符均為輸入
30
x = tf.placeholder(tf.float32, [None, n_input])
31
y = tf.placeholder(tf.float32, [None, n_classes])
32
keep_prob = tf.placeholder(tf.float32) #dropout (keep probability)
           

2層

# 2層CNN
2
def CNN_Net_two(x,weights,biases,dropout=0.8,m=1):
3
    # 将輸入張量調整成圖檔格式
4
    # CNN圖像識别,這裡将前40天的多因子資料假設成圖檔資料
5
    x = tf.reshape(x, shape=[-1,40,17,1])
6
    
7
    # 卷積層1
8
    x = tf.nn.conv2d(x, weights['wc1'], strides=[1,m,m,1],padding='SAME')
9
    # x*W + b
10
    x = tf.nn.bias_add(x,biases['bc1'])
11
    # 激活函數
12
    x = tf.nn.relu(x)
13
    
14
    # 卷積層2 感受野 5 5 16 64 移動步長1
15
    x = tf.nn.conv2d(x, weights['wc2'], strides=[1,m,m,1],padding='SAME')
16
    x = tf.nn.bias_add(x,biases['bc2'])
17
    x = tf.nn.relu(x)
18
    
19
    # 全連接配接層
20
    x = tf.reshape(x,[-1,weights['wd1'].get_shape().as_list()[0]])
21
    x = tf.add(tf.matmul(x,weights['wd1']),biases['bd1'])
22
    x = tf.nn.relu(x)
23
    
24
    # Apply Dropout
25
    x = tf.nn.dropout(x,dropout)
26
    # Output, class prediction
27
    x = tf.add(tf.matmul(x,weights['out']),biases['out'])
28
    return x
29
​
30
# Store layers weight & bias
31
weights = {
32
    'wc1': tf.Variable(tf.random_normal([5, 5, 1, 16])),
33
    'wc2': tf.Variable(tf.random_normal([5, 5, 16, 64])),
34
    # fully connected, 7*7*64 inputs, 1024 outputs
35
    'wd1': tf.Variable(tf.random_normal([40*17*64, 1024])),
36
    'out': tf.Variable(tf.random_normal([1024, n_classes]))
37
}
38
​
39
biases = {
40
    'bc1': tf.Variable(tf.random_normal([16])),
41
    'bc2': tf.Variable(tf.random_normal([64])),
42
    'bd1': tf.Variable(tf.random_normal([1024])),
43
    'out': tf.Variable(tf.random_normal([n_classes]))
           

3層

def CNN_Net_three(x,weights,biases,dropout=0.8,m=1):
2
    
3
    x = tf.reshape(x, shape=[-1,40,17,1])
4
    
5
    # 卷積層1
6
    x = tf.nn.conv2d(x, weights['wc1'], strides=[1,m,m,1],padding='SAME')
7
    x = tf.nn.bias_add(x,biases['bc1'])
8
    x = tf.nn.relu(x)
9
    
10
    # 卷積層2 
11
    x = tf.nn.conv2d(x, weights['wc2'], strides=[1,m,m,1],padding='SAME')
12
    x = tf.nn.bias_add(x,biases['bc2'])
13
    x = tf.nn.relu(x)
14
    
15
    # 卷積層3 
16
    x = tf.nn.conv2d(x, weights['wc3'], strides=[1,m,m,1],padding='SAME')
17
    x = tf.nn.bias_add(x,biases['bc3'])
18
    x = tf.nn.relu(x)    
19
    
20
    # 全連接配接層
21
    x = tf.reshape(x,[-1,weights['wd1'].get_shape().as_list()[0]])
22
    x = tf.add(tf.matmul(x,weights['wd1']),biases['bd1'])
23
    x = tf.nn.relu(x)
24
    
25
    # Apply Dropout
26
    x = tf.nn.dropout(x,dropout)
27
    # Output, class prediction
28
    x = tf.add(tf.matmul(x,weights['out']),biases['out'])
29
    return x
30
​
31
# Store layers weight & bias
32
weights = {
33
    'wc1': tf.Variable(tf.random_normal([5, 5, 1, 16])),
34
    'wc2': tf.Variable(tf.random_normal([5, 5, 16, 32])),
35
    'wc3': tf.Variable(tf.random_normal([5, 5, 32, 64])),
36
    # fully connected, 7*7*64 inputs, 1024 outputs
37
    'wd1': tf.Variable(tf.random_normal([40*17*64, 1024])),
38
    'out': tf.Variable(tf.random_normal([1024, n_classes]))
39
}
40
​
41
biases = {
42
    'bc1': tf.Variable(tf.random_normal([16])),
43
    'bc2': tf.Variable(tf.random_normal([32])),
44
    'bc3': tf.Variable(tf.random_normal([64])),
45
    'bd1': tf.Variable(tf.random_normal([1024])),
46
    'out': tf.Variable(tf.random_normal([n_classes]))
47
}
           
%%time
2
# 模型優化
3
pred = CNN_Net_two(x,weights,biases,dropout=keep_prob)
4
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred,y))
5
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
6
correct_pred = tf.equal(tf.argmax(pred,1),tf.arg_max(y,1))
7
# tf.argmax(input,axis=None) 由于标簽的資料格式是 -1 0 1 3列,該語句是表示傳回值最大也就是1的索引,兩個索引相同則是預測正确。
8
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
9
# 更改資料格式,降低均值
10
init = tf.global_variables_initializer()
11
with tf.Session() as sess:
12
    sess.run(init)
13
    # for step in range(300):
14
    for step in range(1):
15
        for i in range(int(len(fac)/batch_size)):
16
            batch_x = fac[i*batch_size:(i+1)*batch_size]
17
            batch_y = ret[i*batch_size:(i+1)*batch_size]
18
            sess.run(optimizer,feed_dict={x:batch_x,y:batch_y,keep_prob:dropout})
19
            if i % 10 ==0:
20
                print(i,'----',(int(len(fac)/batch_size)))
21
        loss, acc = sess.run([cost, accuracy], feed_dict={x: batch_x,y: batch_y,keep_prob: 1.})
22
        print("Iter " + str(step*batch_size) + ", Minibatch Loss= " + \
23
                  "{:.6f}".format(loss) + ", Training Accuracy= " + \
24
                  "{:.5f}".format(acc))
25
    print("Optimization Finished!")   
26
    sess.close()
           

4層

def CNN_Net_five(x,weights,biases,dropout=0.8,m=1):
2
    
3
    x = tf.reshape(x, shape=[-1,40,17,1])
4
    
5
    # 卷積層1
6
    x = tf.nn.conv2d(x, weights['wc1'], strides=[1,m,m,1],padding='SAME')
7
    x = tf.nn.bias_add(x,biases['bc1'])
8
    x = tf.nn.relu(x)
9
    
10
    # 卷積層2 
11
    x = tf.nn.conv2d(x, weights['wc2'], strides=[1,m,m,1],padding='SAME')
12
    x = tf.nn.bias_add(x,biases['bc2'])
13
    x = tf.nn.relu(x)
14
    
15
    # 卷積層3 
16
    x = tf.nn.conv2d(x, weights['wc3'], strides=[1,m,m,1],padding='SAME')
17
    x = tf.nn.bias_add(x,biases['bc3'])
18
    x = tf.nn.relu(x)    
19
    
20
    # 卷積層4 
21
    x = tf.nn.conv2d(x, weights['wc4'], strides=[1,m,m,1],padding='SAME')
22
    x = tf.nn.bias_add(x,biases['bc4'])
23
    x = tf.nn.relu(x) 
24
    
25
    # 卷積層5 
26
    x = tf.nn.conv2d(x, weights['wc5'], strides=[1,m,m,1],padding='SAME')
27
    x = tf.nn.bias_add(x,biases['bc5'])
28
    x = tf.nn.relu(x) 
29
    
30
    # 全連接配接層
31
    x = tf.reshape(x,[-1,weights['wd1'].get_shape().as_list()[0]])
32
    x = tf.add(tf.matmul(x,weights['wd1']),biases['bd1'])
33
    x = tf.nn.relu(x)
34
    
35
    # Apply Dropout
36
    x = tf.nn.dropout(x,dropout)
37
    # Output, class prediction
38
    x = tf.add(tf.matmul(x,weights['out']),biases['out'])
39
    return x
40
​
41
# Store layers weight & bias
42
weights = {
43
    'wc1': tf.Variable(tf.random_normal([5, 5, 1, 16])),
44
    'wc2': tf.Variable(tf.random_normal([5, 5, 16, 32])),
45
    'wc3': tf.Variable(tf.random_normal([5, 5, 32, 64])),
46
    'wc4': tf.Variable(tf.random_normal([5, 5, 64, 32])),
47
    'wc5': tf.Variable(tf.random_normal([5, 5, 32, 16])),
48
    # fully connected, 7*7*64 inputs, 1024 outputs
49
    'wd1': tf.Variable(tf.random_normal([40*17*16, 1024])),
50
    'out': tf.Variable(tf.random_normal([1024, n_classes]))
51
}
52
​
53
biases = {
54
    'bc1': tf.Variable(tf.random_normal([16])),
55
    'bc2': tf.Variable(tf.random_normal([32])),
56
    'bc3': tf.Variable(tf.random_normal([64])),
57
    'bc4': tf.Variable(tf.random_normal([32])),
58
    'bc5': tf.Variable(tf.random_normal([16])),
59
    'bd1': tf.Variable(tf.random_normal([1024])),
60
    'out': tf.Variable(tf.random_normal([n_classes]))
           
%%time
2
# 模型優化
3
pred = CNN_Net_five(x,weights,biases,dropout=keep_prob)
4
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred,y))
5
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
6
correct_pred = tf.equal(tf.argmax(pred,1),tf.arg_max(y,1))
7
# tf.argmax(input,axis=None) 由于标簽的資料格式是 -1 0 1 3列,該語句是表示傳回值最大也就是1的索引,兩個索引相同則是預測正确。
8
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))
9
# 更改資料格式,降低均值
10
init = tf.global_variables_initializer()
11
​
12
with tf.Session() as sess:
13
    sess.run(init)
14
    for step in range(1):
15
        for i in range(int(len(fac)/batch_size)):
16
            batch_x = fac[i*batch_size:(i+1)*batch_size]
17
            batch_y = ret[i*batch_size:(i+1)*batch_size]
18
            sess.run(optimizer,feed_dict={x:batch_x,y:batch_y,keep_prob:dropout})
19
            print(i,'----',(int(len(fac)/batch_size)))
20
        loss, acc = sess.run([cost, accuracy], feed_dict={x: batch_x,y: batch_y,keep_prob: 1.})
21
        print("Iter " + str(step*batch_size) + ", Minibatch Loss= " + \
22
                  "{:.6f}".format(loss) + ", Training Accuracy= " + \
23
                  "{:.5f}".format(acc))
24
    print("Optimization Finished!") 
25
    sess.close()
           

我優化參數之後準确率大概在94%

本文詳細源代碼請戳