1

Ich habe Schwierigkeiten bei der Suche nach Dokumentation, Studien oder Blogs, die mir beim Erstellen von Textsequenz (Features) Classifier helfen können. Die Textsequenz, die ich habe, enthält Protokolle des Netzwerks.Tensor Shape Error: Muss Rang 2 sein, ist aber Rang 3

Ich baue ein GRU-Modell mit TensorFlow, mit einer SVM als Klassifikationsfunktion. Ich habe Probleme mit den Tensorformen. Es sagt ValueError: Shape must be rank 2 but is rank 3 for 'MatMul' (op: 'MatMul') with input shapes: [?,23,1], [512,2]. Here is a sample der Daten, die ich zum Trainieren meines neuronalen Netzwerks verwende.

Das Ziel meines Projekts ist es, dieses GRU-SVM-Modell für die Intrusion Detection unter Kyoto University's honeypot system intrusion detection dataset zu verwenden. Das Dataset verfügt über 23 Features und ein Label (falls ein Netzwerkeingriff vorliegt oder keins).

import data 
import numpy as np 
import os 
import tensorflow as tf 


BATCH_SIZE = 200 
CELLSIZE = 512 
NLAYERS = 3 
SVMC = 1 
learning_rate = 0.01 

TRAIN_PATH = '/home/darth/GitHub Projects/gru_svm/dataset/train/6' 

def main(): 
    examples, labels, keys = data.input_pipeline(path=TRAIN_PATH, batch_size=BATCH_SIZE, num_epochs=1) 

    seqlen = examples.shape[1] 

    x = tf.placeholder(shape=[None, seqlen, 1], dtype=tf.float32) 
    y = tf.placeholder(shape=[None, 2], dtype=tf.float32) 
    Hin = tf.placeholder(shape=[None, CELLSIZE*NLAYERS], dtype=tf.float32) 

    # cell = tf.contrib.rnn.GRUCell(CELLSIZE) 
    network = [] 
    for index in range(NLAYERS): 
     network.append(tf.contrib.rnn.GRUCell(CELLSIZE)) 

    mcell = tf.contrib.rnn.MultiRNNCell(network, state_is_tuple=False) 
    Hr, H = tf.nn.dynamic_rnn(mcell, x, initial_state=Hin, dtype=tf.float32) 

    Hf = tf.transpose(Hr, [1, 0, 2]) 
    last = tf.gather(Hf, int(Hf.get_shape()[0]) - 1) 

    weight = tf.Variable(tf.truncated_normal([CELLSIZE, 2], stddev=0.01), tf.float32) 
    bias = tf.Variable(tf.constant(0.1, shape=[2])) 
    logits = tf.matmul(last, weight) + bias 

    regularization_loss = 0.5 * tf.reduce_sum(tf.square(weight)) 
    hinge_loss = tf.reduce_sum(tf.maximum(tf.zeros([BATCH_SIZE, 1]), 1 - y * logits)) 
    loss = regularization_loss + SVMC * hinge_loss 

    train_step = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(loss) 

    init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) 

    with tf.Session() as sess: 
     sess.run(init_op) 

     train_loss = 0 

     coord = tf.train.Coordinator() 
     threads = tf.train.start_queue_runners(coord=coord) 

     try: 
      for index in range(100): 
       for j in range(1000): 
        example_batch, label_batch, key_batch = sess.run([examples, labels, keys]) 
        _, train_loss_ = sess.run([train_step, loss], 
         feed_dict = { x : example_batch, 
             y : label_batch, 
             Hin : np.zeros([BATCH_SIZE, CELLSIZE * NLAYERS]) 
            }) 
        train_loss += train_loss_ 
       print('[{}] loss : {}'.format(index, (train_loss/1000))) 
       train_loss = 0 
     except tf.errors.OutOfRangeError: 
      print('EOF reached.') 
     except KeyboardInterrupt: 
      print('Interrupted by user at {}'.format(index)) 
     finally: 
      coord.request_stop() 
     coord.join(threads) 

main() 

Hinweis: Der Grund, warum ich meine MultiRNNCell gebaut wie ich (Snippet isoliert unten), weil ich einen Fehler wie diese post aufwies, wurde.

network = [] 
for index in range(NLAYERS): 
    network.append(tf.contrib.rnn.GRUCell(CELLSIZE)) 

Vielen Dank im Voraus für Ihre Antwort!

import data 
import numpy as np 
import os 
import tensorflow as tf 


BATCH_SIZE = 200 
CELLSIZE = 512 
NLAYERS = 3 
SVMC = 1 
learning_rate = 0.01 

TRAIN_PATH = '/home/darth/GitHub Projects/gru_svm/dataset/train/6' 

def main(): 
    examples, labels, keys = data.input_pipeline(path=TRAIN_PATH, batch_size=BATCH_SIZE, num_epochs=1) 

    seqlen = examples.shape[1] 

    x = tf.placeholder(shape=[None, seqlen, 1], dtype=tf.float32, name='x') 
    y_input = tf.placeholder(shape=[None], dtype=tf.int32, name='y_input') 
    y = tf.one_hot(y_input, 2, dtype=tf.float32, name='y') 
    Hin = tf.placeholder(shape=[None, CELLSIZE*NLAYERS], dtype=tf.float32, name='Hin') 

    network = [] 
    for index in range(NLAYERS): 
     network.append(tf.contrib.rnn.GRUCell(CELLSIZE)) 

    mcell = tf.contrib.rnn.MultiRNNCell(network, state_is_tuple=False) 
    Hr, H = tf.nn.dynamic_rnn(mcell, x, initial_state=Hin, dtype=tf.float32) 

    Hf = tf.transpose(Hr, [1, 0, 2]) 
    last = tf.gather(Hf, int(Hf.get_shape()[0]) - 1) 

    weight = tf.Variable(tf.truncated_normal([CELLSIZE, 2], stddev=0.01), tf.float32, name='weights') 
    bias = tf.Variable(tf.constant(0.1, shape=[2]), name='bias') 
    logits = tf.matmul(last, weight) + bias 

    regularization_loss = 0.5 * tf.reduce_sum(tf.square(weight)) 
    hinge_loss = tf.reduce_sum(tf.maximum(tf.zeros([BATCH_SIZE, 1]), 1 - y * logits)) 
    loss = regularization_loss + SVMC * hinge_loss 

    train_step = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(loss) 

    init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) 

    with tf.Session() as sess: 
     sess.run(init_op) 

     train_loss = 0 

     coord = tf.train.Coordinator() 
     threads = tf.train.start_queue_runners(coord=coord) 

     try: 
      for index in range(100): 
       example_batch, label_batch, key_batch = sess.run([examples, labels, keys]) 
       _, train_loss_ = sess.run([train_step, loss], 
        feed_dict = { x : example_batch[..., np.newaxis], 
            y_input : label_batch, 
            Hin : np.zeros([BATCH_SIZE, CELLSIZE * NLAYERS]) 
           }) 
       train_loss += train_loss_ 
       print('[{}] loss : {}'.format(index, (train_loss/1000))) 
       print('Weights : {}'.format(sess.run(weight))) 
       print('Biases : {}'.format(sess.run(bias))) 
       train_loss = 0 
     except tf.errors.OutOfRangeError: 
      print('EOF reached.') 
     except KeyboardInterrupt: 
      print('Interrupted by user at {}'.format(index)) 
     finally: 
      coord.request_stop() 
     coord.join(threads) 

main() 

Mein nächster Schritt zu validieren ist, wenn die Ergebnisse Ich bin immer richtig sind:

-Update 2017.08.01 Die Quelle basiert auf der @ jdehesa sugestions verbessert wurde.

+1

Danke für die vorgeschlagenen bearbeiten. Ich musste es jedoch ablehnen. Sehen Sie meinen Beitrag, um zu sehen, warum. https://StackOverflow.com/a/45443606/2336654 – piRSquared

Antwort

1

Das Problem ist in der Zeile:

logits = tf.matmul(x, weight) + bias 

Ich denke, was Sie gemeint war:

logits = tf.matmul(last, weight) + bias 
+0

Ich tat das, aber jetzt habe ich ein neues Problem: 'ValueError: kann Wert der Form (200, 23) für Tensor 'nicht Platzhalter: 0', der hat shape '(?, 23, 1)' ' –

+0

@AbienFredAgarap Das ist ein anderer Fehler (der erste war während der _Construction_ des Graphen und dieser während seiner _execution_). Versuchen Sie, 'x: example_batch [..., np.newaxis]' in 'feed_dict' zu übergeben. – jdehesa

+0

Soll ich das 'x: example_batch [..., np.newaxis]' wie es ist? Weil, als ich es tat, habe ich den gleichen Fehler. Entschuldigung, nur neu dazu. –

Verwandte Themen