如何测试我开发的神经网络张量流?

2024-05-19 02:28:04 发布

您现在位置: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)

每一个历元(迭代)我都得到了损失函数的值。 现在我想在我的验证/测试集上尝试它。 有人现在我该怎么办?在

谢谢


Tags: thetoinfordatalenlayerstensorflow
1条回答
网友
1楼 · 发布于 2024-05-19 02:28:04

如果你想在训练过的数据上得到预测,你可以简单地输入如下内容:

tf_p = tf.nn.softmax(prediction)

…在图形中,已将测试数据加载到x_test。然后评估预测:

^{2}$

在你的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激活。在

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

相关问题 更多 >

    热门问题