代码拉取完成,页面将自动刷新
#!/usr/bin/env python
# -*- coding:utf-8 -*-
"""
@version: python2.7
@author: ‘liujiantao‘
@contact: 1329331182@qq.com
@site:
@software: PyCharm
@file: cnn_predict.py
@time: 18-4-8 下午4:47
"""
import time
from sklearn import preprocessing
import tensorflow as tf
import numpy as np
# 变厚矩阵
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
# 偏置
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
# 卷积处理 变厚过程
def conv2d(x, W):
# stride [1, x_movement, y_movement, 1] x_movement、y_movement就是步长
# Must have strides[0] = strides[3] = 1 padding='SAME'表示卷积后长宽不变
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
class CNNPredict(object):
@staticmethod
def fit(pre_features_list, act_Y, test_features, start_time):
train_x_disorder = np.array(pre_features_list)
train_y_disorder = np.array(act_Y)
train_y_disorder = train_y_disorder.reshape(-1, 1)
test_features = np.array(test_features)
train_x_disorder_3 = train_x_disorder[:, 3:5]
train_x_disorder = np.column_stack([train_x_disorder, train_x_disorder_3]) # 随意给x增加了1列,x变为16列,可以reshape为4*4矩阵了 没啥用,就是凑个正方形
test_features_3 = test_features[:, 3:5]
test_features = np.column_stack([test_features, test_features_3]) # 随意给x增加了1列,x变为16列,可以reshape为4*4矩阵了 没啥用,就是凑个正方形
# 随机挑选
# train_x_disorder, test_x_disorder, train_y_disorder, test_y_disorder = train_test_split(x, y, train_size=0.8, random_state=33)
# # 数据标准化
ss_x = preprocessing.StandardScaler()
train_x_disorder = ss_x.fit_transform(train_x_disorder)
test_features = ss_x.transform(test_features)
# ss_y = preprocessing.StandardScaler()
# train_y_disorder = ss_y.fit_transform(train_y_disorder.reshape(-1, 1))
# test_y_disorder = ss_y.transform(test_y_disorder.reshape(-1, 1))
# define placeholder for inputs to network
xs = tf.placeholder(tf.float32, [None, 36]) # 原始数据的维度:15
ys = tf.placeholder(tf.float32, [None, 1]) # 输出数据为维度:1
keep_prob = tf.placeholder(tf.float32) # dropout的比例
x_image = tf.reshape(xs, [-1, 6, 6, 1]) # 原始数据16变成二维图片4*4
## conv1 layer ##第一卷积层
W_conv1 = weight_variable([2, 2, 1, 72]) # patch 2x2, in size 1, out size 32,每个像素变成32个像素,就是变厚的过程
b_conv1 = bias_variable([72])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) # output size 2x2x32,长宽不变,高度为32的三维图像
# h_pool1 = max_pool_2x2(h_conv1) # output size 2x2x32 长宽缩小一倍
## conv2 layer ##第二卷积层
W_conv2 = weight_variable([2, 2, 72, 144]) # patch 2x2, in size 32, out size 64
b_conv2 = bias_variable([144])
h_conv2 = tf.nn.relu(conv2d(h_conv1, W_conv2) + b_conv2) # 输入第一层的处理结果 输出shape 4*4*64
## fc1 layer ## full connection 全连接层
W_fc1 = weight_variable([6 * 6 * 144, 2160]) # 4x4 ,高度为64的三维图片,然后把它拉成512长的一维数组
b_fc1 = bias_variable([2160])
h_pool2_flat = tf.reshape(h_conv2, [-1, 6 * 6 * 144]) # 把4*4,高度为64的三维图片拉成一维数组 降维处理
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) # 把数组中扔掉比例为keep_prob的元素
## fc2 layer ## full connection
W_fc2 = weight_variable([2160, 1]) # 512长的一维数组压缩为长度为1的数组
b_fc2 = bias_variable([1]) # 偏置
# 最后的计算结果
prediction = tf.matmul(h_fc1_drop, W_fc2) + b_fc2
# prediction = tf.nn.relu(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
# 计算 predition与y 差距 所用方法很简单就是用 suare()平方,sum()求和,mean()平均值
cross_entropy = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction), reduction_indices=[1]))
# 0.01学习效率,minimize(loss)减小loss误差
train_step = tf.train.AdamOptimizer(0.005).minimize(cross_entropy)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
# 训练200次
for i in range(5000):
sess.run(train_step, feed_dict={xs: train_x_disorder, ys: train_y_disorder, keep_prob: 0.7})
loss = sess.run(cross_entropy,
feed_dict={xs: train_x_disorder, ys: train_y_disorder, keep_prob: 0.7})
print(i, 'error=',loss) # 输出loss值
if (time.time() - start_time ) > 20000:
break
if loss < 0.05:
break
prediction_value = sess.run(prediction, feed_dict={xs: test_features, keep_prob: 0.7})
return prediction_value
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。