# -*- coding: utf-8 -*-
"""
Created on Thu Aug 9 20:36:50 2018
@author: dtter
"""
import numpy as np
import tensorflow as tf
import pandas as pd
data = pd.read_csv('1234.csv') # Series 类型
# print(data.x1) 以下的data['xn] == data.xn
x_data = np.array((data['x1'], data['x2'], data['x3'], data['x4'], data['x5'], data['x6'], data['x7'], data['x8']))
# print(x_data) # (8, 28)
y_data = np.array((data['y1'], data['y2'], data['y3'], data['y4'], data['y5'], data['y6']))
# print(y_data.shape) # (6, 28)
num_data = x_data.shape[1] # 越里面越高维 shape[0]表示有多少行,shape[1]表示有多少列 #28
# print(num_data)
m = round(-num_data * 0.2) # 截取的数据集位置,并取整
tf.reset_default_graph() ## 利用这个可清空default graph以及nodes
# x_data[行, 列]
# np.row_stack([x_data[:, :m], x_data[:1, :][0][:m]]) --> np.row_stack(mat,a)将a加入mat的行中(为了4×4的方阵)
# np.transpose()转置矩阵
x_data_train = np.transpose(np.row_stack([x_data[:, :m], x_data[:, :m]]))
# print(x_data_train.shape) # (22, 16)
x_data_test = np.transpose(np.row_stack([x_data[:, m:], x_data[:, m:]]))
# print(x_data_test.shape) # (6, 16)
y_data_train = np.transpose(y_data[:, :m])
# print(y_data_train.shape) # (22, 6)
y_data_test = np.transpose(y_data[:, m:])
# print(y_data_test.shape) # (6,6)
# 权重
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
# 一个截断的产生正态分布的函数 tf.truncated_normal(shape(生成张量的维度), mean, stddev)产生正态分布的值不会>2×stddev
return tf.Variable(initial)
# 偏置
def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
# 卷积处理 变厚过程
def conv2d(x, W):
# strides [1, x_movement, y_movement, 1] x_movement、y_movement就是步长
# Must have strides[0] = strides[3] = 1 padding='SAME'表示卷积后长宽不变 W==filters[ filter1, filter2, filter3(要变的) , num_filter] x==input
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
# pool 长宽缩小一倍
'''
def max_pool_2x2(x):
# stride [1, x_movement, y_movement, 1]
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
'''
# define placeholder for inputs to network
xs = tf.placeholder(tf.float32, [None, 16]) # 原始数据的维度:16
ys = tf.placeholder(tf.float32, [None, 1]) # 输出数据为维度:1
keep_prob = tf.placeholder(tf.float32) # dropout的比例
x_image = tf.reshape(xs, [-1, 4, 4, 1]) # 原始数据16变成二维图片4*4
## conv1 layer ##第一卷积层
W_conv1 = weight_variable([2, 2, 1, 32]) # patch 2x2, in size 1, out size 32,每个像素变成32个像素,就是变厚的过程(32个filters)
# print(W_conv1.shape)
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) # conv2d的输出为4×4*32output size 4x4x32,长宽不变,高度为32的三维图像
# print(h_conv1.shape,'--------')
# h_pool1 = max_pool_2x2(h_conv1) # output size 2x2x32 长宽缩小一倍
## conv2 layer ##第二卷积层
W_conv2 = weight_variable([2, 2, 32, 64]) # patch 2x2, in size 32, out size= num_filters 64
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_conv1, W_conv2) + b_conv2) # 输入第一层的处理结果 输出shape 4*4*64
# print(h_conv2.shape)
## fc1 layer ## full connection 全连接层
W_fc1 = weight_variable([4 * 4 * 64, 512]) # 4*4 ,高度为64的三维图片,然后把它拉成512长的一维数组
# print(W_fc1.shape)
b_fc1 = bias_variable([512])
h_pool2_flat = tf.reshape(h_conv2, [-1, 4 * 4 * 64]) # 把4*4,高度为64的三维图片拉成一维数组 降维处理
print(h_pool2_flat.shape, '===')
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
print(h_fc1.shape)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) # 把数组中保留比例为keep_prob的元素
print(h_fc1_drop.shape, '=========================')
## fc2 layer ## full connection
W_fc2 = weight_variable([512, 1]) # 512长的一维数组压缩为长度为1的数组
b_fc2 = bias_variable([1]) # 偏置
# 最后的计算结果
prediction = tf.matmul(h_fc1_drop, W_fc2) + b_fc2
print(prediction.shape)
# 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(prediction * tf.log(ys), reduction_indices=[1]))
# reduction_indices=[1] 对数据横向求和
MSE = tf.reduce_mean(tf.reduce_sum(tf.square(ys - prediction), reduction_indices=[1])) # 改成 cross_entropy
# 0.01学习效率,minimize(loss)减小loss误差
train_step = tf.train.AdamOptimizer(0.01).minimize(MSE)
# train_step = tf.train.AdamOptimizer(0.01).minimize(cross_entropy)
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
j = 0
k = 1
for _ in range(6): ## 最后得到的化学性质的量
for i in range(1000): # 先训练一千步 ## 一个一个性质的训练并预测
sess.run(train_step, feed_dict={xs: x_data_train, ys: y_data_train[:, j:k], keep_prob: 0.8})
prediction_value = sess.run(prediction, feed_dict={xs: x_data_test, ys: y_data_test[:, j:k], keep_prob: 1.0})
# print(y_data_test[:, j:k].T[0])
y = y_data_test[:, j:k].T[0]
# print(y)
j += 1
k += 1
y_ = prediction_value.reshape(1, -1)[0]
print(y_)
MSE2 = tf.reduce_sum(tf.square(y_ - y))
print('Test_MSE: ', sess.run(MSE2))
## 最后得到6行数据【水模型化学性质】 列数与测试集的大小有关系