Tensorflow 中的二元分类问题:
我浏览了在线教程并尝试使用门控循环单元 (GRU) 将其应用于实时问题。我已经尝试了所有我知道的改进分类的可能性。
1) 开始添加堆叠 RNN(GRU) 层 2) 增加每个 RNN 层的隐藏单元 3) 为隐藏层添加“sigmoid”和“RelU”激活函数 4) 标准化输入数据 5) 更改超参数
请找到数据集的链接: https ://github.com/madhurilalitha/Myfirstproject/blob/master/ApplicationLayerTrainingData1.txt
如果您可以浏览数据集,则它具有“正常”和“非正常”标签。我将“正常”编码为“1 0”,将异常编码为“0 1”。我还将数据集更改为以下形状的 3D:
新列车 X 的形状 (7995, 5, 40) 新列车 Y 的形状 (7995, 2) 新测试 X 的形状 (1994, 5, 40) 新测试 Y 的形状 (1994, 2)
我不太确定我在哪里遗漏了逻辑,有人可以帮我找出代码中的错误吗?
测试数据的分类准确率为52.3%。即使在训练数据上,它也能以相同的精度执行。请在下面找到代码:
#Hyper Parameters for the model
learning_rate = 0.001
n_classes = 2
display_step = 100
input_features = train_X.shape[1] #No of selected features(columns)
training_cycles = 1000
time_steps = 5 # No of time-steps to backpropogate
hidden_units = 50 #No of GRU units in a GRU Hidden Layer
#Input Placeholders
with tf.name_scope('input'):
x = tf.placeholder(tf.float64,shape = [None,time_steps,input_features], name
= "x-input")
y = tf.placeholder(tf.float64, shape = [None,n_classes],name = "y-input")
#Weights and Biases
with tf.name_scope("weights"):
W = tf.Variable(tf.random_normal([hidden_units,n_classes]),name = "layer-
weights")
with tf.name_scope("biases"):
b = tf.Variable(tf.random_normal([n_classes]),name = "unit-biases")
# Unstack to get a list of 'time_steps' tensors of shape (batch_size,
input_features)
x_ = tf.unstack(x,time_steps,axis =1)
#Defining a single GRU cell
gru_cell = tf.contrib.rnn.GRUCell(hidden_units)
#GRU Output
with tf.variable_scope('MyGRUCel36'):
gruoutputs,grustates =
tf.contrib.rnn.static_rnn(gru_cell,x_,dtype=tf.float64)
#Linear Activation , using gru inner loop last output
output = tf.add(tf.matmul(gruoutputs[-1],tf.cast(W,tf.float64)),tf.cast(b,tf.float64))
#Defining the loss function
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits = output))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
#Training the Model
sess = tf.InteractiveSession()
sess.run(tf.global_variables_initializer())
for i in range (training_cycles):
_,c = sess.run([optimizer,cost], feed_dict = {x:newtrain_X, y:newtrain_Y})
if (i) % display_step == 0:
print ("Cost for the training cycle : ",i," : is : ",sess.run(cost, feed_dict ={x :newtrain_X,y:newtrain_Y}))
correct = tf.equal(tf.argmax(output, 1), tf.argmax(y,1))
accuracy = tf.reduce_mean(tf.cast(correct, 'float'))
print('Accuracy on the overall test set is :',accuracy.eval({x:newtest_X, y:newtest_Y}))