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

时间:2016-11-17 12:33:32

标签: python 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)

每个时代(迭代)我都得到了损失函数的值并且都是好的。 现在我想在我的验证/测试集上尝试它。 现在有人应该怎么做?

由于

1 个答案:

答案 0 :(得分:1)

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

var samlStrategy = new passportSaml.Strategy({
  ...

  //--- Service Provider Certificate
  privateCert    : {
    key : fs.readFileSync('./certificats/mydomain.key', 'utf-8'),
    passphrase : 'strong passphrase'
  }, 
  ...
},
(req, profile, done) => {
  ...
});

...在图表中,已将测试数据加载到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() 应该再次包含测试数据集的softmax激活。

(我实际上并没有运行这段代码,但它应该让你知道如何实现它。)

相关问题