【问题标题】:Tensor Shape Error: Must be rank 2 but is rank 3张量形状错误:必须为 2 级但为 3 级
【发布时间】:2017-08-01 23:18:42
【问题描述】:

我在搜索可以帮助我构建文本序列(特征)分类器的文档、研究或博客时遇到了困难。我拥有的文本序列包含网络日志。

我正在使用 TensorFlow 构建 GRU 模型,并使用 SVM 作为分类函数。我对张量形状有疑问。它说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 用于训练我的神经网络的数据。

我的项目的目标是使用这个 GRU-SVM 模型对Kyoto University's honeypot system intrusion detection dataset 进行入侵检测。该数据集有 23 个特征和一个标签(如果网络中存在入侵或没有)。

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()

注意:我之所以像以前那样构建我的MultiRNNCell(sn-p 在下面隔离)是因为我遇到了与post 类似的错误。

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

提前感谢您的回复!

2017 年 8 月 1 日更新 根据@jdehesa 的建议改进了源代码:

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()

我的下一步是验证我得到的结果是否正确。

【问题讨论】:

标签: python machine-learning tensorflow neural-network deep-learning


【解决方案1】:

问题出在一行:

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

我想你的意思是:

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

【讨论】:

  • 我做到了,但现在,我有一个新问题:ValueError: Cannot feed value of shape (200, 23) for Tensor 'Placeholder:0', which has shape '(?, 23, 1)'
  • @AbienFredAgarap 这是一个不同的错误(第一个是在图表的构造期间,而这个是在执行期间)。尝试在feed_dict 中传递x : example_batch[..., np.newaxis]
  • 我应该把x : example_batch[..., np.newaxis] 原样写吗?因为当我这样做时,我得到了同样的错误。抱歉,刚接触这个。
  • @AbienFredAgarap 是的,我的意思是,当你在 feed_dict = ... 中使用 dict 时,将 x : example_batch 替换为 x : example_batch[..., np.newaxis](其余部分保持不变)。你可能会得到更多的错误,但你不应该得到同样的错误。
  • 对不起,我的错。我正在编辑错误的来源。这是我遇到的新错误:ValueError: Cannot feed value of shape (200,) for Tensor 'Placeholder_1:0', which has shape '(?, 2)'。真的很抱歉。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-24
  • 2018-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-12
  • 2019-05-10
相关资源
最近更新 更多