Skip to main content
 首页 » 编程设计

python之如何测试我的神经网络开发的 Tensorflow

2025年02月15日33mate10pro

我刚刚用 tensorflow 写完神经网络 附加代码:

import tensorflow as tensorFlow 
import csv 
 
# read data from csv 
file = open('stub.csv') 
reader = csv.reader(file) 
temp = list(reader) 
del temp[0] 
 
# change data from string to float (Tensorflow) 
# create data & goal lists 
data = [] 
goal = [] 
for i in range(len(temp)): 
    data.append(map(float, temp[i])) 
    goal.append([data[i][6], 0.0]) 
    del data[i][6] 
 
# change lists to tuple 
data = tuple(tuple(x) for x in data) 
goal = tuple(goal) 
 
# create training data and test data by 70-30 
a = int(len(data) * 0.6)  # training set 60% 
b = int(len(data) * 0.8)  # validation & test: each one is 20% 
trainData = data[0:a]  # 60% 
validationData = data[b: len(data)] 
testData = data[a: b]  # 20% 
 
trainGoal = goal[0:a] 
validationGoal = goal[b:len(data)] 
testGoal = goal[a: b] 
 
numberOfLayers = 500 
nodesLayer = [] 
# define the numbers of nodes in hidden layers 
for i in range(numberOfLayers): 
  nodesLayer.append(500) 
 
# define our goal class 
classes = 2 
batchSize = 2000 
 
# x for input, y for output 
sizeOfRow = len(data[0]) 
x = tensorFlow.placeholder(dtype= tensorFlow.float32, shape=[None, sizeOfRow]) 
y = tensorFlow.placeholder(dtype= tensorFlow.float32, shape=[None, classes]) 
 
hiddenLayers = [] 
layers = [] 
def neuralNetworkModel(x): 
  # first step: (input * weights) + bias, linear operation like y = ax + b 
  # each layer connection to other layer will represent by nodes(i) * nodes(i+1) 
 
  for i in range(0,numberOfLayers): 
    if i == 0: 
      hiddenLayers.append({"weights": tensorFlow.Variable(tensorFlow.random_normal([sizeOfRow, nodesLayer[i]])), 
                      "biases": tensorFlow.Variable(tensorFlow.random_normal([nodesLayer[i]]))}) 
 
    elif i > 0 and i < numberOfLayers-1: 
      hiddenLayers.append({"weights" : tensorFlow.Variable(tensorFlow.random_normal([nodesLayer[i], nodesLayer[i+1]])), 
                  "biases" : tensorFlow.Variable(tensorFlow.random_normal([nodesLayer[i+1]]))}) 
    else: 
      outputLayer = {"weights": tensorFlow.Variable(tensorFlow.random_normal([nodesLayer[i], classes])), 
                  "biases": tensorFlow.Variable(tensorFlow.random_normal([classes]))} 
 
  # create the layers 
  for i in range(numberOfLayers): 
    if i == 0: 
      layers.append(tensorFlow.add(tensorFlow.matmul(x, hiddenLayers[i]["weights"]), hiddenLayers[i]["biases"])) 
      layers.append(tensorFlow.nn.relu(layers[i]))  # pass values to activation function (i.e sigmoid, softmax) and add it to the layer 
 
    elif i >0 and i < numberOfLayers-1: 
      layers.append(tensorFlow.add(tensorFlow.matmul(layers[i-1], hiddenLayers[i]["weights"]), hiddenLayers[i]["biases"])) 
      layers.append(tensorFlow.nn.relu(layers[i])) 
 
  output = tensorFlow.matmul(layers[numberOfLayers-1], outputLayer["weights"]) + outputLayer["biases"] 
 
  return output 
 
 
def neuralNetworkTrain(data, x, y): 
  prediction = neuralNetworkModel(x) 
  # using softmax function, normalize values to range(0,1) 
  cost = tensorFlow.reduce_mean(tensorFlow.nn.softmax_cross_entropy_with_logits(prediction, y)) 
 
  # minimize the cost function 
  # using AdamOptimizer algorithm 
  optimizer = tensorFlow.train.AdadeltaOptimizer().minimize(cost) 
  epochs = 2 # feed machine forward + backpropagation = epoch 
 
  # build sessions and train the model 
  with tensorFlow.Session() as sess: 
    sess.run(tensorFlow.initialize_all_variables()) 
 
    for epoch in range(epochs): 
      epochLoss = 0 
      i = 0 
      for _ in range(int(len(data) / batchSize)): 
        ex, ey = nextBatch(i) # takes 500 examples 
        i += 1 
        feedDict = {x :ex, y:ey } 
        _, cos = sess.run([optimizer,cost], feed_dict= feedDict) # start session to optimize the cost function 
        epochLoss += cos 
      print("Epoch", epoch + 1, "completed out of", epochs, "loss:", epochLoss) 
 
    correct = tensorFlow.equal(tensorFlow.argmax(prediction,1), tensorFlow.argmax(y, 1)) 
    accuracy = tensorFlow.reduce_mean(tensorFlow.cast(correct, "float")) 
    print("Accuracy:", accuracy.eval({ x: trainData, y:trainGoal})) 
 
 
# takes 500 examples each iteration 
def nextBatch(num): 
  # Return the next `batch_size` examples from this data set. 
  # case: using our data & batch size 
  num *= batchSize 
  if num < (len(data) - batchSize): 
    return data[num: num+batchSize], goal[num: num +batchSize] 
 
neuralNetworkTrain(trainData, x, y) 

每个时期(迭代)我都得到了损失函数的值,一切都很好。 现在我想在我的验证/测试集上尝试一下。 有人现在我应该怎么做?

谢谢

请您参考如下方法:

如果你想对训练数据进行预测,你可以简单地输入如下内容:

tf_p = tf.nn.softmax(prediction) 

...在您的图表中,已将您的测试数据加载到 x_test 中。然后评估预测:

[p] = session.run([tf_p], feed_dict = { 
            x : x_test,  
            y : y_test 
        } 
    ) 

在 neuralNetworkTrain 方法的末尾,您最终应该将它们放在 p 中。

...或者使用tf.train.Saver:

或者,您可以使用 tf.train.Saver 对象来保存和恢复(并可选择保留)您的模型。为此,您在初始化所有变量后创建一个保护程序:

... 
tf.initialize_all_variables().run() 
saver = tf.train.Saver() 
... 

然后在完成训练后将其保存在 neuralNetworkTrain 方法的末尾:

... 
model_path = saver.save(sess) 

然后您构建一个新的图表进行评估,并在您的测试数据上运行之前恢复模型:

# Load test dataset into X_test 
... 
tf_x = tf.constant(X_test)   
tf_p = tf.nn.softmax(neuralNetworkModel(tf_x))  
 
with tf.Session() as session: 
    tf.initialize_all_variables().run() 
    saver.restore(session, model_path) 
    p = tf_p.eval() 

并且,p 应该再次包含测试数据集的 softmax 激活。

(恐怕我还没有实际运行这段代码,但它应该让您了解如何实现它。)