TensorFlow--卷积神经网络&GPS数据预测区块车流量大小模型测试代码

数据地址:链接:https://pan.baidu.com/s/1KDPmGenKzOJ2nUoWPYFavw 密码:ir84

import numpy as np
import csv
import tensorflow as tf


url = "/home/enche/test/test.csv"
data = csv.reader(open(url))
x_ = []
y_ = []
for i in data:
    sum = 0
    for j in range(len(i)):
        i[j] = np.float32(i[j])
        sum += i[j]
    for j in range(len(i)):
        if(j            x_.append(np.float32(i[j])/sum)
        else:
            y_.append(np.float32(i[j]))




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):
  return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')


def max_pool_2x2(x):
  return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
                        strides=[1, 2, 2, 1], padding='SAME')
x = tf.placeholder(shape=[None], dtype='float32')
x_image = tf.reshape(x, [-1,20,20,1])


W_conv1 = weight_variable([4, 4, 1, 32])
b_conv1 = bias_variable([32])


h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)


W_conv2 = weight_variable([4, 4, 32, 64])
b_conv2 = bias_variable([64])


h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)


W_fc1 = weight_variable([5 * 5 * 64, 1024])
b_fc1 = bias_variable([1024])


h_pool2_flat = tf.reshape(h_pool2, [-1, 5*5*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)


keep_prob = tf.placeholder("float")
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)


W_fc2 = weight_variable([1024, 1])
b_fc2 = bias_variable([1])


y_conv=tf.matmul(h_fc1_drop, W_fc2) + b_fc2


y = tf.placeholder(shape=[None], dtype='float32')
y_image = tf.reshape(y, [-1, 1])
loss = tf.reduce_sum(tf.square(y_conv-y_image))
optimizer = tf.train.GradientDescentOptimizer(0.000000001)
train = optimizer.minimize(loss)




with tf.Session() as sess:
    saver = tf.train.Saver()
    saver.restore(sess, "/home/model130000")
    result = sess.run(y_conv, feed_dict={ keep_prob:1, x:x_, y:y_})
    print sess.run(loss, feed_dict={ keep_prob:1, x:x_, y:y_})
    for i in range(result.__len__()):
        print result[i],y_[i]
    # 70000 239366.0
    # 68000 243342.0
    # 66000 247044.0
    # 64000 250747.0
    # 62000 254308.0
    # 60000 257458.0
    #
    #
    #
    # 48000 277859.0
    # 46000 281133.0
    # 44000 282650.0
    # 42000 288050.0
    # 40000 289083.0
    # 38000 292242.0
    # 36000 295092.0
    # 34000 298342.0
    # 32000 301756.0
    # 30000 306160.0
    # 28000 308702.0
    # 26000
    # 24000 316646.0
    # 22000 320482.0
    # 20000 324170.0
    # 18000 328742.0
    # 16000 333287.0
    # 14000 338346.0
    # 12000 344755.0
    # print result
    accuracy_rate = 0.0
    for i in range(result.__len__()):
      accuracy_rate += abs(result[i]-y_[i])/y_[i]
    print accuracy_rate/result.__len__()

你可能感兴趣的:(机器学习)