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%
本文详细源代码请戳

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值