Tensorflow中的神经网络模型解决任务调度问题
Neural Network model in Tensorflow to solve task scheduling problems
我是 Tensorflow 的新手,正在尝试在 Tensorflow 中构建神经网络模型来解决任务调度问题。
我构建的模型有2个隐藏层,输入层有36个节点,输出层有22个节点。节点(输入层和输出层)中的所有值都是归一化浮点数(值介于 0.0 和 1.0 之间)。我按照在线示例构建模型,因为我需要从 csv 文件导入数据:http://tneal.org/post/tensorflow-iris/TensorFlowIris/
我一开始是用9个样本的数据来训练网络,得到了过拟合的结果,所以我把样本数增加到1000个,但是结果变得很奇怪,甚至不再过拟合了(当相同的数据集用于训练和测试,输出的预测值和实际值不相同。
当我调整学习率的值时,预测结果发生了变化,甚至出现了一些负值或非常大的值。我也尝试过更改优化器、隐藏层中的节点数、成本函数,但仍然没有任何改进。
这是我在 python 中编写的脚本:
import csv
import tensorflow as tf
import numpy as np
import pandas as pd
resource_file = "testGraphs/testgraph_input_output_CCR_1.0_Norm.csv"
respd = pd.read_csv(resource_file)
#print(respd.head())
n_nodes = 12
n_nodes_hl1 = 30
n_nodes_hl2 = 25
n_classes = n_nodes*2-2
#batch_size = 100
shuffled_res = respd.sample(frac = 1)
trainSet_res = shuffled_res[0:len(shuffled_res)]
testSet_res = shuffled_res[len(shuffled_res)-2:]
x = tf.placeholder('float32',[None,n_nodes*3])
y = tf.placeholder('float32',[None,n_classes])
def nerual_network_model(data):
hidden_1_layer = {'weights':tf.Variable(tf.random_normal([n_nodes*3,n_nodes_hl1])), 'biases':tf.Variable(tf.random_normal([n_nodes_hl1]))}
hidden_2_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl1,n_nodes_hl2])), 'biases':tf.Variable(tf.random_normal([n_nodes_hl2]))}
output_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl2,n_classes])), 'biases':tf.Variable(tf.random_normal([n_classes]))}
#input_data * weights + biases
l1 = tf.add(tf.matmul(data,hidden_1_layer['weights']),hidden_1_layer['biases'])
l1 = tf.nn.relu(l1)
l2 = tf.add(tf.matmul(l1,hidden_2_layer['weights']),hidden_2_layer['biases'])
l2 = tf.nn.relu(l2)
output = tf.matmul(l2,output_layer['weights'])+output_layer['biases']
return output
def train_nerual_network(x):
prediction = nerual_network_model(x)
#cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(prediction,y))
cost = tf.reduce_mean(tf.square(prediction-y))
#cost = tf.pow(prediction-y,2)
optimizer = tf.train.AdamOptimizer(0.001).minimize(cost)
sess = tf.InteractiveSession()
tf.global_variables_initializer().run()
input_labels = ['In0','Weight0','Out0','In1','Weight1','Out1','In2','Weight2','Out2','In3','Weight3','Out3','In4','Weight4','Out4','In5','Weight5','Out5','In6','Weight6','Out6','In7','Weight7','Out7','In8','Weight8','Out8','In9','Weight9','Out9','In10','Weight10','Out10','In11','Weight11','Out11']
output_labels = ['ProcessorForNode1','StartingTime1','ProcessorForNode2','StartingTime2','ProcessorForNode3','StartingTime3','ProcessorForNode4','StartingTime4','ProcessorForNode5','StartingTime5','ProcessorForNode6','StartingTime6','ProcessorForNode7','StartingTime7','ProcessorForNode8','StartingTime8','ProcessorForNode9','StartingTime9','ProcessorForNode10','StartingTime10','ProcessorForNode11','StartingTime11']
for i in range(1000):
train_res = trainSet_res.sample(100)
sess.run(optimizer,feed_dict={x: [j for j in train_res[input_labels].values],
y:[j for j in train_res[output_labels].values]})
#correct = tf.equal(tf.argmax(prediction,1),tf.argmax(y,1))
# accuracy = tf.reduce_mean(tf.cast(correct,'float32'))
#print sess.run(accuracy, feed_dict={x: [j for j in testSet_res[input_labels].values],
# y:[j for j in testSet_res[output_labels].values]})
print sess.run(prediction, feed_dict={x: [j for j in testSet_res[input_labels].values],
y:[j for j in testSet_res[output_labels].values]})
print sess.run(y, feed_dict={x: [j for j in testSet_res[input_labels].values],
y:[j for j in testSet_res[output_labels].values]})
结果如下:
Prediction values above and actual values below
谁能告诉我这个模型中出现问题的原因是什么?
谢谢。
这是一个很宽泛的问题,你的问题可能涉及到很多事情(数据,优化未调整and/or没有收敛,模型设计不充分,实现中的错误等.. .)
一种可能是退后一步,实现您能想到的最简单的模型,但它仍然可以解决问题。例如。如果你有一个回归问题(我不确定你这样做 - 'StartingTimeX'
可能是一个回归问题,但 'ProcessorForNodeX'
看起来更像是一个分类问题),你可以从一个简单的线性开始回归模型。如果此模型为您提供的结果足以满足您的应用需求,则无需再做任何事情。通常在机器学习中,解决手头任务的最简单模型就是您应该瞄准的模型 (Occam's razor)。
如果简单模型不够充分(比如泛化不够好),可以考虑如何改进结果,比如通过使用更复杂的模型。
我是 Tensorflow 的新手,正在尝试在 Tensorflow 中构建神经网络模型来解决任务调度问题。
我构建的模型有2个隐藏层,输入层有36个节点,输出层有22个节点。节点(输入层和输出层)中的所有值都是归一化浮点数(值介于 0.0 和 1.0 之间)。我按照在线示例构建模型,因为我需要从 csv 文件导入数据:http://tneal.org/post/tensorflow-iris/TensorFlowIris/
我一开始是用9个样本的数据来训练网络,得到了过拟合的结果,所以我把样本数增加到1000个,但是结果变得很奇怪,甚至不再过拟合了(当相同的数据集用于训练和测试,输出的预测值和实际值不相同。
当我调整学习率的值时,预测结果发生了变化,甚至出现了一些负值或非常大的值。我也尝试过更改优化器、隐藏层中的节点数、成本函数,但仍然没有任何改进。
这是我在 python 中编写的脚本:
import csv
import tensorflow as tf
import numpy as np
import pandas as pd
resource_file = "testGraphs/testgraph_input_output_CCR_1.0_Norm.csv"
respd = pd.read_csv(resource_file)
#print(respd.head())
n_nodes = 12
n_nodes_hl1 = 30
n_nodes_hl2 = 25
n_classes = n_nodes*2-2
#batch_size = 100
shuffled_res = respd.sample(frac = 1)
trainSet_res = shuffled_res[0:len(shuffled_res)]
testSet_res = shuffled_res[len(shuffled_res)-2:]
x = tf.placeholder('float32',[None,n_nodes*3])
y = tf.placeholder('float32',[None,n_classes])
def nerual_network_model(data):
hidden_1_layer = {'weights':tf.Variable(tf.random_normal([n_nodes*3,n_nodes_hl1])), 'biases':tf.Variable(tf.random_normal([n_nodes_hl1]))}
hidden_2_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl1,n_nodes_hl2])), 'biases':tf.Variable(tf.random_normal([n_nodes_hl2]))}
output_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl2,n_classes])), 'biases':tf.Variable(tf.random_normal([n_classes]))}
#input_data * weights + biases
l1 = tf.add(tf.matmul(data,hidden_1_layer['weights']),hidden_1_layer['biases'])
l1 = tf.nn.relu(l1)
l2 = tf.add(tf.matmul(l1,hidden_2_layer['weights']),hidden_2_layer['biases'])
l2 = tf.nn.relu(l2)
output = tf.matmul(l2,output_layer['weights'])+output_layer['biases']
return output
def train_nerual_network(x):
prediction = nerual_network_model(x)
#cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(prediction,y))
cost = tf.reduce_mean(tf.square(prediction-y))
#cost = tf.pow(prediction-y,2)
optimizer = tf.train.AdamOptimizer(0.001).minimize(cost)
sess = tf.InteractiveSession()
tf.global_variables_initializer().run()
input_labels = ['In0','Weight0','Out0','In1','Weight1','Out1','In2','Weight2','Out2','In3','Weight3','Out3','In4','Weight4','Out4','In5','Weight5','Out5','In6','Weight6','Out6','In7','Weight7','Out7','In8','Weight8','Out8','In9','Weight9','Out9','In10','Weight10','Out10','In11','Weight11','Out11']
output_labels = ['ProcessorForNode1','StartingTime1','ProcessorForNode2','StartingTime2','ProcessorForNode3','StartingTime3','ProcessorForNode4','StartingTime4','ProcessorForNode5','StartingTime5','ProcessorForNode6','StartingTime6','ProcessorForNode7','StartingTime7','ProcessorForNode8','StartingTime8','ProcessorForNode9','StartingTime9','ProcessorForNode10','StartingTime10','ProcessorForNode11','StartingTime11']
for i in range(1000):
train_res = trainSet_res.sample(100)
sess.run(optimizer,feed_dict={x: [j for j in train_res[input_labels].values],
y:[j for j in train_res[output_labels].values]})
#correct = tf.equal(tf.argmax(prediction,1),tf.argmax(y,1))
# accuracy = tf.reduce_mean(tf.cast(correct,'float32'))
#print sess.run(accuracy, feed_dict={x: [j for j in testSet_res[input_labels].values],
# y:[j for j in testSet_res[output_labels].values]})
print sess.run(prediction, feed_dict={x: [j for j in testSet_res[input_labels].values],
y:[j for j in testSet_res[output_labels].values]})
print sess.run(y, feed_dict={x: [j for j in testSet_res[input_labels].values],
y:[j for j in testSet_res[output_labels].values]})
结果如下: Prediction values above and actual values below
谁能告诉我这个模型中出现问题的原因是什么? 谢谢。
这是一个很宽泛的问题,你的问题可能涉及到很多事情(数据,优化未调整and/or没有收敛,模型设计不充分,实现中的错误等.. .)
一种可能是退后一步,实现您能想到的最简单的模型,但它仍然可以解决问题。例如。如果你有一个回归问题(我不确定你这样做 - 'StartingTimeX'
可能是一个回归问题,但 'ProcessorForNodeX'
看起来更像是一个分类问题),你可以从一个简单的线性开始回归模型。如果此模型为您提供的结果足以满足您的应用需求,则无需再做任何事情。通常在机器学习中,解决手头任务的最简单模型就是您应该瞄准的模型 (Occam's razor)。
如果简单模型不够充分(比如泛化不够好),可以考虑如何改进结果,比如通过使用更复杂的模型。